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 #include <chrono> // NOLINT(build/c++11)
16 #include <cmath>
17 #include <cstdint>
18 #include <ctime>
19 #include <iomanip>
20 #include <limits>
21 #include <random>
22 #include <string>
23 
24 #include "gmock/gmock.h"
25 #include "gtest/gtest.h"
26 #include "absl/time/time.h"
27 
28 namespace {
29 
30 constexpr int64_t kint64max = std::numeric_limits<int64_t>::max();
31 constexpr int64_t kint64min = std::numeric_limits<int64_t>::min();
32 
33 // Approximates the given number of years. This is only used to make some test
34 // code more readable.
35 absl::Duration ApproxYears(int64_t n) { return absl::Hours(n) * 365 * 24; }
36 
37 // A gMock matcher to match timespec values. Use this matcher like:
38 // timespec ts1, ts2;
39 // EXPECT_THAT(ts1, TimespecMatcher(ts2));
40 MATCHER_P(TimespecMatcher, ts, "") {
41  if (ts.tv_sec == arg.tv_sec && ts.tv_nsec == arg.tv_nsec)
42  return true;
43  *result_listener << "expected: {" << ts.tv_sec << ", " << ts.tv_nsec << "} ";
44  *result_listener << "actual: {" << arg.tv_sec << ", " << arg.tv_nsec << "}";
45  return false;
46 }
47 
48 // A gMock matcher to match timeval values. Use this matcher like:
49 // timeval tv1, tv2;
50 // EXPECT_THAT(tv1, TimevalMatcher(tv2));
51 MATCHER_P(TimevalMatcher, tv, "") {
52  if (tv.tv_sec == arg.tv_sec && tv.tv_usec == arg.tv_usec)
53  return true;
54  *result_listener << "expected: {" << tv.tv_sec << ", " << tv.tv_usec << "} ";
55  *result_listener << "actual: {" << arg.tv_sec << ", " << arg.tv_usec << "}";
56  return false;
57 }
58 
59 TEST(Duration, ConstExpr) {
60  constexpr absl::Duration d0 = absl::ZeroDuration();
61  static_assert(d0 == absl::ZeroDuration(), "ZeroDuration()");
62  constexpr absl::Duration d1 = absl::Seconds(1);
63  static_assert(d1 == absl::Seconds(1), "Seconds(1)");
64  static_assert(d1 != absl::ZeroDuration(), "Seconds(1)");
65  constexpr absl::Duration d2 = absl::InfiniteDuration();
66  static_assert(d2 == absl::InfiniteDuration(), "InfiniteDuration()");
67  static_assert(d2 != absl::ZeroDuration(), "InfiniteDuration()");
68 }
69 
70 TEST(Duration, ValueSemantics) {
71  // If this compiles, the test passes.
72  constexpr absl::Duration a; // Default construction
73  constexpr absl::Duration b = a; // Copy construction
74  constexpr absl::Duration c(b); // Copy construction (again)
75 
77  d = c; // Assignment
78 }
79 
80 TEST(Duration, Factories) {
81  constexpr absl::Duration zero = absl::ZeroDuration();
82  constexpr absl::Duration nano = absl::Nanoseconds(1);
83  constexpr absl::Duration micro = absl::Microseconds(1);
84  constexpr absl::Duration milli = absl::Milliseconds(1);
85  constexpr absl::Duration sec = absl::Seconds(1);
86  constexpr absl::Duration min = absl::Minutes(1);
87  constexpr absl::Duration hour = absl::Hours(1);
88 
89  EXPECT_EQ(zero, absl::Duration());
90  EXPECT_EQ(zero, absl::Seconds(0));
91  EXPECT_EQ(nano, absl::Nanoseconds(1));
92  EXPECT_EQ(micro, absl::Nanoseconds(1000));
93  EXPECT_EQ(milli, absl::Microseconds(1000));
94  EXPECT_EQ(sec, absl::Milliseconds(1000));
95  EXPECT_EQ(min, absl::Seconds(60));
96  EXPECT_EQ(hour, absl::Minutes(60));
97 
98  // Tests factory limits
100 
101  EXPECT_GT(inf, absl::Seconds(kint64max));
102  EXPECT_LT(-inf, absl::Seconds(kint64min));
103  EXPECT_LT(-inf, absl::Seconds(-kint64max));
104 
105  EXPECT_EQ(inf, absl::Minutes(kint64max));
106  EXPECT_EQ(-inf, absl::Minutes(kint64min));
107  EXPECT_EQ(-inf, absl::Minutes(-kint64max));
108  EXPECT_GT(inf, absl::Minutes(kint64max / 60));
109  EXPECT_LT(-inf, absl::Minutes(kint64min / 60));
110  EXPECT_LT(-inf, absl::Minutes(-kint64max / 60));
111 
112  EXPECT_EQ(inf, absl::Hours(kint64max));
113  EXPECT_EQ(-inf, absl::Hours(kint64min));
114  EXPECT_EQ(-inf, absl::Hours(-kint64max));
115  EXPECT_GT(inf, absl::Hours(kint64max / 3600));
116  EXPECT_LT(-inf, absl::Hours(kint64min / 3600));
117  EXPECT_LT(-inf, absl::Hours(-kint64max / 3600));
118 }
119 
120 TEST(Duration, ToConversion) {
121 #define TEST_DURATION_CONVERSION(UNIT) \
122  do { \
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)); \
137  } while (0)
138 
145 
146 #undef TEST_DURATION_CONVERSION
147 }
148 
149 template <int64_t N>
150 void TestToConversion() {
151  constexpr absl::Duration nano = absl::Nanoseconds(N);
152  EXPECT_EQ(N, absl::ToInt64Nanoseconds(nano));
153  EXPECT_EQ(0, absl::ToInt64Microseconds(nano));
154  EXPECT_EQ(0, absl::ToInt64Milliseconds(nano));
155  EXPECT_EQ(0, absl::ToInt64Seconds(nano));
156  EXPECT_EQ(0, absl::ToInt64Minutes(nano));
157  EXPECT_EQ(0, absl::ToInt64Hours(nano));
158  const absl::Duration micro = absl::Microseconds(N);
159  EXPECT_EQ(N * 1000, absl::ToInt64Nanoseconds(micro));
160  EXPECT_EQ(N, absl::ToInt64Microseconds(micro));
161  EXPECT_EQ(0, absl::ToInt64Milliseconds(micro));
162  EXPECT_EQ(0, absl::ToInt64Seconds(micro));
163  EXPECT_EQ(0, absl::ToInt64Minutes(micro));
164  EXPECT_EQ(0, absl::ToInt64Hours(micro));
165  const absl::Duration milli = absl::Milliseconds(N);
166  EXPECT_EQ(N * 1000 * 1000, absl::ToInt64Nanoseconds(milli));
167  EXPECT_EQ(N * 1000, absl::ToInt64Microseconds(milli));
168  EXPECT_EQ(N, absl::ToInt64Milliseconds(milli));
169  EXPECT_EQ(0, absl::ToInt64Seconds(milli));
170  EXPECT_EQ(0, absl::ToInt64Minutes(milli));
171  EXPECT_EQ(0, absl::ToInt64Hours(milli));
172  const absl::Duration sec = absl::Seconds(N);
173  EXPECT_EQ(N * 1000 * 1000 * 1000, absl::ToInt64Nanoseconds(sec));
174  EXPECT_EQ(N * 1000 * 1000, absl::ToInt64Microseconds(sec));
175  EXPECT_EQ(N * 1000, absl::ToInt64Milliseconds(sec));
176  EXPECT_EQ(N, absl::ToInt64Seconds(sec));
177  EXPECT_EQ(0, absl::ToInt64Minutes(sec));
178  EXPECT_EQ(0, absl::ToInt64Hours(sec));
179  const absl::Duration min = absl::Minutes(N);
180  EXPECT_EQ(N * 60 * 1000 * 1000 * 1000, absl::ToInt64Nanoseconds(min));
181  EXPECT_EQ(N * 60 * 1000 * 1000, absl::ToInt64Microseconds(min));
182  EXPECT_EQ(N * 60 * 1000, absl::ToInt64Milliseconds(min));
183  EXPECT_EQ(N * 60, absl::ToInt64Seconds(min));
184  EXPECT_EQ(N, absl::ToInt64Minutes(min));
185  EXPECT_EQ(0, absl::ToInt64Hours(min));
186  const absl::Duration hour = absl::Hours(N);
187  EXPECT_EQ(N * 60 * 60 * 1000 * 1000 * 1000, absl::ToInt64Nanoseconds(hour));
188  EXPECT_EQ(N * 60 * 60 * 1000 * 1000, absl::ToInt64Microseconds(hour));
189  EXPECT_EQ(N * 60 * 60 * 1000, absl::ToInt64Milliseconds(hour));
190  EXPECT_EQ(N * 60 * 60, absl::ToInt64Seconds(hour));
191  EXPECT_EQ(N * 60, absl::ToInt64Minutes(hour));
192  EXPECT_EQ(N, absl::ToInt64Hours(hour));
193 }
194 
195 TEST(Duration, ToConversionDeprecated) {
196  TestToConversion<43>();
197  TestToConversion<1>();
198  TestToConversion<0>();
199  TestToConversion<-1>();
200  TestToConversion<-43>();
201 }
202 
203 template <int64_t N>
204 void TestFromChronoBasicEquality() {
205  using std::chrono::nanoseconds;
206  using std::chrono::microseconds;
207  using std::chrono::milliseconds;
208  using std::chrono::seconds;
209  using std::chrono::minutes;
210  using std::chrono::hours;
211 
212  static_assert(absl::Nanoseconds(N) == absl::FromChrono(nanoseconds(N)), "");
213  static_assert(absl::Microseconds(N) == absl::FromChrono(microseconds(N)), "");
214  static_assert(absl::Milliseconds(N) == absl::FromChrono(milliseconds(N)), "");
215  static_assert(absl::Seconds(N) == absl::FromChrono(seconds(N)), "");
216  static_assert(absl::Minutes(N) == absl::FromChrono(minutes(N)), "");
217  static_assert(absl::Hours(N) == absl::FromChrono(hours(N)), "");
218 }
219 
220 TEST(Duration, FromChrono) {
221  TestFromChronoBasicEquality<-123>();
222  TestFromChronoBasicEquality<-1>();
223  TestFromChronoBasicEquality<0>();
224  TestFromChronoBasicEquality<1>();
225  TestFromChronoBasicEquality<123>();
226 
227  // Minutes (might, depending on the platform) saturate at +inf.
228  const auto chrono_minutes_max = std::chrono::minutes::max();
229  const auto minutes_max = absl::FromChrono(chrono_minutes_max);
230  const int64_t minutes_max_count = chrono_minutes_max.count();
231  if (minutes_max_count > kint64max / 60) {
232  EXPECT_EQ(absl::InfiniteDuration(), minutes_max);
233  } else {
234  EXPECT_EQ(absl::Minutes(minutes_max_count), minutes_max);
235  }
236 
237  // Minutes (might, depending on the platform) saturate at -inf.
238  const auto chrono_minutes_min = std::chrono::minutes::min();
239  const auto minutes_min = absl::FromChrono(chrono_minutes_min);
240  const int64_t minutes_min_count = chrono_minutes_min.count();
241  if (minutes_min_count < kint64min / 60) {
242  EXPECT_EQ(-absl::InfiniteDuration(), minutes_min);
243  } else {
244  EXPECT_EQ(absl::Minutes(minutes_min_count), minutes_min);
245  }
246 
247  // Hours (might, depending on the platform) saturate at +inf.
248  const auto chrono_hours_max = std::chrono::hours::max();
249  const auto hours_max = absl::FromChrono(chrono_hours_max);
250  const int64_t hours_max_count = chrono_hours_max.count();
251  if (hours_max_count > kint64max / 3600) {
252  EXPECT_EQ(absl::InfiniteDuration(), hours_max);
253  } else {
254  EXPECT_EQ(absl::Hours(hours_max_count), hours_max);
255  }
256 
257  // Hours (might, depending on the platform) saturate at -inf.
258  const auto chrono_hours_min = std::chrono::hours::min();
259  const auto hours_min = absl::FromChrono(chrono_hours_min);
260  const int64_t hours_min_count = chrono_hours_min.count();
261  if (hours_min_count < kint64min / 3600) {
262  EXPECT_EQ(-absl::InfiniteDuration(), hours_min);
263  } else {
264  EXPECT_EQ(absl::Hours(hours_min_count), hours_min);
265  }
266 }
267 
268 template <int64_t N>
269 void TestToChrono() {
270  using std::chrono::nanoseconds;
271  using std::chrono::microseconds;
272  using std::chrono::milliseconds;
273  using std::chrono::seconds;
274  using std::chrono::minutes;
275  using std::chrono::hours;
276 
277  EXPECT_EQ(nanoseconds(N), absl::ToChronoNanoseconds(absl::Nanoseconds(N)));
278  EXPECT_EQ(microseconds(N), absl::ToChronoMicroseconds(absl::Microseconds(N)));
279  EXPECT_EQ(milliseconds(N), absl::ToChronoMilliseconds(absl::Milliseconds(N)));
280  EXPECT_EQ(seconds(N), absl::ToChronoSeconds(absl::Seconds(N)));
281 
282  constexpr auto absl_minutes = absl::Minutes(N);
283  auto chrono_minutes = minutes(N);
284  if (absl_minutes == -absl::InfiniteDuration()) {
285  chrono_minutes = minutes::min();
286  } else if (absl_minutes == absl::InfiniteDuration()) {
287  chrono_minutes = minutes::max();
288  }
289  EXPECT_EQ(chrono_minutes, absl::ToChronoMinutes(absl_minutes));
290 
291  constexpr auto absl_hours = absl::Hours(N);
292  auto chrono_hours = hours(N);
293  if (absl_hours == -absl::InfiniteDuration()) {
294  chrono_hours = hours::min();
295  } else if (absl_hours == absl::InfiniteDuration()) {
296  chrono_hours = hours::max();
297  }
298  EXPECT_EQ(chrono_hours, absl::ToChronoHours(absl_hours));
299 }
300 
301 TEST(Duration, ToChrono) {
302  using std::chrono::nanoseconds;
303  using std::chrono::microseconds;
304  using std::chrono::milliseconds;
305  using std::chrono::seconds;
306  using std::chrono::minutes;
307  using std::chrono::hours;
308 
309  TestToChrono<kint64min>();
310  TestToChrono<-1>();
311  TestToChrono<0>();
312  TestToChrono<1>();
313  TestToChrono<kint64max>();
314 
315  // Verify truncation toward zero.
316  const auto tick = absl::Nanoseconds(1) / 4;
317  EXPECT_EQ(nanoseconds(0), absl::ToChronoNanoseconds(tick));
318  EXPECT_EQ(nanoseconds(0), absl::ToChronoNanoseconds(-tick));
319  EXPECT_EQ(microseconds(0), absl::ToChronoMicroseconds(tick));
320  EXPECT_EQ(microseconds(0), absl::ToChronoMicroseconds(-tick));
321  EXPECT_EQ(milliseconds(0), absl::ToChronoMilliseconds(tick));
322  EXPECT_EQ(milliseconds(0), absl::ToChronoMilliseconds(-tick));
323  EXPECT_EQ(seconds(0), absl::ToChronoSeconds(tick));
324  EXPECT_EQ(seconds(0), absl::ToChronoSeconds(-tick));
325  EXPECT_EQ(minutes(0), absl::ToChronoMinutes(tick));
326  EXPECT_EQ(minutes(0), absl::ToChronoMinutes(-tick));
327  EXPECT_EQ(hours(0), absl::ToChronoHours(tick));
328  EXPECT_EQ(hours(0), absl::ToChronoHours(-tick));
329 
330  // Verifies +/- infinity saturation at max/min.
331  constexpr auto inf = absl::InfiniteDuration();
332  EXPECT_EQ(nanoseconds::min(), absl::ToChronoNanoseconds(-inf));
333  EXPECT_EQ(nanoseconds::max(), absl::ToChronoNanoseconds(inf));
334  EXPECT_EQ(microseconds::min(), absl::ToChronoMicroseconds(-inf));
335  EXPECT_EQ(microseconds::max(), absl::ToChronoMicroseconds(inf));
336  EXPECT_EQ(milliseconds::min(), absl::ToChronoMilliseconds(-inf));
337  EXPECT_EQ(milliseconds::max(), absl::ToChronoMilliseconds(inf));
338  EXPECT_EQ(seconds::min(), absl::ToChronoSeconds(-inf));
339  EXPECT_EQ(seconds::max(), absl::ToChronoSeconds(inf));
340  EXPECT_EQ(minutes::min(), absl::ToChronoMinutes(-inf));
341  EXPECT_EQ(minutes::max(), absl::ToChronoMinutes(inf));
342  EXPECT_EQ(hours::min(), absl::ToChronoHours(-inf));
343  EXPECT_EQ(hours::max(), absl::ToChronoHours(inf));
344 }
345 
346 TEST(Duration, FactoryOverloads) {
347  enum E { kOne = 1 };
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)));
362 
369 
370 #undef TEST_FACTORY_OVERLOADS
371 
372  EXPECT_EQ(absl::Milliseconds(1500), absl::Seconds(1.5));
373  EXPECT_LT(absl::Nanoseconds(1), absl::Nanoseconds(1.5));
374  EXPECT_GT(absl::Nanoseconds(2), absl::Nanoseconds(1.5));
375 
376  const double dbl_inf = std::numeric_limits<double>::infinity();
377  EXPECT_EQ(absl::InfiniteDuration(), absl::Nanoseconds(dbl_inf));
378  EXPECT_EQ(absl::InfiniteDuration(), absl::Microseconds(dbl_inf));
379  EXPECT_EQ(absl::InfiniteDuration(), absl::Milliseconds(dbl_inf));
380  EXPECT_EQ(absl::InfiniteDuration(), absl::Seconds(dbl_inf));
381  EXPECT_EQ(absl::InfiniteDuration(), absl::Minutes(dbl_inf));
382  EXPECT_EQ(absl::InfiniteDuration(), absl::Hours(dbl_inf));
383  EXPECT_EQ(-absl::InfiniteDuration(), absl::Nanoseconds(-dbl_inf));
384  EXPECT_EQ(-absl::InfiniteDuration(), absl::Microseconds(-dbl_inf));
385  EXPECT_EQ(-absl::InfiniteDuration(), absl::Milliseconds(-dbl_inf));
386  EXPECT_EQ(-absl::InfiniteDuration(), absl::Seconds(-dbl_inf));
387  EXPECT_EQ(-absl::InfiniteDuration(), absl::Minutes(-dbl_inf));
388  EXPECT_EQ(-absl::InfiniteDuration(), absl::Hours(-dbl_inf));
389 }
390 
391 TEST(Duration, InfinityExamples) {
392  // These examples are used in the documentation in time.h. They are
393  // written so that they can be copy-n-pasted easily.
394 
395  constexpr absl::Duration inf = absl::InfiniteDuration();
396  constexpr absl::Duration d = absl::Seconds(1); // Any finite duration
397 
398  EXPECT_TRUE(inf == inf + inf);
399  EXPECT_TRUE(inf == inf + d);
400  EXPECT_TRUE(inf == inf - inf);
401  EXPECT_TRUE(-inf == d - inf);
402 
403  EXPECT_TRUE(inf == d * 1e100);
404  EXPECT_TRUE(0 == d / inf); // NOLINT(readability/check)
405 
406  // Division by zero returns infinity, or kint64min/MAX where necessary.
407  EXPECT_TRUE(inf == d / 0);
408  EXPECT_TRUE(kint64max == d / absl::ZeroDuration());
409 }
410 
411 TEST(Duration, InfinityComparison) {
413  const absl::Duration any_dur = absl::Seconds(1);
414 
415  // Equality
416  EXPECT_EQ(inf, inf);
417  EXPECT_EQ(-inf, -inf);
418  EXPECT_NE(inf, -inf);
419  EXPECT_NE(any_dur, inf);
420  EXPECT_NE(any_dur, -inf);
421 
422  // Relational
423  EXPECT_GT(inf, any_dur);
424  EXPECT_LT(-inf, any_dur);
425  EXPECT_LT(-inf, inf);
426  EXPECT_GT(inf, -inf);
427 }
428 
429 TEST(Duration, InfinityAddition) {
430  const absl::Duration sec_max = absl::Seconds(kint64max);
431  const absl::Duration sec_min = absl::Seconds(kint64min);
432  const absl::Duration any_dur = absl::Seconds(1);
434 
435  // Addition
436  EXPECT_EQ(inf, inf + inf);
437  EXPECT_EQ(inf, inf + -inf);
438  EXPECT_EQ(-inf, -inf + inf);
439  EXPECT_EQ(-inf, -inf + -inf);
440 
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);
445 
446  // Interesting case
447  absl::Duration almost_inf = sec_max + absl::Nanoseconds(999999999);
448  EXPECT_GT(inf, almost_inf);
449  almost_inf += -absl::Nanoseconds(999999999);
450  EXPECT_GT(inf, almost_inf);
451 
452  // Addition overflow/underflow
453  EXPECT_EQ(inf, sec_max + absl::Seconds(1));
454  EXPECT_EQ(inf, sec_max + sec_max);
455  EXPECT_EQ(-inf, sec_min + -absl::Seconds(1));
456  EXPECT_EQ(-inf, sec_min + -sec_max);
457 
458  // For reference: IEEE 754 behavior
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)); // We return inf
462  EXPECT_TRUE(std::isnan(-dbl_inf + dbl_inf)); // We return inf
463  EXPECT_TRUE(std::isinf(-dbl_inf + -dbl_inf));
464 }
465 
466 TEST(Duration, InfinitySubtraction) {
467  const absl::Duration sec_max = absl::Seconds(kint64max);
468  const absl::Duration sec_min = absl::Seconds(kint64min);
469  const absl::Duration any_dur = absl::Seconds(1);
471 
472  // Subtraction
473  EXPECT_EQ(inf, inf - inf);
474  EXPECT_EQ(inf, inf - -inf);
475  EXPECT_EQ(-inf, -inf - inf);
476  EXPECT_EQ(-inf, -inf - -inf);
477 
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);
482 
483  // Subtraction overflow/underflow
484  EXPECT_EQ(inf, sec_max - -absl::Seconds(1));
485  EXPECT_EQ(inf, sec_max - -sec_max);
486  EXPECT_EQ(-inf, sec_min - absl::Seconds(1));
487  EXPECT_EQ(-inf, sec_min - sec_max);
488 
489  // Interesting case
490  absl::Duration almost_neg_inf = sec_min;
491  EXPECT_LT(-inf, almost_neg_inf);
492  almost_neg_inf -= -absl::Nanoseconds(1);
493  EXPECT_LT(-inf, almost_neg_inf);
494 
495  // For reference: IEEE 754 behavior
496  const double dbl_inf = std::numeric_limits<double>::infinity();
497  EXPECT_TRUE(std::isnan(dbl_inf - dbl_inf)); // We return 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)); // We return inf
501 }
502 
503 TEST(Duration, InfinityMultiplication) {
504  const absl::Duration sec_max = absl::Seconds(kint64max);
505  const absl::Duration sec_min = absl::Seconds(kint64min);
507 
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));
521 
522  TEST_INF_MUL_WITH_TYPE(int64_t); // NOLINT(readability/function)
523  TEST_INF_MUL_WITH_TYPE(double); // NOLINT(readability/function)
524 
525 #undef TEST_INF_MUL_WITH_TYPE
526 
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);
532 
533  const absl::Duration any_dur = absl::Seconds(1);
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);
538 
539  // Fixed-point multiplication will produce a finite value, whereas floating
540  // point fuzziness will overflow to inf.
541  EXPECT_NE(absl::InfiniteDuration(), absl::Seconds(1) * kint64max);
542  EXPECT_EQ(inf, absl::Seconds(1) * static_cast<double>(kint64max));
543  EXPECT_NE(-absl::InfiniteDuration(), absl::Seconds(1) * kint64min);
544  EXPECT_EQ(-inf, absl::Seconds(1) * static_cast<double>(kint64min));
545 
546  // Note that sec_max * or / by 1.0 overflows to inf due to the 53-bit
547  // limitations of double.
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);
553 }
554 
555 TEST(Duration, InfinityDivision) {
556  const absl::Duration sec_max = absl::Seconds(kint64max);
557  const absl::Duration sec_min = absl::Seconds(kint64min);
559 
560  // Division of Duration by a double
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));
566 
567  TEST_INF_DIV_WITH_TYPE(int64_t); // NOLINT(readability/function)
568  TEST_INF_DIV_WITH_TYPE(double); // NOLINT(readability/function)
569 
570 #undef TEST_INF_DIV_WITH_TYPE
571 
572  // Division of Duration by a double overflow/underflow
573  EXPECT_EQ(inf, sec_max / 0.5);
574  EXPECT_EQ(inf, sec_min / -0.5);
575  EXPECT_EQ(inf, ((sec_max / 0.5) + absl::Seconds(1)) / 0.5);
576  EXPECT_EQ(-inf, sec_max / -0.5);
577  EXPECT_EQ(-inf, sec_min / 0.5);
578  EXPECT_EQ(-inf, ((sec_min / 0.5) - absl::Seconds(1)) / 0.5);
579 
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);
585 
586  const absl::Duration any_dur = absl::Seconds(1);
587  EXPECT_EQ(absl::ZeroDuration(), any_dur / dbl_inf);
588  EXPECT_EQ(absl::ZeroDuration(), any_dur / -dbl_inf);
589  EXPECT_EQ(absl::ZeroDuration(), -any_dur / dbl_inf);
590  EXPECT_EQ(absl::ZeroDuration(), -any_dur / -dbl_inf);
591 }
592 
593 TEST(Duration, InfinityModulus) {
594  const absl::Duration sec_max = absl::Seconds(kint64max);
595  const absl::Duration any_dur = absl::Seconds(1);
597 
598  EXPECT_EQ(inf, inf % inf);
599  EXPECT_EQ(inf, inf % -inf);
600  EXPECT_EQ(-inf, -inf % -inf);
601  EXPECT_EQ(-inf, -inf % inf);
602 
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);
607 
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);
612 
613  // Remainder isn't affected by overflow.
614  EXPECT_EQ(absl::ZeroDuration(), sec_max % absl::Seconds(1));
615  EXPECT_EQ(absl::ZeroDuration(), sec_max % absl::Milliseconds(1));
616  EXPECT_EQ(absl::ZeroDuration(), sec_max % absl::Microseconds(1));
617  EXPECT_EQ(absl::ZeroDuration(), sec_max % absl::Nanoseconds(1));
618  EXPECT_EQ(absl::ZeroDuration(), sec_max % absl::Nanoseconds(1) / 4);
619 }
620 
621 TEST(Duration, InfinityIDiv) {
622  const absl::Duration sec_max = absl::Seconds(kint64max);
623  const absl::Duration any_dur = absl::Seconds(1);
625  const double dbl_inf = std::numeric_limits<double>::infinity();
626 
627  // IDivDuration (int64_t return value + a remainer)
629  EXPECT_EQ(kint64max, absl::IDivDuration(inf, inf, &rem));
630  EXPECT_EQ(inf, rem);
631 
632  rem = absl::ZeroDuration();
633  EXPECT_EQ(kint64max, absl::IDivDuration(-inf, -inf, &rem));
634  EXPECT_EQ(-inf, rem);
635 
636  rem = absl::ZeroDuration();
637  EXPECT_EQ(kint64max, absl::IDivDuration(inf, any_dur, &rem));
638  EXPECT_EQ(inf, rem);
639 
640  rem = absl::ZeroDuration();
641  EXPECT_EQ(0, absl::IDivDuration(any_dur, inf, &rem));
642  EXPECT_EQ(any_dur, rem);
643 
644  rem = absl::ZeroDuration();
645  EXPECT_EQ(kint64max, absl::IDivDuration(-inf, -any_dur, &rem));
646  EXPECT_EQ(-inf, rem);
647 
648  rem = absl::ZeroDuration();
649  EXPECT_EQ(0, absl::IDivDuration(-any_dur, -inf, &rem));
650  EXPECT_EQ(-any_dur, rem);
651 
652  rem = absl::ZeroDuration();
653  EXPECT_EQ(kint64min, absl::IDivDuration(-inf, inf, &rem));
654  EXPECT_EQ(-inf, rem);
655 
656  rem = absl::ZeroDuration();
657  EXPECT_EQ(kint64min, absl::IDivDuration(inf, -inf, &rem));
658  EXPECT_EQ(inf, rem);
659 
660  rem = absl::ZeroDuration();
661  EXPECT_EQ(kint64min, absl::IDivDuration(-inf, any_dur, &rem));
662  EXPECT_EQ(-inf, rem);
663 
664  rem = absl::ZeroDuration();
665  EXPECT_EQ(0, absl::IDivDuration(-any_dur, inf, &rem));
666  EXPECT_EQ(-any_dur, rem);
667 
668  rem = absl::ZeroDuration();
669  EXPECT_EQ(kint64min, absl::IDivDuration(inf, -any_dur, &rem));
670  EXPECT_EQ(inf, rem);
671 
672  rem = absl::ZeroDuration();
673  EXPECT_EQ(0, absl::IDivDuration(any_dur, -inf, &rem));
674  EXPECT_EQ(any_dur, rem);
675 
676  // IDivDuration overflow/underflow
677  rem = any_dur;
678  EXPECT_EQ(kint64max,
679  absl::IDivDuration(sec_max, absl::Nanoseconds(1) / 4, &rem));
680  EXPECT_EQ(sec_max - absl::Nanoseconds(kint64max) / 4, rem);
681 
682  rem = any_dur;
683  EXPECT_EQ(kint64max,
684  absl::IDivDuration(sec_max, absl::Milliseconds(1), &rem));
685  EXPECT_EQ(sec_max - absl::Milliseconds(kint64max), rem);
686 
687  rem = any_dur;
688  EXPECT_EQ(kint64max,
689  absl::IDivDuration(-sec_max, -absl::Milliseconds(1), &rem));
690  EXPECT_EQ(-sec_max + absl::Milliseconds(kint64max), rem);
691 
692  rem = any_dur;
693  EXPECT_EQ(kint64min,
694  absl::IDivDuration(-sec_max, absl::Milliseconds(1), &rem));
695  EXPECT_EQ(-sec_max - absl::Milliseconds(kint64min), rem);
696 
697  rem = any_dur;
698  EXPECT_EQ(kint64min,
699  absl::IDivDuration(sec_max, -absl::Milliseconds(1), &rem));
700  EXPECT_EQ(sec_max + absl::Milliseconds(kint64min), rem);
701 
702  //
703  // operator/(Duration, Duration) is a wrapper for IDivDuration().
704  //
705 
706  // IEEE 754 says inf / inf should be nan, but int64_t doesn't have
707  // nan so we'll return kint64max/kint64min instead.
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);
713 
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);
719 
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);
725  EXPECT_EQ(0, absl::ZeroDuration() / inf);
726 
727  // Division of Duration by a Duration overflow/underflow
728  EXPECT_EQ(kint64max, sec_max / absl::Milliseconds(1));
729  EXPECT_EQ(kint64max, -sec_max / -absl::Milliseconds(1));
730  EXPECT_EQ(kint64min, -sec_max / absl::Milliseconds(1));
731  EXPECT_EQ(kint64min, sec_max / -absl::Milliseconds(1));
732 }
733 
734 TEST(Duration, InfinityFDiv) {
735  const absl::Duration any_dur = absl::Seconds(1);
737  const double dbl_inf = std::numeric_limits<double>::infinity();
738 
739  EXPECT_EQ(dbl_inf, absl::FDivDuration(inf, inf));
740  EXPECT_EQ(dbl_inf, absl::FDivDuration(-inf, -inf));
741  EXPECT_EQ(dbl_inf, absl::FDivDuration(inf, any_dur));
742  EXPECT_EQ(0.0, absl::FDivDuration(any_dur, inf));
743  EXPECT_EQ(dbl_inf, absl::FDivDuration(-inf, -any_dur));
744  EXPECT_EQ(0.0, absl::FDivDuration(-any_dur, -inf));
745 
746  EXPECT_EQ(-dbl_inf, absl::FDivDuration(-inf, inf));
747  EXPECT_EQ(-dbl_inf, absl::FDivDuration(inf, -inf));
748  EXPECT_EQ(-dbl_inf, absl::FDivDuration(-inf, any_dur));
749  EXPECT_EQ(0.0, absl::FDivDuration(-any_dur, inf));
750  EXPECT_EQ(-dbl_inf, absl::FDivDuration(inf, -any_dur));
751  EXPECT_EQ(0.0, absl::FDivDuration(any_dur, -inf));
752 }
753 
754 TEST(Duration, DivisionByZero) {
755  const absl::Duration zero = absl::ZeroDuration();
757  const absl::Duration any_dur = absl::Seconds(1);
758  const double dbl_inf = std::numeric_limits<double>::infinity();
759  const double dbl_denorm = std::numeric_limits<double>::denorm_min();
760 
761  // IEEE 754 behavior
762  double z = 0.0, two = 2.0;
763  EXPECT_TRUE(std::isinf(two / z));
764  EXPECT_TRUE(std::isnan(z / z)); // We'll return inf
765 
766  // Operator/(Duration, double)
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);
773 
774  // Tests dividing by a number very close to, but not quite zero.
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);
781 
782  // IDiv
783  absl::Duration rem = zero;
784  EXPECT_EQ(kint64max, absl::IDivDuration(zero, zero, &rem));
785  EXPECT_EQ(inf, rem);
786 
787  rem = zero;
788  EXPECT_EQ(kint64max, absl::IDivDuration(any_dur, zero, &rem));
789  EXPECT_EQ(inf, rem);
790 
791  rem = zero;
792  EXPECT_EQ(kint64min, absl::IDivDuration(-any_dur, zero, &rem));
793  EXPECT_EQ(-inf, rem);
794 
795  // Operator/(Duration, Duration)
796  EXPECT_EQ(kint64max, zero / zero);
797  EXPECT_EQ(kint64max, any_dur / zero);
798  EXPECT_EQ(kint64min, -any_dur / zero);
799 
800  // FDiv
801  EXPECT_EQ(dbl_inf, absl::FDivDuration(zero, zero));
802  EXPECT_EQ(dbl_inf, absl::FDivDuration(any_dur, zero));
803  EXPECT_EQ(-dbl_inf, absl::FDivDuration(-any_dur, zero));
804 }
805 
806 TEST(Duration, NaN) {
807  // Note that IEEE 754 does not define the behavior of a nan's sign when it is
808  // copied, so the code below allows for either + or - InfiniteDuration.
809 #define TEST_NAN_HANDLING(NAME, NAN) \
810  do { \
811  const auto inf = absl::InfiniteDuration(); \
812  auto x = NAME(NAN); \
813  EXPECT_TRUE(x == inf || x == -inf); \
814  auto y = NAME(42); \
815  y *= NAN; \
816  EXPECT_TRUE(y == inf || y == -inf); \
817  auto z = NAME(42); \
818  z /= NAN; \
819  EXPECT_TRUE(z == inf || z == -inf); \
820  } while (0)
821 
822  const double nan = std::numeric_limits<double>::quiet_NaN();
829 
836 
837 #undef TEST_NAN_HANDLING
838 }
839 
840 TEST(Duration, Range) {
841  const absl::Duration range = ApproxYears(100 * 1e9);
842  const absl::Duration range_future = range;
843  const absl::Duration range_past = -range;
844 
845  EXPECT_LT(range_future, absl::InfiniteDuration());
846  EXPECT_GT(range_past, -absl::InfiniteDuration());
847 
848  const absl::Duration full_range = range_future - range_past;
849  EXPECT_GT(full_range, absl::ZeroDuration());
850  EXPECT_LT(full_range, absl::InfiniteDuration());
851 
852  const absl::Duration neg_full_range = range_past - range_future;
853  EXPECT_LT(neg_full_range, absl::ZeroDuration());
854  EXPECT_GT(neg_full_range, -absl::InfiniteDuration());
855 
856  EXPECT_LT(neg_full_range, full_range);
857  EXPECT_EQ(neg_full_range, -full_range);
858 }
859 
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), "");
870 
877 
878 #undef TEST_REL_OPS
879 }
880 
881 TEST(Duration, Addition) {
882 #define TEST_ADD_OPS(UNIT) \
883  do { \
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); \
891  a += UNIT(1); \
892  EXPECT_EQ(UNIT(2), a); \
893  a -= UNIT(1); \
894  EXPECT_EQ(UNIT(1), a); \
895  } while (0)
896 
903 
904 #undef TEST_ADD_OPS
905 
906  EXPECT_EQ(absl::Seconds(2), absl::Seconds(3) - 2 * absl::Milliseconds(500));
907  EXPECT_EQ(absl::Seconds(2) + absl::Milliseconds(500),
909 
910  EXPECT_EQ(absl::Seconds(1) + absl::Milliseconds(998),
912 
913  EXPECT_EQ(absl::Milliseconds(-1),
915 
916  // Tests fractions of a nanoseconds. These are implementation details only.
917  EXPECT_GT(absl::Nanoseconds(1), absl::Nanoseconds(1) / 2);
918  EXPECT_EQ(absl::Nanoseconds(1),
919  absl::Nanoseconds(1) / 2 + absl::Nanoseconds(1) / 2);
920  EXPECT_GT(absl::Nanoseconds(1) / 4, absl::Nanoseconds(0));
921  EXPECT_EQ(absl::Nanoseconds(1) / 8, absl::Nanoseconds(0));
922 
923  // Tests subtraction that will cause wrap around of the rep_lo_ bits.
926  absl::Duration ans_3_8 = absl::Seconds(3) + absl::Milliseconds(800);
927  EXPECT_EQ(ans_3_8, d_7_5 - d_3_7);
928 
929  // Subtracting min_duration
930  absl::Duration min_dur = absl::Seconds(kint64min);
931  EXPECT_EQ(absl::Seconds(0), min_dur - min_dur);
932  EXPECT_EQ(absl::Seconds(kint64max), absl::Seconds(-1) - min_dur);
933 }
934 
935 TEST(Duration, Negation) {
936  // By storing negations of various values in constexpr variables we
937  // verify that the initializers are constant expressions.
938  constexpr absl::Duration negated_zero_duration = -absl::ZeroDuration();
939  EXPECT_EQ(negated_zero_duration, absl::ZeroDuration());
940 
941  constexpr absl::Duration negated_infinite_duration =
943  EXPECT_NE(negated_infinite_duration, absl::InfiniteDuration());
944  EXPECT_EQ(-negated_infinite_duration, absl::InfiniteDuration());
945 
946  // The public APIs to check if a duration is infinite depend on using
947  // -InfiniteDuration(), but we're trying to test operator- here, so we
948  // need to use the lower-level internal query IsInfiniteDuration.
949  EXPECT_TRUE(
950  absl::time_internal::IsInfiniteDuration(negated_infinite_duration));
951 
952  // The largest Duration is kint64max seconds and kTicksPerSecond - 1 ticks.
953  // Using the absl::time_internal::MakeDuration API is the cleanest way to
954  // construct that Duration.
955  constexpr absl::Duration max_duration = absl::time_internal::MakeDuration(
956  kint64max, absl::time_internal::kTicksPerSecond - 1);
957  constexpr absl::Duration negated_max_duration = -max_duration;
958  // The largest negatable value is one tick above the minimum representable;
959  // it's the negation of max_duration.
960  constexpr absl::Duration nearly_min_duration =
961  absl::time_internal::MakeDuration(kint64min, int64_t{1});
962  constexpr absl::Duration negated_nearly_min_duration = -nearly_min_duration;
963 
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);
967 
968  constexpr absl::Duration min_duration =
970  constexpr absl::Duration negated_min_duration = -min_duration;
971  EXPECT_EQ(negated_min_duration, absl::InfiniteDuration());
972 }
973 
974 TEST(Duration, AbsoluteValue) {
976  EXPECT_EQ(absl::Seconds(1), AbsDuration(absl::Seconds(1)));
977  EXPECT_EQ(absl::Seconds(1), AbsDuration(absl::Seconds(-1)));
978 
981 
982  absl::Duration max_dur =
983  absl::Seconds(kint64max) + (absl::Seconds(1) - absl::Nanoseconds(1) / 4);
984  EXPECT_EQ(max_dur, AbsDuration(max_dur));
985 
986  absl::Duration min_dur = absl::Seconds(kint64min);
987  EXPECT_EQ(absl::InfiniteDuration(), AbsDuration(min_dur));
988  EXPECT_EQ(max_dur, AbsDuration(min_dur + absl::Nanoseconds(1) / 4));
989 }
990 
991 TEST(Duration, Multiplication) {
992 #define TEST_MUL_OPS(UNIT) \
993  do { \
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); \
1004  a *= 2.5; \
1005  EXPECT_EQ(UNIT(5), a); \
1006  a /= 2.5; \
1007  EXPECT_EQ(UNIT(2), a); \
1008  a %= UNIT(1); \
1009  EXPECT_EQ(UNIT(0), a); \
1010  absl::Duration big = UNIT(1000000000); \
1011  big *= 3; \
1012  big /= 3; \
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))); \
1022  } while (0)
1023 
1030 
1031 #undef TEST_MUL_OPS
1032 
1033  // Ensures that multiplication and division by 1 with a maxed-out durations
1034  // doesn't lose precision.
1035  absl::Duration max_dur =
1036  absl::Seconds(kint64max) + (absl::Seconds(1) - absl::Nanoseconds(1) / 4);
1037  absl::Duration min_dur = absl::Seconds(kint64min);
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);
1042 
1043  // Tests division on a Duration with a large number of significant digits.
1044  // Tests when the digits span hi and lo as well as only in hi.
1045  absl::Duration sigfigs = absl::Seconds(2000000000) + absl::Nanoseconds(3);
1046  EXPECT_EQ(absl::Seconds(666666666) + absl::Nanoseconds(666666667) +
1047  absl::Nanoseconds(1) / 2,
1048  sigfigs / 3);
1049  sigfigs = absl::Seconds(7000000000LL);
1050  EXPECT_EQ(absl::Seconds(2333333333) + absl::Nanoseconds(333333333) +
1051  absl::Nanoseconds(1) / 4,
1052  sigfigs / 3);
1053 
1054  EXPECT_EQ(absl::Seconds(7) + absl::Milliseconds(500), absl::Seconds(3) * 2.5);
1055  EXPECT_EQ(absl::Seconds(8) * -1 + absl::Milliseconds(300),
1056  (absl::Seconds(2) + absl::Milliseconds(200)) * -3.5);
1057  EXPECT_EQ(-absl::Seconds(8) + absl::Milliseconds(300),
1058  (absl::Seconds(2) + absl::Milliseconds(200)) * -3.5);
1059  EXPECT_EQ(absl::Seconds(1) + absl::Milliseconds(875),
1060  (absl::Seconds(7) + absl::Milliseconds(500)) / 4);
1061  EXPECT_EQ(absl::Seconds(30),
1062  (absl::Seconds(7) + absl::Milliseconds(500)) / 0.25);
1063  EXPECT_EQ(absl::Seconds(3),
1064  (absl::Seconds(7) + absl::Milliseconds(500)) / 2.5);
1065 
1066  // Tests division remainder.
1067  EXPECT_EQ(absl::Nanoseconds(0), absl::Nanoseconds(7) % absl::Nanoseconds(1));
1068  EXPECT_EQ(absl::Nanoseconds(0), absl::Nanoseconds(0) % absl::Nanoseconds(10));
1069  EXPECT_EQ(absl::Nanoseconds(2), absl::Nanoseconds(7) % absl::Nanoseconds(5));
1070  EXPECT_EQ(absl::Nanoseconds(2), absl::Nanoseconds(2) % absl::Nanoseconds(5));
1071 
1072  EXPECT_EQ(absl::Nanoseconds(1), absl::Nanoseconds(10) % absl::Nanoseconds(3));
1073  EXPECT_EQ(absl::Nanoseconds(1),
1075  EXPECT_EQ(absl::Nanoseconds(-1),
1077  EXPECT_EQ(absl::Nanoseconds(-1),
1079 
1080  EXPECT_EQ(absl::Milliseconds(100),
1081  absl::Seconds(1) % absl::Milliseconds(300));
1082  EXPECT_EQ(
1083  absl::Milliseconds(300),
1085 
1086  EXPECT_EQ(absl::Nanoseconds(1), absl::Nanoseconds(1) % absl::Seconds(1));
1087  EXPECT_EQ(absl::Nanoseconds(-1), absl::Nanoseconds(-1) % absl::Seconds(1));
1088  EXPECT_EQ(0, absl::Nanoseconds(-1) / absl::Seconds(1)); // Actual -1e-9
1089 
1090  // Tests identity a = (a/b)*b + a%b
1091 #define TEST_MOD_IDENTITY(a, b) \
1092  EXPECT_EQ((a), ((a) / (b))*(b) + ((a)%(b)))
1093 
1098 
1102 
1107 
1111 
1112  // Mixed seconds + subseconds
1115 
1116  TEST_MOD_IDENTITY(absl::Seconds(0), mixed_a);
1117  TEST_MOD_IDENTITY(mixed_a, mixed_a);
1118  TEST_MOD_IDENTITY(mixed_a, mixed_b);
1119  TEST_MOD_IDENTITY(mixed_b, mixed_a);
1120 
1121  TEST_MOD_IDENTITY(-mixed_a, mixed_b);
1122  TEST_MOD_IDENTITY(mixed_a, -mixed_b);
1123  TEST_MOD_IDENTITY(-mixed_a, -mixed_b);
1124 
1125 #undef TEST_MOD_IDENTITY
1126 }
1127 
1128 TEST(Duration, Truncation) {
1129  const absl::Duration d = absl::Nanoseconds(1234567890);
1130  const absl::Duration inf = absl::InfiniteDuration();
1131  for (int unit_sign : {1, -1}) { // sign shouldn't matter
1132  EXPECT_EQ(absl::Nanoseconds(1234567890),
1133  Trunc(d, unit_sign * absl::Nanoseconds(1)));
1134  EXPECT_EQ(absl::Microseconds(1234567),
1135  Trunc(d, unit_sign * absl::Microseconds(1)));
1136  EXPECT_EQ(absl::Milliseconds(1234),
1137  Trunc(d, unit_sign * absl::Milliseconds(1)));
1138  EXPECT_EQ(absl::Seconds(1), Trunc(d, unit_sign * absl::Seconds(1)));
1139  EXPECT_EQ(inf, Trunc(inf, unit_sign * absl::Seconds(1)));
1140 
1141  EXPECT_EQ(absl::Nanoseconds(-1234567890),
1142  Trunc(-d, unit_sign * absl::Nanoseconds(1)));
1143  EXPECT_EQ(absl::Microseconds(-1234567),
1144  Trunc(-d, unit_sign * absl::Microseconds(1)));
1145  EXPECT_EQ(absl::Milliseconds(-1234),
1146  Trunc(-d, unit_sign * absl::Milliseconds(1)));
1147  EXPECT_EQ(absl::Seconds(-1), Trunc(-d, unit_sign * absl::Seconds(1)));
1148  EXPECT_EQ(-inf, Trunc(-inf, unit_sign * absl::Seconds(1)));
1149  }
1150 }
1151 
1152 TEST(Duration, Flooring) {
1153  const absl::Duration d = absl::Nanoseconds(1234567890);
1154  const absl::Duration inf = absl::InfiniteDuration();
1155  for (int unit_sign : {1, -1}) { // sign shouldn't matter
1156  EXPECT_EQ(absl::Nanoseconds(1234567890),
1157  absl::Floor(d, unit_sign * absl::Nanoseconds(1)));
1158  EXPECT_EQ(absl::Microseconds(1234567),
1159  absl::Floor(d, unit_sign * absl::Microseconds(1)));
1160  EXPECT_EQ(absl::Milliseconds(1234),
1161  absl::Floor(d, unit_sign * absl::Milliseconds(1)));
1162  EXPECT_EQ(absl::Seconds(1), absl::Floor(d, unit_sign * absl::Seconds(1)));
1163  EXPECT_EQ(inf, absl::Floor(inf, unit_sign * absl::Seconds(1)));
1164 
1165  EXPECT_EQ(absl::Nanoseconds(-1234567890),
1166  absl::Floor(-d, unit_sign * absl::Nanoseconds(1)));
1167  EXPECT_EQ(absl::Microseconds(-1234568),
1168  absl::Floor(-d, unit_sign * absl::Microseconds(1)));
1169  EXPECT_EQ(absl::Milliseconds(-1235),
1170  absl::Floor(-d, unit_sign * absl::Milliseconds(1)));
1171  EXPECT_EQ(absl::Seconds(-2), absl::Floor(-d, unit_sign * absl::Seconds(1)));
1172  EXPECT_EQ(-inf, absl::Floor(-inf, unit_sign * absl::Seconds(1)));
1173  }
1174 }
1175 
1176 TEST(Duration, Ceiling) {
1177  const absl::Duration d = absl::Nanoseconds(1234567890);
1178  const absl::Duration inf = absl::InfiniteDuration();
1179  for (int unit_sign : {1, -1}) { // // sign shouldn't matter
1180  EXPECT_EQ(absl::Nanoseconds(1234567890),
1181  absl::Ceil(d, unit_sign * absl::Nanoseconds(1)));
1182  EXPECT_EQ(absl::Microseconds(1234568),
1183  absl::Ceil(d, unit_sign * absl::Microseconds(1)));
1184  EXPECT_EQ(absl::Milliseconds(1235),
1185  absl::Ceil(d, unit_sign * absl::Milliseconds(1)));
1186  EXPECT_EQ(absl::Seconds(2), absl::Ceil(d, unit_sign * absl::Seconds(1)));
1187  EXPECT_EQ(inf, absl::Ceil(inf, unit_sign * absl::Seconds(1)));
1188 
1189  EXPECT_EQ(absl::Nanoseconds(-1234567890),
1190  absl::Ceil(-d, unit_sign * absl::Nanoseconds(1)));
1191  EXPECT_EQ(absl::Microseconds(-1234567),
1192  absl::Ceil(-d, unit_sign * absl::Microseconds(1)));
1193  EXPECT_EQ(absl::Milliseconds(-1234),
1194  absl::Ceil(-d, unit_sign * absl::Milliseconds(1)));
1195  EXPECT_EQ(absl::Seconds(-1), absl::Ceil(-d, unit_sign * absl::Seconds(1)));
1196  EXPECT_EQ(-inf, absl::Ceil(-inf, unit_sign * absl::Seconds(1)));
1197  }
1198 }
1199 
1200 TEST(Duration, RoundTripUnits) {
1201  const int kRange = 100000;
1202 
1203 #define ROUND_TRIP_UNIT(U, LOW, HIGH) \
1204  do { \
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)); \
1211  else \
1212  EXPECT_EQ(i, absl::U(i) / absl::U(1)); \
1213  } \
1214  } while (0)
1215 
1216  ROUND_TRIP_UNIT(Nanoseconds, kint64min, kint64min + kRange);
1217  ROUND_TRIP_UNIT(Nanoseconds, -kRange, kRange);
1218  ROUND_TRIP_UNIT(Nanoseconds, kint64max - kRange, kint64max);
1219 
1220  ROUND_TRIP_UNIT(Microseconds, kint64min, kint64min + kRange);
1221  ROUND_TRIP_UNIT(Microseconds, -kRange, kRange);
1222  ROUND_TRIP_UNIT(Microseconds, kint64max - kRange, kint64max);
1223 
1224  ROUND_TRIP_UNIT(Milliseconds, kint64min, kint64min + kRange);
1225  ROUND_TRIP_UNIT(Milliseconds, -kRange, kRange);
1226  ROUND_TRIP_UNIT(Milliseconds, kint64max - kRange, kint64max);
1227 
1228  ROUND_TRIP_UNIT(Seconds, kint64min, kint64min + kRange);
1229  ROUND_TRIP_UNIT(Seconds, -kRange, kRange);
1230  ROUND_TRIP_UNIT(Seconds, kint64max - kRange, kint64max);
1231 
1232  ROUND_TRIP_UNIT(Minutes, kint64min / 60, kint64min / 60 + kRange);
1233  ROUND_TRIP_UNIT(Minutes, -kRange, kRange);
1234  ROUND_TRIP_UNIT(Minutes, kint64max / 60 - kRange, kint64max / 60);
1235 
1236  ROUND_TRIP_UNIT(Hours, kint64min / 3600, kint64min / 3600 + kRange);
1237  ROUND_TRIP_UNIT(Hours, -kRange, kRange);
1238  ROUND_TRIP_UNIT(Hours, kint64max / 3600 - kRange, kint64max / 3600);
1239 
1240 #undef ROUND_TRIP_UNIT
1241 }
1242 
1243 TEST(Duration, TruncConversions) {
1244  // Tests ToTimespec()/DurationFromTimespec()
1245  const struct {
1246  absl::Duration d;
1247  timespec ts;
1248  } to_ts[] = {
1249  {absl::Seconds(1) + absl::Nanoseconds(1), {1, 1}},
1250  {absl::Seconds(1) + absl::Nanoseconds(1) / 2, {1, 0}},
1251  {absl::Seconds(1) + absl::Nanoseconds(0), {1, 0}},
1252  {absl::Seconds(0) + absl::Nanoseconds(0), {0, 0}},
1253  {absl::Seconds(0) - absl::Nanoseconds(1) / 2, {0, 0}},
1254  {absl::Seconds(0) - absl::Nanoseconds(1), {-1, 999999999}},
1255  {absl::Seconds(-1) + absl::Nanoseconds(1), {-1, 1}},
1256  {absl::Seconds(-1) + absl::Nanoseconds(1) / 2, {-1, 1}},
1257  {absl::Seconds(-1) + absl::Nanoseconds(0), {-1, 0}},
1258  {absl::Seconds(-1) - absl::Nanoseconds(1) / 2, {-1, 0}},
1259  };
1260  for (const auto& test : to_ts) {
1261  EXPECT_THAT(absl::ToTimespec(test.d), TimespecMatcher(test.ts));
1262  }
1263  const struct {
1264  timespec ts;
1265  absl::Duration d;
1266  } from_ts[] = {
1267  {{1, 1}, absl::Seconds(1) + absl::Nanoseconds(1)},
1268  {{1, 0}, absl::Seconds(1) + absl::Nanoseconds(0)},
1269  {{0, 0}, absl::Seconds(0) + absl::Nanoseconds(0)},
1270  {{0, -1}, absl::Seconds(0) - absl::Nanoseconds(1)},
1271  {{-1, 999999999}, absl::Seconds(0) - absl::Nanoseconds(1)},
1272  {{-1, 1}, absl::Seconds(-1) + absl::Nanoseconds(1)},
1273  {{-1, 0}, absl::Seconds(-1) + absl::Nanoseconds(0)},
1274  {{-1, -1}, absl::Seconds(-1) - absl::Nanoseconds(1)},
1275  {{-2, 999999999}, absl::Seconds(-1) - absl::Nanoseconds(1)},
1276  };
1277  for (const auto& test : from_ts) {
1278  EXPECT_EQ(test.d, absl::DurationFromTimespec(test.ts));
1279  }
1280 
1281  // Tests ToTimeval()/DurationFromTimeval() (same as timespec above)
1282  const struct {
1283  absl::Duration d;
1284  timeval tv;
1285  } to_tv[] = {
1286  {absl::Seconds(1) + absl::Microseconds(1), {1, 1}},
1287  {absl::Seconds(1) + absl::Microseconds(1) / 2, {1, 0}},
1288  {absl::Seconds(1) + absl::Microseconds(0), {1, 0}},
1289  {absl::Seconds(0) + absl::Microseconds(0), {0, 0}},
1290  {absl::Seconds(0) - absl::Microseconds(1) / 2, {0, 0}},
1291  {absl::Seconds(0) - absl::Microseconds(1), {-1, 999999}},
1292  {absl::Seconds(-1) + absl::Microseconds(1), {-1, 1}},
1293  {absl::Seconds(-1) + absl::Microseconds(1) / 2, {-1, 1}},
1294  {absl::Seconds(-1) + absl::Microseconds(0), {-1, 0}},
1295  {absl::Seconds(-1) - absl::Microseconds(1) / 2, {-1, 0}},
1296  };
1297  for (const auto& test : to_tv) {
1298  EXPECT_THAT(absl::ToTimeval(test.d), TimevalMatcher(test.tv));
1299  }
1300  const struct {
1301  timeval tv;
1302  absl::Duration d;
1303  } from_tv[] = {
1304  {{1, 1}, absl::Seconds(1) + absl::Microseconds(1)},
1305  {{1, 0}, absl::Seconds(1) + absl::Microseconds(0)},
1306  {{0, 0}, absl::Seconds(0) + absl::Microseconds(0)},
1307  {{0, -1}, absl::Seconds(0) - absl::Microseconds(1)},
1308  {{-1, 999999}, absl::Seconds(0) - absl::Microseconds(1)},
1309  {{-1, 1}, absl::Seconds(-1) + absl::Microseconds(1)},
1310  {{-1, 0}, absl::Seconds(-1) + absl::Microseconds(0)},
1311  {{-1, -1}, absl::Seconds(-1) - absl::Microseconds(1)},
1312  {{-2, 999999}, absl::Seconds(-1) - absl::Microseconds(1)},
1313  };
1314  for (const auto& test : from_tv) {
1315  EXPECT_EQ(test.d, absl::DurationFromTimeval(test.tv));
1316  }
1317 }
1318 
1319 TEST(Duration, SmallConversions) {
1320  // Special tests for conversions of small durations.
1321 
1322  EXPECT_EQ(absl::ZeroDuration(), absl::Seconds(0));
1323  // TODO(bww): Is the next one OK?
1324  EXPECT_EQ(absl::ZeroDuration(), absl::Seconds(0.124999999e-9));
1325  EXPECT_EQ(absl::Nanoseconds(1) / 4, absl::Seconds(0.125e-9));
1326  EXPECT_EQ(absl::Nanoseconds(1) / 4, absl::Seconds(0.250e-9));
1327  EXPECT_EQ(absl::Nanoseconds(1) / 2, absl::Seconds(0.375e-9));
1328  EXPECT_EQ(absl::Nanoseconds(1) / 2, absl::Seconds(0.500e-9));
1329  EXPECT_EQ(absl::Nanoseconds(3) / 4, absl::Seconds(0.625e-9));
1330  EXPECT_EQ(absl::Nanoseconds(3) / 4, absl::Seconds(0.750e-9));
1331  EXPECT_EQ(absl::Nanoseconds(1), absl::Seconds(0.875e-9));
1332  EXPECT_EQ(absl::Nanoseconds(1), absl::Seconds(1.000e-9));
1333 
1334  EXPECT_EQ(absl::ZeroDuration(), absl::Seconds(-0.124999999e-9));
1335  EXPECT_EQ(-absl::Nanoseconds(1) / 4, absl::Seconds(-0.125e-9));
1336  EXPECT_EQ(-absl::Nanoseconds(1) / 4, absl::Seconds(-0.250e-9));
1337  EXPECT_EQ(-absl::Nanoseconds(1) / 2, absl::Seconds(-0.375e-9));
1338  EXPECT_EQ(-absl::Nanoseconds(1) / 2, absl::Seconds(-0.500e-9));
1339  EXPECT_EQ(-absl::Nanoseconds(3) / 4, absl::Seconds(-0.625e-9));
1340  EXPECT_EQ(-absl::Nanoseconds(3) / 4, absl::Seconds(-0.750e-9));
1341  EXPECT_EQ(-absl::Nanoseconds(1), absl::Seconds(-0.875e-9));
1342  EXPECT_EQ(-absl::Nanoseconds(1), absl::Seconds(-1.000e-9));
1343 
1344  timespec ts;
1345  ts.tv_sec = 0;
1346  ts.tv_nsec = 0;
1347  EXPECT_THAT(ToTimespec(absl::Nanoseconds(0)), TimespecMatcher(ts));
1348  // TODO(bww): Are the next three OK?
1349  EXPECT_THAT(ToTimespec(absl::Nanoseconds(1) / 4), TimespecMatcher(ts));
1350  EXPECT_THAT(ToTimespec(absl::Nanoseconds(2) / 4), TimespecMatcher(ts));
1351  EXPECT_THAT(ToTimespec(absl::Nanoseconds(3) / 4), TimespecMatcher(ts));
1352  ts.tv_nsec = 1;
1353  EXPECT_THAT(ToTimespec(absl::Nanoseconds(4) / 4), TimespecMatcher(ts));
1354  EXPECT_THAT(ToTimespec(absl::Nanoseconds(5) / 4), TimespecMatcher(ts));
1355  EXPECT_THAT(ToTimespec(absl::Nanoseconds(6) / 4), TimespecMatcher(ts));
1356  EXPECT_THAT(ToTimespec(absl::Nanoseconds(7) / 4), TimespecMatcher(ts));
1357  ts.tv_nsec = 2;
1358  EXPECT_THAT(ToTimespec(absl::Nanoseconds(8) / 4), TimespecMatcher(ts));
1359 
1360  timeval tv;
1361  tv.tv_sec = 0;
1362  tv.tv_usec = 0;
1363  EXPECT_THAT(ToTimeval(absl::Nanoseconds(0)), TimevalMatcher(tv));
1364  // TODO(bww): Is the next one OK?
1365  EXPECT_THAT(ToTimeval(absl::Nanoseconds(999)), TimevalMatcher(tv));
1366  tv.tv_usec = 1;
1367  EXPECT_THAT(ToTimeval(absl::Nanoseconds(1000)), TimevalMatcher(tv));
1368  EXPECT_THAT(ToTimeval(absl::Nanoseconds(1999)), TimevalMatcher(tv));
1369  tv.tv_usec = 2;
1370  EXPECT_THAT(ToTimeval(absl::Nanoseconds(2000)), TimevalMatcher(tv));
1371 }
1372 
1373 void VerifySameAsMul(double time_as_seconds, int* const misses) {
1374  auto direct_seconds = absl::Seconds(time_as_seconds);
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)
1381  << time_as_seconds;
1382  }
1383 }
1384 
1385 // For a variety of interesting durations, we find the exact point
1386 // where one double converts to that duration, and the very next double
1387 // converts to the next duration. For both of those points, verify that
1388 // Seconds(point) returns the same duration as point * Seconds(1.0)
1389 TEST(Duration, ToDoubleSecondsCheckEdgeCases) {
1391  constexpr auto duration_tick = absl::time_internal::MakeDuration(0, 1u);
1392  int misses = 0;
1393  for (int64_t seconds = 0; seconds < 99; ++seconds) {
1394  uint32_t tick_vals[] = {0, +999, +999999, +999999999, kTicksPerSecond - 1,
1395  0, 1000, 1000000, 1000000000, kTicksPerSecond,
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,
1400  5, 6, 7, 8, 9};
1401  for (uint32_t ticks : tick_vals) {
1402  absl::Duration s_plus_t = absl::Seconds(seconds) + ticks * duration_tick;
1403  for (absl::Duration d : {s_plus_t, -s_plus_t}) {
1404  absl::Duration after_d = d + duration_tick;
1405  EXPECT_NE(d, after_d);
1406  EXPECT_EQ(after_d - d, duration_tick);
1407 
1408  double low_edge = ToDoubleSeconds(d);
1409  EXPECT_EQ(d, absl::Seconds(low_edge));
1410 
1411  double high_edge = ToDoubleSeconds(after_d);
1412  EXPECT_EQ(after_d, absl::Seconds(high_edge));
1413 
1414  for (;;) {
1415  double midpoint = low_edge + (high_edge - low_edge) / 2;
1416  if (midpoint == low_edge || midpoint == high_edge) break;
1417  absl::Duration mid_duration = absl::Seconds(midpoint);
1418  if (mid_duration == d) {
1419  low_edge = midpoint;
1420  } else {
1421  EXPECT_EQ(mid_duration, after_d);
1422  high_edge = midpoint;
1423  }
1424  }
1425  // Now low_edge is the highest double that converts to Duration d,
1426  // and high_edge is the lowest double that converts to Duration after_d.
1427  VerifySameAsMul(low_edge, &misses);
1428  VerifySameAsMul(high_edge, &misses);
1429  }
1430  }
1431  }
1432 }
1433 
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);
1438  // We want doubles distributed from 1/8ns up to 2^63, where
1439  // as many values are tested from 1ns to 2ns as from 1sec to 2sec,
1440  // so even distribute along a log-scale of those values, and
1441  // exponentiate before using them. (9.223377e+18 is just slightly
1442  // out of bounds for absl::Duration.)
1443  std::uniform_real_distribution<double> uniform(std::log(0.125e-9),
1444  std::log(9.223377e+18));
1445  int misses = 0;
1446  for (int i = 0; i < 1000000; ++i) {
1447  double d = std::exp(uniform(gen));
1448  VerifySameAsMul(d, &misses);
1449  VerifySameAsMul(-d, &misses);
1450  }
1451 }
1452 
1453 TEST(Duration, ConversionSaturation) {
1454  absl::Duration d;
1455 
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();
1460  timeval tv;
1461  tv.tv_sec = max_timeval_sec;
1462  tv.tv_usec = 999998;
1463  d = absl::DurationFromTimeval(tv);
1464  tv = ToTimeval(d);
1465  EXPECT_EQ(max_timeval_sec, tv.tv_sec);
1466  EXPECT_EQ(999998, tv.tv_usec);
1467  d += absl::Microseconds(1);
1468  tv = ToTimeval(d);
1469  EXPECT_EQ(max_timeval_sec, tv.tv_sec);
1470  EXPECT_EQ(999999, tv.tv_usec);
1471  d += absl::Microseconds(1); // no effect
1472  tv = ToTimeval(d);
1473  EXPECT_EQ(max_timeval_sec, tv.tv_sec);
1474  EXPECT_EQ(999999, tv.tv_usec);
1475 
1476  tv.tv_sec = min_timeval_sec;
1477  tv.tv_usec = 1;
1478  d = absl::DurationFromTimeval(tv);
1479  tv = ToTimeval(d);
1480  EXPECT_EQ(min_timeval_sec, tv.tv_sec);
1481  EXPECT_EQ(1, tv.tv_usec);
1482  d -= absl::Microseconds(1);
1483  tv = ToTimeval(d);
1484  EXPECT_EQ(min_timeval_sec, tv.tv_sec);
1485  EXPECT_EQ(0, tv.tv_usec);
1486  d -= absl::Microseconds(1); // no effect
1487  tv = ToTimeval(d);
1488  EXPECT_EQ(min_timeval_sec, tv.tv_sec);
1489  EXPECT_EQ(0, tv.tv_usec);
1490 
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();
1495  timespec ts;
1496  ts.tv_sec = max_timespec_sec;
1497  ts.tv_nsec = 999999998;
1499  ts = absl::ToTimespec(d);
1500  EXPECT_EQ(max_timespec_sec, ts.tv_sec);
1501  EXPECT_EQ(999999998, ts.tv_nsec);
1502  d += absl::Nanoseconds(1);
1503  ts = absl::ToTimespec(d);
1504  EXPECT_EQ(max_timespec_sec, ts.tv_sec);
1505  EXPECT_EQ(999999999, ts.tv_nsec);
1506  d += absl::Nanoseconds(1); // no effect
1507  ts = absl::ToTimespec(d);
1508  EXPECT_EQ(max_timespec_sec, ts.tv_sec);
1509  EXPECT_EQ(999999999, ts.tv_nsec);
1510 
1511  ts.tv_sec = min_timespec_sec;
1512  ts.tv_nsec = 1;
1514  ts = absl::ToTimespec(d);
1515  EXPECT_EQ(min_timespec_sec, ts.tv_sec);
1516  EXPECT_EQ(1, ts.tv_nsec);
1517  d -= absl::Nanoseconds(1);
1518  ts = absl::ToTimespec(d);
1519  EXPECT_EQ(min_timespec_sec, ts.tv_sec);
1520  EXPECT_EQ(0, ts.tv_nsec);
1521  d -= absl::Nanoseconds(1); // no effect
1522  ts = absl::ToTimespec(d);
1523  EXPECT_EQ(min_timespec_sec, ts.tv_sec);
1524  EXPECT_EQ(0, ts.tv_nsec);
1525 }
1526 
1527 TEST(Duration, FormatDuration) {
1528  // Example from Go's docs.
1529  EXPECT_EQ("72h3m0.5s",
1531  absl::Milliseconds(500)));
1532  // Go's largest time: 2540400h10m10.000000000s
1533  EXPECT_EQ("2540400h10m10s",
1535  absl::Seconds(10)));
1536 
1537  EXPECT_EQ("0", absl::FormatDuration(absl::ZeroDuration()));
1538  EXPECT_EQ("0", absl::FormatDuration(absl::Seconds(0)));
1539  EXPECT_EQ("0", absl::FormatDuration(absl::Nanoseconds(0)));
1540 
1541  EXPECT_EQ("1ns", absl::FormatDuration(absl::Nanoseconds(1)));
1542  EXPECT_EQ("1us", absl::FormatDuration(absl::Microseconds(1)));
1543  EXPECT_EQ("1ms", absl::FormatDuration(absl::Milliseconds(1)));
1544  EXPECT_EQ("1s", absl::FormatDuration(absl::Seconds(1)));
1545  EXPECT_EQ("1m", absl::FormatDuration(absl::Minutes(1)));
1546  EXPECT_EQ("1h", absl::FormatDuration(absl::Hours(1)));
1547 
1548  EXPECT_EQ("1h1m", absl::FormatDuration(absl::Hours(1) + absl::Minutes(1)));
1549  EXPECT_EQ("1h1s", absl::FormatDuration(absl::Hours(1) + absl::Seconds(1)));
1550  EXPECT_EQ("1m1s", absl::FormatDuration(absl::Minutes(1) + absl::Seconds(1)));
1551 
1552  EXPECT_EQ("1h0.25s",
1554  EXPECT_EQ("1m0.25s",
1556  EXPECT_EQ("1h1m0.25s",
1558  absl::Milliseconds(250)));
1559  EXPECT_EQ("1h0.0005s",
1561  EXPECT_EQ("1h0.0000005s",
1563 
1564  // Subsecond special case.
1565  EXPECT_EQ("1.5ns", absl::FormatDuration(absl::Nanoseconds(1) +
1566  absl::Nanoseconds(1) / 2));
1567  EXPECT_EQ("1.25ns", absl::FormatDuration(absl::Nanoseconds(1) +
1568  absl::Nanoseconds(1) / 4));
1569  EXPECT_EQ("1ns", absl::FormatDuration(absl::Nanoseconds(1) +
1570  absl::Nanoseconds(1) / 9));
1571  EXPECT_EQ("1.2us", absl::FormatDuration(absl::Microseconds(1) +
1572  absl::Nanoseconds(200)));
1573  EXPECT_EQ("1.2ms", absl::FormatDuration(absl::Milliseconds(1) +
1574  absl::Microseconds(200)));
1575  EXPECT_EQ("1.0002ms", absl::FormatDuration(absl::Milliseconds(1) +
1576  absl::Nanoseconds(200)));
1577  EXPECT_EQ("1.00001ms", absl::FormatDuration(absl::Milliseconds(1) +
1578  absl::Nanoseconds(10)));
1579  EXPECT_EQ("1.000001ms",
1581 
1582  // Negative durations.
1583  EXPECT_EQ("-1ns", absl::FormatDuration(absl::Nanoseconds(-1)));
1584  EXPECT_EQ("-1us", absl::FormatDuration(absl::Microseconds(-1)));
1585  EXPECT_EQ("-1ms", absl::FormatDuration(absl::Milliseconds(-1)));
1586  EXPECT_EQ("-1s", absl::FormatDuration(absl::Seconds(-1)));
1587  EXPECT_EQ("-1m", absl::FormatDuration(absl::Minutes(-1)));
1588  EXPECT_EQ("-1h", absl::FormatDuration(absl::Hours(-1)));
1589 
1590  EXPECT_EQ("-1h1m",
1592  EXPECT_EQ("-1h1s",
1594  EXPECT_EQ("-1m1s",
1596 
1597  EXPECT_EQ("-1ns", absl::FormatDuration(absl::Nanoseconds(-1)));
1598  EXPECT_EQ("-1.2us", absl::FormatDuration(
1599  -(absl::Microseconds(1) + absl::Nanoseconds(200))));
1600  EXPECT_EQ("-1.2ms", absl::FormatDuration(
1601  -(absl::Milliseconds(1) + absl::Microseconds(200))));
1602  EXPECT_EQ("-1.0002ms", absl::FormatDuration(-(absl::Milliseconds(1) +
1603  absl::Nanoseconds(200))));
1604  EXPECT_EQ("-1.00001ms", absl::FormatDuration(-(absl::Milliseconds(1) +
1605  absl::Nanoseconds(10))));
1606  EXPECT_EQ("-1.000001ms", absl::FormatDuration(-(absl::Milliseconds(1) +
1607  absl::Nanoseconds(1))));
1608 
1609  //
1610  // Interesting corner cases.
1611  //
1612 
1613  const absl::Duration qns = absl::Nanoseconds(1) / 4;
1614  const absl::Duration max_dur =
1615  absl::Seconds(kint64max) + (absl::Seconds(1) - qns);
1616  const absl::Duration min_dur = absl::Seconds(kint64min);
1617 
1618  EXPECT_EQ("0.25ns", absl::FormatDuration(qns));
1619  EXPECT_EQ("-0.25ns", absl::FormatDuration(-qns));
1620  EXPECT_EQ("2562047788015215h30m7.99999999975s",
1621  absl::FormatDuration(max_dur));
1622  EXPECT_EQ("-2562047788015215h30m8s", absl::FormatDuration(min_dur));
1623 
1624  // Tests printing full precision from units that print using FDivDuration
1625  EXPECT_EQ("55.00000000025s", absl::FormatDuration(absl::Seconds(55) + qns));
1626  EXPECT_EQ("55.00000025ms",
1628  EXPECT_EQ("55.00025us", absl::FormatDuration(absl::Microseconds(55) + qns));
1629  EXPECT_EQ("55.25ns", absl::FormatDuration(absl::Nanoseconds(55) + qns));
1630 
1631  // Formatting infinity
1632  EXPECT_EQ("inf", absl::FormatDuration(absl::InfiniteDuration()));
1633  EXPECT_EQ("-inf", absl::FormatDuration(-absl::InfiniteDuration()));
1634 
1635  // Formatting approximately +/- 100 billion years
1636  const absl::Duration huge_range = ApproxYears(100000000000);
1637  EXPECT_EQ("876000000000000h", absl::FormatDuration(huge_range));
1638  EXPECT_EQ("-876000000000000h", absl::FormatDuration(-huge_range));
1639 
1640  EXPECT_EQ("876000000000000h0.999999999s",
1641  absl::FormatDuration(huge_range +
1642  (absl::Seconds(1) - absl::Nanoseconds(1))));
1643  EXPECT_EQ("876000000000000h0.9999999995s",
1645  huge_range + (absl::Seconds(1) - absl::Nanoseconds(1) / 2)));
1646  EXPECT_EQ("876000000000000h0.99999999975s",
1648  huge_range + (absl::Seconds(1) - absl::Nanoseconds(1) / 4)));
1649 
1650  EXPECT_EQ("-876000000000000h0.999999999s",
1651  absl::FormatDuration(-huge_range -
1652  (absl::Seconds(1) - absl::Nanoseconds(1))));
1653  EXPECT_EQ("-876000000000000h0.9999999995s",
1655  -huge_range - (absl::Seconds(1) - absl::Nanoseconds(1) / 2)));
1656  EXPECT_EQ("-876000000000000h0.99999999975s",
1658  -huge_range - (absl::Seconds(1) - absl::Nanoseconds(1) / 4)));
1659 }
1660 
1661 TEST(Duration, ParseDuration) {
1662  absl::Duration d;
1663 
1664  // No specified unit. Should only work for zero and infinity.
1665  EXPECT_TRUE(absl::ParseDuration("0", &d));
1666  EXPECT_EQ(absl::ZeroDuration(), d);
1667  EXPECT_TRUE(absl::ParseDuration("+0", &d));
1668  EXPECT_EQ(absl::ZeroDuration(), d);
1669  EXPECT_TRUE(absl::ParseDuration("-0", &d));
1670  EXPECT_EQ(absl::ZeroDuration(), d);
1671 
1672  EXPECT_TRUE(absl::ParseDuration("inf", &d));
1673  EXPECT_EQ(absl::InfiniteDuration(), d);
1674  EXPECT_TRUE(absl::ParseDuration("+inf", &d));
1675  EXPECT_EQ(absl::InfiniteDuration(), d);
1676  EXPECT_TRUE(absl::ParseDuration("-inf", &d));
1677  EXPECT_EQ(-absl::InfiniteDuration(), d);
1678  EXPECT_FALSE(absl::ParseDuration("infBlah", &d));
1679 
1680  // Illegal input forms.
1681  EXPECT_FALSE(absl::ParseDuration("", &d));
1682  EXPECT_FALSE(absl::ParseDuration("0.0", &d));
1683  EXPECT_FALSE(absl::ParseDuration(".0", &d));
1684  EXPECT_FALSE(absl::ParseDuration(".", &d));
1685  EXPECT_FALSE(absl::ParseDuration("01", &d));
1686  EXPECT_FALSE(absl::ParseDuration("1", &d));
1687  EXPECT_FALSE(absl::ParseDuration("-1", &d));
1688  EXPECT_FALSE(absl::ParseDuration("2", &d));
1689  EXPECT_FALSE(absl::ParseDuration("2 s", &d));
1690  EXPECT_FALSE(absl::ParseDuration(".s", &d));
1691  EXPECT_FALSE(absl::ParseDuration("-.s", &d));
1692  EXPECT_FALSE(absl::ParseDuration("s", &d));
1693  EXPECT_FALSE(absl::ParseDuration(" 2s", &d));
1694  EXPECT_FALSE(absl::ParseDuration("2s ", &d));
1695  EXPECT_FALSE(absl::ParseDuration(" 2s ", &d));
1696  EXPECT_FALSE(absl::ParseDuration("2mt", &d));
1697  EXPECT_FALSE(absl::ParseDuration("1e3s", &d));
1698 
1699  // One unit type.
1700  EXPECT_TRUE(absl::ParseDuration("1ns", &d));
1701  EXPECT_EQ(absl::Nanoseconds(1), d);
1702  EXPECT_TRUE(absl::ParseDuration("1us", &d));
1703  EXPECT_EQ(absl::Microseconds(1), d);
1704  EXPECT_TRUE(absl::ParseDuration("1ms", &d));
1705  EXPECT_EQ(absl::Milliseconds(1), d);
1706  EXPECT_TRUE(absl::ParseDuration("1s", &d));
1707  EXPECT_EQ(absl::Seconds(1), d);
1708  EXPECT_TRUE(absl::ParseDuration("2m", &d));
1709  EXPECT_EQ(absl::Minutes(2), d);
1710  EXPECT_TRUE(absl::ParseDuration("2h", &d));
1711  EXPECT_EQ(absl::Hours(2), d);
1712 
1713  // Huge counts of a unit.
1714  EXPECT_TRUE(absl::ParseDuration("9223372036854775807us", &d));
1715  EXPECT_EQ(absl::Microseconds(9223372036854775807), d);
1716  EXPECT_TRUE(absl::ParseDuration("-9223372036854775807us", &d));
1717  EXPECT_EQ(absl::Microseconds(-9223372036854775807), d);
1718 
1719  // Multiple units.
1720  EXPECT_TRUE(absl::ParseDuration("2h3m4s", &d));
1721  EXPECT_EQ(absl::Hours(2) + absl::Minutes(3) + absl::Seconds(4), d);
1722  EXPECT_TRUE(absl::ParseDuration("3m4s5us", &d));
1723  EXPECT_EQ(absl::Minutes(3) + absl::Seconds(4) + absl::Microseconds(5), d);
1724  EXPECT_TRUE(absl::ParseDuration("2h3m4s5ms6us7ns", &d));
1725  EXPECT_EQ(absl::Hours(2) + absl::Minutes(3) + absl::Seconds(4) +
1727  absl::Nanoseconds(7),
1728  d);
1729 
1730  // Multiple units out of order.
1731  EXPECT_TRUE(absl::ParseDuration("2us3m4s5h", &d));
1732  EXPECT_EQ(absl::Hours(5) + absl::Minutes(3) + absl::Seconds(4) +
1733  absl::Microseconds(2),
1734  d);
1735 
1736  // Fractional values of units.
1737  EXPECT_TRUE(absl::ParseDuration("1.5ns", &d));
1738  EXPECT_EQ(1.5 * absl::Nanoseconds(1), d);
1739  EXPECT_TRUE(absl::ParseDuration("1.5us", &d));
1740  EXPECT_EQ(1.5 * absl::Microseconds(1), d);
1741  EXPECT_TRUE(absl::ParseDuration("1.5ms", &d));
1742  EXPECT_EQ(1.5 * absl::Milliseconds(1), d);
1743  EXPECT_TRUE(absl::ParseDuration("1.5s", &d));
1744  EXPECT_EQ(1.5 * absl::Seconds(1), d);
1745  EXPECT_TRUE(absl::ParseDuration("1.5m", &d));
1746  EXPECT_EQ(1.5 * absl::Minutes(1), d);
1747  EXPECT_TRUE(absl::ParseDuration("1.5h", &d));
1748  EXPECT_EQ(1.5 * absl::Hours(1), d);
1749 
1750  // Huge fractional counts of a unit.
1751  EXPECT_TRUE(absl::ParseDuration("0.4294967295s", &d));
1752  EXPECT_EQ(absl::Nanoseconds(429496729) + absl::Nanoseconds(1) / 2, d);
1753  EXPECT_TRUE(absl::ParseDuration("0.429496729501234567890123456789s", &d));
1754  EXPECT_EQ(absl::Nanoseconds(429496729) + absl::Nanoseconds(1) / 2, d);
1755 
1756  // Negative durations.
1757  EXPECT_TRUE(absl::ParseDuration("-1s", &d));
1758  EXPECT_EQ(absl::Seconds(-1), d);
1759  EXPECT_TRUE(absl::ParseDuration("-1m", &d));
1760  EXPECT_EQ(absl::Minutes(-1), d);
1761  EXPECT_TRUE(absl::ParseDuration("-1h", &d));
1762  EXPECT_EQ(absl::Hours(-1), d);
1763 
1764  EXPECT_TRUE(absl::ParseDuration("-1h2s", &d));
1765  EXPECT_EQ(-(absl::Hours(1) + absl::Seconds(2)), d);
1766  EXPECT_FALSE(absl::ParseDuration("1h-2s", &d));
1767  EXPECT_FALSE(absl::ParseDuration("-1h-2s", &d));
1768  EXPECT_FALSE(absl::ParseDuration("-1h -2s", &d));
1769 }
1770 
1771 TEST(Duration, FormatParseRoundTrip) {
1772 #define TEST_PARSE_ROUNDTRIP(d) \
1773  do { \
1774  std::string s = absl::FormatDuration(d); \
1775  absl::Duration dur; \
1776  EXPECT_TRUE(absl::ParseDuration(s, &dur)); \
1777  EXPECT_EQ(d, dur); \
1778  } while (0)
1779 
1787 
1794 
1798 
1800  absl::Nanoseconds(1) / 4); // 1.25ns
1801 
1802  const absl::Duration huge_range = ApproxYears(100000000000);
1803  TEST_PARSE_ROUNDTRIP(huge_range);
1804  TEST_PARSE_ROUNDTRIP(huge_range + (absl::Seconds(1) - absl::Nanoseconds(1)));
1805 
1806 #undef TEST_PARSE_ROUNDTRIP
1807 }
1808 
1809 } // namespace
timespec ToTimespec(Duration d)
Definition: duration.cc:598
std::chrono::microseconds ToChronoMicroseconds(Duration d)
Definition: duration.cc:658
int64_t ToInt64Minutes(Duration d)
Definition: duration.cc:566
constexpr int64_t kTicksPerSecond
Definition: time.h:99
constexpr Duration Hours(int64_t n)
Definition: time.h:1467
#define TEST_MUL_OPS(UNIT)
#define TEST_MOD_IDENTITY(a, b)
Duration DurationFromTimespec(timespec ts)
Definition: duration.cc:516
int64_t ToInt64Microseconds(Duration d)
Definition: duration.cc:544
double ToDoubleSeconds(Duration d)
Definition: duration.cc:588
std::chrono::minutes ToChronoMinutes(Duration d)
Definition: duration.cc:667
constexpr Duration Microseconds(int64_t n)
Definition: time.h:1455
Duration Floor(const Duration d, const Duration unit)
Definition: duration.cc:502
std::chrono::duration< std::int_fast64_t > seconds
Definition: time_zone.h:37
constexpr Duration Milliseconds(int64_t n)
Definition: time.h:1458
#define TEST_DURATION_CONVERSION(UNIT)
cctz::time_point< cctz::seconds > sec
Definition: format.cc:38
#define TEST_PARSE_ROUNDTRIP(d)
#define TEST_FACTORY_OVERLOADS(NAME)
timeval ToTimeval(Duration d)
Definition: duration.cc:628
int64_t ToInt64Nanoseconds(Duration d)
Definition: duration.cc:536
int64_t ToInt64Seconds(Duration d)
Definition: duration.cc:560
#define TEST_INF_MUL_WITH_TYPE(T)
int64_t ToInt64Hours(Duration d)
Definition: duration.cc:572
constexpr Duration MakeDuration(int64_t hi, uint32_t lo)
Definition: time.h:1313
std::chrono::seconds ToChronoSeconds(Duration d)
Definition: duration.cc:664
#define TEST_NAN_HANDLING(NAME, NAN)
std::chrono::hours ToChronoHours(Duration d)
Definition: duration.cc:670
std::string FormatDuration(Duration d)
Definition: duration.cc:758
int64_t IDivDuration(Duration num, Duration den, Duration *rem)
Definition: time.h:263
constexpr Duration Minutes(int64_t n)
Definition: time.h:1464
bool log
Definition: mutex.cc:293
#define ROUND_TRIP_UNIT(U, LOW, HIGH)
std::chrono::milliseconds ToChronoMilliseconds(Duration d)
Definition: duration.cc:661
void * arg
Definition: mutex.cc:292
int64_t ToInt64Milliseconds(Duration d)
Definition: duration.cc:552
double FDivDuration(Duration num, Duration den)
Definition: duration.cc:476
constexpr Duration Seconds(int64_t n)
Definition: time.h:1461
Duration Trunc(Duration d, Duration unit)
Definition: duration.cc:498
TEST(Symbolize, Unimplemented)
Duration DurationFromTimeval(timeval tv)
Definition: duration.cc:524
constexpr Duration Nanoseconds(int64_t n)
Definition: time.h:1452
uint64_t b
Definition: layout_test.cc:50
#define TEST_ADD_OPS(UNIT)
Duration AbsDuration(Duration d)
Definition: time.h:291
#define TEST_REL_OPS(UNIT)
constexpr Duration InfiniteDuration()
Definition: time.h:1513
constexpr Duration ZeroDuration()
Definition: time.h:286
#define TEST_INF_DIV_WITH_TYPE(T)
Time FromChrono(const std::chrono::system_clock::time_point &tp)
Definition: time.cc:328
Duration Ceil(const Duration d, const Duration unit)
Definition: duration.cc:507
constexpr bool IsInfiniteDuration(Duration d)
Definition: time.h:1347
bool ParseDuration(const std::string &dur_string, Duration *d)
Definition: duration.cc:863
std::chrono::nanoseconds ToChronoNanoseconds(Duration d)
Definition: duration.cc:655


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:19:56