24 #include "gmock/gmock.h" 25 #include "gtest/gtest.h" 30 constexpr int64_t kint64max = std::numeric_limits<int64_t>::max();
31 constexpr int64_t kint64min = std::numeric_limits<int64_t>::min();
40 MATCHER_P(TimespecMatcher, ts,
"") {
41 if (ts.tv_sec ==
arg.tv_sec && ts.tv_nsec ==
arg.tv_nsec)
43 *result_listener <<
"expected: {" << ts.tv_sec <<
", " << ts.tv_nsec <<
"} ";
44 *result_listener <<
"actual: {" <<
arg.tv_sec <<
", " <<
arg.tv_nsec <<
"}";
51 MATCHER_P(TimevalMatcher, tv,
"") {
52 if (tv.tv_sec ==
arg.tv_sec && tv.tv_usec ==
arg.tv_usec)
54 *result_listener <<
"expected: {" << tv.tv_sec <<
", " << tv.tv_usec <<
"} ";
55 *result_listener <<
"actual: {" <<
arg.tv_sec <<
", " <<
arg.tv_usec <<
"}";
59 TEST(Duration, ConstExpr) {
70 TEST(Duration, ValueSemantics) {
80 TEST(Duration, Factories) {
120 TEST(Duration, ToConversion) {
121 #define TEST_DURATION_CONVERSION(UNIT) \ 123 const absl::Duration d = absl::UNIT(1.5); \ 124 constexpr absl::Duration z = absl::ZeroDuration(); \ 125 constexpr absl::Duration inf = absl::InfiniteDuration(); \ 126 constexpr double dbl_inf = std::numeric_limits<double>::infinity(); \ 127 EXPECT_EQ(kint64min, absl::ToInt64##UNIT(-inf)); \ 128 EXPECT_EQ(-1, absl::ToInt64##UNIT(-d)); \ 129 EXPECT_EQ(0, absl::ToInt64##UNIT(z)); \ 130 EXPECT_EQ(1, absl::ToInt64##UNIT(d)); \ 131 EXPECT_EQ(kint64max, absl::ToInt64##UNIT(inf)); \ 132 EXPECT_EQ(-dbl_inf, absl::ToDouble##UNIT(-inf)); \ 133 EXPECT_EQ(-1.5, absl::ToDouble##UNIT(-d)); \ 134 EXPECT_EQ(0, absl::ToDouble##UNIT(z)); \ 135 EXPECT_EQ(1.5, absl::ToDouble##UNIT(d)); \ 136 EXPECT_EQ(dbl_inf, absl::ToDouble##UNIT(inf)); \ 146 #undef TEST_DURATION_CONVERSION 150 void TestToConversion() {
195 TEST(Duration, ToConversionDeprecated) {
196 TestToConversion<43>();
197 TestToConversion<1>();
198 TestToConversion<0>();
199 TestToConversion<-1>();
200 TestToConversion<-43>();
204 void TestFromChronoBasicEquality() {
205 using std::chrono::nanoseconds;
206 using std::chrono::microseconds;
207 using std::chrono::milliseconds;
209 using std::chrono::minutes;
210 using std::chrono::hours;
221 TestFromChronoBasicEquality<-123>();
222 TestFromChronoBasicEquality<-1>();
223 TestFromChronoBasicEquality<0>();
224 TestFromChronoBasicEquality<1>();
225 TestFromChronoBasicEquality<123>();
228 const auto chrono_minutes_max = std::chrono::minutes::max();
230 const int64_t minutes_max_count = chrono_minutes_max.count();
231 if (minutes_max_count > kint64max / 60) {
238 const auto chrono_minutes_min = std::chrono::minutes::min();
240 const int64_t minutes_min_count = chrono_minutes_min.count();
241 if (minutes_min_count < kint64min / 60) {
248 const auto chrono_hours_max = std::chrono::hours::max();
250 const int64_t hours_max_count = chrono_hours_max.count();
251 if (hours_max_count > kint64max / 3600) {
254 EXPECT_EQ(
absl::Hours(hours_max_count), hours_max);
258 const auto chrono_hours_min = std::chrono::hours::min();
260 const int64_t hours_min_count = chrono_hours_min.count();
261 if (hours_min_count < kint64min / 3600) {
264 EXPECT_EQ(
absl::Hours(hours_min_count), hours_min);
269 void TestToChrono() {
270 using std::chrono::nanoseconds;
271 using std::chrono::microseconds;
272 using std::chrono::milliseconds;
274 using std::chrono::minutes;
275 using std::chrono::hours;
283 auto chrono_minutes = minutes(N);
285 chrono_minutes = minutes::min();
287 chrono_minutes = minutes::max();
292 auto chrono_hours = hours(N);
294 chrono_hours = hours::min();
296 chrono_hours = hours::max();
301 TEST(Duration, ToChrono) {
302 using std::chrono::nanoseconds;
303 using std::chrono::microseconds;
304 using std::chrono::milliseconds;
306 using std::chrono::minutes;
307 using std::chrono::hours;
309 TestToChrono<kint64min>();
313 TestToChrono<kint64max>();
346 TEST(Duration, FactoryOverloads) {
348 #define TEST_FACTORY_OVERLOADS(NAME) \ 349 EXPECT_EQ(1, NAME(kOne) / NAME(kOne)); \ 350 EXPECT_EQ(1, NAME(static_cast<int8_t>(1)) / NAME(1)); \ 351 EXPECT_EQ(1, NAME(static_cast<int16_t>(1)) / NAME(1)); \ 352 EXPECT_EQ(1, NAME(static_cast<int32_t>(1)) / NAME(1)); \ 353 EXPECT_EQ(1, NAME(static_cast<int64_t>(1)) / NAME(1)); \ 354 EXPECT_EQ(1, NAME(static_cast<uint8_t>(1)) / NAME(1)); \ 355 EXPECT_EQ(1, NAME(static_cast<uint16_t>(1)) / NAME(1)); \ 356 EXPECT_EQ(1, NAME(static_cast<uint32_t>(1)) / NAME(1)); \ 357 EXPECT_EQ(1, NAME(static_cast<uint64_t>(1)) / NAME(1)); \ 358 EXPECT_EQ(NAME(1) / 2, NAME(static_cast<float>(0.5))); \ 359 EXPECT_EQ(NAME(1) / 2, NAME(static_cast<double>(0.5))); \ 360 EXPECT_EQ(1.5, absl::FDivDuration(NAME(static_cast<float>(1.5)), NAME(1))); \ 361 EXPECT_EQ(1.5, absl::FDivDuration(NAME(static_cast<double>(1.5)), NAME(1))); 370 #undef TEST_FACTORY_OVERLOADS 376 const double dbl_inf = std::numeric_limits<double>::infinity();
391 TEST(Duration, InfinityExamples) {
398 EXPECT_TRUE(inf == inf + inf);
399 EXPECT_TRUE(inf == inf + d);
400 EXPECT_TRUE(inf == inf - inf);
401 EXPECT_TRUE(-inf == d - inf);
403 EXPECT_TRUE(inf == d * 1e100);
404 EXPECT_TRUE(0 == d / inf);
407 EXPECT_TRUE(inf == d / 0);
411 TEST(Duration, InfinityComparison) {
417 EXPECT_EQ(-inf, -inf);
418 EXPECT_NE(inf, -inf);
419 EXPECT_NE(any_dur, inf);
420 EXPECT_NE(any_dur, -inf);
423 EXPECT_GT(inf, any_dur);
424 EXPECT_LT(-inf, any_dur);
425 EXPECT_LT(-inf, inf);
426 EXPECT_GT(inf, -inf);
429 TEST(Duration, InfinityAddition) {
436 EXPECT_EQ(inf, inf + inf);
437 EXPECT_EQ(inf, inf + -inf);
438 EXPECT_EQ(-inf, -inf + inf);
439 EXPECT_EQ(-inf, -inf + -inf);
441 EXPECT_EQ(inf, inf + any_dur);
442 EXPECT_EQ(inf, any_dur + inf);
443 EXPECT_EQ(-inf, -inf + any_dur);
444 EXPECT_EQ(-inf, any_dur + -inf);
448 EXPECT_GT(inf, almost_inf);
450 EXPECT_GT(inf, almost_inf);
454 EXPECT_EQ(inf, sec_max + sec_max);
456 EXPECT_EQ(-inf, sec_min + -sec_max);
459 const double dbl_inf = std::numeric_limits<double>::infinity();
460 EXPECT_TRUE(std::isinf(dbl_inf + dbl_inf));
461 EXPECT_TRUE(std::isnan(dbl_inf + -dbl_inf));
462 EXPECT_TRUE(std::isnan(-dbl_inf + dbl_inf));
463 EXPECT_TRUE(std::isinf(-dbl_inf + -dbl_inf));
466 TEST(Duration, InfinitySubtraction) {
473 EXPECT_EQ(inf, inf - inf);
474 EXPECT_EQ(inf, inf - -inf);
475 EXPECT_EQ(-inf, -inf - inf);
476 EXPECT_EQ(-inf, -inf - -inf);
478 EXPECT_EQ(inf, inf - any_dur);
479 EXPECT_EQ(-inf, any_dur - inf);
480 EXPECT_EQ(-inf, -inf - any_dur);
481 EXPECT_EQ(inf, any_dur - -inf);
485 EXPECT_EQ(inf, sec_max - -sec_max);
487 EXPECT_EQ(-inf, sec_min - sec_max);
491 EXPECT_LT(-inf, almost_neg_inf);
493 EXPECT_LT(-inf, almost_neg_inf);
496 const double dbl_inf = std::numeric_limits<double>::infinity();
497 EXPECT_TRUE(std::isnan(dbl_inf - dbl_inf));
498 EXPECT_TRUE(std::isinf(dbl_inf - -dbl_inf));
499 EXPECT_TRUE(std::isinf(-dbl_inf - dbl_inf));
500 EXPECT_TRUE(std::isnan(-dbl_inf - -dbl_inf));
503 TEST(Duration, InfinityMultiplication) {
508 #define TEST_INF_MUL_WITH_TYPE(T) \ 509 EXPECT_EQ(inf, inf * static_cast<T>(2)); \ 510 EXPECT_EQ(-inf, inf * static_cast<T>(-2)); \ 511 EXPECT_EQ(-inf, -inf * static_cast<T>(2)); \ 512 EXPECT_EQ(inf, -inf * static_cast<T>(-2)); \ 513 EXPECT_EQ(inf, inf * static_cast<T>(0)); \ 514 EXPECT_EQ(-inf, -inf * static_cast<T>(0)); \ 515 EXPECT_EQ(inf, sec_max * static_cast<T>(2)); \ 516 EXPECT_EQ(inf, sec_min * static_cast<T>(-2)); \ 517 EXPECT_EQ(inf, (sec_max / static_cast<T>(2)) * static_cast<T>(3)); \ 518 EXPECT_EQ(-inf, sec_max * static_cast<T>(-2)); \ 519 EXPECT_EQ(-inf, sec_min * static_cast<T>(2)); \ 520 EXPECT_EQ(-inf, (sec_min / static_cast<T>(2)) * static_cast<T>(3)); 525 #undef TEST_INF_MUL_WITH_TYPE 527 const double dbl_inf = std::numeric_limits<double>::infinity();
528 EXPECT_EQ(inf, inf * dbl_inf);
529 EXPECT_EQ(-inf, -inf * dbl_inf);
530 EXPECT_EQ(-inf, inf * -dbl_inf);
531 EXPECT_EQ(inf, -inf * -dbl_inf);
534 EXPECT_EQ(inf, any_dur * dbl_inf);
535 EXPECT_EQ(-inf, -any_dur * dbl_inf);
536 EXPECT_EQ(-inf, any_dur * -dbl_inf);
537 EXPECT_EQ(inf, -any_dur * -dbl_inf);
542 EXPECT_EQ(inf,
absl::Seconds(1) * static_cast<double>(kint64max));
544 EXPECT_EQ(-inf,
absl::Seconds(1) * static_cast<double>(kint64min));
548 EXPECT_NE(inf, sec_max);
549 EXPECT_NE(inf, sec_max / 1);
550 EXPECT_EQ(inf, sec_max / 1.0);
551 EXPECT_NE(inf, sec_max * 1);
552 EXPECT_EQ(inf, sec_max * 1.0);
555 TEST(Duration, InfinityDivision) {
561 #define TEST_INF_DIV_WITH_TYPE(T) \ 562 EXPECT_EQ(inf, inf / static_cast<T>(2)); \ 563 EXPECT_EQ(-inf, inf / static_cast<T>(-2)); \ 564 EXPECT_EQ(-inf, -inf / static_cast<T>(2)); \ 565 EXPECT_EQ(inf, -inf / static_cast<T>(-2)); 570 #undef TEST_INF_DIV_WITH_TYPE 573 EXPECT_EQ(inf, sec_max / 0.5);
574 EXPECT_EQ(inf, sec_min / -0.5);
576 EXPECT_EQ(-inf, sec_max / -0.5);
577 EXPECT_EQ(-inf, sec_min / 0.5);
580 const double dbl_inf = std::numeric_limits<double>::infinity();
581 EXPECT_EQ(inf, inf / dbl_inf);
582 EXPECT_EQ(-inf, inf / -dbl_inf);
583 EXPECT_EQ(-inf, -inf / dbl_inf);
584 EXPECT_EQ(inf, -inf / -dbl_inf);
593 TEST(Duration, InfinityModulus) {
598 EXPECT_EQ(inf, inf % inf);
599 EXPECT_EQ(inf, inf % -inf);
600 EXPECT_EQ(-inf, -inf % -inf);
601 EXPECT_EQ(-inf, -inf % inf);
603 EXPECT_EQ(any_dur, any_dur % inf);
604 EXPECT_EQ(any_dur, any_dur % -inf);
605 EXPECT_EQ(-any_dur, -any_dur % inf);
606 EXPECT_EQ(-any_dur, -any_dur % -inf);
608 EXPECT_EQ(inf, inf % -any_dur);
609 EXPECT_EQ(inf, inf % any_dur);
610 EXPECT_EQ(-inf, -inf % -any_dur);
611 EXPECT_EQ(-inf, -inf % any_dur);
621 TEST(Duration, InfinityIDiv) {
625 const double dbl_inf = std::numeric_limits<double>::infinity();
634 EXPECT_EQ(-inf, rem);
642 EXPECT_EQ(any_dur, rem);
646 EXPECT_EQ(-inf, rem);
650 EXPECT_EQ(-any_dur, rem);
654 EXPECT_EQ(-inf, rem);
662 EXPECT_EQ(-inf, rem);
666 EXPECT_EQ(-any_dur, rem);
674 EXPECT_EQ(any_dur, rem);
708 EXPECT_TRUE(std::isnan(dbl_inf / dbl_inf));
709 EXPECT_EQ(kint64max, inf / inf);
710 EXPECT_EQ(kint64max, -inf / -inf);
711 EXPECT_EQ(kint64min, -inf / inf);
712 EXPECT_EQ(kint64min, inf / -inf);
714 EXPECT_TRUE(std::isinf(dbl_inf / 2.0));
715 EXPECT_EQ(kint64max, inf / any_dur);
716 EXPECT_EQ(kint64max, -inf / -any_dur);
717 EXPECT_EQ(kint64min, -inf / any_dur);
718 EXPECT_EQ(kint64min, inf / -any_dur);
720 EXPECT_EQ(0.0, 2.0 / dbl_inf);
721 EXPECT_EQ(0, any_dur / inf);
722 EXPECT_EQ(0, any_dur / -inf);
723 EXPECT_EQ(0, -any_dur / inf);
724 EXPECT_EQ(0, -any_dur / -inf);
734 TEST(Duration, InfinityFDiv) {
737 const double dbl_inf = std::numeric_limits<double>::infinity();
754 TEST(Duration, DivisionByZero) {
758 const double dbl_inf = std::numeric_limits<double>::infinity();
759 const double dbl_denorm = std::numeric_limits<double>::denorm_min();
762 double z = 0.0, two = 2.0;
763 EXPECT_TRUE(std::isinf(two / z));
764 EXPECT_TRUE(std::isnan(z / z));
767 EXPECT_EQ(inf, zero / 0.0);
768 EXPECT_EQ(-inf, zero / -0.0);
769 EXPECT_EQ(inf, any_dur / 0.0);
770 EXPECT_EQ(-inf, any_dur / -0.0);
771 EXPECT_EQ(-inf, -any_dur / 0.0);
772 EXPECT_EQ(inf, -any_dur / -0.0);
775 EXPECT_EQ(zero, zero / dbl_denorm);
776 EXPECT_EQ(zero, zero / -dbl_denorm);
777 EXPECT_EQ(inf, any_dur / dbl_denorm);
778 EXPECT_EQ(-inf, any_dur / -dbl_denorm);
779 EXPECT_EQ(-inf, -any_dur / dbl_denorm);
780 EXPECT_EQ(inf, -any_dur / -dbl_denorm);
793 EXPECT_EQ(-inf, rem);
796 EXPECT_EQ(kint64max, zero / zero);
797 EXPECT_EQ(kint64max, any_dur / zero);
798 EXPECT_EQ(kint64min, -any_dur / zero);
806 TEST(Duration, NaN) {
809 #define TEST_NAN_HANDLING(NAME, NAN) \ 811 const auto inf = absl::InfiniteDuration(); \ 812 auto x = NAME(NAN); \ 813 EXPECT_TRUE(x == inf || x == -inf); \ 816 EXPECT_TRUE(y == inf || y == -inf); \ 819 EXPECT_TRUE(z == inf || z == -inf); \ 822 const double nan = std::numeric_limits<double>::quiet_NaN();
837 #undef TEST_NAN_HANDLING 840 TEST(Duration, Range) {
856 EXPECT_LT(neg_full_range, full_range);
857 EXPECT_EQ(neg_full_range, -full_range);
860 TEST(Duration, RelationalOperators) {
861 #define TEST_REL_OPS(UNIT) \ 862 static_assert(UNIT(2) == UNIT(2), ""); \ 863 static_assert(UNIT(1) != UNIT(2), ""); \ 864 static_assert(UNIT(1) < UNIT(2), ""); \ 865 static_assert(UNIT(3) > UNIT(2), ""); \ 866 static_assert(UNIT(1) <= UNIT(2), ""); \ 867 static_assert(UNIT(2) <= UNIT(2), ""); \ 868 static_assert(UNIT(3) >= UNIT(2), ""); \ 869 static_assert(UNIT(2) >= UNIT(2), ""); 881 TEST(Duration, Addition) {
882 #define TEST_ADD_OPS(UNIT) \ 884 EXPECT_EQ(UNIT(2), UNIT(1) + UNIT(1)); \ 885 EXPECT_EQ(UNIT(1), UNIT(2) - UNIT(1)); \ 886 EXPECT_EQ(UNIT(0), UNIT(2) - UNIT(2)); \ 887 EXPECT_EQ(UNIT(-1), UNIT(1) - UNIT(2)); \ 888 EXPECT_EQ(UNIT(-2), UNIT(0) - UNIT(2)); \ 889 EXPECT_EQ(UNIT(-2), UNIT(1) - UNIT(3)); \ 890 absl::Duration a = UNIT(1); \ 892 EXPECT_EQ(UNIT(2), a); \ 894 EXPECT_EQ(UNIT(1), a); \ 927 EXPECT_EQ(ans_3_8, d_7_5 - d_3_7);
935 TEST(Duration, Negation) {
962 constexpr
absl::Duration negated_nearly_min_duration = -nearly_min_duration;
964 EXPECT_EQ(negated_max_duration, nearly_min_duration);
965 EXPECT_EQ(negated_nearly_min_duration, max_duration);
966 EXPECT_EQ(-(-max_duration), max_duration);
974 TEST(Duration, AbsoluteValue) {
991 TEST(Duration, Multiplication) {
992 #define TEST_MUL_OPS(UNIT) \ 994 EXPECT_EQ(UNIT(5), UNIT(2) * 2.5); \ 995 EXPECT_EQ(UNIT(2), UNIT(5) / 2.5); \ 996 EXPECT_EQ(UNIT(-5), UNIT(-2) * 2.5); \ 997 EXPECT_EQ(UNIT(-5), -UNIT(2) * 2.5); \ 998 EXPECT_EQ(UNIT(-5), UNIT(2) * -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(5) / -2.5); \ 1002 EXPECT_EQ(UNIT(2), UNIT(11) % UNIT(3)); \ 1003 absl::Duration a = UNIT(2); \ 1005 EXPECT_EQ(UNIT(5), a); \ 1007 EXPECT_EQ(UNIT(2), a); \ 1009 EXPECT_EQ(UNIT(0), a); \ 1010 absl::Duration big = UNIT(1000000000); \ 1013 EXPECT_EQ(UNIT(1000000000), big); \ 1014 EXPECT_EQ(-UNIT(2), -UNIT(2)); \ 1015 EXPECT_EQ(-UNIT(2), UNIT(2) * -1); \ 1016 EXPECT_EQ(-UNIT(2), -1 * UNIT(2)); \ 1017 EXPECT_EQ(-UNIT(-2), UNIT(2)); \ 1018 EXPECT_EQ(2, UNIT(2) / UNIT(1)); \ 1019 absl::Duration rem; \ 1020 EXPECT_EQ(2, absl::IDivDuration(UNIT(2), UNIT(1), &rem)); \ 1021 EXPECT_EQ(2.0, absl::FDivDuration(UNIT(2), UNIT(1))); \ 1038 EXPECT_EQ(max_dur, max_dur * 1);
1039 EXPECT_EQ(max_dur, max_dur / 1);
1040 EXPECT_EQ(min_dur, min_dur * 1);
1041 EXPECT_EQ(min_dur, min_dur / 1);
1091 #define TEST_MOD_IDENTITY(a, b) \ 1092 EXPECT_EQ((a), ((a) / (b))*(b) + ((a)%(b))) 1125 #undef TEST_MOD_IDENTITY 1128 TEST(Duration, Truncation) {
1131 for (
int unit_sign : {1, -1}) {
1152 TEST(Duration, Flooring) {
1155 for (
int unit_sign : {1, -1}) {
1176 TEST(Duration, Ceiling) {
1179 for (
int unit_sign : {1, -1}) {
1200 TEST(Duration, RoundTripUnits) {
1201 const int kRange = 100000;
1203 #define ROUND_TRIP_UNIT(U, LOW, HIGH) \ 1205 for (int64_t i = LOW; i < HIGH; ++i) { \ 1206 absl::Duration d = absl::U(i); \ 1207 if (d == absl::InfiniteDuration()) \ 1208 EXPECT_EQ(kint64max, d / absl::U(1)); \ 1209 else if (d == -absl::InfiniteDuration()) \ 1210 EXPECT_EQ(kint64min, d / absl::U(1)); \ 1212 EXPECT_EQ(i, absl::U(i) / absl::U(1)); \ 1240 #undef ROUND_TRIP_UNIT 1243 TEST(Duration, TruncConversions) {
1260 for (
const auto& test : to_ts) {
1277 for (
const auto& test : from_ts) {
1297 for (
const auto& test : to_tv) {
1314 for (
const auto& test : from_tv) {
1319 TEST(Duration, SmallConversions) {
1373 void VerifySameAsMul(
double time_as_seconds,
int*
const misses) {
1375 auto mul_by_one_second = time_as_seconds *
absl::Seconds(1);
1376 if (direct_seconds != mul_by_one_second) {
1377 if (*misses > 10)
return;
1378 ASSERT_LE(++(*misses), 10) <<
"Too many errors, not reporting more.";
1379 EXPECT_EQ(direct_seconds, mul_by_one_second)
1380 <<
"given double time_as_seconds = " << std::setprecision(17)
1389 TEST(Duration, ToDoubleSecondsCheckEdgeCases) {
1394 uint32_t tick_vals[] = {0, +999, +999999, +999999999, kTicksPerSecond - 1,
1396 1, 1001, 1000001, 1000000001, kTicksPerSecond + 1,
1397 2, 1002, 1000002, 1000000002, kTicksPerSecond + 2,
1398 3, 1003, 1000003, 1000000003, kTicksPerSecond + 3,
1399 4, 1004, 1000004, 1000000004, kTicksPerSecond + 4,
1401 for (uint32_t ticks : tick_vals) {
1405 EXPECT_NE(d, after_d);
1406 EXPECT_EQ(after_d - d, duration_tick);
1415 double midpoint = low_edge + (high_edge - low_edge) / 2;
1416 if (midpoint == low_edge || midpoint == high_edge)
break;
1418 if (mid_duration == d) {
1419 low_edge = midpoint;
1421 EXPECT_EQ(mid_duration, after_d);
1422 high_edge = midpoint;
1427 VerifySameAsMul(low_edge, &misses);
1428 VerifySameAsMul(high_edge, &misses);
1434 TEST(Duration, ToDoubleSecondsCheckRandom) {
1435 std::random_device rd;
1436 std::seed_seq seed({rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd()});
1437 std::mt19937_64 gen(seed);
1443 std::uniform_real_distribution<double> uniform(
std::log(0.125e-9),
1446 for (
int i = 0;
i < 1000000; ++
i) {
1447 double d = std::exp(uniform(gen));
1448 VerifySameAsMul(d, &misses);
1449 VerifySameAsMul(-d, &misses);
1453 TEST(Duration, ConversionSaturation) {
1456 const auto max_timeval_sec =
1457 std::numeric_limits<decltype(timeval::tv_sec)>::max();
1458 const auto min_timeval_sec =
1459 std::numeric_limits<decltype(timeval::tv_sec)>::min();
1461 tv.tv_sec = max_timeval_sec;
1462 tv.tv_usec = 999998;
1465 EXPECT_EQ(max_timeval_sec, tv.tv_sec);
1466 EXPECT_EQ(999998, tv.tv_usec);
1469 EXPECT_EQ(max_timeval_sec, tv.tv_sec);
1470 EXPECT_EQ(999999, tv.tv_usec);
1473 EXPECT_EQ(max_timeval_sec, tv.tv_sec);
1474 EXPECT_EQ(999999, tv.tv_usec);
1476 tv.tv_sec = min_timeval_sec;
1480 EXPECT_EQ(min_timeval_sec, tv.tv_sec);
1481 EXPECT_EQ(1, tv.tv_usec);
1484 EXPECT_EQ(min_timeval_sec, tv.tv_sec);
1485 EXPECT_EQ(0, tv.tv_usec);
1488 EXPECT_EQ(min_timeval_sec, tv.tv_sec);
1489 EXPECT_EQ(0, tv.tv_usec);
1491 const auto max_timespec_sec =
1492 std::numeric_limits<decltype(timespec::tv_sec)>::max();
1493 const auto min_timespec_sec =
1494 std::numeric_limits<decltype(timespec::tv_sec)>::min();
1496 ts.tv_sec = max_timespec_sec;
1497 ts.tv_nsec = 999999998;
1500 EXPECT_EQ(max_timespec_sec, ts.tv_sec);
1501 EXPECT_EQ(999999998, ts.tv_nsec);
1504 EXPECT_EQ(max_timespec_sec, ts.tv_sec);
1505 EXPECT_EQ(999999999, ts.tv_nsec);
1508 EXPECT_EQ(max_timespec_sec, ts.tv_sec);
1509 EXPECT_EQ(999999999, ts.tv_nsec);
1511 ts.tv_sec = min_timespec_sec;
1515 EXPECT_EQ(min_timespec_sec, ts.tv_sec);
1516 EXPECT_EQ(1, ts.tv_nsec);
1519 EXPECT_EQ(min_timespec_sec, ts.tv_sec);
1520 EXPECT_EQ(0, ts.tv_nsec);
1523 EXPECT_EQ(min_timespec_sec, ts.tv_sec);
1524 EXPECT_EQ(0, ts.tv_nsec);
1529 EXPECT_EQ(
"72h3m0.5s",
1533 EXPECT_EQ(
"2540400h10m10s",
1552 EXPECT_EQ(
"1h0.25s",
1554 EXPECT_EQ(
"1m0.25s",
1556 EXPECT_EQ(
"1h1m0.25s",
1559 EXPECT_EQ(
"1h0.0005s",
1561 EXPECT_EQ(
"1h0.0000005s",
1579 EXPECT_EQ(
"1.000001ms",
1620 EXPECT_EQ(
"2562047788015215h30m7.99999999975s",
1626 EXPECT_EQ(
"55.00000025ms",
1640 EXPECT_EQ(
"876000000000000h0.999999999s",
1643 EXPECT_EQ(
"876000000000000h0.9999999995s",
1646 EXPECT_EQ(
"876000000000000h0.99999999975s",
1650 EXPECT_EQ(
"-876000000000000h0.999999999s",
1653 EXPECT_EQ(
"-876000000000000h0.9999999995s",
1656 EXPECT_EQ(
"-876000000000000h0.99999999975s",
1771 TEST(Duration, FormatParseRoundTrip) {
1772 #define TEST_PARSE_ROUNDTRIP(d) \ 1774 std::string s = absl::FormatDuration(d); \ 1775 absl::Duration dur; \ 1776 EXPECT_TRUE(absl::ParseDuration(s, &dur)); \ 1777 EXPECT_EQ(d, dur); \ 1806 #undef TEST_PARSE_ROUNDTRIP timespec ToTimespec(Duration d)
std::chrono::microseconds ToChronoMicroseconds(Duration d)
int64_t ToInt64Minutes(Duration d)
constexpr int64_t kTicksPerSecond
constexpr Duration Hours(int64_t n)
#define TEST_MUL_OPS(UNIT)
#define TEST_MOD_IDENTITY(a, b)
Duration DurationFromTimespec(timespec ts)
int64_t ToInt64Microseconds(Duration d)
double ToDoubleSeconds(Duration d)
std::chrono::minutes ToChronoMinutes(Duration d)
constexpr Duration Microseconds(int64_t n)
Duration Floor(const Duration d, const Duration unit)
std::chrono::duration< std::int_fast64_t > seconds
constexpr Duration Milliseconds(int64_t n)
#define TEST_DURATION_CONVERSION(UNIT)
#define TEST_PARSE_ROUNDTRIP(d)
#define TEST_FACTORY_OVERLOADS(NAME)
timeval ToTimeval(Duration d)
int64_t ToInt64Nanoseconds(Duration d)
int64_t ToInt64Seconds(Duration d)
#define TEST_INF_MUL_WITH_TYPE(T)
int64_t ToInt64Hours(Duration d)
constexpr Duration MakeDuration(int64_t hi, uint32_t lo)
std::chrono::seconds ToChronoSeconds(Duration d)
#define TEST_NAN_HANDLING(NAME, NAN)
std::chrono::hours ToChronoHours(Duration d)
std::string FormatDuration(Duration d)
int64_t IDivDuration(Duration num, Duration den, Duration *rem)
constexpr Duration Minutes(int64_t n)
#define ROUND_TRIP_UNIT(U, LOW, HIGH)
std::chrono::milliseconds ToChronoMilliseconds(Duration d)
int64_t ToInt64Milliseconds(Duration d)
double FDivDuration(Duration num, Duration den)
constexpr Duration Seconds(int64_t n)
Duration Trunc(Duration d, Duration unit)
TEST(Symbolize, Unimplemented)
Duration DurationFromTimeval(timeval tv)
constexpr Duration Nanoseconds(int64_t n)
#define TEST_ADD_OPS(UNIT)
Duration AbsDuration(Duration d)
#define TEST_REL_OPS(UNIT)
constexpr Duration InfiniteDuration()
constexpr Duration ZeroDuration()
#define TEST_INF_DIV_WITH_TYPE(T)
Time FromChrono(const std::chrono::system_clock::time_point &tp)
Duration Ceil(const Duration d, const Duration unit)
constexpr bool IsInfiniteDuration(Duration d)
bool ParseDuration(const std::string &dur_string, Duration *d)
std::chrono::nanoseconds ToChronoNanoseconds(Duration d)