bloaty/third_party/abseil-cpp/absl/time/duration_test.cc
Go to the documentation of this file.
1 // Copyright 2017 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #if defined(_MSC_VER)
16 #include <winsock2.h> // for timeval
17 #endif
18 
19 #include <chrono> // NOLINT(build/c++11)
20 #include <cmath>
21 #include <cstdint>
22 #include <ctime>
23 #include <iomanip>
24 #include <limits>
25 #include <random>
26 #include <string>
27 
28 #include "gmock/gmock.h"
29 #include "gtest/gtest.h"
30 #include "absl/time/time.h"
31 
32 namespace {
33 
36 
37 // Approximates the given number of years. This is only used to make some test
38 // code more readable.
39 absl::Duration ApproxYears(int64_t n) { return absl::Hours(n) * 365 * 24; }
40 
41 // A gMock matcher to match timespec values. Use this matcher like:
42 // timespec ts1, ts2;
43 // EXPECT_THAT(ts1, TimespecMatcher(ts2));
44 MATCHER_P(TimespecMatcher, ts, "") {
45  if (ts.tv_sec == arg.tv_sec && ts.tv_nsec == arg.tv_nsec)
46  return true;
47  *result_listener << "expected: {" << ts.tv_sec << ", " << ts.tv_nsec << "} ";
48  *result_listener << "actual: {" << arg.tv_sec << ", " << arg.tv_nsec << "}";
49  return false;
50 }
51 
52 // A gMock matcher to match timeval values. Use this matcher like:
53 // timeval tv1, tv2;
54 // EXPECT_THAT(tv1, TimevalMatcher(tv2));
55 MATCHER_P(TimevalMatcher, tv, "") {
56  if (tv.tv_sec == arg.tv_sec && tv.tv_usec == arg.tv_usec)
57  return true;
58  *result_listener << "expected: {" << tv.tv_sec << ", " << tv.tv_usec << "} ";
59  *result_listener << "actual: {" << arg.tv_sec << ", " << arg.tv_usec << "}";
60  return false;
61 }
62 
63 TEST(Duration, ConstExpr) {
64  constexpr absl::Duration d0 = absl::ZeroDuration();
65  static_assert(d0 == absl::ZeroDuration(), "ZeroDuration()");
66  constexpr absl::Duration d1 = absl::Seconds(1);
67  static_assert(d1 == absl::Seconds(1), "Seconds(1)");
68  static_assert(d1 != absl::ZeroDuration(), "Seconds(1)");
70  static_assert(d2 == absl::InfiniteDuration(), "InfiniteDuration()");
71  static_assert(d2 != absl::ZeroDuration(), "InfiniteDuration()");
72 }
73 
74 TEST(Duration, ValueSemantics) {
75  // If this compiles, the test passes.
76  constexpr absl::Duration a; // Default construction
77  constexpr absl::Duration b = a; // Copy construction
78  constexpr absl::Duration c(b); // Copy construction (again)
79 
81  d = c; // Assignment
82 }
83 
84 TEST(Duration, Factories) {
85  constexpr absl::Duration zero = absl::ZeroDuration();
86  constexpr absl::Duration nano = absl::Nanoseconds(1);
87  constexpr absl::Duration micro = absl::Microseconds(1);
88  constexpr absl::Duration milli = absl::Milliseconds(1);
89  constexpr absl::Duration sec = absl::Seconds(1);
90  constexpr absl::Duration min = absl::Minutes(1);
91  constexpr absl::Duration hour = absl::Hours(1);
92 
93  EXPECT_EQ(zero, absl::Duration());
94  EXPECT_EQ(zero, absl::Seconds(0));
95  EXPECT_EQ(nano, absl::Nanoseconds(1));
96  EXPECT_EQ(micro, absl::Nanoseconds(1000));
97  EXPECT_EQ(milli, absl::Microseconds(1000));
98  EXPECT_EQ(sec, absl::Milliseconds(1000));
100  EXPECT_EQ(hour, absl::Minutes(60));
101 
102  // Tests factory limits
104 
108 
115 
120  EXPECT_LT(-inf, absl::Hours(kint64min / 3600));
121  EXPECT_LT(-inf, absl::Hours(-kint64max / 3600));
122 }
123 
124 TEST(Duration, ToConversion) {
125 #define TEST_DURATION_CONVERSION(UNIT) \
126  do { \
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)); \
141  } while (0)
142 
149 
150 #undef TEST_DURATION_CONVERSION
151 }
152 
153 template <int64_t N>
154 void TestToConversion() {
155  constexpr absl::Duration nano = absl::Nanoseconds(N);
161  EXPECT_EQ(0, absl::ToInt64Hours(nano));
162  const absl::Duration micro = absl::Microseconds(N);
163  EXPECT_EQ(N * 1000, absl::ToInt64Nanoseconds(micro));
166  EXPECT_EQ(0, absl::ToInt64Seconds(micro));
167  EXPECT_EQ(0, absl::ToInt64Minutes(micro));
168  EXPECT_EQ(0, absl::ToInt64Hours(micro));
169  const absl::Duration milli = absl::Milliseconds(N);
170  EXPECT_EQ(N * 1000 * 1000, absl::ToInt64Nanoseconds(milli));
171  EXPECT_EQ(N * 1000, absl::ToInt64Microseconds(milli));
173  EXPECT_EQ(0, absl::ToInt64Seconds(milli));
174  EXPECT_EQ(0, absl::ToInt64Minutes(milli));
175  EXPECT_EQ(0, absl::ToInt64Hours(milli));
176  const absl::Duration sec = absl::Seconds(N);
177  EXPECT_EQ(N * 1000 * 1000 * 1000, absl::ToInt64Nanoseconds(sec));
178  EXPECT_EQ(N * 1000 * 1000, absl::ToInt64Microseconds(sec));
179  EXPECT_EQ(N * 1000, absl::ToInt64Milliseconds(sec));
182  EXPECT_EQ(0, absl::ToInt64Hours(sec));
184  EXPECT_EQ(N * 60 * 1000 * 1000 * 1000, absl::ToInt64Nanoseconds(min));
185  EXPECT_EQ(N * 60 * 1000 * 1000, absl::ToInt64Microseconds(min));
186  EXPECT_EQ(N * 60 * 1000, absl::ToInt64Milliseconds(min));
190  const absl::Duration hour = absl::Hours(N);
191  EXPECT_EQ(N * 60 * 60 * 1000 * 1000 * 1000, absl::ToInt64Nanoseconds(hour));
192  EXPECT_EQ(N * 60 * 60 * 1000 * 1000, absl::ToInt64Microseconds(hour));
193  EXPECT_EQ(N * 60 * 60 * 1000, absl::ToInt64Milliseconds(hour));
194  EXPECT_EQ(N * 60 * 60, absl::ToInt64Seconds(hour));
195  EXPECT_EQ(N * 60, absl::ToInt64Minutes(hour));
197 }
198 
199 TEST(Duration, ToConversionDeprecated) {
200  TestToConversion<43>();
201  TestToConversion<1>();
202  TestToConversion<0>();
203  TestToConversion<-1>();
204  TestToConversion<-43>();
205 }
206 
207 template <int64_t N>
208 void TestFromChronoBasicEquality() {
209  using std::chrono::nanoseconds;
210  using std::chrono::microseconds;
211  using std::chrono::milliseconds;
212  using std::chrono::seconds;
213  using std::chrono::minutes;
214  using std::chrono::hours;
215 
216  static_assert(absl::Nanoseconds(N) == absl::FromChrono(nanoseconds(N)), "");
217  static_assert(absl::Microseconds(N) == absl::FromChrono(microseconds(N)), "");
218  static_assert(absl::Milliseconds(N) == absl::FromChrono(milliseconds(N)), "");
219  static_assert(absl::Seconds(N) == absl::FromChrono(seconds(N)), "");
220  static_assert(absl::Minutes(N) == absl::FromChrono(minutes(N)), "");
221  static_assert(absl::Hours(N) == absl::FromChrono(hours(N)), "");
222 }
223 
225  TestFromChronoBasicEquality<-123>();
226  TestFromChronoBasicEquality<-1>();
227  TestFromChronoBasicEquality<0>();
228  TestFromChronoBasicEquality<1>();
229  TestFromChronoBasicEquality<123>();
230 
231  // Minutes (might, depending on the platform) saturate at +inf.
232  const auto chrono_minutes_max = std::chrono::minutes::max();
233  const auto minutes_max = absl::FromChrono(chrono_minutes_max);
234  const int64_t minutes_max_count = chrono_minutes_max.count();
235  if (minutes_max_count > kint64max / 60) {
236  EXPECT_EQ(absl::InfiniteDuration(), minutes_max);
237  } else {
238  EXPECT_EQ(absl::Minutes(minutes_max_count), minutes_max);
239  }
240 
241  // Minutes (might, depending on the platform) saturate at -inf.
242  const auto chrono_minutes_min = std::chrono::minutes::min();
243  const auto minutes_min = absl::FromChrono(chrono_minutes_min);
244  const int64_t minutes_min_count = chrono_minutes_min.count();
245  if (minutes_min_count < kint64min / 60) {
246  EXPECT_EQ(-absl::InfiniteDuration(), minutes_min);
247  } else {
248  EXPECT_EQ(absl::Minutes(minutes_min_count), minutes_min);
249  }
250 
251  // Hours (might, depending on the platform) saturate at +inf.
252  const auto chrono_hours_max = std::chrono::hours::max();
253  const auto hours_max = absl::FromChrono(chrono_hours_max);
254  const int64_t hours_max_count = chrono_hours_max.count();
255  if (hours_max_count > kint64max / 3600) {
256  EXPECT_EQ(absl::InfiniteDuration(), hours_max);
257  } else {
258  EXPECT_EQ(absl::Hours(hours_max_count), hours_max);
259  }
260 
261  // Hours (might, depending on the platform) saturate at -inf.
262  const auto chrono_hours_min = std::chrono::hours::min();
263  const auto hours_min = absl::FromChrono(chrono_hours_min);
264  const int64_t hours_min_count = chrono_hours_min.count();
265  if (hours_min_count < kint64min / 3600) {
266  EXPECT_EQ(-absl::InfiniteDuration(), hours_min);
267  } else {
268  EXPECT_EQ(absl::Hours(hours_min_count), hours_min);
269  }
270 }
271 
272 template <int64_t N>
273 void TestToChrono() {
274  using std::chrono::nanoseconds;
275  using std::chrono::microseconds;
276  using std::chrono::milliseconds;
277  using std::chrono::seconds;
278  using std::chrono::minutes;
279  using std::chrono::hours;
280 
285 
286  constexpr auto absl_minutes = absl::Minutes(N);
287  auto chrono_minutes = minutes(N);
288  if (absl_minutes == -absl::InfiniteDuration()) {
289  chrono_minutes = minutes::min();
290  } else if (absl_minutes == absl::InfiniteDuration()) {
291  chrono_minutes = minutes::max();
292  }
293  EXPECT_EQ(chrono_minutes, absl::ToChronoMinutes(absl_minutes));
294 
295  constexpr auto absl_hours = absl::Hours(N);
296  auto chrono_hours = hours(N);
297  if (absl_hours == -absl::InfiniteDuration()) {
298  chrono_hours = hours::min();
299  } else if (absl_hours == absl::InfiniteDuration()) {
300  chrono_hours = hours::max();
301  }
302  EXPECT_EQ(chrono_hours, absl::ToChronoHours(absl_hours));
303 }
304 
305 TEST(Duration, ToChrono) {
306  using std::chrono::nanoseconds;
307  using std::chrono::microseconds;
308  using std::chrono::milliseconds;
309  using std::chrono::seconds;
310  using std::chrono::minutes;
311  using std::chrono::hours;
312 
313  TestToChrono<kint64min>();
314  TestToChrono<-1>();
315  TestToChrono<0>();
316  TestToChrono<1>();
317  TestToChrono<kint64max>();
318 
319  // Verify truncation toward zero.
320  const auto tick = absl::Nanoseconds(1) / 4;
321  EXPECT_EQ(nanoseconds(0), absl::ToChronoNanoseconds(tick));
322  EXPECT_EQ(nanoseconds(0), absl::ToChronoNanoseconds(-tick));
323  EXPECT_EQ(microseconds(0), absl::ToChronoMicroseconds(tick));
324  EXPECT_EQ(microseconds(0), absl::ToChronoMicroseconds(-tick));
325  EXPECT_EQ(milliseconds(0), absl::ToChronoMilliseconds(tick));
326  EXPECT_EQ(milliseconds(0), absl::ToChronoMilliseconds(-tick));
329  EXPECT_EQ(minutes(0), absl::ToChronoMinutes(tick));
330  EXPECT_EQ(minutes(0), absl::ToChronoMinutes(-tick));
331  EXPECT_EQ(hours(0), absl::ToChronoHours(tick));
332  EXPECT_EQ(hours(0), absl::ToChronoHours(-tick));
333 
334  // Verifies +/- infinity saturation at max/min.
335  constexpr auto inf = absl::InfiniteDuration();
348 }
349 
350 TEST(Duration, FactoryOverloads) {
351  enum E { kOne = 1 };
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)));
366 
373 
374 #undef TEST_FACTORY_OVERLOADS
375 
379 
380  const double dbl_inf = std::numeric_limits<double>::infinity();
393 }
394 
395 TEST(Duration, InfinityExamples) {
396  // These examples are used in the documentation in time.h. They are
397  // written so that they can be copy-n-pasted easily.
398 
400  constexpr absl::Duration d = absl::Seconds(1); // Any finite duration
401 
402  EXPECT_TRUE(inf == inf + inf);
403  EXPECT_TRUE(inf == inf + d);
404  EXPECT_TRUE(inf == inf - inf);
405  EXPECT_TRUE(-inf == d - inf);
406 
407  EXPECT_TRUE(inf == d * 1e100);
408  EXPECT_TRUE(0 == d / inf); // NOLINT(readability/check)
409 
410  // Division by zero returns infinity, or kint64min/MAX where necessary.
411  EXPECT_TRUE(inf == d / 0);
413 }
414 
415 TEST(Duration, InfinityComparison) {
417  const absl::Duration any_dur = absl::Seconds(1);
418 
419  // Equality
420  EXPECT_EQ(inf, inf);
421  EXPECT_EQ(-inf, -inf);
422  EXPECT_NE(inf, -inf);
423  EXPECT_NE(any_dur, inf);
424  EXPECT_NE(any_dur, -inf);
425 
426  // Relational
427  EXPECT_GT(inf, any_dur);
428  EXPECT_LT(-inf, any_dur);
429  EXPECT_LT(-inf, inf);
430  EXPECT_GT(inf, -inf);
431 }
432 
433 TEST(Duration, InfinityAddition) {
434  const absl::Duration sec_max = absl::Seconds(kint64max);
435  const absl::Duration sec_min = absl::Seconds(kint64min);
436  const absl::Duration any_dur = absl::Seconds(1);
438 
439  // Addition
440  EXPECT_EQ(inf, inf + inf);
441  EXPECT_EQ(inf, inf + -inf);
442  EXPECT_EQ(-inf, -inf + inf);
443  EXPECT_EQ(-inf, -inf + -inf);
444 
445  EXPECT_EQ(inf, inf + any_dur);
446  EXPECT_EQ(inf, any_dur + inf);
447  EXPECT_EQ(-inf, -inf + any_dur);
448  EXPECT_EQ(-inf, any_dur + -inf);
449 
450  // Interesting case
451  absl::Duration almost_inf = sec_max + absl::Nanoseconds(999999999);
452  EXPECT_GT(inf, almost_inf);
453  almost_inf += -absl::Nanoseconds(999999999);
454  EXPECT_GT(inf, almost_inf);
455 
456  // Addition overflow/underflow
457  EXPECT_EQ(inf, sec_max + absl::Seconds(1));
458  EXPECT_EQ(inf, sec_max + sec_max);
459  EXPECT_EQ(-inf, sec_min + -absl::Seconds(1));
460  EXPECT_EQ(-inf, sec_min + -sec_max);
461 
462  // For reference: IEEE 754 behavior
463  const double dbl_inf = std::numeric_limits<double>::infinity();
464  EXPECT_TRUE(std::isinf(dbl_inf + dbl_inf));
465  EXPECT_TRUE(std::isnan(dbl_inf + -dbl_inf)); // We return inf
466  EXPECT_TRUE(std::isnan(-dbl_inf + dbl_inf)); // We return inf
467  EXPECT_TRUE(std::isinf(-dbl_inf + -dbl_inf));
468 }
469 
470 TEST(Duration, InfinitySubtraction) {
471  const absl::Duration sec_max = absl::Seconds(kint64max);
472  const absl::Duration sec_min = absl::Seconds(kint64min);
473  const absl::Duration any_dur = absl::Seconds(1);
475 
476  // Subtraction
477  EXPECT_EQ(inf, inf - inf);
478  EXPECT_EQ(inf, inf - -inf);
479  EXPECT_EQ(-inf, -inf - inf);
480  EXPECT_EQ(-inf, -inf - -inf);
481 
482  EXPECT_EQ(inf, inf - any_dur);
483  EXPECT_EQ(-inf, any_dur - inf);
484  EXPECT_EQ(-inf, -inf - any_dur);
485  EXPECT_EQ(inf, any_dur - -inf);
486 
487  // Subtraction overflow/underflow
488  EXPECT_EQ(inf, sec_max - -absl::Seconds(1));
489  EXPECT_EQ(inf, sec_max - -sec_max);
490  EXPECT_EQ(-inf, sec_min - absl::Seconds(1));
491  EXPECT_EQ(-inf, sec_min - sec_max);
492 
493  // Interesting case
494  absl::Duration almost_neg_inf = sec_min;
495  EXPECT_LT(-inf, almost_neg_inf);
496  almost_neg_inf -= -absl::Nanoseconds(1);
497  EXPECT_LT(-inf, almost_neg_inf);
498 
499  // For reference: IEEE 754 behavior
500  const double dbl_inf = std::numeric_limits<double>::infinity();
501  EXPECT_TRUE(std::isnan(dbl_inf - dbl_inf)); // We return inf
502  EXPECT_TRUE(std::isinf(dbl_inf - -dbl_inf));
503  EXPECT_TRUE(std::isinf(-dbl_inf - dbl_inf));
504  EXPECT_TRUE(std::isnan(-dbl_inf - -dbl_inf)); // We return inf
505 }
506 
507 TEST(Duration, InfinityMultiplication) {
508  const absl::Duration sec_max = absl::Seconds(kint64max);
509  const absl::Duration sec_min = absl::Seconds(kint64min);
511 
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));
525 
526  TEST_INF_MUL_WITH_TYPE(int64_t); // NOLINT(readability/function)
527  TEST_INF_MUL_WITH_TYPE(double); // NOLINT(readability/function)
528 
529 #undef TEST_INF_MUL_WITH_TYPE
530 
531  const double dbl_inf = std::numeric_limits<double>::infinity();
532  EXPECT_EQ(inf, inf * dbl_inf);
533  EXPECT_EQ(-inf, -inf * dbl_inf);
534  EXPECT_EQ(-inf, inf * -dbl_inf);
535  EXPECT_EQ(inf, -inf * -dbl_inf);
536 
537  const absl::Duration any_dur = absl::Seconds(1);
538  EXPECT_EQ(inf, any_dur * dbl_inf);
539  EXPECT_EQ(-inf, -any_dur * dbl_inf);
540  EXPECT_EQ(-inf, any_dur * -dbl_inf);
541  EXPECT_EQ(inf, -any_dur * -dbl_inf);
542 
543  // Fixed-point multiplication will produce a finite value, whereas floating
544  // point fuzziness will overflow to inf.
546  EXPECT_EQ(inf, absl::Seconds(1) * static_cast<double>(kint64max));
548  EXPECT_EQ(-inf, absl::Seconds(1) * static_cast<double>(kint64min));
549 
550  // Note that sec_max * or / by 1.0 overflows to inf due to the 53-bit
551  // limitations of double.
552  EXPECT_NE(inf, sec_max);
553  EXPECT_NE(inf, sec_max / 1);
554  EXPECT_EQ(inf, sec_max / 1.0);
555  EXPECT_NE(inf, sec_max * 1);
556  EXPECT_EQ(inf, sec_max * 1.0);
557 }
558 
559 TEST(Duration, InfinityDivision) {
560  const absl::Duration sec_max = absl::Seconds(kint64max);
561  const absl::Duration sec_min = absl::Seconds(kint64min);
563 
564  // Division of Duration by a double
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));
570 
571  TEST_INF_DIV_WITH_TYPE(int64_t); // NOLINT(readability/function)
572  TEST_INF_DIV_WITH_TYPE(double); // NOLINT(readability/function)
573 
574 #undef TEST_INF_DIV_WITH_TYPE
575 
576  // Division of Duration by a double overflow/underflow
577  EXPECT_EQ(inf, sec_max / 0.5);
578  EXPECT_EQ(inf, sec_min / -0.5);
579  EXPECT_EQ(inf, ((sec_max / 0.5) + absl::Seconds(1)) / 0.5);
580  EXPECT_EQ(-inf, sec_max / -0.5);
581  EXPECT_EQ(-inf, sec_min / 0.5);
582  EXPECT_EQ(-inf, ((sec_min / 0.5) - absl::Seconds(1)) / 0.5);
583 
584  const double dbl_inf = std::numeric_limits<double>::infinity();
585  EXPECT_EQ(inf, inf / dbl_inf);
586  EXPECT_EQ(-inf, inf / -dbl_inf);
587  EXPECT_EQ(-inf, -inf / dbl_inf);
588  EXPECT_EQ(inf, -inf / -dbl_inf);
589 
590  const absl::Duration any_dur = absl::Seconds(1);
591  EXPECT_EQ(absl::ZeroDuration(), any_dur / dbl_inf);
592  EXPECT_EQ(absl::ZeroDuration(), any_dur / -dbl_inf);
593  EXPECT_EQ(absl::ZeroDuration(), -any_dur / dbl_inf);
594  EXPECT_EQ(absl::ZeroDuration(), -any_dur / -dbl_inf);
595 }
596 
597 TEST(Duration, InfinityModulus) {
598  const absl::Duration sec_max = absl::Seconds(kint64max);
599  const absl::Duration any_dur = absl::Seconds(1);
601 
602  EXPECT_EQ(inf, inf % inf);
603  EXPECT_EQ(inf, inf % -inf);
604  EXPECT_EQ(-inf, -inf % -inf);
605  EXPECT_EQ(-inf, -inf % inf);
606 
607  EXPECT_EQ(any_dur, any_dur % inf);
608  EXPECT_EQ(any_dur, any_dur % -inf);
609  EXPECT_EQ(-any_dur, -any_dur % inf);
610  EXPECT_EQ(-any_dur, -any_dur % -inf);
611 
612  EXPECT_EQ(inf, inf % -any_dur);
613  EXPECT_EQ(inf, inf % any_dur);
614  EXPECT_EQ(-inf, -inf % -any_dur);
615  EXPECT_EQ(-inf, -inf % any_dur);
616 
617  // Remainder isn't affected by overflow.
618  EXPECT_EQ(absl::ZeroDuration(), sec_max % absl::Seconds(1));
622  EXPECT_EQ(absl::ZeroDuration(), sec_max % absl::Nanoseconds(1) / 4);
623 }
624 
625 TEST(Duration, InfinityIDiv) {
626  const absl::Duration sec_max = absl::Seconds(kint64max);
627  const absl::Duration any_dur = absl::Seconds(1);
629  const double dbl_inf = std::numeric_limits<double>::infinity();
630 
631  // IDivDuration (int64_t return value + a remainer)
634  EXPECT_EQ(inf, rem);
635 
636  rem = absl::ZeroDuration();
638  EXPECT_EQ(-inf, rem);
639 
640  rem = absl::ZeroDuration();
641  EXPECT_EQ(kint64max, absl::IDivDuration(inf, any_dur, &rem));
642  EXPECT_EQ(inf, rem);
643 
644  rem = absl::ZeroDuration();
645  EXPECT_EQ(0, absl::IDivDuration(any_dur, inf, &rem));
646  EXPECT_EQ(any_dur, rem);
647 
648  rem = absl::ZeroDuration();
649  EXPECT_EQ(kint64max, absl::IDivDuration(-inf, -any_dur, &rem));
650  EXPECT_EQ(-inf, rem);
651 
652  rem = absl::ZeroDuration();
653  EXPECT_EQ(0, absl::IDivDuration(-any_dur, -inf, &rem));
654  EXPECT_EQ(-any_dur, rem);
655 
656  rem = absl::ZeroDuration();
658  EXPECT_EQ(-inf, rem);
659 
660  rem = absl::ZeroDuration();
662  EXPECT_EQ(inf, rem);
663 
664  rem = absl::ZeroDuration();
665  EXPECT_EQ(kint64min, absl::IDivDuration(-inf, any_dur, &rem));
666  EXPECT_EQ(-inf, rem);
667 
668  rem = absl::ZeroDuration();
669  EXPECT_EQ(0, absl::IDivDuration(-any_dur, inf, &rem));
670  EXPECT_EQ(-any_dur, rem);
671 
672  rem = absl::ZeroDuration();
673  EXPECT_EQ(kint64min, absl::IDivDuration(inf, -any_dur, &rem));
674  EXPECT_EQ(inf, rem);
675 
676  rem = absl::ZeroDuration();
677  EXPECT_EQ(0, absl::IDivDuration(any_dur, -inf, &rem));
678  EXPECT_EQ(any_dur, rem);
679 
680  // IDivDuration overflow/underflow
681  rem = any_dur;
683  absl::IDivDuration(sec_max, absl::Nanoseconds(1) / 4, &rem));
684  EXPECT_EQ(sec_max - absl::Nanoseconds(kint64max) / 4, rem);
685 
686  rem = any_dur;
688  absl::IDivDuration(sec_max, absl::Milliseconds(1), &rem));
689  EXPECT_EQ(sec_max - absl::Milliseconds(kint64max), rem);
690 
691  rem = any_dur;
693  absl::IDivDuration(-sec_max, -absl::Milliseconds(1), &rem));
694  EXPECT_EQ(-sec_max + absl::Milliseconds(kint64max), rem);
695 
696  rem = any_dur;
698  absl::IDivDuration(-sec_max, absl::Milliseconds(1), &rem));
699  EXPECT_EQ(-sec_max - absl::Milliseconds(kint64min), rem);
700 
701  rem = any_dur;
703  absl::IDivDuration(sec_max, -absl::Milliseconds(1), &rem));
704  EXPECT_EQ(sec_max + absl::Milliseconds(kint64min), rem);
705 
706  //
707  // operator/(Duration, Duration) is a wrapper for IDivDuration().
708  //
709 
710  // IEEE 754 says inf / inf should be nan, but int64_t doesn't have
711  // nan so we'll return kint64max/kint64min instead.
712  EXPECT_TRUE(std::isnan(dbl_inf / dbl_inf));
714  EXPECT_EQ(kint64max, -inf / -inf);
715  EXPECT_EQ(kint64min, -inf / inf);
716  EXPECT_EQ(kint64min, inf / -inf);
717 
718  EXPECT_TRUE(std::isinf(dbl_inf / 2.0));
719  EXPECT_EQ(kint64max, inf / any_dur);
720  EXPECT_EQ(kint64max, -inf / -any_dur);
721  EXPECT_EQ(kint64min, -inf / any_dur);
722  EXPECT_EQ(kint64min, inf / -any_dur);
723 
724  EXPECT_EQ(0.0, 2.0 / dbl_inf);
725  EXPECT_EQ(0, any_dur / inf);
726  EXPECT_EQ(0, any_dur / -inf);
727  EXPECT_EQ(0, -any_dur / inf);
728  EXPECT_EQ(0, -any_dur / -inf);
730 
731  // Division of Duration by a Duration overflow/underflow
732  EXPECT_EQ(kint64max, sec_max / absl::Milliseconds(1));
733  EXPECT_EQ(kint64max, -sec_max / -absl::Milliseconds(1));
734  EXPECT_EQ(kint64min, -sec_max / absl::Milliseconds(1));
735  EXPECT_EQ(kint64min, sec_max / -absl::Milliseconds(1));
736 }
737 
738 TEST(Duration, InfinityFDiv) {
739  const absl::Duration any_dur = absl::Seconds(1);
741  const double dbl_inf = std::numeric_limits<double>::infinity();
742 
743  EXPECT_EQ(dbl_inf, absl::FDivDuration(inf, inf));
744  EXPECT_EQ(dbl_inf, absl::FDivDuration(-inf, -inf));
745  EXPECT_EQ(dbl_inf, absl::FDivDuration(inf, any_dur));
746  EXPECT_EQ(0.0, absl::FDivDuration(any_dur, inf));
747  EXPECT_EQ(dbl_inf, absl::FDivDuration(-inf, -any_dur));
748  EXPECT_EQ(0.0, absl::FDivDuration(-any_dur, -inf));
749 
750  EXPECT_EQ(-dbl_inf, absl::FDivDuration(-inf, inf));
751  EXPECT_EQ(-dbl_inf, absl::FDivDuration(inf, -inf));
752  EXPECT_EQ(-dbl_inf, absl::FDivDuration(-inf, any_dur));
753  EXPECT_EQ(0.0, absl::FDivDuration(-any_dur, inf));
754  EXPECT_EQ(-dbl_inf, absl::FDivDuration(inf, -any_dur));
755  EXPECT_EQ(0.0, absl::FDivDuration(any_dur, -inf));
756 }
757 
758 TEST(Duration, DivisionByZero) {
759  const absl::Duration zero = absl::ZeroDuration();
761  const absl::Duration any_dur = absl::Seconds(1);
762  const double dbl_inf = std::numeric_limits<double>::infinity();
763  const double dbl_denorm = std::numeric_limits<double>::denorm_min();
764 
765  // Operator/(Duration, double)
766  EXPECT_EQ(inf, zero / 0.0);
767  EXPECT_EQ(-inf, zero / -0.0);
768  EXPECT_EQ(inf, any_dur / 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 
773  // Tests dividing by a number very close to, but not quite zero.
774  EXPECT_EQ(zero, zero / dbl_denorm);
775  EXPECT_EQ(zero, zero / -dbl_denorm);
776  EXPECT_EQ(inf, any_dur / 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 
781  // IDiv
782  absl::Duration rem = zero;
783  EXPECT_EQ(kint64max, absl::IDivDuration(zero, zero, &rem));
784  EXPECT_EQ(inf, rem);
785 
786  rem = zero;
787  EXPECT_EQ(kint64max, absl::IDivDuration(any_dur, zero, &rem));
788  EXPECT_EQ(inf, rem);
789 
790  rem = zero;
791  EXPECT_EQ(kint64min, absl::IDivDuration(-any_dur, zero, &rem));
792  EXPECT_EQ(-inf, rem);
793 
794  // Operator/(Duration, Duration)
795  EXPECT_EQ(kint64max, zero / zero);
796  EXPECT_EQ(kint64max, any_dur / zero);
797  EXPECT_EQ(kint64min, -any_dur / zero);
798 
799  // FDiv
800  EXPECT_EQ(dbl_inf, absl::FDivDuration(zero, zero));
801  EXPECT_EQ(dbl_inf, absl::FDivDuration(any_dur, zero));
802  EXPECT_EQ(-dbl_inf, absl::FDivDuration(-any_dur, zero));
803 }
804 
805 TEST(Duration, NaN) {
806  // Note that IEEE 754 does not define the behavior of a nan's sign when it is
807  // copied, so the code below allows for either + or - InfiniteDuration.
808 #define TEST_NAN_HANDLING(NAME, NAN) \
809  do { \
810  const auto inf = absl::InfiniteDuration(); \
811  auto x = NAME(NAN); \
812  EXPECT_TRUE(x == inf || x == -inf); \
813  auto y = NAME(42); \
814  y *= NAN; \
815  EXPECT_TRUE(y == inf || y == -inf); \
816  auto z = NAME(42); \
817  z /= NAN; \
818  EXPECT_TRUE(z == inf || z == -inf); \
819  } while (0)
820 
821  const double nan = std::numeric_limits<double>::quiet_NaN();
828 
835 
836 #undef TEST_NAN_HANDLING
837 }
838 
839 TEST(Duration, Range) {
840  const absl::Duration range = ApproxYears(100 * 1e9);
841  const absl::Duration range_future = range;
842  const absl::Duration range_past = -range;
843 
844  EXPECT_LT(range_future, absl::InfiniteDuration());
845  EXPECT_GT(range_past, -absl::InfiniteDuration());
846 
847  const absl::Duration full_range = range_future - range_past;
848  EXPECT_GT(full_range, absl::ZeroDuration());
849  EXPECT_LT(full_range, absl::InfiniteDuration());
850 
851  const absl::Duration neg_full_range = range_past - range_future;
852  EXPECT_LT(neg_full_range, absl::ZeroDuration());
853  EXPECT_GT(neg_full_range, -absl::InfiniteDuration());
854 
855  EXPECT_LT(neg_full_range, full_range);
856  EXPECT_EQ(neg_full_range, -full_range);
857 }
858 
859 TEST(Duration, RelationalOperators) {
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), "");
869 
876 
877 #undef TEST_REL_OPS
878 }
879 
880 TEST(Duration, Addition) {
881 #define TEST_ADD_OPS(UNIT) \
882  do { \
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); \
890  a += UNIT(1); \
891  EXPECT_EQ(UNIT(2), a); \
892  a -= UNIT(1); \
893  EXPECT_EQ(UNIT(1), a); \
894  } while (0)
895 
902 
903 #undef TEST_ADD_OPS
904 
908 
911 
914 
915  // Tests fractions of a nanoseconds. These are implementation details only.
918  absl::Nanoseconds(1) / 2 + absl::Nanoseconds(1) / 2);
921 
922  // Tests subtraction that will cause wrap around of the rep_lo_ bits.
925  absl::Duration ans_3_8 = absl::Seconds(3) + absl::Milliseconds(800);
926  EXPECT_EQ(ans_3_8, d_7_5 - d_3_7);
927 
928  // Subtracting min_duration
930  EXPECT_EQ(absl::Seconds(0), min_dur - min_dur);
932 }
933 
934 TEST(Duration, Negation) {
935  // By storing negations of various values in constexpr variables we
936  // verify that the initializers are constant expressions.
937  constexpr absl::Duration negated_zero_duration = -absl::ZeroDuration();
938  EXPECT_EQ(negated_zero_duration, absl::ZeroDuration());
939 
940  constexpr absl::Duration negated_infinite_duration =
942  EXPECT_NE(negated_infinite_duration, absl::InfiniteDuration());
943  EXPECT_EQ(-negated_infinite_duration, absl::InfiniteDuration());
944 
945  // The public APIs to check if a duration is infinite depend on using
946  // -InfiniteDuration(), but we're trying to test operator- here, so we
947  // need to use the lower-level internal query IsInfiniteDuration.
948  EXPECT_TRUE(
949  absl::time_internal::IsInfiniteDuration(negated_infinite_duration));
950 
951  // The largest Duration is kint64max seconds and kTicksPerSecond - 1 ticks.
952  // Using the absl::time_internal::MakeDuration API is the cleanest way to
953  // construct that Duration.
954  constexpr absl::Duration max_duration = absl::time_internal::MakeDuration(
956  constexpr absl::Duration negated_max_duration = -max_duration;
957  // The largest negatable value is one tick above the minimum representable;
958  // it's the negation of max_duration.
959  constexpr absl::Duration nearly_min_duration =
961  constexpr absl::Duration negated_nearly_min_duration = -nearly_min_duration;
962 
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);
966 
967  constexpr absl::Duration min_duration =
969  constexpr absl::Duration negated_min_duration = -min_duration;
970  EXPECT_EQ(negated_min_duration, absl::InfiniteDuration());
971 }
972 
973 TEST(Duration, AbsoluteValue) {
977 
980 
981  absl::Duration max_dur =
983  EXPECT_EQ(max_dur, AbsDuration(max_dur));
984 
987  EXPECT_EQ(max_dur, AbsDuration(min_dur + absl::Nanoseconds(1) / 4));
988 }
989 
990 TEST(Duration, Multiplication) {
991 #define TEST_MUL_OPS(UNIT) \
992  do { \
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); \
1003  a *= 2.5; \
1004  EXPECT_EQ(UNIT(5), a); \
1005  a /= 2.5; \
1006  EXPECT_EQ(UNIT(2), a); \
1007  a %= UNIT(1); \
1008  EXPECT_EQ(UNIT(0), a); \
1009  absl::Duration big = UNIT(1000000000); \
1010  big *= 3; \
1011  big /= 3; \
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))); \
1021  } while (0)
1022 
1029 
1030 #undef TEST_MUL_OPS
1031 
1032  // Ensures that multiplication and division by 1 with a maxed-out durations
1033  // doesn't lose precision.
1034  absl::Duration max_dur =
1037  EXPECT_EQ(max_dur, max_dur * 1);
1038  EXPECT_EQ(max_dur, max_dur / 1);
1039  EXPECT_EQ(min_dur, min_dur * 1);
1040  EXPECT_EQ(min_dur, min_dur / 1);
1041 
1042  // Tests division on a Duration with a large number of significant digits.
1043  // Tests when the digits span hi and lo as well as only in hi.
1044  absl::Duration sigfigs = absl::Seconds(2000000000) + absl::Nanoseconds(3);
1045  EXPECT_EQ(absl::Seconds(666666666) + absl::Nanoseconds(666666667) +
1046  absl::Nanoseconds(1) / 2,
1047  sigfigs / 3);
1048  sigfigs = absl::Seconds(int64_t{7000000000});
1049  EXPECT_EQ(absl::Seconds(2333333333) + absl::Nanoseconds(333333333) +
1050  absl::Nanoseconds(1) / 4,
1051  sigfigs / 3);
1052 
1055  (absl::Seconds(2) + absl::Milliseconds(200)) * -3.5);
1057  (absl::Seconds(2) + absl::Milliseconds(200)) * -3.5);
1059  (absl::Seconds(7) + absl::Milliseconds(500)) / 4);
1061  (absl::Seconds(7) + absl::Milliseconds(500)) / 0.25);
1063  (absl::Seconds(7) + absl::Milliseconds(500)) / 2.5);
1064 
1065  // Tests division remainder.
1070 
1078 
1080  absl::Seconds(1) % absl::Milliseconds(300));
1081  EXPECT_EQ(
1082  absl::Milliseconds(300),
1084 
1087  EXPECT_EQ(0, absl::Nanoseconds(-1) / absl::Seconds(1)); // Actual -1e-9
1088 
1089  // Tests identity a = (a/b)*b + a%b
1090 #define TEST_MOD_IDENTITY(a, b) \
1091  EXPECT_EQ((a), ((a) / (b))*(b) + ((a)%(b)))
1092 
1097 
1101 
1106 
1110 
1111  // Mixed seconds + subseconds
1114 
1115  TEST_MOD_IDENTITY(absl::Seconds(0), mixed_a);
1116  TEST_MOD_IDENTITY(mixed_a, mixed_a);
1117  TEST_MOD_IDENTITY(mixed_a, mixed_b);
1118  TEST_MOD_IDENTITY(mixed_b, mixed_a);
1119 
1120  TEST_MOD_IDENTITY(-mixed_a, mixed_b);
1121  TEST_MOD_IDENTITY(mixed_a, -mixed_b);
1122  TEST_MOD_IDENTITY(-mixed_a, -mixed_b);
1123 
1124 #undef TEST_MOD_IDENTITY
1125 }
1126 
1127 TEST(Duration, Truncation) {
1128  const absl::Duration d = absl::Nanoseconds(1234567890);
1130  for (int unit_sign : {1, -1}) { // sign shouldn't matter
1131  EXPECT_EQ(absl::Nanoseconds(1234567890),
1132  Trunc(d, unit_sign * absl::Nanoseconds(1)));
1133  EXPECT_EQ(absl::Microseconds(1234567),
1134  Trunc(d, unit_sign * absl::Microseconds(1)));
1136  Trunc(d, unit_sign * absl::Milliseconds(1)));
1137  EXPECT_EQ(absl::Seconds(1), Trunc(d, unit_sign * absl::Seconds(1)));
1138  EXPECT_EQ(inf, Trunc(inf, unit_sign * absl::Seconds(1)));
1139 
1140  EXPECT_EQ(absl::Nanoseconds(-1234567890),
1141  Trunc(-d, unit_sign * absl::Nanoseconds(1)));
1142  EXPECT_EQ(absl::Microseconds(-1234567),
1143  Trunc(-d, unit_sign * absl::Microseconds(1)));
1145  Trunc(-d, unit_sign * absl::Milliseconds(1)));
1146  EXPECT_EQ(absl::Seconds(-1), Trunc(-d, unit_sign * absl::Seconds(1)));
1147  EXPECT_EQ(-inf, Trunc(-inf, unit_sign * absl::Seconds(1)));
1148  }
1149 }
1150 
1151 TEST(Duration, Flooring) {
1152  const absl::Duration d = absl::Nanoseconds(1234567890);
1154  for (int unit_sign : {1, -1}) { // sign shouldn't matter
1155  EXPECT_EQ(absl::Nanoseconds(1234567890),
1156  absl::Floor(d, unit_sign * absl::Nanoseconds(1)));
1157  EXPECT_EQ(absl::Microseconds(1234567),
1158  absl::Floor(d, unit_sign * absl::Microseconds(1)));
1160  absl::Floor(d, unit_sign * absl::Milliseconds(1)));
1161  EXPECT_EQ(absl::Seconds(1), absl::Floor(d, unit_sign * absl::Seconds(1)));
1162  EXPECT_EQ(inf, absl::Floor(inf, unit_sign * absl::Seconds(1)));
1163 
1164  EXPECT_EQ(absl::Nanoseconds(-1234567890),
1165  absl::Floor(-d, unit_sign * absl::Nanoseconds(1)));
1166  EXPECT_EQ(absl::Microseconds(-1234568),
1167  absl::Floor(-d, unit_sign * absl::Microseconds(1)));
1169  absl::Floor(-d, unit_sign * absl::Milliseconds(1)));
1170  EXPECT_EQ(absl::Seconds(-2), absl::Floor(-d, unit_sign * absl::Seconds(1)));
1171  EXPECT_EQ(-inf, absl::Floor(-inf, unit_sign * absl::Seconds(1)));
1172  }
1173 }
1174 
1175 TEST(Duration, Ceiling) {
1176  const absl::Duration d = absl::Nanoseconds(1234567890);
1178  for (int unit_sign : {1, -1}) { // // sign shouldn't matter
1179  EXPECT_EQ(absl::Nanoseconds(1234567890),
1180  absl::Ceil(d, unit_sign * absl::Nanoseconds(1)));
1181  EXPECT_EQ(absl::Microseconds(1234568),
1182  absl::Ceil(d, unit_sign * absl::Microseconds(1)));
1184  absl::Ceil(d, unit_sign * absl::Milliseconds(1)));
1185  EXPECT_EQ(absl::Seconds(2), absl::Ceil(d, unit_sign * absl::Seconds(1)));
1186  EXPECT_EQ(inf, absl::Ceil(inf, unit_sign * absl::Seconds(1)));
1187 
1188  EXPECT_EQ(absl::Nanoseconds(-1234567890),
1189  absl::Ceil(-d, unit_sign * absl::Nanoseconds(1)));
1190  EXPECT_EQ(absl::Microseconds(-1234567),
1191  absl::Ceil(-d, unit_sign * absl::Microseconds(1)));
1193  absl::Ceil(-d, unit_sign * absl::Milliseconds(1)));
1194  EXPECT_EQ(absl::Seconds(-1), absl::Ceil(-d, unit_sign * absl::Seconds(1)));
1195  EXPECT_EQ(-inf, absl::Ceil(-inf, unit_sign * absl::Seconds(1)));
1196  }
1197 }
1198 
1199 TEST(Duration, RoundTripUnits) {
1200  const int kRange = 100000;
1201 
1202 #define ROUND_TRIP_UNIT(U, LOW, HIGH) \
1203  do { \
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)); \
1210  else \
1211  EXPECT_EQ(i, absl::U(i) / absl::U(1)); \
1212  } \
1213  } while (0)
1214 
1216  ROUND_TRIP_UNIT(Nanoseconds, -kRange, kRange);
1218 
1220  ROUND_TRIP_UNIT(Microseconds, -kRange, kRange);
1222 
1224  ROUND_TRIP_UNIT(Milliseconds, -kRange, kRange);
1226 
1228  ROUND_TRIP_UNIT(Seconds, -kRange, kRange);
1230 
1231  ROUND_TRIP_UNIT(Minutes, kint64min / 60, kint64min / 60 + kRange);
1232  ROUND_TRIP_UNIT(Minutes, -kRange, kRange);
1233  ROUND_TRIP_UNIT(Minutes, kint64max / 60 - kRange, kint64max / 60);
1234 
1235  ROUND_TRIP_UNIT(Hours, kint64min / 3600, kint64min / 3600 + kRange);
1236  ROUND_TRIP_UNIT(Hours, -kRange, kRange);
1237  ROUND_TRIP_UNIT(Hours, kint64max / 3600 - kRange, kint64max / 3600);
1238 
1239 #undef ROUND_TRIP_UNIT
1240 }
1241 
1242 TEST(Duration, TruncConversions) {
1243  // Tests ToTimespec()/DurationFromTimespec()
1244  const struct {
1245  absl::Duration d;
1246  timespec ts;
1247  } to_ts[] = {
1248  {absl::Seconds(1) + absl::Nanoseconds(1), {1, 1}},
1249  {absl::Seconds(1) + absl::Nanoseconds(1) / 2, {1, 0}},
1250  {absl::Seconds(1) + absl::Nanoseconds(0), {1, 0}},
1251  {absl::Seconds(0) + absl::Nanoseconds(0), {0, 0}},
1252  {absl::Seconds(0) - absl::Nanoseconds(1) / 2, {0, 0}},
1253  {absl::Seconds(0) - absl::Nanoseconds(1), {-1, 999999999}},
1254  {absl::Seconds(-1) + absl::Nanoseconds(1), {-1, 1}},
1255  {absl::Seconds(-1) + absl::Nanoseconds(1) / 2, {-1, 1}},
1256  {absl::Seconds(-1) + absl::Nanoseconds(0), {-1, 0}},
1257  {absl::Seconds(-1) - absl::Nanoseconds(1) / 2, {-1, 0}},
1258  };
1259  for (const auto& test : to_ts) {
1260  EXPECT_THAT(absl::ToTimespec(test.d), TimespecMatcher(test.ts));
1261  }
1262  const struct {
1263  timespec ts;
1264  absl::Duration d;
1265  } from_ts[] = {
1266  {{1, 1}, absl::Seconds(1) + absl::Nanoseconds(1)},
1267  {{1, 0}, absl::Seconds(1) + absl::Nanoseconds(0)},
1268  {{0, 0}, absl::Seconds(0) + absl::Nanoseconds(0)},
1269  {{0, -1}, absl::Seconds(0) - absl::Nanoseconds(1)},
1270  {{-1, 999999999}, absl::Seconds(0) - absl::Nanoseconds(1)},
1271  {{-1, 1}, absl::Seconds(-1) + absl::Nanoseconds(1)},
1272  {{-1, 0}, absl::Seconds(-1) + absl::Nanoseconds(0)},
1273  {{-1, -1}, absl::Seconds(-1) - absl::Nanoseconds(1)},
1274  {{-2, 999999999}, absl::Seconds(-1) - absl::Nanoseconds(1)},
1275  };
1276  for (const auto& test : from_ts) {
1278  }
1279 
1280  // Tests ToTimeval()/DurationFromTimeval() (same as timespec above)
1281  const struct {
1282  absl::Duration d;
1283  timeval tv;
1284  } to_tv[] = {
1285  {absl::Seconds(1) + absl::Microseconds(1), {1, 1}},
1286  {absl::Seconds(1) + absl::Microseconds(1) / 2, {1, 0}},
1287  {absl::Seconds(1) + absl::Microseconds(0), {1, 0}},
1288  {absl::Seconds(0) + absl::Microseconds(0), {0, 0}},
1289  {absl::Seconds(0) - absl::Microseconds(1) / 2, {0, 0}},
1290  {absl::Seconds(0) - absl::Microseconds(1), {-1, 999999}},
1291  {absl::Seconds(-1) + absl::Microseconds(1), {-1, 1}},
1292  {absl::Seconds(-1) + absl::Microseconds(1) / 2, {-1, 1}},
1293  {absl::Seconds(-1) + absl::Microseconds(0), {-1, 0}},
1294  {absl::Seconds(-1) - absl::Microseconds(1) / 2, {-1, 0}},
1295  };
1296  for (const auto& test : to_tv) {
1297  EXPECT_THAT(absl::ToTimeval(test.d), TimevalMatcher(test.tv));
1298  }
1299  const struct {
1300  timeval tv;
1301  absl::Duration d;
1302  } from_tv[] = {
1303  {{1, 1}, absl::Seconds(1) + absl::Microseconds(1)},
1304  {{1, 0}, absl::Seconds(1) + absl::Microseconds(0)},
1305  {{0, 0}, absl::Seconds(0) + absl::Microseconds(0)},
1306  {{0, -1}, absl::Seconds(0) - absl::Microseconds(1)},
1307  {{-1, 999999}, absl::Seconds(0) - absl::Microseconds(1)},
1308  {{-1, 1}, absl::Seconds(-1) + absl::Microseconds(1)},
1309  {{-1, 0}, absl::Seconds(-1) + absl::Microseconds(0)},
1310  {{-1, -1}, absl::Seconds(-1) - absl::Microseconds(1)},
1311  {{-2, 999999}, absl::Seconds(-1) - absl::Microseconds(1)},
1312  };
1313  for (const auto& test : from_tv) {
1315  }
1316 }
1317 
1318 TEST(Duration, SmallConversions) {
1319  // Special tests for conversions of small durations.
1320 
1322  // TODO(bww): Is the next one OK?
1323  EXPECT_EQ(absl::ZeroDuration(), absl::Seconds(std::nextafter(0.125e-9, 0)));
1324  EXPECT_EQ(absl::Nanoseconds(1) / 4, absl::Seconds(0.125e-9));
1325  EXPECT_EQ(absl::Nanoseconds(1) / 4, absl::Seconds(0.250e-9));
1326  EXPECT_EQ(absl::Nanoseconds(1) / 2, absl::Seconds(0.375e-9));
1327  EXPECT_EQ(absl::Nanoseconds(1) / 2, absl::Seconds(0.500e-9));
1328  EXPECT_EQ(absl::Nanoseconds(3) / 4, absl::Seconds(0.625e-9));
1329  EXPECT_EQ(absl::Nanoseconds(3) / 4, absl::Seconds(0.750e-9));
1330  EXPECT_EQ(absl::Nanoseconds(1), absl::Seconds(0.875e-9));
1331  EXPECT_EQ(absl::Nanoseconds(1), absl::Seconds(1.000e-9));
1332 
1333  EXPECT_EQ(absl::ZeroDuration(), absl::Seconds(std::nextafter(-0.125e-9, 0)));
1334  EXPECT_EQ(-absl::Nanoseconds(1) / 4, absl::Seconds(-0.125e-9));
1335  EXPECT_EQ(-absl::Nanoseconds(1) / 4, absl::Seconds(-0.250e-9));
1336  EXPECT_EQ(-absl::Nanoseconds(1) / 2, absl::Seconds(-0.375e-9));
1337  EXPECT_EQ(-absl::Nanoseconds(1) / 2, absl::Seconds(-0.500e-9));
1338  EXPECT_EQ(-absl::Nanoseconds(3) / 4, absl::Seconds(-0.625e-9));
1339  EXPECT_EQ(-absl::Nanoseconds(3) / 4, absl::Seconds(-0.750e-9));
1340  EXPECT_EQ(-absl::Nanoseconds(1), absl::Seconds(-0.875e-9));
1341  EXPECT_EQ(-absl::Nanoseconds(1), absl::Seconds(-1.000e-9));
1342 
1343  timespec ts;
1344  ts.tv_sec = 0;
1345  ts.tv_nsec = 0;
1346  EXPECT_THAT(ToTimespec(absl::Nanoseconds(0)), TimespecMatcher(ts));
1347  // TODO(bww): Are the next three OK?
1348  EXPECT_THAT(ToTimespec(absl::Nanoseconds(1) / 4), TimespecMatcher(ts));
1349  EXPECT_THAT(ToTimespec(absl::Nanoseconds(2) / 4), TimespecMatcher(ts));
1350  EXPECT_THAT(ToTimespec(absl::Nanoseconds(3) / 4), TimespecMatcher(ts));
1351  ts.tv_nsec = 1;
1352  EXPECT_THAT(ToTimespec(absl::Nanoseconds(4) / 4), TimespecMatcher(ts));
1353  EXPECT_THAT(ToTimespec(absl::Nanoseconds(5) / 4), TimespecMatcher(ts));
1354  EXPECT_THAT(ToTimespec(absl::Nanoseconds(6) / 4), TimespecMatcher(ts));
1355  EXPECT_THAT(ToTimespec(absl::Nanoseconds(7) / 4), TimespecMatcher(ts));
1356  ts.tv_nsec = 2;
1357  EXPECT_THAT(ToTimespec(absl::Nanoseconds(8) / 4), TimespecMatcher(ts));
1358 
1359  timeval tv;
1360  tv.tv_sec = 0;
1361  tv.tv_usec = 0;
1362  EXPECT_THAT(ToTimeval(absl::Nanoseconds(0)), TimevalMatcher(tv));
1363  // TODO(bww): Is the next one OK?
1364  EXPECT_THAT(ToTimeval(absl::Nanoseconds(999)), TimevalMatcher(tv));
1365  tv.tv_usec = 1;
1366  EXPECT_THAT(ToTimeval(absl::Nanoseconds(1000)), TimevalMatcher(tv));
1367  EXPECT_THAT(ToTimeval(absl::Nanoseconds(1999)), TimevalMatcher(tv));
1368  tv.tv_usec = 2;
1369  EXPECT_THAT(ToTimeval(absl::Nanoseconds(2000)), TimevalMatcher(tv));
1370 }
1371 
1372 void VerifyApproxSameAsMul(double time_as_seconds, int* const misses) {
1373  auto direct_seconds = absl::Seconds(time_as_seconds);
1374  auto mul_by_one_second = time_as_seconds * absl::Seconds(1);
1375  // These are expected to differ by up to one tick due to fused multiply/add
1376  // contraction.
1377  if (absl::AbsDuration(direct_seconds - mul_by_one_second) >
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)
1383  << time_as_seconds;
1384  }
1385 }
1386 
1387 // For a variety of interesting durations, we find the exact point
1388 // where one double converts to that duration, and the very next double
1389 // converts to the next duration. For both of those points, verify that
1390 // Seconds(point) returns a duration near point * Seconds(1.0). (They may
1391 // not be exactly equal due to fused multiply/add contraction.)
1392 TEST(Duration, ToDoubleSecondsCheckEdgeCases) {
1394  constexpr auto duration_tick = absl::time_internal::MakeDuration(0, 1u);
1395  int misses = 0;
1396  for (int64_t seconds = 0; seconds < 99; ++seconds) {
1397  uint32_t tick_vals[] = {0, +999, +999999, +999999999, kTicksPerSecond - 1,
1398  0, 1000, 1000000, 1000000000, kTicksPerSecond,
1399  1, 1001, 1000001, 1000000001, kTicksPerSecond + 1,
1400  2, 1002, 1000002, 1000000002, kTicksPerSecond + 2,
1401  3, 1003, 1000003, 1000000003, kTicksPerSecond + 3,
1402  4, 1004, 1000004, 1000000004, kTicksPerSecond + 4,
1403  5, 6, 7, 8, 9};
1404  for (uint32_t ticks : tick_vals) {
1405  absl::Duration s_plus_t = absl::Seconds(seconds) + ticks * duration_tick;
1406  for (absl::Duration d : {s_plus_t, -s_plus_t}) {
1407  absl::Duration after_d = d + duration_tick;
1408  EXPECT_NE(d, after_d);
1409  EXPECT_EQ(after_d - d, duration_tick);
1410 
1411  double low_edge = ToDoubleSeconds(d);
1412  EXPECT_EQ(d, absl::Seconds(low_edge));
1413 
1414  double high_edge = ToDoubleSeconds(after_d);
1415  EXPECT_EQ(after_d, absl::Seconds(high_edge));
1416 
1417  for (;;) {
1418  double midpoint = low_edge + (high_edge - low_edge) / 2;
1419  if (midpoint == low_edge || midpoint == high_edge) break;
1420  absl::Duration mid_duration = absl::Seconds(midpoint);
1421  if (mid_duration == d) {
1422  low_edge = midpoint;
1423  } else {
1424  EXPECT_EQ(mid_duration, after_d);
1425  high_edge = midpoint;
1426  }
1427  }
1428  // Now low_edge is the highest double that converts to Duration d,
1429  // and high_edge is the lowest double that converts to Duration after_d.
1430  VerifyApproxSameAsMul(low_edge, &misses);
1431  VerifyApproxSameAsMul(high_edge, &misses);
1432  }
1433  }
1434  }
1435 }
1436 
1437 TEST(Duration, ToDoubleSecondsCheckRandom) {
1438  std::random_device rd;
1439  std::seed_seq seed({rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd()});
1440  std::mt19937_64 gen(seed);
1441  // We want doubles distributed from 1/8ns up to 2^63, where
1442  // as many values are tested from 1ns to 2ns as from 1sec to 2sec,
1443  // so even distribute along a log-scale of those values, and
1444  // exponentiate before using them. (9.223377e+18 is just slightly
1445  // out of bounds for absl::Duration.)
1446  std::uniform_real_distribution<double> uniform(std::log(0.125e-9),
1447  std::log(9.223377e+18));
1448  int misses = 0;
1449  for (int i = 0; i < 1000000; ++i) {
1450  double d = std::exp(uniform(gen));
1451  VerifyApproxSameAsMul(d, &misses);
1452  VerifyApproxSameAsMul(-d, &misses);
1453  }
1454 }
1455 
1456 TEST(Duration, ConversionSaturation) {
1457  absl::Duration d;
1458 
1459  const auto max_timeval_sec =
1460  std::numeric_limits<decltype(timeval::tv_sec)>::max();
1461  const auto min_timeval_sec =
1462  std::numeric_limits<decltype(timeval::tv_sec)>::min();
1463  timeval tv;
1464  tv.tv_sec = max_timeval_sec;
1465  tv.tv_usec = 999998;
1467  tv = ToTimeval(d);
1468  EXPECT_EQ(max_timeval_sec, tv.tv_sec);
1469  EXPECT_EQ(999998, tv.tv_usec);
1470  d += absl::Microseconds(1);
1471  tv = ToTimeval(d);
1472  EXPECT_EQ(max_timeval_sec, tv.tv_sec);
1473  EXPECT_EQ(999999, tv.tv_usec);
1474  d += absl::Microseconds(1); // no effect
1475  tv = ToTimeval(d);
1476  EXPECT_EQ(max_timeval_sec, tv.tv_sec);
1477  EXPECT_EQ(999999, tv.tv_usec);
1478 
1479  tv.tv_sec = min_timeval_sec;
1480  tv.tv_usec = 1;
1482  tv = ToTimeval(d);
1483  EXPECT_EQ(min_timeval_sec, tv.tv_sec);
1484  EXPECT_EQ(1, tv.tv_usec);
1485  d -= absl::Microseconds(1);
1486  tv = ToTimeval(d);
1487  EXPECT_EQ(min_timeval_sec, tv.tv_sec);
1488  EXPECT_EQ(0, tv.tv_usec);
1489  d -= absl::Microseconds(1); // no effect
1490  tv = ToTimeval(d);
1491  EXPECT_EQ(min_timeval_sec, tv.tv_sec);
1492  EXPECT_EQ(0, tv.tv_usec);
1493 
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();
1498  timespec ts;
1499  ts.tv_sec = max_timespec_sec;
1500  ts.tv_nsec = 999999998;
1502  ts = absl::ToTimespec(d);
1503  EXPECT_EQ(max_timespec_sec, ts.tv_sec);
1504  EXPECT_EQ(999999998, ts.tv_nsec);
1505  d += absl::Nanoseconds(1);
1506  ts = absl::ToTimespec(d);
1507  EXPECT_EQ(max_timespec_sec, ts.tv_sec);
1508  EXPECT_EQ(999999999, ts.tv_nsec);
1509  d += absl::Nanoseconds(1); // no effect
1510  ts = absl::ToTimespec(d);
1511  EXPECT_EQ(max_timespec_sec, ts.tv_sec);
1512  EXPECT_EQ(999999999, ts.tv_nsec);
1513 
1514  ts.tv_sec = min_timespec_sec;
1515  ts.tv_nsec = 1;
1517  ts = absl::ToTimespec(d);
1518  EXPECT_EQ(min_timespec_sec, ts.tv_sec);
1519  EXPECT_EQ(1, ts.tv_nsec);
1520  d -= absl::Nanoseconds(1);
1521  ts = absl::ToTimespec(d);
1522  EXPECT_EQ(min_timespec_sec, ts.tv_sec);
1523  EXPECT_EQ(0, ts.tv_nsec);
1524  d -= absl::Nanoseconds(1); // no effect
1525  ts = absl::ToTimespec(d);
1526  EXPECT_EQ(min_timespec_sec, ts.tv_sec);
1527  EXPECT_EQ(0, ts.tv_nsec);
1528 }
1529 
1531  // Example from Go's docs.
1532  EXPECT_EQ("72h3m0.5s",
1534  absl::Milliseconds(500)));
1535  // Go's largest time: 2540400h10m10.000000000s
1536  EXPECT_EQ("2540400h10m10s",
1538  absl::Seconds(10)));
1539 
1543 
1550 
1554 
1555  EXPECT_EQ("1h0.25s",
1557  EXPECT_EQ("1m0.25s",
1559  EXPECT_EQ("1h1m0.25s",
1561  absl::Milliseconds(250)));
1562  EXPECT_EQ("1h0.0005s",
1564  EXPECT_EQ("1h0.0000005s",
1566 
1567  // Subsecond special case.
1569  absl::Nanoseconds(1) / 2));
1571  absl::Nanoseconds(1) / 4));
1573  absl::Nanoseconds(1) / 9));
1575  absl::Nanoseconds(200)));
1577  absl::Microseconds(200)));
1579  absl::Nanoseconds(200)));
1581  absl::Nanoseconds(10)));
1582  EXPECT_EQ("1.000001ms",
1584 
1585  // Negative durations.
1592 
1593  EXPECT_EQ("-1h1m",
1595  EXPECT_EQ("-1h1s",
1597  EXPECT_EQ("-1m1s",
1599 
1601  EXPECT_EQ("-1.2us", absl::FormatDuration(
1602  -(absl::Microseconds(1) + absl::Nanoseconds(200))));
1603  EXPECT_EQ("-1.2ms", absl::FormatDuration(
1604  -(absl::Milliseconds(1) + absl::Microseconds(200))));
1606  absl::Nanoseconds(200))));
1608  absl::Nanoseconds(10))));
1609  EXPECT_EQ("-1.000001ms", absl::FormatDuration(-(absl::Milliseconds(1) +
1610  absl::Nanoseconds(1))));
1611 
1612  //
1613  // Interesting corner cases.
1614  //
1615 
1616  const absl::Duration qns = absl::Nanoseconds(1) / 4;
1617  const absl::Duration max_dur =
1618  absl::Seconds(kint64max) + (absl::Seconds(1) - qns);
1619  const absl::Duration min_dur = absl::Seconds(kint64min);
1620 
1621  EXPECT_EQ("0.25ns", absl::FormatDuration(qns));
1622  EXPECT_EQ("-0.25ns", absl::FormatDuration(-qns));
1623  EXPECT_EQ("2562047788015215h30m7.99999999975s",
1624  absl::FormatDuration(max_dur));
1625  EXPECT_EQ("-2562047788015215h30m8s", absl::FormatDuration(min_dur));
1626 
1627  // Tests printing full precision from units that print using FDivDuration
1628  EXPECT_EQ("55.00000000025s", absl::FormatDuration(absl::Seconds(55) + qns));
1629  EXPECT_EQ("55.00000025ms",
1631  EXPECT_EQ("55.00025us", absl::FormatDuration(absl::Microseconds(55) + qns));
1632  EXPECT_EQ("55.25ns", absl::FormatDuration(absl::Nanoseconds(55) + qns));
1633 
1634  // Formatting infinity
1637 
1638  // Formatting approximately +/- 100 billion years
1639  const absl::Duration huge_range = ApproxYears(100000000000);
1640  EXPECT_EQ("876000000000000h", absl::FormatDuration(huge_range));
1641  EXPECT_EQ("-876000000000000h", absl::FormatDuration(-huge_range));
1642 
1643  EXPECT_EQ("876000000000000h0.999999999s",
1644  absl::FormatDuration(huge_range +
1645  (absl::Seconds(1) - absl::Nanoseconds(1))));
1646  EXPECT_EQ("876000000000000h0.9999999995s",
1648  huge_range + (absl::Seconds(1) - absl::Nanoseconds(1) / 2)));
1649  EXPECT_EQ("876000000000000h0.99999999975s",
1651  huge_range + (absl::Seconds(1) - absl::Nanoseconds(1) / 4)));
1652 
1653  EXPECT_EQ("-876000000000000h0.999999999s",
1654  absl::FormatDuration(-huge_range -
1655  (absl::Seconds(1) - absl::Nanoseconds(1))));
1656  EXPECT_EQ("-876000000000000h0.9999999995s",
1658  -huge_range - (absl::Seconds(1) - absl::Nanoseconds(1) / 2)));
1659  EXPECT_EQ("-876000000000000h0.99999999975s",
1661  -huge_range - (absl::Seconds(1) - absl::Nanoseconds(1) / 4)));
1662 }
1663 
1665  absl::Duration d;
1666 
1667  // No specified unit. Should only work for zero and infinity.
1674 
1675  EXPECT_TRUE(absl::ParseDuration("inf", &d));
1677  EXPECT_TRUE(absl::ParseDuration("+inf", &d));
1679  EXPECT_TRUE(absl::ParseDuration("-inf", &d));
1681  EXPECT_FALSE(absl::ParseDuration("infBlah", &d));
1682 
1683  // Illegal input forms.
1698  EXPECT_FALSE(absl::ParseDuration(" 2s ", &d));
1700  EXPECT_FALSE(absl::ParseDuration("1e3s", &d));
1701 
1702  // One unit type.
1703  EXPECT_TRUE(absl::ParseDuration("1ns", &d));
1705  EXPECT_TRUE(absl::ParseDuration("1us", &d));
1707  EXPECT_TRUE(absl::ParseDuration("1ms", &d));
1710  EXPECT_EQ(absl::Seconds(1), d);
1712  EXPECT_EQ(absl::Minutes(2), d);
1714  EXPECT_EQ(absl::Hours(2), d);
1715 
1716  // Huge counts of a unit.
1717  EXPECT_TRUE(absl::ParseDuration("9223372036854775807us", &d));
1718  EXPECT_EQ(absl::Microseconds(9223372036854775807), d);
1719  EXPECT_TRUE(absl::ParseDuration("-9223372036854775807us", &d));
1720  EXPECT_EQ(absl::Microseconds(-9223372036854775807), d);
1721 
1722  // Multiple units.
1723  EXPECT_TRUE(absl::ParseDuration("2h3m4s", &d));
1725  EXPECT_TRUE(absl::ParseDuration("3m4s5us", &d));
1727  EXPECT_TRUE(absl::ParseDuration("2h3m4s5ms6us7ns", &d));
1730  absl::Nanoseconds(7),
1731  d);
1732 
1733  // Multiple units out of order.
1734  EXPECT_TRUE(absl::ParseDuration("2us3m4s5h", &d));
1736  absl::Microseconds(2),
1737  d);
1738 
1739  // Fractional values of units.
1740  EXPECT_TRUE(absl::ParseDuration("1.5ns", &d));
1741  EXPECT_EQ(1.5 * absl::Nanoseconds(1), d);
1742  EXPECT_TRUE(absl::ParseDuration("1.5us", &d));
1743  EXPECT_EQ(1.5 * absl::Microseconds(1), d);
1744  EXPECT_TRUE(absl::ParseDuration("1.5ms", &d));
1745  EXPECT_EQ(1.5 * absl::Milliseconds(1), d);
1746  EXPECT_TRUE(absl::ParseDuration("1.5s", &d));
1747  EXPECT_EQ(1.5 * absl::Seconds(1), d);
1748  EXPECT_TRUE(absl::ParseDuration("1.5m", &d));
1749  EXPECT_EQ(1.5 * absl::Minutes(1), d);
1750  EXPECT_TRUE(absl::ParseDuration("1.5h", &d));
1751  EXPECT_EQ(1.5 * absl::Hours(1), d);
1752 
1753  // Huge fractional counts of a unit.
1754  EXPECT_TRUE(absl::ParseDuration("0.4294967295s", &d));
1755  EXPECT_EQ(absl::Nanoseconds(429496729) + absl::Nanoseconds(1) / 2, d);
1756  EXPECT_TRUE(absl::ParseDuration("0.429496729501234567890123456789s", &d));
1757  EXPECT_EQ(absl::Nanoseconds(429496729) + absl::Nanoseconds(1) / 2, d);
1758 
1759  // Negative durations.
1760  EXPECT_TRUE(absl::ParseDuration("-1s", &d));
1761  EXPECT_EQ(absl::Seconds(-1), d);
1762  EXPECT_TRUE(absl::ParseDuration("-1m", &d));
1763  EXPECT_EQ(absl::Minutes(-1), d);
1764  EXPECT_TRUE(absl::ParseDuration("-1h", &d));
1765  EXPECT_EQ(absl::Hours(-1), d);
1766 
1767  EXPECT_TRUE(absl::ParseDuration("-1h2s", &d));
1768  EXPECT_EQ(-(absl::Hours(1) + absl::Seconds(2)), d);
1769  EXPECT_FALSE(absl::ParseDuration("1h-2s", &d));
1770  EXPECT_FALSE(absl::ParseDuration("-1h-2s", &d));
1771  EXPECT_FALSE(absl::ParseDuration("-1h -2s", &d));
1772 }
1773 
1774 TEST(Duration, FormatParseRoundTrip) {
1775 #define TEST_PARSE_ROUNDTRIP(d) \
1776  do { \
1777  std::string s = absl::FormatDuration(d); \
1778  absl::Duration dur; \
1779  EXPECT_TRUE(absl::ParseDuration(s, &dur)); \
1780  EXPECT_EQ(d, dur); \
1781  } while (0)
1782 
1790 
1797 
1801 
1803  absl::Nanoseconds(1) / 4); // 1.25ns
1804 
1805  const absl::Duration huge_range = ApproxYears(100000000000);
1806  TEST_PARSE_ROUNDTRIP(huge_range);
1807  TEST_PARSE_ROUNDTRIP(huge_range + (absl::Seconds(1) - absl::Nanoseconds(1)));
1808 
1809 #undef TEST_PARSE_ROUNDTRIP
1810 }
1811 
1812 } // namespace
EXPECT_FALSE
#define EXPECT_FALSE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1970
absl::ToChronoMicroseconds
std::chrono::microseconds ToChronoMicroseconds(Duration d)
Definition: abseil-cpp/absl/time/duration.cc:666
absl::time_internal::cctz::seconds
std::chrono::duration< std::int_fast64_t > seconds
Definition: abseil-cpp/absl/time/internal/cctz/include/cctz/time_zone.h:40
absl::FromChrono
Time FromChrono(const std::chrono::system_clock::time_point &tp)
Definition: third_party/abseil-cpp/absl/time/time.cc:334
TEST_REL_OPS
#define TEST_REL_OPS(UNIT)
absl::FormatConversionChar::E
@ E
absl::ToChronoMilliseconds
std::chrono::milliseconds ToChronoMilliseconds(Duration d)
Definition: abseil-cpp/absl/time/duration.cc:669
absl::ZeroDuration
constexpr Duration ZeroDuration()
Definition: third_party/abseil-cpp/absl/time/time.h:308
absl::time_internal::MakeDuration
constexpr Duration MakeDuration(int64_t hi, uint32_t lo)
Definition: third_party/abseil-cpp/absl/time/time.h:1392
TEST_PARSE_ROUNDTRIP
#define TEST_PARSE_ROUNDTRIP(d)
EXPECT_THAT
#define EXPECT_THAT(value, matcher)
tests.google.protobuf.internal.message_test.isnan
def isnan(val)
Definition: bloaty/third_party/protobuf/python/compatibility_tests/v2.5.0/tests/google/protobuf/internal/message_test.py:65
absl::time_internal::kTicksPerSecond
constexpr int64_t kTicksPerSecond
Definition: third_party/abseil-cpp/absl/time/time.h:108
capstone.range
range
Definition: third_party/bloaty/third_party/capstone/bindings/python/capstone/__init__.py:6
test
Definition: spinlock_test.cc:36
absl::Nanoseconds
constexpr Duration Nanoseconds(T n)
Definition: third_party/abseil-cpp/absl/time/time.h:407
seed
static const uint8_t seed[20]
Definition: dsa_test.cc:79
TEST_INF_DIV_WITH_TYPE
#define TEST_INF_DIV_WITH_TYPE(T)
EXPECT_GT
#define EXPECT_GT(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2036
absl::ToInt64Minutes
int64_t ToInt64Minutes(Duration d)
Definition: abseil-cpp/absl/time/duration.cc:574
u
OPENSSL_EXPORT pem_password_cb void * u
Definition: pem.h:351
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
ASSERT_LE
#define ASSERT_LE(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2064
absl::ToInt64Microseconds
int64_t ToInt64Microseconds(Duration d)
Definition: abseil-cpp/absl/time/duration.cc:552
absl::ToChronoHours
std::chrono::hours ToChronoHours(Duration d)
Definition: abseil-cpp/absl/time/duration.cc:678
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
absl::ToInt64Milliseconds
int64_t ToInt64Milliseconds(Duration d)
Definition: abseil-cpp/absl/time/duration.cc:560
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
absl::Floor
Duration Floor(const Duration d, const Duration unit)
Definition: abseil-cpp/absl/time/duration.cc:510
absl::time_internal::IsInfiniteDuration
constexpr bool IsInfiniteDuration(Duration d)
Definition: third_party/abseil-cpp/absl/time/time.h:1426
absl::Milliseconds
constexpr Duration Milliseconds(T n)
Definition: third_party/abseil-cpp/absl/time/time.h:415
grpc_core::ParseDuration
Duration ParseDuration(const google_protobuf_Duration *proto_duration)
Definition: xds_common_types.h:39
c
void c(T a)
Definition: miscompile_with_no_unique_address_test.cc:40
absl::Microseconds
constexpr Duration Microseconds(T n)
Definition: third_party/abseil-cpp/absl/time/time.h:411
d2
static const fe d2
Definition: curve25519_tables.h:39
absl::ToChronoMinutes
std::chrono::minutes ToChronoMinutes(Duration d)
Definition: abseil-cpp/absl/time/duration.cc:675
absl::ABSL_NAMESPACE_BEGIN::kint64max
constexpr int64_t kint64max
Definition: abseil-cpp/absl/time/duration.cc:84
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
TEST_MOD_IDENTITY
#define TEST_MOD_IDENTITY(a, b)
TEST_NAN_HANDLING
#define TEST_NAN_HANDLING(NAME, NAN)
TEST
#define TEST(name, init_size,...)
Definition: arena_test.cc:75
max
int max
Definition: bloaty/third_party/zlib/examples/enough.c:170
EXPECT_NE
#define EXPECT_NE(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2028
absl::ToTimeval
timeval ToTimeval(Duration d)
Definition: abseil-cpp/absl/time/duration.cc:636
tick
uv_timer_t tick
Definition: libuv/docs/code/tty-gravity/main.c:8
arg
Definition: cmdline.cc:40
absl::Duration
Definition: third_party/abseil-cpp/absl/time/time.h:159
absl::ToDoubleSeconds
double ToDoubleSeconds(Duration d)
Definition: abseil-cpp/absl/time/duration.cc:596
gen
OPENSSL_EXPORT GENERAL_NAME * gen
Definition: x509v3.h:495
absl::FDivDuration
double FDivDuration(Duration num, Duration den)
Definition: abseil-cpp/absl/time/duration.cc:484
absl::ParseDuration
bool ParseDuration(absl::string_view dur_sv, Duration *d)
Definition: abseil-cpp/absl/time/duration.cc:902
min
#define min(a, b)
Definition: qsort.h:83
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
d
static const fe d
Definition: curve25519_tables.h:19
TEST_ADD_OPS
#define TEST_ADD_OPS(UNIT)
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
absl::FormatDuration
std::string FormatDuration(Duration d)
Definition: abseil-cpp/absl/time/duration.cc:768
inf
const char inf[]
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/upb.c:12099
absl::ToChronoSeconds
std::chrono::seconds ToChronoSeconds(Duration d)
Definition: abseil-cpp/absl/time/duration.cc:672
TEST_FACTORY_OVERLOADS
#define TEST_FACTORY_OVERLOADS(NAME)
absl::Seconds
constexpr Duration Seconds(T n)
Definition: third_party/abseil-cpp/absl/time/time.h:419
ROUND_TRIP_UNIT
#define ROUND_TRIP_UNIT(U, LOW, HIGH)
absl::ToTimespec
timespec ToTimespec(Duration d)
Definition: abseil-cpp/absl/time/duration.cc:606
timeval::tv_sec
long tv_sec
Definition: setup_once.h:121
timeval::tv_usec
long tv_usec
Definition: setup_once.h:122
N
#define N
Definition: sync_test.cc:37
Range
Range(0, 512)
timeval
Definition: setup_once.h:113
absl::Hours
constexpr Duration Hours(T n)
Definition: third_party/abseil-cpp/absl/time/time.h:427
absl::ToInt64Hours
int64_t ToInt64Hours(Duration d)
Definition: abseil-cpp/absl/time/duration.cc:580
EXPECT_LT
#define EXPECT_LT(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2032
tests.google.protobuf.internal.message_test.isinf
def isinf(val)
Definition: bloaty/third_party/protobuf/python/compatibility_tests/v2.5.0/tests/google/protobuf/internal/message_test.py:68
MATCHER_P
#define MATCHER_P(name, p0, description)
Definition: bloaty/third_party/googletest/googlemock/include/gmock/gmock-generated-matchers.h:311
absl::IDivDuration
int64_t IDivDuration(Duration num, Duration den, Duration *rem)
Definition: third_party/abseil-cpp/absl/time/time.h:285
absl::DurationFromTimeval
Duration DurationFromTimeval(timeval tv)
Definition: abseil-cpp/absl/time/duration.cc:532
log
bool log
Definition: abseil-cpp/absl/synchronization/mutex.cc:310
TEST_INF_MUL_WITH_TYPE
#define TEST_INF_MUL_WITH_TYPE(T)
absl::Minutes
constexpr Duration Minutes(T n)
Definition: third_party/abseil-cpp/absl/time/time.h:423
absl::Ceil
Duration Ceil(const Duration d, const Duration unit)
Definition: abseil-cpp/absl/time/duration.cc:515
EXPECT_TRUE
#define EXPECT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1967
absl::AbsDuration
Duration AbsDuration(Duration d)
Definition: third_party/abseil-cpp/absl/time/time.h:313
absl::ToChronoNanoseconds
std::chrono::nanoseconds ToChronoNanoseconds(Duration d)
Definition: abseil-cpp/absl/time/duration.cc:663
TEST_MUL_OPS
#define TEST_MUL_OPS(UNIT)
Duration
Definition: bloaty/third_party/protobuf/src/google/protobuf/duration.pb.h:69
absl::Trunc
Duration Trunc(Duration d, Duration unit)
Definition: abseil-cpp/absl/time/duration.cc:506
absl::InfiniteDuration
constexpr Duration InfiniteDuration()
Definition: third_party/abseil-cpp/absl/time/time.h:1573
TEST_DURATION_CONVERSION
#define TEST_DURATION_CONVERSION(UNIT)
absl::DurationFromTimespec
Duration DurationFromTimespec(timespec ts)
Definition: abseil-cpp/absl/time/duration.cc:524
absl::ABSL_NAMESPACE_BEGIN::kint64min
constexpr int64_t kint64min
Definition: abseil-cpp/absl/time/duration.cc:85
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
ticks
static unsigned long ticks
Definition: benchmark-loop-count.c:30
absl::ToInt64Nanoseconds
int64_t ToInt64Nanoseconds(Duration d)
Definition: abseil-cpp/absl/time/duration.cc:544
absl::ToInt64Seconds
int64_t ToInt64Seconds(Duration d)
Definition: abseil-cpp/absl/time/duration.cc:568


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:17