28 #include "gmock/gmock.h"
29 #include "gtest/gtest.h"
30 #include "absl/time/time.h"
45 if (ts.tv_sec ==
arg.tv_sec && ts.tv_nsec ==
arg.tv_nsec)
47 *result_listener <<
"expected: {" << ts.tv_sec <<
", " << ts.tv_nsec <<
"} ";
48 *result_listener <<
"actual: {" <<
arg.tv_sec <<
", " <<
arg.tv_nsec <<
"}";
56 if (tv.tv_sec ==
arg.tv_sec && tv.tv_usec ==
arg.tv_usec)
58 *result_listener <<
"expected: {" << tv.tv_sec <<
", " << tv.tv_usec <<
"} ";
59 *result_listener <<
"actual: {" <<
arg.tv_sec <<
", " <<
arg.tv_usec <<
"}";
125 #define TEST_DURATION_CONVERSION(UNIT) \
127 const absl::Duration d = absl::UNIT(1.5); \
128 constexpr absl::Duration z = absl::ZeroDuration(); \
129 constexpr absl::Duration inf = absl::InfiniteDuration(); \
130 constexpr double dbl_inf = std::numeric_limits<double>::infinity(); \
131 EXPECT_EQ(kint64min, absl::ToInt64##UNIT(-inf)); \
132 EXPECT_EQ(-1, absl::ToInt64##UNIT(-d)); \
133 EXPECT_EQ(0, absl::ToInt64##UNIT(z)); \
134 EXPECT_EQ(1, absl::ToInt64##UNIT(d)); \
135 EXPECT_EQ(kint64max, absl::ToInt64##UNIT(inf)); \
136 EXPECT_EQ(-dbl_inf, absl::ToDouble##UNIT(-inf)); \
137 EXPECT_EQ(-1.5, absl::ToDouble##UNIT(-d)); \
138 EXPECT_EQ(0, absl::ToDouble##UNIT(z)); \
139 EXPECT_EQ(1.5, absl::ToDouble##UNIT(d)); \
140 EXPECT_EQ(dbl_inf, absl::ToDouble##UNIT(inf)); \
150 #undef TEST_DURATION_CONVERSION
154 void TestToConversion() {
200 TestToConversion<43>();
201 TestToConversion<1>();
202 TestToConversion<0>();
203 TestToConversion<-1>();
204 TestToConversion<-43>();
208 void TestFromChronoBasicEquality() {
209 using std::chrono::nanoseconds;
210 using std::chrono::microseconds;
211 using std::chrono::milliseconds;
213 using std::chrono::minutes;
214 using std::chrono::hours;
225 TestFromChronoBasicEquality<-123>();
226 TestFromChronoBasicEquality<-1>();
227 TestFromChronoBasicEquality<0>();
228 TestFromChronoBasicEquality<1>();
229 TestFromChronoBasicEquality<123>();
234 const int64_t minutes_max_count = chrono_minutes_max.count();
235 if (minutes_max_count >
kint64max / 60) {
244 const int64_t minutes_min_count = chrono_minutes_min.count();
245 if (minutes_min_count <
kint64min / 60) {
254 const int64_t hours_max_count = chrono_hours_max.count();
255 if (hours_max_count >
kint64max / 3600) {
264 const int64_t hours_min_count = chrono_hours_min.count();
265 if (hours_min_count <
kint64min / 3600) {
273 void TestToChrono() {
274 using std::chrono::nanoseconds;
275 using std::chrono::microseconds;
276 using std::chrono::milliseconds;
278 using std::chrono::minutes;
279 using std::chrono::hours;
287 auto chrono_minutes = minutes(
N);
296 auto chrono_hours = hours(
N);
306 using std::chrono::nanoseconds;
307 using std::chrono::microseconds;
308 using std::chrono::milliseconds;
310 using std::chrono::minutes;
311 using std::chrono::hours;
313 TestToChrono<kint64min>();
317 TestToChrono<kint64max>();
352 #define TEST_FACTORY_OVERLOADS(NAME) \
353 EXPECT_EQ(1, NAME(kOne) / NAME(kOne)); \
354 EXPECT_EQ(1, NAME(static_cast<int8_t>(1)) / NAME(1)); \
355 EXPECT_EQ(1, NAME(static_cast<int16_t>(1)) / NAME(1)); \
356 EXPECT_EQ(1, NAME(static_cast<int32_t>(1)) / NAME(1)); \
357 EXPECT_EQ(1, NAME(static_cast<int64_t>(1)) / NAME(1)); \
358 EXPECT_EQ(1, NAME(static_cast<uint8_t>(1)) / NAME(1)); \
359 EXPECT_EQ(1, NAME(static_cast<uint16_t>(1)) / NAME(1)); \
360 EXPECT_EQ(1, NAME(static_cast<uint32_t>(1)) / NAME(1)); \
361 EXPECT_EQ(1, NAME(static_cast<uint64_t>(1)) / NAME(1)); \
362 EXPECT_EQ(NAME(1) / 2, NAME(static_cast<float>(0.5))); \
363 EXPECT_EQ(NAME(1) / 2, NAME(static_cast<double>(0.5))); \
364 EXPECT_EQ(1.5, absl::FDivDuration(NAME(static_cast<float>(1.5)), NAME(1))); \
365 EXPECT_EQ(1.5, absl::FDivDuration(NAME(static_cast<double>(1.5)), NAME(1)));
374 #undef TEST_FACTORY_OVERLOADS
380 const double dbl_inf = std::numeric_limits<double>::infinity();
463 const double dbl_inf = std::numeric_limits<double>::infinity();
500 const double dbl_inf = std::numeric_limits<double>::infinity();
512 #define TEST_INF_MUL_WITH_TYPE(T) \
513 EXPECT_EQ(inf, inf * static_cast<T>(2)); \
514 EXPECT_EQ(-inf, inf * static_cast<T>(-2)); \
515 EXPECT_EQ(-inf, -inf * static_cast<T>(2)); \
516 EXPECT_EQ(inf, -inf * static_cast<T>(-2)); \
517 EXPECT_EQ(inf, inf * static_cast<T>(0)); \
518 EXPECT_EQ(-inf, -inf * static_cast<T>(0)); \
519 EXPECT_EQ(inf, sec_max * static_cast<T>(2)); \
520 EXPECT_EQ(inf, sec_min * static_cast<T>(-2)); \
521 EXPECT_EQ(inf, (sec_max / static_cast<T>(2)) * static_cast<T>(3)); \
522 EXPECT_EQ(-inf, sec_max * static_cast<T>(-2)); \
523 EXPECT_EQ(-inf, sec_min * static_cast<T>(2)); \
524 EXPECT_EQ(-inf, (sec_min / static_cast<T>(2)) * static_cast<T>(3));
529 #undef TEST_INF_MUL_WITH_TYPE
531 const double dbl_inf = std::numeric_limits<double>::infinity();
565 #define TEST_INF_DIV_WITH_TYPE(T) \
566 EXPECT_EQ(inf, inf / static_cast<T>(2)); \
567 EXPECT_EQ(-inf, inf / static_cast<T>(-2)); \
568 EXPECT_EQ(-inf, -inf / static_cast<T>(2)); \
569 EXPECT_EQ(inf, -inf / static_cast<T>(-2));
574 #undef TEST_INF_DIV_WITH_TYPE
584 const double dbl_inf = std::numeric_limits<double>::infinity();
629 const double dbl_inf = std::numeric_limits<double>::infinity();
741 const double dbl_inf = std::numeric_limits<double>::infinity();
762 const double dbl_inf = std::numeric_limits<double>::infinity();
763 const double dbl_denorm = std::numeric_limits<double>::denorm_min();
808 #define TEST_NAN_HANDLING(NAME, NAN) \
810 const auto inf = absl::InfiniteDuration(); \
811 auto x = NAME(NAN); \
812 EXPECT_TRUE(x == inf || x == -inf); \
815 EXPECT_TRUE(y == inf || y == -inf); \
818 EXPECT_TRUE(z == inf || z == -inf); \
821 const double nan = std::numeric_limits<double>::quiet_NaN();
836 #undef TEST_NAN_HANDLING
860 #define TEST_REL_OPS(UNIT) \
861 static_assert(UNIT(2) == UNIT(2), ""); \
862 static_assert(UNIT(1) != UNIT(2), ""); \
863 static_assert(UNIT(1) < UNIT(2), ""); \
864 static_assert(UNIT(3) > UNIT(2), ""); \
865 static_assert(UNIT(1) <= UNIT(2), ""); \
866 static_assert(UNIT(2) <= UNIT(2), ""); \
867 static_assert(UNIT(3) >= UNIT(2), ""); \
868 static_assert(UNIT(2) >= UNIT(2), "");
881 #define TEST_ADD_OPS(UNIT) \
883 EXPECT_EQ(UNIT(2), UNIT(1) + UNIT(1)); \
884 EXPECT_EQ(UNIT(1), UNIT(2) - UNIT(1)); \
885 EXPECT_EQ(UNIT(0), UNIT(2) - UNIT(2)); \
886 EXPECT_EQ(UNIT(-1), UNIT(1) - UNIT(2)); \
887 EXPECT_EQ(UNIT(-2), UNIT(0) - UNIT(2)); \
888 EXPECT_EQ(UNIT(-2), UNIT(1) - UNIT(3)); \
889 absl::Duration a = UNIT(1); \
891 EXPECT_EQ(UNIT(2), a); \
893 EXPECT_EQ(UNIT(1), a); \
961 constexpr
absl::Duration negated_nearly_min_duration = -nearly_min_duration;
963 EXPECT_EQ(negated_max_duration, nearly_min_duration);
964 EXPECT_EQ(negated_nearly_min_duration, max_duration);
965 EXPECT_EQ(-(-max_duration), max_duration);
991 #define TEST_MUL_OPS(UNIT) \
993 EXPECT_EQ(UNIT(5), UNIT(2) * 2.5); \
994 EXPECT_EQ(UNIT(2), UNIT(5) / 2.5); \
995 EXPECT_EQ(UNIT(-5), UNIT(-2) * 2.5); \
996 EXPECT_EQ(UNIT(-5), -UNIT(2) * 2.5); \
997 EXPECT_EQ(UNIT(-5), UNIT(2) * -2.5); \
998 EXPECT_EQ(UNIT(-2), UNIT(-5) / 2.5); \
999 EXPECT_EQ(UNIT(-2), -UNIT(5) / 2.5); \
1000 EXPECT_EQ(UNIT(-2), UNIT(5) / -2.5); \
1001 EXPECT_EQ(UNIT(2), UNIT(11) % UNIT(3)); \
1002 absl::Duration a = UNIT(2); \
1004 EXPECT_EQ(UNIT(5), a); \
1006 EXPECT_EQ(UNIT(2), a); \
1008 EXPECT_EQ(UNIT(0), a); \
1009 absl::Duration big = UNIT(1000000000); \
1012 EXPECT_EQ(UNIT(1000000000), big); \
1013 EXPECT_EQ(-UNIT(2), -UNIT(2)); \
1014 EXPECT_EQ(-UNIT(2), UNIT(2) * -1); \
1015 EXPECT_EQ(-UNIT(2), -1 * UNIT(2)); \
1016 EXPECT_EQ(-UNIT(-2), UNIT(2)); \
1017 EXPECT_EQ(2, UNIT(2) / UNIT(1)); \
1018 absl::Duration rem; \
1019 EXPECT_EQ(2, absl::IDivDuration(UNIT(2), UNIT(1), &rem)); \
1020 EXPECT_EQ(2.0, absl::FDivDuration(UNIT(2), UNIT(1))); \
1090 #define TEST_MOD_IDENTITY(a, b) \
1091 EXPECT_EQ((a), ((a) / (b))*(b) + ((a)%(b)))
1124 #undef TEST_MOD_IDENTITY
1130 for (
int unit_sign : {1, -1}) {
1154 for (
int unit_sign : {1, -1}) {
1178 for (
int unit_sign : {1, -1}) {
1200 const int kRange = 100000;
1202 #define ROUND_TRIP_UNIT(U, LOW, HIGH) \
1204 for (int64_t i = LOW; i < HIGH; ++i) { \
1205 absl::Duration d = absl::U(i); \
1206 if (d == absl::InfiniteDuration()) \
1207 EXPECT_EQ(kint64max, d / absl::U(1)); \
1208 else if (d == -absl::InfiniteDuration()) \
1209 EXPECT_EQ(kint64min, d / absl::U(1)); \
1211 EXPECT_EQ(i, absl::U(i) / absl::U(1)); \
1239 #undef ROUND_TRIP_UNIT
1259 for (
const auto&
test : to_ts) {
1276 for (
const auto&
test : from_ts) {
1296 for (
const auto&
test : to_tv) {
1313 for (
const auto&
test : from_tv) {
1372 void VerifyApproxSameAsMul(
double time_as_seconds,
int*
const misses) {
1374 auto mul_by_one_second = time_as_seconds *
absl::Seconds(1);
1379 if (*misses > 10)
return;
1380 ASSERT_LE(++(*misses), 10) <<
"Too many errors, not reporting more.";
1381 EXPECT_EQ(direct_seconds, mul_by_one_second)
1382 <<
"given double time_as_seconds = " << std::setprecision(17)
1418 double midpoint = low_edge + (high_edge - low_edge) / 2;
1419 if (midpoint == low_edge || midpoint == high_edge)
break;
1421 if (mid_duration ==
d) {
1422 low_edge = midpoint;
1425 high_edge = midpoint;
1430 VerifyApproxSameAsMul(low_edge, &misses);
1431 VerifyApproxSameAsMul(high_edge, &misses);
1438 std::random_device rd;
1439 std::seed_seq
seed({rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd()});
1446 std::uniform_real_distribution<double> uniform(
std::log(0.125e-9),
1449 for (
int i = 0;
i < 1000000; ++
i) {
1450 double d = std::exp(uniform(
gen));
1451 VerifyApproxSameAsMul(
d, &misses);
1452 VerifyApproxSameAsMul(-
d, &misses);
1459 const auto max_timeval_sec =
1461 const auto min_timeval_sec =
1464 tv.
tv_sec = max_timeval_sec;
1479 tv.
tv_sec = min_timeval_sec;
1494 const auto max_timespec_sec =
1495 std::numeric_limits<decltype(timespec::tv_sec)>
::max();
1496 const auto min_timespec_sec =
1497 std::numeric_limits<decltype(timespec::tv_sec)>
::min();
1499 ts.tv_sec = max_timespec_sec;
1500 ts.tv_nsec = 999999998;
1514 ts.tv_sec = min_timespec_sec;
1623 EXPECT_EQ(
"2562047788015215h30m7.99999999975s",
1643 EXPECT_EQ(
"876000000000000h0.999999999s",
1646 EXPECT_EQ(
"876000000000000h0.9999999995s",
1649 EXPECT_EQ(
"876000000000000h0.99999999975s",
1653 EXPECT_EQ(
"-876000000000000h0.999999999s",
1656 EXPECT_EQ(
"-876000000000000h0.9999999995s",
1659 EXPECT_EQ(
"-876000000000000h0.99999999975s",
1775 #define TEST_PARSE_ROUNDTRIP(d) \
1777 std::string s = absl::FormatDuration(d); \
1778 absl::Duration dur; \
1779 EXPECT_TRUE(absl::ParseDuration(s, &dur)); \
1780 EXPECT_EQ(d, dur); \
1809 #undef TEST_PARSE_ROUNDTRIP