mirror of
https://github.com/ETLCPP/etl.git
synced 2026-10-28 18:07:10 +08:00
Full etl:chrono::duration implementation and unit tests
This commit is contained in:
parent
fedf429d02
commit
a27aa2a556
@ -185,6 +185,40 @@ namespace etl
|
||||
return etl::chrono::duration<TRep, TPeriod>(etl::chrono::duration_values<TRep>::max());
|
||||
}
|
||||
|
||||
//***********************************************************************
|
||||
ETL_CONSTEXPR duration& operator ++()
|
||||
{
|
||||
++value;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
//***********************************************************************
|
||||
ETL_CONSTEXPR duration operator ++(int)
|
||||
{
|
||||
duration temp(*this);
|
||||
++value;
|
||||
|
||||
return temp;
|
||||
}
|
||||
|
||||
//***********************************************************************
|
||||
ETL_CONSTEXPR duration& operator --()
|
||||
{
|
||||
--value;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
//***********************************************************************
|
||||
ETL_CONSTEXPR duration operator --(int)
|
||||
{
|
||||
duration temp(*this);
|
||||
--value;
|
||||
|
||||
return temp;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
TRep value;
|
||||
@ -353,6 +387,130 @@ namespace etl
|
||||
{
|
||||
return !(lhs < rhs);
|
||||
}
|
||||
|
||||
//***********************************************************************
|
||||
/// Operator +
|
||||
//***********************************************************************
|
||||
template <typename TRep1, typename TPeriod1, typename TRep2, typename TPeriod2>
|
||||
ETL_CONSTEXPR14 typename etl::common_type<etl::chrono::duration<TRep1, TPeriod1>, etl::chrono::duration<TRep2, TPeriod2> >::type
|
||||
operator +(const etl::chrono::duration<TRep1, TPeriod1>& lhs, const etl::chrono::duration<TRep2, TPeriod2>& rhs)
|
||||
{
|
||||
// Determine the common type of the two durations.
|
||||
using common_duration = typename etl::common_type<etl::chrono::duration<TRep1, TPeriod1>, etl::chrono::duration<TRep2, TPeriod2>>::type;
|
||||
|
||||
// Convert both durations to the common type.
|
||||
common_duration lhs_converted = etl::chrono::duration_cast<common_duration>(lhs);
|
||||
common_duration rhs_converted = etl::chrono::duration_cast<common_duration>(rhs);
|
||||
|
||||
// Return the sum of the two converted durations.
|
||||
return common_duration(lhs_converted.count() + rhs_converted.count());
|
||||
}
|
||||
|
||||
//***********************************************************************
|
||||
/// Operator -
|
||||
//***********************************************************************
|
||||
template <typename TRep1, typename TPeriod1, typename TRep2, typename TPeriod2>
|
||||
ETL_CONSTEXPR14 typename etl::common_type<etl::chrono::duration<TRep1, TPeriod1>, etl::chrono::duration<TRep2, TPeriod2> >::type
|
||||
operator -(const etl::chrono::duration<TRep1, TPeriod1>& lhs, const etl::chrono::duration<TRep2, TPeriod2>& rhs)
|
||||
{
|
||||
// Determine the common type of the two durations.
|
||||
using common_duration = typename etl::common_type<etl::chrono::duration<TRep1, TPeriod1>, etl::chrono::duration<TRep2, TPeriod2>>::type;
|
||||
|
||||
// Convert both durations to the common type.
|
||||
common_duration lhs_converted = etl::chrono::duration_cast<common_duration>(lhs);
|
||||
common_duration rhs_converted = etl::chrono::duration_cast<common_duration>(rhs);
|
||||
|
||||
// Return the sum of the two converted durations.
|
||||
return common_duration(lhs_converted.count() - rhs_converted.count());
|
||||
}
|
||||
|
||||
//***********************************************************************
|
||||
/// Operator *
|
||||
//***********************************************************************
|
||||
template <typename TRep1, typename TPeriod1, typename TRep2>
|
||||
ETL_CONSTEXPR14 etl::chrono::duration<typename etl::common_type<TRep1, TRep2>::type, TPeriod1>
|
||||
operator *(const etl::chrono::duration<TRep1, TPeriod1>& lhs, const TRep2& rhs)
|
||||
{
|
||||
using common_rep = typename etl::common_type<TRep1, TRep2>::type;
|
||||
using result_duration = etl::chrono::duration<common_rep, TPeriod1>;
|
||||
|
||||
// Multiply the count of the duration by the scalar value
|
||||
return result_duration(static_cast<common_rep>(lhs.count()) * static_cast<common_rep>(rhs));
|
||||
}
|
||||
|
||||
//***********************************************************************
|
||||
/// Operator *
|
||||
//***********************************************************************
|
||||
template <typename TRep1, typename TRep2, typename TPeriod2>
|
||||
ETL_CONSTEXPR14 etl::chrono::duration<typename etl::common_type<TRep1, TRep2>::type, TPeriod2>
|
||||
operator *(const TRep1& lhs, const etl::chrono::duration<TRep2, TPeriod2>& rhs)
|
||||
{
|
||||
using common_rep = typename etl::common_type<TRep1, TRep2>::type;
|
||||
using result_duration = etl::chrono::duration<common_rep, TPeriod2>;
|
||||
|
||||
// Multiply the count of the duration by the scalar value
|
||||
return result_duration(static_cast<common_rep>(rhs.count()) * static_cast<common_rep>(lhs));
|
||||
}
|
||||
|
||||
//***********************************************************************
|
||||
/// Operator /
|
||||
//***********************************************************************
|
||||
template <typename TRep1, typename TPeriod1, typename TRep2>
|
||||
ETL_CONSTEXPR14 etl::chrono::duration<typename etl::common_type<TRep1, TRep2>::type, TPeriod1>
|
||||
operator /(const etl::chrono::duration<TRep1, TPeriod1>& lhs, const TRep2& rhs)
|
||||
{
|
||||
using common_rep = typename etl::common_type<TRep1, TRep2>::type;
|
||||
using result_duration = etl::chrono::duration<common_rep, TPeriod1>;
|
||||
|
||||
// Multiply the count of the duration by the scalar value
|
||||
return result_duration(static_cast<common_rep>(lhs.count()) / static_cast<common_rep>(rhs));
|
||||
}
|
||||
|
||||
//***********************************************************************
|
||||
/// Operator /
|
||||
//***********************************************************************
|
||||
template <typename TRep1, typename TPeriod1, typename TRep2, typename TPeriod2>
|
||||
ETL_CONSTEXPR14 typename etl::common_type<TRep1, TRep2>::type
|
||||
operator /(const etl::chrono::duration<TRep1, TPeriod1>& lhs, const etl::chrono::duration<TRep2, TPeriod2>& rhs)
|
||||
{
|
||||
// Determine the common type of the two durations.
|
||||
using common_duration = typename etl::common_type<etl::chrono::duration<TRep1, TPeriod1>, etl::chrono::duration<TRep2, TPeriod2>>::type;
|
||||
|
||||
common_duration lhs_converted = etl::chrono::duration_cast<common_duration>(lhs);
|
||||
common_duration rhs_converted = etl::chrono::duration_cast<common_duration>(rhs);
|
||||
|
||||
return typename etl::common_type<TRep1, TRep2>::type(lhs_converted.count() / rhs_converted.count());
|
||||
}
|
||||
|
||||
//***********************************************************************
|
||||
/// Operator %
|
||||
//***********************************************************************
|
||||
template <typename TRep1, typename TPeriod1, typename TRep2>
|
||||
ETL_CONSTEXPR14 etl::chrono::duration<typename etl::common_type<TRep1, TRep2>::type, TPeriod1>
|
||||
operator %(const etl::chrono::duration<TRep1, TPeriod1>& lhs, const TRep2& rhs)
|
||||
{
|
||||
using common_rep = typename etl::common_type<TRep1, TRep2>::type;
|
||||
using result_duration = etl::chrono::duration<common_rep, TPeriod1>;
|
||||
|
||||
// Mod the count of the duration by the scalar value
|
||||
return result_duration(static_cast<common_rep>(lhs.count()) % static_cast<common_rep>(rhs));
|
||||
}
|
||||
|
||||
//***********************************************************************
|
||||
/// Operator %
|
||||
//***********************************************************************
|
||||
template <typename TRep1, typename TPeriod1, typename TRep2, typename TPeriod2>
|
||||
ETL_CONSTEXPR14 typename etl::common_type<TRep1, TRep2>::type
|
||||
operator %(const etl::chrono::duration<TRep1, TPeriod1>& lhs, const etl::chrono::duration<TRep2, TPeriod2>& rhs)
|
||||
{
|
||||
// Determine the common type of the two durations.
|
||||
using common_duration = typename etl::common_type<etl::chrono::duration<TRep1, TPeriod1>, etl::chrono::duration<TRep2, TPeriod2>>::type;
|
||||
|
||||
common_duration lhs_converted = etl::chrono::duration_cast<common_duration>(lhs);
|
||||
common_duration rhs_converted = etl::chrono::duration_cast<common_duration>(rhs);
|
||||
|
||||
return typename etl::common_type<TRep1, TRep2>::type(lhs_converted.count() % rhs_converted.count());
|
||||
}
|
||||
}
|
||||
|
||||
#if ETL_HAS_CHRONO_LITERALS_DURATION
|
||||
|
||||
@ -38,7 +38,20 @@ SOFTWARE.
|
||||
#include <algorithm>
|
||||
#include <limits>
|
||||
#include <vector>
|
||||
#include <chrono>
|
||||
|
||||
// Set to 0 to reference against std::chrono
|
||||
#define ETL_USING_ETL_CHRONO 1
|
||||
|
||||
#if ETL_USING_ETL_CHRONO
|
||||
#define Chrono etl::chrono
|
||||
#else
|
||||
#if ETL_USING_CPP20
|
||||
#include <chrono>
|
||||
#define Chrono std::chrono
|
||||
#else
|
||||
#error std::chrono not supported
|
||||
#endif
|
||||
#endif
|
||||
|
||||
namespace
|
||||
{
|
||||
@ -47,7 +60,7 @@ namespace
|
||||
//*************************************************************************
|
||||
TEST(test_default_constructor)
|
||||
{
|
||||
using duration_type = etl::chrono::duration<size_t, etl::milli>;
|
||||
using duration_type = Chrono::duration<size_t, etl::milli>;
|
||||
|
||||
duration_type duration;
|
||||
|
||||
@ -59,7 +72,7 @@ namespace
|
||||
//*************************************************************************
|
||||
TEST(test_duration_values_zero_min_max)
|
||||
{
|
||||
using duration_values_type = etl::chrono::duration_values<size_t>;
|
||||
using duration_values_type = Chrono::duration_values<size_t>;
|
||||
|
||||
CHECK_EQUAL(size_t(0), duration_values_type::zero());
|
||||
CHECK_EQUAL(std::numeric_limits<size_t>::min(), duration_values_type::min());
|
||||
@ -69,7 +82,7 @@ namespace
|
||||
//*************************************************************************
|
||||
TEST(test_duration_zero_min_max)
|
||||
{
|
||||
using duration_type = etl::chrono::duration<size_t, etl::milli>;
|
||||
using duration_type = Chrono::duration<size_t, etl::milli>;
|
||||
|
||||
CHECK_EQUAL(duration_type(0).count(), duration_type::zero().count());
|
||||
CHECK_EQUAL(duration_type(std::numeric_limits<size_t>::min()).count(), duration_type::min().count());
|
||||
@ -79,45 +92,45 @@ namespace
|
||||
//*************************************************************************
|
||||
TEST(test_predefined_duration_periods)
|
||||
{
|
||||
CHECK_EQUAL((etl::ratio<1U, 1000000000U>::type::num), etl::chrono::nanoseconds::period::num);
|
||||
CHECK_EQUAL((etl::ratio<1U, 1000000000U>::type::den), etl::chrono::nanoseconds::period::den);
|
||||
CHECK_EQUAL((etl::ratio<1U, 1000000000U>::type::num), Chrono::nanoseconds::period::num);
|
||||
CHECK_EQUAL((etl::ratio<1U, 1000000000U>::type::den), Chrono::nanoseconds::period::den);
|
||||
|
||||
CHECK_EQUAL((etl::ratio<1U, 1000000U>::type::num), etl::chrono::microseconds::period::num);
|
||||
CHECK_EQUAL((etl::ratio<1U, 1000000U>::type::den), etl::chrono::microseconds::period::den);
|
||||
CHECK_EQUAL((etl::ratio<1U, 1000000U>::type::num), Chrono::microseconds::period::num);
|
||||
CHECK_EQUAL((etl::ratio<1U, 1000000U>::type::den), Chrono::microseconds::period::den);
|
||||
|
||||
CHECK_EQUAL((etl::ratio<1U, 1000U>::type::num), etl::chrono::milliseconds::period::num);
|
||||
CHECK_EQUAL((etl::ratio<1U, 1000U>::type::den), etl::chrono::milliseconds::period::den);
|
||||
CHECK_EQUAL((etl::ratio<1U, 1000U>::type::num), Chrono::milliseconds::period::num);
|
||||
CHECK_EQUAL((etl::ratio<1U, 1000U>::type::den), Chrono::milliseconds::period::den);
|
||||
|
||||
CHECK_EQUAL(etl::ratio<1U>::type::num, etl::chrono::seconds::period::num);
|
||||
CHECK_EQUAL(etl::ratio<1U>::type::den, etl::chrono::seconds::period::den);
|
||||
CHECK_EQUAL(etl::ratio<1U>::type::num, Chrono::seconds::period::num);
|
||||
CHECK_EQUAL(etl::ratio<1U>::type::den, Chrono::seconds::period::den);
|
||||
|
||||
CHECK_EQUAL(etl::ratio<60U>::type::num, etl::chrono::minutes::period::num);
|
||||
CHECK_EQUAL(etl::ratio<60U>::type::den, etl::chrono::minutes::period::den);
|
||||
CHECK_EQUAL(etl::ratio<60U>::type::num, Chrono::minutes::period::num);
|
||||
CHECK_EQUAL(etl::ratio<60U>::type::den, Chrono::minutes::period::den);
|
||||
|
||||
CHECK_EQUAL(etl::ratio<3600U>::type::num, etl::chrono::hours::period::num);
|
||||
CHECK_EQUAL(etl::ratio<3600U>::type::den, etl::chrono::hours::period::den);
|
||||
CHECK_EQUAL(etl::ratio<3600U>::type::num, Chrono::hours::period::num);
|
||||
CHECK_EQUAL(etl::ratio<3600U>::type::den, Chrono::hours::period::den);
|
||||
|
||||
CHECK_EQUAL(etl::ratio<86400U>::type::num, etl::chrono::days::period::num);
|
||||
CHECK_EQUAL(etl::ratio<86400U>::type::den, etl::chrono::days::period::den);
|
||||
CHECK_EQUAL(etl::ratio<86400U>::type::num, Chrono::days::period::num);
|
||||
CHECK_EQUAL(etl::ratio<86400U>::type::den, Chrono::days::period::den);
|
||||
|
||||
CHECK_EQUAL(etl::ratio<604800U>::type::num, etl::chrono::weeks::period::num);
|
||||
CHECK_EQUAL(etl::ratio<604800U>::type::den, etl::chrono::weeks::period::den);
|
||||
CHECK_EQUAL(etl::ratio<604800U>::type::num, Chrono::weeks::period::num);
|
||||
CHECK_EQUAL(etl::ratio<604800U>::type::den, Chrono::weeks::period::den);
|
||||
|
||||
CHECK_EQUAL(etl::ratio<604800U>::type::num, etl::chrono::weeks::period::num);
|
||||
CHECK_EQUAL(etl::ratio<604800U>::type::den, etl::chrono::weeks::period::den);
|
||||
CHECK_EQUAL(etl::ratio<604800U>::type::num, Chrono::weeks::period::num);
|
||||
CHECK_EQUAL(etl::ratio<604800U>::type::den, Chrono::weeks::period::den);
|
||||
|
||||
CHECK_EQUAL(etl::ratio<2629746U>::type::num, etl::chrono::months::period::num);
|
||||
CHECK_EQUAL(etl::ratio<2629746U>::type::den, etl::chrono::months::period::den);
|
||||
CHECK_EQUAL(etl::ratio<2629746U>::type::num, Chrono::months::period::num);
|
||||
CHECK_EQUAL(etl::ratio<2629746U>::type::den, Chrono::months::period::den);
|
||||
|
||||
CHECK_EQUAL(etl::ratio<31556952U>::type::num, etl::chrono::years::period::num);
|
||||
CHECK_EQUAL(etl::ratio<31556952U>::type::den, etl::chrono::years::period::den);
|
||||
CHECK_EQUAL(etl::ratio<31556952U>::type::num, Chrono::years::period::num);
|
||||
CHECK_EQUAL(etl::ratio<31556952U>::type::den, Chrono::years::period::den);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_duration_common_type)
|
||||
{
|
||||
using duration_type1 = etl::chrono::duration<size_t, etl::milli>;
|
||||
using duration_type2 = etl::chrono::duration<int, etl::micro>;
|
||||
using duration_type1 = Chrono::duration<size_t, etl::milli>;
|
||||
using duration_type2 = Chrono::duration<int, etl::micro>;
|
||||
|
||||
using duration_type = etl::common_type<duration_type1, duration_type2>::type;
|
||||
|
||||
@ -128,11 +141,11 @@ namespace
|
||||
//*************************************************************************
|
||||
TEST(test_duration_cast_with_same_rep_and_period_for_duration_type1_and_duration_type2)
|
||||
{
|
||||
using duration_type1 = etl::chrono::duration<size_t, etl::milli>;
|
||||
using duration_type2 = etl::chrono::duration<int, etl::ratio<etl::milli::num * 10, etl::milli::den * 10>>;
|
||||
using duration_type1 = Chrono::duration<size_t, etl::milli>;
|
||||
using duration_type2 = Chrono::duration<int, etl::ratio<etl::milli::num * 10, etl::milli::den * 10>>;
|
||||
|
||||
duration_type1 dur1(245);
|
||||
duration_type2 dur2 = etl::chrono::duration_cast<duration_type2>(dur1);
|
||||
duration_type2 dur2 = Chrono::duration_cast<duration_type2>(dur1);
|
||||
|
||||
CHECK_EQUAL(duration_type1::period::num, duration_type2::period::num);
|
||||
CHECK_EQUAL(duration_type1::period::den, duration_type2::period::den);
|
||||
@ -142,11 +155,11 @@ namespace
|
||||
//*************************************************************************
|
||||
TEST(test_duration_cast_where_ratio_divide_of_periods_num_is_1)
|
||||
{
|
||||
using duration_type1 = etl::chrono::duration<int, etl::micro>;
|
||||
using duration_type2 = etl::chrono::duration<size_t, etl::milli>;
|
||||
using duration_type1 = Chrono::duration<int, etl::micro>;
|
||||
using duration_type2 = Chrono::duration<size_t, etl::milli>;
|
||||
|
||||
duration_type1 dur1(245000);
|
||||
duration_type2 dur2 = etl::chrono::duration_cast<duration_type2>(dur1);
|
||||
duration_type2 dur2 = Chrono::duration_cast<duration_type2>(dur1);
|
||||
|
||||
typedef typename etl::ratio_divide<duration_type1::period, duration_type2::period>::type ratio_divide_t;
|
||||
|
||||
@ -159,11 +172,11 @@ namespace
|
||||
//*************************************************************************
|
||||
TEST(test_duration_cast_where_ratio_divide_of_periods_den_is_1)
|
||||
{
|
||||
using duration_type1 = etl::chrono::duration<size_t, etl::milli>;
|
||||
using duration_type2 = etl::chrono::duration<int, etl::micro>;
|
||||
using duration_type1 = Chrono::duration<size_t, etl::milli>;
|
||||
using duration_type2 = Chrono::duration<int, etl::micro>;
|
||||
|
||||
duration_type1 dur1(245);
|
||||
duration_type2 dur2 = etl::chrono::duration_cast<duration_type2>(dur1);
|
||||
duration_type2 dur2 = Chrono::duration_cast<duration_type2>(dur1);
|
||||
|
||||
typedef typename etl::ratio_divide<duration_type1::period, duration_type2::period>::type ratio_divide_t;
|
||||
|
||||
@ -176,11 +189,11 @@ namespace
|
||||
//*************************************************************************
|
||||
TEST(test_duration_cast_when_rep_and_period_are_not_equal_and_ratio_divide_of_periods_num_and_den_are_not_1)
|
||||
{
|
||||
using duration_type1 = etl::chrono::duration<size_t, etl::ratio<etl::milli::num * 3, etl::milli::den>>;
|
||||
using duration_type2 = etl::chrono::duration<int, etl::ratio<etl::milli::num * 7, etl::milli::den>>;
|
||||
using duration_type1 = Chrono::duration<size_t, etl::ratio<etl::milli::num * 3, etl::milli::den>>;
|
||||
using duration_type2 = Chrono::duration<int, etl::ratio<etl::milli::num * 7, etl::milli::den>>;
|
||||
|
||||
duration_type1 dur1(245);
|
||||
duration_type2 dur2 = etl::chrono::duration_cast<duration_type2>(dur1);
|
||||
duration_type2 dur2 = Chrono::duration_cast<duration_type2>(dur1);
|
||||
|
||||
typedef typename etl::ratio_divide<duration_type1::period, duration_type2::period>::type ratio_divide_t;
|
||||
|
||||
@ -196,12 +209,12 @@ namespace
|
||||
int Two_Hours = 2;
|
||||
int Four_Hours = 4;
|
||||
|
||||
etl::chrono::hours hours1(Two_Hours);
|
||||
etl::chrono::hours hours2(Four_Hours);
|
||||
etl::chrono::seconds seconds(0);
|
||||
Chrono::hours hours1(Two_Hours);
|
||||
Chrono::hours hours2(Four_Hours);
|
||||
Chrono::seconds seconds(0);
|
||||
|
||||
seconds = hours1;
|
||||
int seconds_per_hour = etl::chrono::hours::period::num / etl::chrono::seconds::period::num;
|
||||
int seconds_per_hour = Chrono::hours::period::num / Chrono::seconds::period::num;
|
||||
CHECK_EQUAL(seconds.count(), hours1.count() * seconds_per_hour);
|
||||
CHECK_EQUAL(Two_Hours, hours1.count());
|
||||
|
||||
@ -213,8 +226,8 @@ namespace
|
||||
//*************************************************************************
|
||||
TEST(test_duration_comparison_operators)
|
||||
{
|
||||
using duration_type1 = etl::chrono::duration<size_t, etl::milli>;
|
||||
using duration_type2 = etl::chrono::duration<int, etl::micro>;
|
||||
using duration_type1 = Chrono::duration<size_t, etl::milli>;
|
||||
using duration_type2 = Chrono::duration<int, etl::micro>;
|
||||
|
||||
duration_type1 dur1a(245);
|
||||
duration_type1 dur1b(245);
|
||||
@ -267,7 +280,7 @@ namespace
|
||||
//*************************************************************************
|
||||
TEST(test_duration_unary_operators)
|
||||
{
|
||||
using duration_type = etl::chrono::duration<int, etl::milli>;
|
||||
using duration_type = Chrono::duration<int, etl::milli>;
|
||||
|
||||
duration_type dur(245);
|
||||
|
||||
@ -281,7 +294,7 @@ namespace
|
||||
//*************************************************************************
|
||||
TEST(test_duration_hashes_are_unique)
|
||||
{
|
||||
using duration_type = etl::chrono::duration<size_t, etl::milli>;
|
||||
using duration_type = Chrono::duration<size_t, etl::milli>;
|
||||
|
||||
std::vector<size_t> hashes;
|
||||
|
||||
@ -294,5 +307,580 @@ namespace
|
||||
(void)std::unique(hashes.begin(), hashes.end());
|
||||
CHECK_EQUAL(256U, hashes.size());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_duration_pre_increment)
|
||||
{
|
||||
Chrono::hours hours(0);
|
||||
Chrono::minutes minutes(0);
|
||||
Chrono::seconds seconds(0);
|
||||
|
||||
for (int i = 1; i < 100; ++i)
|
||||
{
|
||||
++hours;
|
||||
++minutes;
|
||||
++seconds;
|
||||
|
||||
CHECK_EQUAL(i, hours.count());
|
||||
CHECK_EQUAL(i, minutes.count());
|
||||
CHECK_EQUAL(i, seconds.count());
|
||||
}
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_duration_post_increment)
|
||||
{
|
||||
Chrono::hours hours(0);
|
||||
Chrono::minutes minutes(0);
|
||||
Chrono::seconds seconds(0);
|
||||
|
||||
for (int i = 1; i < 100; ++i)
|
||||
{
|
||||
Chrono::hours last_hours = hours++;
|
||||
Chrono::minutes last_minutes = minutes++;
|
||||
Chrono::seconds last_seconds = seconds++;
|
||||
|
||||
CHECK_EQUAL(i - 1, last_hours.count());
|
||||
CHECK_EQUAL(i - 1, last_minutes.count());
|
||||
CHECK_EQUAL(i - 1, last_seconds.count());
|
||||
|
||||
CHECK_EQUAL(i, hours.count());
|
||||
CHECK_EQUAL(i, minutes.count());
|
||||
CHECK_EQUAL(i, seconds.count());
|
||||
}
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_duration_pre_decrement)
|
||||
{
|
||||
Chrono::hours hours(101);
|
||||
Chrono::minutes minutes(101);
|
||||
Chrono::seconds seconds(101);
|
||||
|
||||
for (int i = 100; i > 0; --i)
|
||||
{
|
||||
--hours;
|
||||
--minutes;
|
||||
--seconds;
|
||||
|
||||
CHECK_EQUAL(i, hours.count());
|
||||
CHECK_EQUAL(i, minutes.count());
|
||||
CHECK_EQUAL(i, seconds.count());
|
||||
}
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_duration_post_decrement)
|
||||
{
|
||||
Chrono::hours hours(101);
|
||||
Chrono::minutes minutes(101);
|
||||
Chrono::seconds seconds(101);
|
||||
|
||||
for (int i = 100; i > 0; --i)
|
||||
{
|
||||
Chrono::hours last_hours = hours--;
|
||||
Chrono::minutes last_minutes = minutes--;
|
||||
Chrono::seconds last_seconds = seconds--;
|
||||
|
||||
CHECK_EQUAL(i + 1, last_hours.count());
|
||||
CHECK_EQUAL(i + 1, last_minutes.count());
|
||||
CHECK_EQUAL(i + 1, last_seconds.count());
|
||||
|
||||
CHECK_EQUAL(i, hours.count());
|
||||
CHECK_EQUAL(i, minutes.count());
|
||||
CHECK_EQUAL(i, seconds.count());
|
||||
}
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_addition_same_type)
|
||||
{
|
||||
Chrono::seconds s1(10); // 10 seconds
|
||||
Chrono::seconds s2(20); // 20 seconds
|
||||
|
||||
auto result = s1 + s2;
|
||||
|
||||
CHECK_EQUAL(30, result.count()); // 10 + 20 = 30 seconds
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_addition_different_types)
|
||||
{
|
||||
Chrono::seconds s1(10); // 10 seconds
|
||||
Chrono::milliseconds ms1(500); // 500 milliseconds
|
||||
|
||||
auto result = s1 + ms1;
|
||||
|
||||
// Result should be in the common type (milliseconds)
|
||||
CHECK_EQUAL(10500, result.count()); // 10 seconds + 500 milliseconds = 10500 milliseconds
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_addition_with_zero)
|
||||
{
|
||||
Chrono::seconds s1(10); // 10 seconds
|
||||
Chrono::seconds s2(0); // 0 seconds
|
||||
|
||||
auto result = s1 + s2;
|
||||
|
||||
CHECK_EQUAL(10, result.count()); // 10 + 0 = 10 seconds
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_addition_negative_duration)
|
||||
{
|
||||
Chrono::seconds s1(10); // 10 seconds
|
||||
Chrono::seconds s2(-5); // -5 seconds
|
||||
|
||||
auto result = s1 + s2;
|
||||
|
||||
CHECK_EQUAL(5, result.count()); // 10 + (-5) = 5 seconds
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_addition_large_values)
|
||||
{
|
||||
Chrono::seconds s1(1000000); // 1,000,000 seconds
|
||||
Chrono::milliseconds ms1(500000); // 500,000 milliseconds
|
||||
|
||||
auto result = s1 + ms1;
|
||||
|
||||
// Result should be in the common type (milliseconds)
|
||||
CHECK_EQUAL(1000500000, result.count()); // 1,000,000 seconds + 500,000 milliseconds = 1,000,500,000 milliseconds
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_subtraction_same_type)
|
||||
{
|
||||
Chrono::seconds s1(20); // 20 seconds
|
||||
Chrono::seconds s2(10); // 10 seconds
|
||||
|
||||
auto result = s1 - s2;
|
||||
|
||||
CHECK_EQUAL(10, result.count()); // 20 - 10 = 10 seconds
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_subtraction_different_types)
|
||||
{
|
||||
Chrono::seconds s1(10); // 10 seconds
|
||||
Chrono::milliseconds ms1(500); // 500 milliseconds
|
||||
|
||||
auto result = s1 - ms1;
|
||||
|
||||
// Result should be in the common type (milliseconds)
|
||||
CHECK_EQUAL(9500, result.count()); // 10 seconds - 500 milliseconds = 9500 milliseconds
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_subtraction_with_zero)
|
||||
{
|
||||
Chrono::seconds s1(10); // 10 seconds
|
||||
Chrono::seconds s2(0); // 0 seconds
|
||||
|
||||
auto result = s1 - s2;
|
||||
|
||||
CHECK_EQUAL(10, result.count()); // 10 - 0 = 10 seconds
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_subtraction_negative_duration)
|
||||
{
|
||||
Chrono::seconds s1(10); // 10 seconds
|
||||
Chrono::seconds s2(-5); // -5 seconds
|
||||
|
||||
auto result = s1 - s2;
|
||||
|
||||
CHECK_EQUAL(15, result.count()); // 10 - (-5) = 15 seconds
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_subtraction_large_values)
|
||||
{
|
||||
Chrono::seconds s1(1000000); // 1,000,000 seconds
|
||||
Chrono::milliseconds ms1(500000); // 500,000 milliseconds
|
||||
|
||||
auto result = s1 - ms1;
|
||||
|
||||
// Result should be in the common type (milliseconds)
|
||||
CHECK_EQUAL(999500000, result.count()); // 1,000,000 seconds - 500,000 milliseconds = 999,500,000 milliseconds
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_subtraction_resulting_in_negative)
|
||||
{
|
||||
Chrono::seconds s1(5); // 5 seconds
|
||||
Chrono::seconds s2(10); // 10 seconds
|
||||
|
||||
auto result = s1 - s2;
|
||||
|
||||
CHECK_EQUAL(-5, result.count()); // 5 - 10 = -5 seconds
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_multiplication_with_scalar_positive_duration_scalar)
|
||||
{
|
||||
etl::chrono::seconds s1(10); // 10 seconds
|
||||
int scalar = 5; // Scalar value
|
||||
|
||||
auto result = s1 * scalar;
|
||||
|
||||
CHECK_EQUAL(50, result.count()); // 10 * 5 = 50 seconds
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_multiplication_with_scalar_negative_duration_scalar)
|
||||
{
|
||||
etl::chrono::seconds s1(10); // 10 seconds
|
||||
int scalar = -3; // Negative scalar value
|
||||
|
||||
auto result = s1 * scalar;
|
||||
|
||||
CHECK_EQUAL(-30, result.count()); // 10 * (-3) = -30 seconds
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_multiplication_with_scalar_zero_duration_scalar)
|
||||
{
|
||||
etl::chrono::seconds s1(10); // 10 seconds
|
||||
int scalar = 0; // Scalar value
|
||||
|
||||
auto result = s1 * scalar;
|
||||
|
||||
CHECK_EQUAL(0, result.count()); // 10 * 0 = 0 seconds
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_multiplication_with_large_scalar_duration_scalar)
|
||||
{
|
||||
etl::chrono::seconds s1(1000); // 1,000 seconds
|
||||
int scalar = 1000000; // Large scalar value
|
||||
|
||||
auto result = s1 * scalar;
|
||||
|
||||
CHECK_EQUAL(1000000000, result.count()); // 1,000 * 1,000,000 = 1,000,000,000 seconds
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_multiplication_with_floating_point_scalar_duration_scalar)
|
||||
{
|
||||
etl::chrono::seconds s1(10); // 10 seconds
|
||||
double scalar = 2.5; // Floating-point scalar value
|
||||
|
||||
auto result = s1 * scalar;
|
||||
|
||||
CHECK_EQUAL(25, result.count()); // 10 * 2.5 = 25 seconds
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_multiplication_with_different_representation_duration_scalar)
|
||||
{
|
||||
etl::chrono::milliseconds ms1(500); // 500 milliseconds
|
||||
int scalar = 3; // Scalar value
|
||||
|
||||
auto result = ms1 * scalar;
|
||||
|
||||
CHECK_EQUAL(1500, result.count()); // 500 * 3 = 1,500 milliseconds
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_multiplication_with_scalar_positive_scalar_duration)
|
||||
{
|
||||
etl::chrono::seconds s1(10); // 10 seconds
|
||||
int scalar = 5; // Scalar value
|
||||
|
||||
auto result = scalar * s1;
|
||||
|
||||
CHECK_EQUAL(50, result.count()); // 10 * 5 = 50 seconds
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_multiplication_with_scalar_negative_scalar_duration)
|
||||
{
|
||||
etl::chrono::seconds s1(10); // 10 seconds
|
||||
int scalar = -3; // Negative scalar value
|
||||
|
||||
auto result = scalar * s1;
|
||||
|
||||
CHECK_EQUAL(-30, result.count()); // 10 * (-3) = -30 seconds
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_multiplication_with_scalar_zero_scalar_duration)
|
||||
{
|
||||
etl::chrono::seconds s1(10); // 10 seconds
|
||||
int scalar = 0; // Scalar value
|
||||
|
||||
auto result = scalar * s1;
|
||||
|
||||
CHECK_EQUAL(0, result.count()); // 10 * 0 = 0 seconds
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_multiplication_with_large_scalar_scalar_duration)
|
||||
{
|
||||
etl::chrono::seconds s1(1000); // 1,000 seconds
|
||||
int scalar = 1000000; // Large scalar value
|
||||
|
||||
auto result = scalar * s1;
|
||||
|
||||
CHECK_EQUAL(1000000000, result.count()); // 1,000 * 1,000,000 = 1,000,000,000 seconds
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_multiplication_with_floating_point_scalar_scalar_duration)
|
||||
{
|
||||
etl::chrono::seconds s1(10); // 10 seconds
|
||||
double scalar = 2.5; // Floating-point scalar value
|
||||
|
||||
auto result = scalar * s1;
|
||||
|
||||
CHECK_EQUAL(25, result.count()); // 10 * 2.5 = 25 seconds
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_multiplication_with_different_representation_scalar_duration)
|
||||
{
|
||||
etl::chrono::milliseconds ms1(500); // 500 milliseconds
|
||||
int scalar = 3; // Scalar value
|
||||
|
||||
auto result = scalar * ms1;
|
||||
|
||||
CHECK_EQUAL(1500, result.count()); // 500 * 3 = 1,500 milliseconds
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_division_with_scalar_positive_duration_scalar)
|
||||
{
|
||||
etl::chrono::seconds s1(10); // 10 seconds
|
||||
int scalar = 5; // Scalar value
|
||||
|
||||
auto result = s1 / scalar;
|
||||
|
||||
CHECK_EQUAL(2, result.count()); // 10 / 5 = 2 seconds
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_division_with_scalar_negative_duration_scalar)
|
||||
{
|
||||
etl::chrono::seconds s1(10); // 10 seconds
|
||||
int scalar = -2; // Negative scalar value
|
||||
|
||||
auto result = s1 / scalar;
|
||||
|
||||
CHECK_EQUAL(-5, result.count()); // 10 / (-2) = -5 seconds
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_division_with_large_scalar_duration_scalar)
|
||||
{
|
||||
etl::chrono::seconds s1(10000000); // 10,000,000 seconds
|
||||
int scalar = 1000000; // Large scalar value
|
||||
|
||||
auto result = s1 / scalar;
|
||||
|
||||
CHECK_EQUAL(10, result.count()); // 10,000,000 / 1,000,000 = 10 seconds
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_division_with_floating_point_scalar_duration_scalar)
|
||||
{
|
||||
etl::chrono::seconds s1(10); // 10 seconds
|
||||
double scalar = 2.5; // Floating-point scalar value
|
||||
|
||||
auto result = s1 / scalar;
|
||||
|
||||
CHECK_EQUAL(4, result.count()); // 10 / 2.5 = 4 seconds
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_division_with_different_representation_duration_scalar)
|
||||
{
|
||||
etl::chrono::milliseconds ms1(500); // 500 milliseconds
|
||||
int scalar = 2; // Scalar value
|
||||
|
||||
auto result = ms1 / scalar;
|
||||
|
||||
CHECK_EQUAL(250, result.count()); // 500 / 2 = 250 milliseconds
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_division_same_type)
|
||||
{
|
||||
etl::chrono::seconds s1(20); // 20 seconds
|
||||
etl::chrono::seconds s2(10); // 10 seconds
|
||||
|
||||
auto result = s1 / s2;
|
||||
|
||||
CHECK_EQUAL(2, result); // 20 / 10 = 2
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_division_different_types)
|
||||
{
|
||||
etl::chrono::seconds s1(10); // 10 seconds
|
||||
etl::chrono::milliseconds ms1(500); // 500 milliseconds
|
||||
|
||||
auto result = s1 / ms1;
|
||||
|
||||
// Result should be in the common type (milliseconds)
|
||||
CHECK_EQUAL(20, result); // 10 seconds / 500 milliseconds = 20
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_division_with_large_values)
|
||||
{
|
||||
etl::chrono::seconds s1(1000000); // 1,000,000 seconds
|
||||
etl::chrono::milliseconds ms1(500000); // 500,000 milliseconds
|
||||
|
||||
auto result = s1 / ms1;
|
||||
|
||||
CHECK_EQUAL(2000, result); // 1,000,000 seconds / 500,000 milliseconds = 2000
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_division_resulting_in_fraction)
|
||||
{
|
||||
etl::chrono::seconds s1(7); // 7 seconds
|
||||
etl::chrono::seconds s2(3); // 3 seconds
|
||||
|
||||
auto result = s1 / s2;
|
||||
|
||||
CHECK_EQUAL(2, result); // 7 / 3 = 2 (integer division)
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_division_by_larger_duration)
|
||||
{
|
||||
etl::chrono::seconds s1(5); // 5 seconds
|
||||
etl::chrono::seconds s2(10); // 10 seconds
|
||||
|
||||
auto result = s1 / s2;
|
||||
|
||||
CHECK_EQUAL(0, result); // 5 / 10 = 0 (integer division)
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_division_by_one)
|
||||
{
|
||||
etl::chrono::seconds s1(10); // 10 seconds
|
||||
etl::chrono::seconds s2(1); // 1 second
|
||||
|
||||
auto result = s1 / s2;
|
||||
|
||||
CHECK_EQUAL(10, result); // 10 / 1 = 10
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_modulus_with_positive_scalar)
|
||||
{
|
||||
etl::chrono::seconds s1(10); // 10 seconds
|
||||
int scalar = 3; // Scalar value
|
||||
|
||||
auto result = s1 % scalar;
|
||||
|
||||
CHECK_EQUAL(1, result.count()); // 10 % 3 = 1 second
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_modulus_with_negative_scalar)
|
||||
{
|
||||
etl::chrono::seconds s1(10); // 10 seconds
|
||||
int scalar = -3; // Negative scalar value
|
||||
|
||||
auto result = s1 % scalar;
|
||||
|
||||
CHECK_EQUAL(1, result.count()); // 10 % -3 = 1 second (modulus result is positive)
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_modulus_with_large_scalar)
|
||||
{
|
||||
etl::chrono::seconds s1(10); // 10 seconds
|
||||
int scalar = 100; // Scalar value larger than duration
|
||||
|
||||
auto result = s1 % scalar;
|
||||
|
||||
CHECK_EQUAL(10, result.count()); // 10 % 100 = 10 seconds
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_modulus_with_different_representation)
|
||||
{
|
||||
etl::chrono::milliseconds ms1(1050); // 1050 milliseconds
|
||||
int scalar = 500; // Scalar value
|
||||
|
||||
auto result = ms1 % scalar;
|
||||
|
||||
CHECK_EQUAL(50, result.count()); // 1050 % 500 = 50 milliseconds
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_modulus_same_type)
|
||||
{
|
||||
etl::chrono::seconds s1(10); // 10 seconds
|
||||
etl::chrono::seconds s2(3); // 3 seconds
|
||||
|
||||
auto result = s1 % s2;
|
||||
|
||||
CHECK_EQUAL(1, result); // 10 % 3 = 1 second
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_modulus_different_types)
|
||||
{
|
||||
etl::chrono::seconds s1(10); // 10 seconds
|
||||
etl::chrono::milliseconds ms1(3000); // 3000 milliseconds
|
||||
|
||||
auto result = s1 % ms1;
|
||||
|
||||
// Result should be in the common type (milliseconds)
|
||||
CHECK_EQUAL(1000, result); // 10 seconds % 3000 milliseconds = 1000 milliseconds
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_modulus_with_large_values)
|
||||
{
|
||||
etl::chrono::seconds s1(1000000); // 1,000,000 seconds
|
||||
etl::chrono::milliseconds ms1(500000); // 500,000 milliseconds
|
||||
|
||||
auto result = s1 % ms1;
|
||||
|
||||
CHECK_EQUAL(0, result); // 1,000,000 seconds % 500,000 milliseconds = 0
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_modulus_resulting_in_fraction)
|
||||
{
|
||||
etl::chrono::seconds s1(7); // 7 seconds
|
||||
etl::chrono::seconds s2(3); // 3 seconds
|
||||
|
||||
auto result = s1 % s2;
|
||||
|
||||
CHECK_EQUAL(1, result); // 7 % 3 = 1 second
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_modulus_by_larger_duration)
|
||||
{
|
||||
etl::chrono::seconds s1(5); // 5 seconds
|
||||
etl::chrono::seconds s2(10); // 10 seconds
|
||||
|
||||
auto result = s1 % s2;
|
||||
|
||||
CHECK_EQUAL(5, result); // 5 % 10 = 5 seconds
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
TEST(test_modulus_with_different_periods)
|
||||
{
|
||||
etl::chrono::seconds s1(10); // 10 seconds
|
||||
etl::chrono::milliseconds ms1(2500); // 2500 milliseconds
|
||||
|
||||
auto result = s1 % ms1;
|
||||
|
||||
// Result should be in the common type (milliseconds)
|
||||
CHECK_EQUAL(0, result); // 10 seconds % 2500 milliseconds = 0 milliseconds
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user