duration_test.cc
Go to the documentation of this file.
00001 // Copyright 2017 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 #include <chrono>  // NOLINT(build/c++11)
00016 #include <cmath>
00017 #include <cstdint>
00018 #include <ctime>
00019 #include <iomanip>
00020 #include <limits>
00021 #include <random>
00022 #include <string>
00023 
00024 #include "gmock/gmock.h"
00025 #include "gtest/gtest.h"
00026 #include "absl/time/time.h"
00027 
00028 namespace {
00029 
00030 constexpr int64_t kint64max = std::numeric_limits<int64_t>::max();
00031 constexpr int64_t kint64min = std::numeric_limits<int64_t>::min();
00032 
00033 // Approximates the given number of years. This is only used to make some test
00034 // code more readable.
00035 absl::Duration ApproxYears(int64_t n) { return absl::Hours(n) * 365 * 24; }
00036 
00037 // A gMock matcher to match timespec values. Use this matcher like:
00038 // timespec ts1, ts2;
00039 // EXPECT_THAT(ts1, TimespecMatcher(ts2));
00040 MATCHER_P(TimespecMatcher, ts, "") {
00041   if (ts.tv_sec == arg.tv_sec && ts.tv_nsec == arg.tv_nsec)
00042     return true;
00043   *result_listener << "expected: {" << ts.tv_sec << ", " << ts.tv_nsec << "} ";
00044   *result_listener << "actual: {" << arg.tv_sec << ", " << arg.tv_nsec << "}";
00045   return false;
00046 }
00047 
00048 // A gMock matcher to match timeval values. Use this matcher like:
00049 // timeval tv1, tv2;
00050 // EXPECT_THAT(tv1, TimevalMatcher(tv2));
00051 MATCHER_P(TimevalMatcher, tv, "") {
00052   if (tv.tv_sec == arg.tv_sec && tv.tv_usec == arg.tv_usec)
00053     return true;
00054   *result_listener << "expected: {" << tv.tv_sec << ", " << tv.tv_usec << "} ";
00055   *result_listener << "actual: {" << arg.tv_sec << ", " << arg.tv_usec << "}";
00056   return false;
00057 }
00058 
00059 TEST(Duration, ConstExpr) {
00060   constexpr absl::Duration d0 = absl::ZeroDuration();
00061   static_assert(d0 == absl::ZeroDuration(), "ZeroDuration()");
00062   constexpr absl::Duration d1 = absl::Seconds(1);
00063   static_assert(d1 == absl::Seconds(1), "Seconds(1)");
00064   static_assert(d1 != absl::ZeroDuration(), "Seconds(1)");
00065   constexpr absl::Duration d2 = absl::InfiniteDuration();
00066   static_assert(d2 == absl::InfiniteDuration(), "InfiniteDuration()");
00067   static_assert(d2 != absl::ZeroDuration(), "InfiniteDuration()");
00068 }
00069 
00070 TEST(Duration, ValueSemantics) {
00071   // If this compiles, the test passes.
00072   constexpr absl::Duration a;      // Default construction
00073   constexpr absl::Duration b = a;  // Copy construction
00074   constexpr absl::Duration c(b);   // Copy construction (again)
00075 
00076   absl::Duration d;
00077   d = c;  // Assignment
00078 }
00079 
00080 TEST(Duration, Factories) {
00081   constexpr absl::Duration zero = absl::ZeroDuration();
00082   constexpr absl::Duration nano = absl::Nanoseconds(1);
00083   constexpr absl::Duration micro = absl::Microseconds(1);
00084   constexpr absl::Duration milli = absl::Milliseconds(1);
00085   constexpr absl::Duration sec = absl::Seconds(1);
00086   constexpr absl::Duration min = absl::Minutes(1);
00087   constexpr absl::Duration hour = absl::Hours(1);
00088 
00089   EXPECT_EQ(zero, absl::Duration());
00090   EXPECT_EQ(zero, absl::Seconds(0));
00091   EXPECT_EQ(nano, absl::Nanoseconds(1));
00092   EXPECT_EQ(micro, absl::Nanoseconds(1000));
00093   EXPECT_EQ(milli, absl::Microseconds(1000));
00094   EXPECT_EQ(sec, absl::Milliseconds(1000));
00095   EXPECT_EQ(min, absl::Seconds(60));
00096   EXPECT_EQ(hour, absl::Minutes(60));
00097 
00098   // Tests factory limits
00099   const absl::Duration inf = absl::InfiniteDuration();
00100 
00101   EXPECT_GT(inf, absl::Seconds(kint64max));
00102   EXPECT_LT(-inf, absl::Seconds(kint64min));
00103   EXPECT_LT(-inf, absl::Seconds(-kint64max));
00104 
00105   EXPECT_EQ(inf, absl::Minutes(kint64max));
00106   EXPECT_EQ(-inf, absl::Minutes(kint64min));
00107   EXPECT_EQ(-inf, absl::Minutes(-kint64max));
00108   EXPECT_GT(inf, absl::Minutes(kint64max / 60));
00109   EXPECT_LT(-inf, absl::Minutes(kint64min / 60));
00110   EXPECT_LT(-inf, absl::Minutes(-kint64max / 60));
00111 
00112   EXPECT_EQ(inf, absl::Hours(kint64max));
00113   EXPECT_EQ(-inf, absl::Hours(kint64min));
00114   EXPECT_EQ(-inf, absl::Hours(-kint64max));
00115   EXPECT_GT(inf, absl::Hours(kint64max / 3600));
00116   EXPECT_LT(-inf, absl::Hours(kint64min / 3600));
00117   EXPECT_LT(-inf, absl::Hours(-kint64max / 3600));
00118 }
00119 
00120 TEST(Duration, ToConversion) {
00121 #define TEST_DURATION_CONVERSION(UNIT)                                  \
00122   do {                                                                  \
00123     const absl::Duration d = absl::UNIT(1.5);                           \
00124     constexpr absl::Duration z = absl::ZeroDuration();                  \
00125     constexpr absl::Duration inf = absl::InfiniteDuration();            \
00126     constexpr double dbl_inf = std::numeric_limits<double>::infinity(); \
00127     EXPECT_EQ(kint64min, absl::ToInt64##UNIT(-inf));                    \
00128     EXPECT_EQ(-1, absl::ToInt64##UNIT(-d));                             \
00129     EXPECT_EQ(0, absl::ToInt64##UNIT(z));                               \
00130     EXPECT_EQ(1, absl::ToInt64##UNIT(d));                               \
00131     EXPECT_EQ(kint64max, absl::ToInt64##UNIT(inf));                     \
00132     EXPECT_EQ(-dbl_inf, absl::ToDouble##UNIT(-inf));                    \
00133     EXPECT_EQ(-1.5, absl::ToDouble##UNIT(-d));                          \
00134     EXPECT_EQ(0, absl::ToDouble##UNIT(z));                              \
00135     EXPECT_EQ(1.5, absl::ToDouble##UNIT(d));                            \
00136     EXPECT_EQ(dbl_inf, absl::ToDouble##UNIT(inf));                      \
00137   } while (0)
00138 
00139   TEST_DURATION_CONVERSION(Nanoseconds);
00140   TEST_DURATION_CONVERSION(Microseconds);
00141   TEST_DURATION_CONVERSION(Milliseconds);
00142   TEST_DURATION_CONVERSION(Seconds);
00143   TEST_DURATION_CONVERSION(Minutes);
00144   TEST_DURATION_CONVERSION(Hours);
00145 
00146 #undef TEST_DURATION_CONVERSION
00147 }
00148 
00149 template <int64_t N>
00150 void TestToConversion() {
00151   constexpr absl::Duration nano = absl::Nanoseconds(N);
00152   EXPECT_EQ(N, absl::ToInt64Nanoseconds(nano));
00153   EXPECT_EQ(0, absl::ToInt64Microseconds(nano));
00154   EXPECT_EQ(0, absl::ToInt64Milliseconds(nano));
00155   EXPECT_EQ(0, absl::ToInt64Seconds(nano));
00156   EXPECT_EQ(0, absl::ToInt64Minutes(nano));
00157   EXPECT_EQ(0, absl::ToInt64Hours(nano));
00158   const absl::Duration micro = absl::Microseconds(N);
00159   EXPECT_EQ(N * 1000, absl::ToInt64Nanoseconds(micro));
00160   EXPECT_EQ(N, absl::ToInt64Microseconds(micro));
00161   EXPECT_EQ(0, absl::ToInt64Milliseconds(micro));
00162   EXPECT_EQ(0, absl::ToInt64Seconds(micro));
00163   EXPECT_EQ(0, absl::ToInt64Minutes(micro));
00164   EXPECT_EQ(0, absl::ToInt64Hours(micro));
00165   const absl::Duration milli = absl::Milliseconds(N);
00166   EXPECT_EQ(N * 1000 * 1000, absl::ToInt64Nanoseconds(milli));
00167   EXPECT_EQ(N * 1000, absl::ToInt64Microseconds(milli));
00168   EXPECT_EQ(N, absl::ToInt64Milliseconds(milli));
00169   EXPECT_EQ(0, absl::ToInt64Seconds(milli));
00170   EXPECT_EQ(0, absl::ToInt64Minutes(milli));
00171   EXPECT_EQ(0, absl::ToInt64Hours(milli));
00172   const absl::Duration sec = absl::Seconds(N);
00173   EXPECT_EQ(N * 1000 * 1000 * 1000, absl::ToInt64Nanoseconds(sec));
00174   EXPECT_EQ(N * 1000 * 1000, absl::ToInt64Microseconds(sec));
00175   EXPECT_EQ(N * 1000, absl::ToInt64Milliseconds(sec));
00176   EXPECT_EQ(N, absl::ToInt64Seconds(sec));
00177   EXPECT_EQ(0, absl::ToInt64Minutes(sec));
00178   EXPECT_EQ(0, absl::ToInt64Hours(sec));
00179   const absl::Duration min = absl::Minutes(N);
00180   EXPECT_EQ(N * 60 * 1000 * 1000 * 1000, absl::ToInt64Nanoseconds(min));
00181   EXPECT_EQ(N * 60 * 1000 * 1000, absl::ToInt64Microseconds(min));
00182   EXPECT_EQ(N * 60 * 1000, absl::ToInt64Milliseconds(min));
00183   EXPECT_EQ(N * 60, absl::ToInt64Seconds(min));
00184   EXPECT_EQ(N, absl::ToInt64Minutes(min));
00185   EXPECT_EQ(0, absl::ToInt64Hours(min));
00186   const absl::Duration hour = absl::Hours(N);
00187   EXPECT_EQ(N * 60 * 60 * 1000 * 1000 * 1000, absl::ToInt64Nanoseconds(hour));
00188   EXPECT_EQ(N * 60 * 60 * 1000 * 1000, absl::ToInt64Microseconds(hour));
00189   EXPECT_EQ(N * 60 * 60 * 1000, absl::ToInt64Milliseconds(hour));
00190   EXPECT_EQ(N * 60 * 60, absl::ToInt64Seconds(hour));
00191   EXPECT_EQ(N * 60, absl::ToInt64Minutes(hour));
00192   EXPECT_EQ(N, absl::ToInt64Hours(hour));
00193 }
00194 
00195 TEST(Duration, ToConversionDeprecated) {
00196   TestToConversion<43>();
00197   TestToConversion<1>();
00198   TestToConversion<0>();
00199   TestToConversion<-1>();
00200   TestToConversion<-43>();
00201 }
00202 
00203 template <int64_t N>
00204 void TestFromChronoBasicEquality() {
00205   using std::chrono::nanoseconds;
00206   using std::chrono::microseconds;
00207   using std::chrono::milliseconds;
00208   using std::chrono::seconds;
00209   using std::chrono::minutes;
00210   using std::chrono::hours;
00211 
00212   static_assert(absl::Nanoseconds(N) == absl::FromChrono(nanoseconds(N)), "");
00213   static_assert(absl::Microseconds(N) == absl::FromChrono(microseconds(N)), "");
00214   static_assert(absl::Milliseconds(N) == absl::FromChrono(milliseconds(N)), "");
00215   static_assert(absl::Seconds(N) == absl::FromChrono(seconds(N)), "");
00216   static_assert(absl::Minutes(N) == absl::FromChrono(minutes(N)), "");
00217   static_assert(absl::Hours(N) == absl::FromChrono(hours(N)), "");
00218 }
00219 
00220 TEST(Duration, FromChrono) {
00221   TestFromChronoBasicEquality<-123>();
00222   TestFromChronoBasicEquality<-1>();
00223   TestFromChronoBasicEquality<0>();
00224   TestFromChronoBasicEquality<1>();
00225   TestFromChronoBasicEquality<123>();
00226 
00227   // Minutes (might, depending on the platform) saturate at +inf.
00228   const auto chrono_minutes_max = std::chrono::minutes::max();
00229   const auto minutes_max = absl::FromChrono(chrono_minutes_max);
00230   const int64_t minutes_max_count = chrono_minutes_max.count();
00231   if (minutes_max_count > kint64max / 60) {
00232     EXPECT_EQ(absl::InfiniteDuration(), minutes_max);
00233   } else {
00234     EXPECT_EQ(absl::Minutes(minutes_max_count), minutes_max);
00235   }
00236 
00237   // Minutes (might, depending on the platform) saturate at -inf.
00238   const auto chrono_minutes_min = std::chrono::minutes::min();
00239   const auto minutes_min = absl::FromChrono(chrono_minutes_min);
00240   const int64_t minutes_min_count = chrono_minutes_min.count();
00241   if (minutes_min_count < kint64min / 60) {
00242     EXPECT_EQ(-absl::InfiniteDuration(), minutes_min);
00243   } else {
00244     EXPECT_EQ(absl::Minutes(minutes_min_count), minutes_min);
00245   }
00246 
00247   // Hours (might, depending on the platform) saturate at +inf.
00248   const auto chrono_hours_max = std::chrono::hours::max();
00249   const auto hours_max = absl::FromChrono(chrono_hours_max);
00250   const int64_t hours_max_count = chrono_hours_max.count();
00251   if (hours_max_count > kint64max / 3600) {
00252     EXPECT_EQ(absl::InfiniteDuration(), hours_max);
00253   } else {
00254     EXPECT_EQ(absl::Hours(hours_max_count), hours_max);
00255   }
00256 
00257   // Hours (might, depending on the platform) saturate at -inf.
00258   const auto chrono_hours_min = std::chrono::hours::min();
00259   const auto hours_min = absl::FromChrono(chrono_hours_min);
00260   const int64_t hours_min_count = chrono_hours_min.count();
00261   if (hours_min_count < kint64min / 3600) {
00262     EXPECT_EQ(-absl::InfiniteDuration(), hours_min);
00263   } else {
00264     EXPECT_EQ(absl::Hours(hours_min_count), hours_min);
00265   }
00266 }
00267 
00268 template <int64_t N>
00269 void TestToChrono() {
00270   using std::chrono::nanoseconds;
00271   using std::chrono::microseconds;
00272   using std::chrono::milliseconds;
00273   using std::chrono::seconds;
00274   using std::chrono::minutes;
00275   using std::chrono::hours;
00276 
00277   EXPECT_EQ(nanoseconds(N), absl::ToChronoNanoseconds(absl::Nanoseconds(N)));
00278   EXPECT_EQ(microseconds(N), absl::ToChronoMicroseconds(absl::Microseconds(N)));
00279   EXPECT_EQ(milliseconds(N), absl::ToChronoMilliseconds(absl::Milliseconds(N)));
00280   EXPECT_EQ(seconds(N), absl::ToChronoSeconds(absl::Seconds(N)));
00281 
00282   constexpr auto absl_minutes = absl::Minutes(N);
00283   auto chrono_minutes = minutes(N);
00284   if (absl_minutes == -absl::InfiniteDuration()) {
00285     chrono_minutes = minutes::min();
00286   } else if (absl_minutes == absl::InfiniteDuration()) {
00287     chrono_minutes = minutes::max();
00288   }
00289   EXPECT_EQ(chrono_minutes, absl::ToChronoMinutes(absl_minutes));
00290 
00291   constexpr auto absl_hours = absl::Hours(N);
00292   auto chrono_hours = hours(N);
00293   if (absl_hours == -absl::InfiniteDuration()) {
00294     chrono_hours = hours::min();
00295   } else if (absl_hours == absl::InfiniteDuration()) {
00296     chrono_hours = hours::max();
00297   }
00298   EXPECT_EQ(chrono_hours, absl::ToChronoHours(absl_hours));
00299 }
00300 
00301 TEST(Duration, ToChrono) {
00302   using std::chrono::nanoseconds;
00303   using std::chrono::microseconds;
00304   using std::chrono::milliseconds;
00305   using std::chrono::seconds;
00306   using std::chrono::minutes;
00307   using std::chrono::hours;
00308 
00309   TestToChrono<kint64min>();
00310   TestToChrono<-1>();
00311   TestToChrono<0>();
00312   TestToChrono<1>();
00313   TestToChrono<kint64max>();
00314 
00315   // Verify truncation toward zero.
00316   const auto tick = absl::Nanoseconds(1) / 4;
00317   EXPECT_EQ(nanoseconds(0), absl::ToChronoNanoseconds(tick));
00318   EXPECT_EQ(nanoseconds(0), absl::ToChronoNanoseconds(-tick));
00319   EXPECT_EQ(microseconds(0), absl::ToChronoMicroseconds(tick));
00320   EXPECT_EQ(microseconds(0), absl::ToChronoMicroseconds(-tick));
00321   EXPECT_EQ(milliseconds(0), absl::ToChronoMilliseconds(tick));
00322   EXPECT_EQ(milliseconds(0), absl::ToChronoMilliseconds(-tick));
00323   EXPECT_EQ(seconds(0), absl::ToChronoSeconds(tick));
00324   EXPECT_EQ(seconds(0), absl::ToChronoSeconds(-tick));
00325   EXPECT_EQ(minutes(0), absl::ToChronoMinutes(tick));
00326   EXPECT_EQ(minutes(0), absl::ToChronoMinutes(-tick));
00327   EXPECT_EQ(hours(0), absl::ToChronoHours(tick));
00328   EXPECT_EQ(hours(0), absl::ToChronoHours(-tick));
00329 
00330   // Verifies +/- infinity saturation at max/min.
00331   constexpr auto inf = absl::InfiniteDuration();
00332   EXPECT_EQ(nanoseconds::min(), absl::ToChronoNanoseconds(-inf));
00333   EXPECT_EQ(nanoseconds::max(), absl::ToChronoNanoseconds(inf));
00334   EXPECT_EQ(microseconds::min(), absl::ToChronoMicroseconds(-inf));
00335   EXPECT_EQ(microseconds::max(), absl::ToChronoMicroseconds(inf));
00336   EXPECT_EQ(milliseconds::min(), absl::ToChronoMilliseconds(-inf));
00337   EXPECT_EQ(milliseconds::max(), absl::ToChronoMilliseconds(inf));
00338   EXPECT_EQ(seconds::min(), absl::ToChronoSeconds(-inf));
00339   EXPECT_EQ(seconds::max(), absl::ToChronoSeconds(inf));
00340   EXPECT_EQ(minutes::min(), absl::ToChronoMinutes(-inf));
00341   EXPECT_EQ(minutes::max(), absl::ToChronoMinutes(inf));
00342   EXPECT_EQ(hours::min(), absl::ToChronoHours(-inf));
00343   EXPECT_EQ(hours::max(), absl::ToChronoHours(inf));
00344 }
00345 
00346 TEST(Duration, FactoryOverloads) {
00347   enum E { kOne = 1 };
00348 #define TEST_FACTORY_OVERLOADS(NAME)                                          \
00349   EXPECT_EQ(1, NAME(kOne) / NAME(kOne));                                      \
00350   EXPECT_EQ(1, NAME(static_cast<int8_t>(1)) / NAME(1));                       \
00351   EXPECT_EQ(1, NAME(static_cast<int16_t>(1)) / NAME(1));                      \
00352   EXPECT_EQ(1, NAME(static_cast<int32_t>(1)) / NAME(1));                      \
00353   EXPECT_EQ(1, NAME(static_cast<int64_t>(1)) / NAME(1));                      \
00354   EXPECT_EQ(1, NAME(static_cast<uint8_t>(1)) / NAME(1));                      \
00355   EXPECT_EQ(1, NAME(static_cast<uint16_t>(1)) / NAME(1));                     \
00356   EXPECT_EQ(1, NAME(static_cast<uint32_t>(1)) / NAME(1));                     \
00357   EXPECT_EQ(1, NAME(static_cast<uint64_t>(1)) / NAME(1));                     \
00358   EXPECT_EQ(NAME(1) / 2, NAME(static_cast<float>(0.5)));                      \
00359   EXPECT_EQ(NAME(1) / 2, NAME(static_cast<double>(0.5)));                     \
00360   EXPECT_EQ(1.5, absl::FDivDuration(NAME(static_cast<float>(1.5)), NAME(1))); \
00361   EXPECT_EQ(1.5, absl::FDivDuration(NAME(static_cast<double>(1.5)), NAME(1)));
00362 
00363   TEST_FACTORY_OVERLOADS(absl::Nanoseconds);
00364   TEST_FACTORY_OVERLOADS(absl::Microseconds);
00365   TEST_FACTORY_OVERLOADS(absl::Milliseconds);
00366   TEST_FACTORY_OVERLOADS(absl::Seconds);
00367   TEST_FACTORY_OVERLOADS(absl::Minutes);
00368   TEST_FACTORY_OVERLOADS(absl::Hours);
00369 
00370 #undef TEST_FACTORY_OVERLOADS
00371 
00372   EXPECT_EQ(absl::Milliseconds(1500), absl::Seconds(1.5));
00373   EXPECT_LT(absl::Nanoseconds(1), absl::Nanoseconds(1.5));
00374   EXPECT_GT(absl::Nanoseconds(2), absl::Nanoseconds(1.5));
00375 
00376   const double dbl_inf = std::numeric_limits<double>::infinity();
00377   EXPECT_EQ(absl::InfiniteDuration(), absl::Nanoseconds(dbl_inf));
00378   EXPECT_EQ(absl::InfiniteDuration(), absl::Microseconds(dbl_inf));
00379   EXPECT_EQ(absl::InfiniteDuration(), absl::Milliseconds(dbl_inf));
00380   EXPECT_EQ(absl::InfiniteDuration(), absl::Seconds(dbl_inf));
00381   EXPECT_EQ(absl::InfiniteDuration(), absl::Minutes(dbl_inf));
00382   EXPECT_EQ(absl::InfiniteDuration(), absl::Hours(dbl_inf));
00383   EXPECT_EQ(-absl::InfiniteDuration(), absl::Nanoseconds(-dbl_inf));
00384   EXPECT_EQ(-absl::InfiniteDuration(), absl::Microseconds(-dbl_inf));
00385   EXPECT_EQ(-absl::InfiniteDuration(), absl::Milliseconds(-dbl_inf));
00386   EXPECT_EQ(-absl::InfiniteDuration(), absl::Seconds(-dbl_inf));
00387   EXPECT_EQ(-absl::InfiniteDuration(), absl::Minutes(-dbl_inf));
00388   EXPECT_EQ(-absl::InfiniteDuration(), absl::Hours(-dbl_inf));
00389 }
00390 
00391 TEST(Duration, InfinityExamples) {
00392   // These examples are used in the documentation in time.h. They are
00393   // written so that they can be copy-n-pasted easily.
00394 
00395   constexpr absl::Duration inf = absl::InfiniteDuration();
00396   constexpr absl::Duration d = absl::Seconds(1);  // Any finite duration
00397 
00398   EXPECT_TRUE(inf == inf + inf);
00399   EXPECT_TRUE(inf == inf + d);
00400   EXPECT_TRUE(inf == inf - inf);
00401   EXPECT_TRUE(-inf == d - inf);
00402 
00403   EXPECT_TRUE(inf == d * 1e100);
00404   EXPECT_TRUE(0 == d / inf);  // NOLINT(readability/check)
00405 
00406   // Division by zero returns infinity, or kint64min/MAX where necessary.
00407   EXPECT_TRUE(inf == d / 0);
00408   EXPECT_TRUE(kint64max == d / absl::ZeroDuration());
00409 }
00410 
00411 TEST(Duration, InfinityComparison) {
00412   const absl::Duration inf = absl::InfiniteDuration();
00413   const absl::Duration any_dur = absl::Seconds(1);
00414 
00415   // Equality
00416   EXPECT_EQ(inf, inf);
00417   EXPECT_EQ(-inf, -inf);
00418   EXPECT_NE(inf, -inf);
00419   EXPECT_NE(any_dur, inf);
00420   EXPECT_NE(any_dur, -inf);
00421 
00422   // Relational
00423   EXPECT_GT(inf, any_dur);
00424   EXPECT_LT(-inf, any_dur);
00425   EXPECT_LT(-inf, inf);
00426   EXPECT_GT(inf, -inf);
00427 }
00428 
00429 TEST(Duration, InfinityAddition) {
00430   const absl::Duration sec_max = absl::Seconds(kint64max);
00431   const absl::Duration sec_min = absl::Seconds(kint64min);
00432   const absl::Duration any_dur = absl::Seconds(1);
00433   const absl::Duration inf = absl::InfiniteDuration();
00434 
00435   // Addition
00436   EXPECT_EQ(inf, inf + inf);
00437   EXPECT_EQ(inf, inf + -inf);
00438   EXPECT_EQ(-inf, -inf + inf);
00439   EXPECT_EQ(-inf, -inf + -inf);
00440 
00441   EXPECT_EQ(inf, inf + any_dur);
00442   EXPECT_EQ(inf, any_dur + inf);
00443   EXPECT_EQ(-inf, -inf + any_dur);
00444   EXPECT_EQ(-inf, any_dur + -inf);
00445 
00446   // Interesting case
00447   absl::Duration almost_inf = sec_max + absl::Nanoseconds(999999999);
00448   EXPECT_GT(inf, almost_inf);
00449   almost_inf += -absl::Nanoseconds(999999999);
00450   EXPECT_GT(inf, almost_inf);
00451 
00452   // Addition overflow/underflow
00453   EXPECT_EQ(inf, sec_max + absl::Seconds(1));
00454   EXPECT_EQ(inf, sec_max + sec_max);
00455   EXPECT_EQ(-inf, sec_min + -absl::Seconds(1));
00456   EXPECT_EQ(-inf, sec_min + -sec_max);
00457 
00458   // For reference: IEEE 754 behavior
00459   const double dbl_inf = std::numeric_limits<double>::infinity();
00460   EXPECT_TRUE(std::isinf(dbl_inf + dbl_inf));
00461   EXPECT_TRUE(std::isnan(dbl_inf + -dbl_inf));  // We return inf
00462   EXPECT_TRUE(std::isnan(-dbl_inf + dbl_inf));  // We return inf
00463   EXPECT_TRUE(std::isinf(-dbl_inf + -dbl_inf));
00464 }
00465 
00466 TEST(Duration, InfinitySubtraction) {
00467   const absl::Duration sec_max = absl::Seconds(kint64max);
00468   const absl::Duration sec_min = absl::Seconds(kint64min);
00469   const absl::Duration any_dur = absl::Seconds(1);
00470   const absl::Duration inf = absl::InfiniteDuration();
00471 
00472   // Subtraction
00473   EXPECT_EQ(inf, inf - inf);
00474   EXPECT_EQ(inf, inf - -inf);
00475   EXPECT_EQ(-inf, -inf - inf);
00476   EXPECT_EQ(-inf, -inf - -inf);
00477 
00478   EXPECT_EQ(inf, inf - any_dur);
00479   EXPECT_EQ(-inf, any_dur - inf);
00480   EXPECT_EQ(-inf, -inf - any_dur);
00481   EXPECT_EQ(inf, any_dur - -inf);
00482 
00483   // Subtraction overflow/underflow
00484   EXPECT_EQ(inf, sec_max - -absl::Seconds(1));
00485   EXPECT_EQ(inf, sec_max - -sec_max);
00486   EXPECT_EQ(-inf, sec_min - absl::Seconds(1));
00487   EXPECT_EQ(-inf, sec_min - sec_max);
00488 
00489   // Interesting case
00490   absl::Duration almost_neg_inf = sec_min;
00491   EXPECT_LT(-inf, almost_neg_inf);
00492   almost_neg_inf -= -absl::Nanoseconds(1);
00493   EXPECT_LT(-inf, almost_neg_inf);
00494 
00495   // For reference: IEEE 754 behavior
00496   const double dbl_inf = std::numeric_limits<double>::infinity();
00497   EXPECT_TRUE(std::isnan(dbl_inf - dbl_inf));  // We return inf
00498   EXPECT_TRUE(std::isinf(dbl_inf - -dbl_inf));
00499   EXPECT_TRUE(std::isinf(-dbl_inf - dbl_inf));
00500   EXPECT_TRUE(std::isnan(-dbl_inf - -dbl_inf));  // We return inf
00501 }
00502 
00503 TEST(Duration, InfinityMultiplication) {
00504   const absl::Duration sec_max = absl::Seconds(kint64max);
00505   const absl::Duration sec_min = absl::Seconds(kint64min);
00506   const absl::Duration inf = absl::InfiniteDuration();
00507 
00508 #define TEST_INF_MUL_WITH_TYPE(T)                                     \
00509   EXPECT_EQ(inf, inf * static_cast<T>(2));                            \
00510   EXPECT_EQ(-inf, inf * static_cast<T>(-2));                          \
00511   EXPECT_EQ(-inf, -inf * static_cast<T>(2));                          \
00512   EXPECT_EQ(inf, -inf * static_cast<T>(-2));                          \
00513   EXPECT_EQ(inf, inf * static_cast<T>(0));                            \
00514   EXPECT_EQ(-inf, -inf * static_cast<T>(0));                          \
00515   EXPECT_EQ(inf, sec_max * static_cast<T>(2));                        \
00516   EXPECT_EQ(inf, sec_min * static_cast<T>(-2));                       \
00517   EXPECT_EQ(inf, (sec_max / static_cast<T>(2)) * static_cast<T>(3));  \
00518   EXPECT_EQ(-inf, sec_max * static_cast<T>(-2));                      \
00519   EXPECT_EQ(-inf, sec_min * static_cast<T>(2));                       \
00520   EXPECT_EQ(-inf, (sec_min / static_cast<T>(2)) * static_cast<T>(3));
00521 
00522   TEST_INF_MUL_WITH_TYPE(int64_t);  // NOLINT(readability/function)
00523   TEST_INF_MUL_WITH_TYPE(double);   // NOLINT(readability/function)
00524 
00525 #undef TEST_INF_MUL_WITH_TYPE
00526 
00527   const double dbl_inf = std::numeric_limits<double>::infinity();
00528   EXPECT_EQ(inf, inf * dbl_inf);
00529   EXPECT_EQ(-inf, -inf * dbl_inf);
00530   EXPECT_EQ(-inf, inf * -dbl_inf);
00531   EXPECT_EQ(inf, -inf * -dbl_inf);
00532 
00533   const absl::Duration any_dur = absl::Seconds(1);
00534   EXPECT_EQ(inf, any_dur * dbl_inf);
00535   EXPECT_EQ(-inf, -any_dur * dbl_inf);
00536   EXPECT_EQ(-inf, any_dur * -dbl_inf);
00537   EXPECT_EQ(inf, -any_dur * -dbl_inf);
00538 
00539   // Fixed-point multiplication will produce a finite value, whereas floating
00540   // point fuzziness will overflow to inf.
00541   EXPECT_NE(absl::InfiniteDuration(), absl::Seconds(1) * kint64max);
00542   EXPECT_EQ(inf, absl::Seconds(1) * static_cast<double>(kint64max));
00543   EXPECT_NE(-absl::InfiniteDuration(), absl::Seconds(1) * kint64min);
00544   EXPECT_EQ(-inf, absl::Seconds(1) * static_cast<double>(kint64min));
00545 
00546   // Note that sec_max * or / by 1.0 overflows to inf due to the 53-bit
00547   // limitations of double.
00548   EXPECT_NE(inf, sec_max);
00549   EXPECT_NE(inf, sec_max / 1);
00550   EXPECT_EQ(inf, sec_max / 1.0);
00551   EXPECT_NE(inf, sec_max * 1);
00552   EXPECT_EQ(inf, sec_max * 1.0);
00553 }
00554 
00555 TEST(Duration, InfinityDivision) {
00556   const absl::Duration sec_max = absl::Seconds(kint64max);
00557   const absl::Duration sec_min = absl::Seconds(kint64min);
00558   const absl::Duration inf = absl::InfiniteDuration();
00559 
00560   // Division of Duration by a double
00561 #define TEST_INF_DIV_WITH_TYPE(T)            \
00562   EXPECT_EQ(inf, inf / static_cast<T>(2));   \
00563   EXPECT_EQ(-inf, inf / static_cast<T>(-2)); \
00564   EXPECT_EQ(-inf, -inf / static_cast<T>(2)); \
00565   EXPECT_EQ(inf, -inf / static_cast<T>(-2));
00566 
00567   TEST_INF_DIV_WITH_TYPE(int64_t);  // NOLINT(readability/function)
00568   TEST_INF_DIV_WITH_TYPE(double);   // NOLINT(readability/function)
00569 
00570 #undef TEST_INF_DIV_WITH_TYPE
00571 
00572   // Division of Duration by a double overflow/underflow
00573   EXPECT_EQ(inf, sec_max / 0.5);
00574   EXPECT_EQ(inf, sec_min / -0.5);
00575   EXPECT_EQ(inf, ((sec_max / 0.5) + absl::Seconds(1)) / 0.5);
00576   EXPECT_EQ(-inf, sec_max / -0.5);
00577   EXPECT_EQ(-inf, sec_min / 0.5);
00578   EXPECT_EQ(-inf, ((sec_min / 0.5) - absl::Seconds(1)) / 0.5);
00579 
00580   const double dbl_inf = std::numeric_limits<double>::infinity();
00581   EXPECT_EQ(inf, inf / dbl_inf);
00582   EXPECT_EQ(-inf, inf / -dbl_inf);
00583   EXPECT_EQ(-inf, -inf / dbl_inf);
00584   EXPECT_EQ(inf, -inf / -dbl_inf);
00585 
00586   const absl::Duration any_dur = absl::Seconds(1);
00587   EXPECT_EQ(absl::ZeroDuration(), any_dur / dbl_inf);
00588   EXPECT_EQ(absl::ZeroDuration(), any_dur / -dbl_inf);
00589   EXPECT_EQ(absl::ZeroDuration(), -any_dur / dbl_inf);
00590   EXPECT_EQ(absl::ZeroDuration(), -any_dur / -dbl_inf);
00591 }
00592 
00593 TEST(Duration, InfinityModulus) {
00594   const absl::Duration sec_max = absl::Seconds(kint64max);
00595   const absl::Duration any_dur = absl::Seconds(1);
00596   const absl::Duration inf = absl::InfiniteDuration();
00597 
00598   EXPECT_EQ(inf, inf % inf);
00599   EXPECT_EQ(inf, inf % -inf);
00600   EXPECT_EQ(-inf, -inf % -inf);
00601   EXPECT_EQ(-inf, -inf % inf);
00602 
00603   EXPECT_EQ(any_dur, any_dur % inf);
00604   EXPECT_EQ(any_dur, any_dur % -inf);
00605   EXPECT_EQ(-any_dur, -any_dur % inf);
00606   EXPECT_EQ(-any_dur, -any_dur % -inf);
00607 
00608   EXPECT_EQ(inf, inf % -any_dur);
00609   EXPECT_EQ(inf, inf % any_dur);
00610   EXPECT_EQ(-inf, -inf % -any_dur);
00611   EXPECT_EQ(-inf, -inf % any_dur);
00612 
00613   // Remainder isn't affected by overflow.
00614   EXPECT_EQ(absl::ZeroDuration(), sec_max % absl::Seconds(1));
00615   EXPECT_EQ(absl::ZeroDuration(), sec_max % absl::Milliseconds(1));
00616   EXPECT_EQ(absl::ZeroDuration(), sec_max % absl::Microseconds(1));
00617   EXPECT_EQ(absl::ZeroDuration(), sec_max % absl::Nanoseconds(1));
00618   EXPECT_EQ(absl::ZeroDuration(), sec_max % absl::Nanoseconds(1) / 4);
00619 }
00620 
00621 TEST(Duration, InfinityIDiv) {
00622   const absl::Duration sec_max = absl::Seconds(kint64max);
00623   const absl::Duration any_dur = absl::Seconds(1);
00624   const absl::Duration inf = absl::InfiniteDuration();
00625   const double dbl_inf = std::numeric_limits<double>::infinity();
00626 
00627   // IDivDuration (int64_t return value + a remainer)
00628   absl::Duration rem = absl::ZeroDuration();
00629   EXPECT_EQ(kint64max, absl::IDivDuration(inf, inf, &rem));
00630   EXPECT_EQ(inf, rem);
00631 
00632   rem = absl::ZeroDuration();
00633   EXPECT_EQ(kint64max, absl::IDivDuration(-inf, -inf, &rem));
00634   EXPECT_EQ(-inf, rem);
00635 
00636   rem = absl::ZeroDuration();
00637   EXPECT_EQ(kint64max, absl::IDivDuration(inf, any_dur, &rem));
00638   EXPECT_EQ(inf, rem);
00639 
00640   rem = absl::ZeroDuration();
00641   EXPECT_EQ(0, absl::IDivDuration(any_dur, inf, &rem));
00642   EXPECT_EQ(any_dur, rem);
00643 
00644   rem = absl::ZeroDuration();
00645   EXPECT_EQ(kint64max, absl::IDivDuration(-inf, -any_dur, &rem));
00646   EXPECT_EQ(-inf, rem);
00647 
00648   rem = absl::ZeroDuration();
00649   EXPECT_EQ(0, absl::IDivDuration(-any_dur, -inf, &rem));
00650   EXPECT_EQ(-any_dur, rem);
00651 
00652   rem = absl::ZeroDuration();
00653   EXPECT_EQ(kint64min, absl::IDivDuration(-inf, inf, &rem));
00654   EXPECT_EQ(-inf, rem);
00655 
00656   rem = absl::ZeroDuration();
00657   EXPECT_EQ(kint64min, absl::IDivDuration(inf, -inf, &rem));
00658   EXPECT_EQ(inf, rem);
00659 
00660   rem = absl::ZeroDuration();
00661   EXPECT_EQ(kint64min, absl::IDivDuration(-inf, any_dur, &rem));
00662   EXPECT_EQ(-inf, rem);
00663 
00664   rem = absl::ZeroDuration();
00665   EXPECT_EQ(0, absl::IDivDuration(-any_dur, inf, &rem));
00666   EXPECT_EQ(-any_dur, rem);
00667 
00668   rem = absl::ZeroDuration();
00669   EXPECT_EQ(kint64min, absl::IDivDuration(inf, -any_dur, &rem));
00670   EXPECT_EQ(inf, rem);
00671 
00672   rem = absl::ZeroDuration();
00673   EXPECT_EQ(0, absl::IDivDuration(any_dur, -inf, &rem));
00674   EXPECT_EQ(any_dur, rem);
00675 
00676   // IDivDuration overflow/underflow
00677   rem = any_dur;
00678   EXPECT_EQ(kint64max,
00679             absl::IDivDuration(sec_max, absl::Nanoseconds(1) / 4, &rem));
00680   EXPECT_EQ(sec_max - absl::Nanoseconds(kint64max) / 4, rem);
00681 
00682   rem = any_dur;
00683   EXPECT_EQ(kint64max,
00684             absl::IDivDuration(sec_max, absl::Milliseconds(1), &rem));
00685   EXPECT_EQ(sec_max - absl::Milliseconds(kint64max), rem);
00686 
00687   rem = any_dur;
00688   EXPECT_EQ(kint64max,
00689             absl::IDivDuration(-sec_max, -absl::Milliseconds(1), &rem));
00690   EXPECT_EQ(-sec_max + absl::Milliseconds(kint64max), rem);
00691 
00692   rem = any_dur;
00693   EXPECT_EQ(kint64min,
00694             absl::IDivDuration(-sec_max, absl::Milliseconds(1), &rem));
00695   EXPECT_EQ(-sec_max - absl::Milliseconds(kint64min), rem);
00696 
00697   rem = any_dur;
00698   EXPECT_EQ(kint64min,
00699             absl::IDivDuration(sec_max, -absl::Milliseconds(1), &rem));
00700   EXPECT_EQ(sec_max + absl::Milliseconds(kint64min), rem);
00701 
00702   //
00703   // operator/(Duration, Duration) is a wrapper for IDivDuration().
00704   //
00705 
00706   // IEEE 754 says inf / inf should be nan, but int64_t doesn't have
00707   // nan so we'll return kint64max/kint64min instead.
00708   EXPECT_TRUE(std::isnan(dbl_inf / dbl_inf));
00709   EXPECT_EQ(kint64max, inf / inf);
00710   EXPECT_EQ(kint64max, -inf / -inf);
00711   EXPECT_EQ(kint64min, -inf / inf);
00712   EXPECT_EQ(kint64min, inf / -inf);
00713 
00714   EXPECT_TRUE(std::isinf(dbl_inf / 2.0));
00715   EXPECT_EQ(kint64max, inf / any_dur);
00716   EXPECT_EQ(kint64max, -inf / -any_dur);
00717   EXPECT_EQ(kint64min, -inf / any_dur);
00718   EXPECT_EQ(kint64min, inf / -any_dur);
00719 
00720   EXPECT_EQ(0.0, 2.0 / dbl_inf);
00721   EXPECT_EQ(0, any_dur / inf);
00722   EXPECT_EQ(0, any_dur / -inf);
00723   EXPECT_EQ(0, -any_dur / inf);
00724   EXPECT_EQ(0, -any_dur / -inf);
00725   EXPECT_EQ(0, absl::ZeroDuration() / inf);
00726 
00727   // Division of Duration by a Duration overflow/underflow
00728   EXPECT_EQ(kint64max, sec_max / absl::Milliseconds(1));
00729   EXPECT_EQ(kint64max, -sec_max / -absl::Milliseconds(1));
00730   EXPECT_EQ(kint64min, -sec_max / absl::Milliseconds(1));
00731   EXPECT_EQ(kint64min, sec_max / -absl::Milliseconds(1));
00732 }
00733 
00734 TEST(Duration, InfinityFDiv) {
00735   const absl::Duration any_dur = absl::Seconds(1);
00736   const absl::Duration inf = absl::InfiniteDuration();
00737   const double dbl_inf = std::numeric_limits<double>::infinity();
00738 
00739   EXPECT_EQ(dbl_inf, absl::FDivDuration(inf, inf));
00740   EXPECT_EQ(dbl_inf, absl::FDivDuration(-inf, -inf));
00741   EXPECT_EQ(dbl_inf, absl::FDivDuration(inf, any_dur));
00742   EXPECT_EQ(0.0, absl::FDivDuration(any_dur, inf));
00743   EXPECT_EQ(dbl_inf, absl::FDivDuration(-inf, -any_dur));
00744   EXPECT_EQ(0.0, absl::FDivDuration(-any_dur, -inf));
00745 
00746   EXPECT_EQ(-dbl_inf, absl::FDivDuration(-inf, inf));
00747   EXPECT_EQ(-dbl_inf, absl::FDivDuration(inf, -inf));
00748   EXPECT_EQ(-dbl_inf, absl::FDivDuration(-inf, any_dur));
00749   EXPECT_EQ(0.0, absl::FDivDuration(-any_dur, inf));
00750   EXPECT_EQ(-dbl_inf, absl::FDivDuration(inf, -any_dur));
00751   EXPECT_EQ(0.0, absl::FDivDuration(any_dur, -inf));
00752 }
00753 
00754 TEST(Duration, DivisionByZero) {
00755   const absl::Duration zero = absl::ZeroDuration();
00756   const absl::Duration inf = absl::InfiniteDuration();
00757   const absl::Duration any_dur = absl::Seconds(1);
00758   const double dbl_inf = std::numeric_limits<double>::infinity();
00759   const double dbl_denorm = std::numeric_limits<double>::denorm_min();
00760 
00761   // IEEE 754 behavior
00762   double z = 0.0, two = 2.0;
00763   EXPECT_TRUE(std::isinf(two / z));
00764   EXPECT_TRUE(std::isnan(z / z));  // We'll return inf
00765 
00766   // Operator/(Duration, double)
00767   EXPECT_EQ(inf, zero / 0.0);
00768   EXPECT_EQ(-inf, zero / -0.0);
00769   EXPECT_EQ(inf, any_dur / 0.0);
00770   EXPECT_EQ(-inf, any_dur / -0.0);
00771   EXPECT_EQ(-inf, -any_dur / 0.0);
00772   EXPECT_EQ(inf, -any_dur / -0.0);
00773 
00774   // Tests dividing by a number very close to, but not quite zero.
00775   EXPECT_EQ(zero, zero / dbl_denorm);
00776   EXPECT_EQ(zero, zero / -dbl_denorm);
00777   EXPECT_EQ(inf, any_dur / dbl_denorm);
00778   EXPECT_EQ(-inf, any_dur / -dbl_denorm);
00779   EXPECT_EQ(-inf, -any_dur / dbl_denorm);
00780   EXPECT_EQ(inf, -any_dur / -dbl_denorm);
00781 
00782   // IDiv
00783   absl::Duration rem = zero;
00784   EXPECT_EQ(kint64max, absl::IDivDuration(zero, zero, &rem));
00785   EXPECT_EQ(inf, rem);
00786 
00787   rem = zero;
00788   EXPECT_EQ(kint64max, absl::IDivDuration(any_dur, zero, &rem));
00789   EXPECT_EQ(inf, rem);
00790 
00791   rem = zero;
00792   EXPECT_EQ(kint64min, absl::IDivDuration(-any_dur, zero, &rem));
00793   EXPECT_EQ(-inf, rem);
00794 
00795   // Operator/(Duration, Duration)
00796   EXPECT_EQ(kint64max, zero / zero);
00797   EXPECT_EQ(kint64max, any_dur / zero);
00798   EXPECT_EQ(kint64min, -any_dur / zero);
00799 
00800   // FDiv
00801   EXPECT_EQ(dbl_inf, absl::FDivDuration(zero, zero));
00802   EXPECT_EQ(dbl_inf, absl::FDivDuration(any_dur, zero));
00803   EXPECT_EQ(-dbl_inf, absl::FDivDuration(-any_dur, zero));
00804 }
00805 
00806 TEST(Duration, NaN) {
00807   // Note that IEEE 754 does not define the behavior of a nan's sign when it is
00808   // copied, so the code below allows for either + or - InfiniteDuration.
00809 #define TEST_NAN_HANDLING(NAME, NAN)           \
00810   do {                                         \
00811     const auto inf = absl::InfiniteDuration(); \
00812     auto x = NAME(NAN);                        \
00813     EXPECT_TRUE(x == inf || x == -inf);        \
00814     auto y = NAME(42);                         \
00815     y *= NAN;                                  \
00816     EXPECT_TRUE(y == inf || y == -inf);        \
00817     auto z = NAME(42);                         \
00818     z /= NAN;                                  \
00819     EXPECT_TRUE(z == inf || z == -inf);        \
00820   } while (0)
00821 
00822   const double nan = std::numeric_limits<double>::quiet_NaN();
00823   TEST_NAN_HANDLING(absl::Nanoseconds, nan);
00824   TEST_NAN_HANDLING(absl::Microseconds, nan);
00825   TEST_NAN_HANDLING(absl::Milliseconds, nan);
00826   TEST_NAN_HANDLING(absl::Seconds, nan);
00827   TEST_NAN_HANDLING(absl::Minutes, nan);
00828   TEST_NAN_HANDLING(absl::Hours, nan);
00829 
00830   TEST_NAN_HANDLING(absl::Nanoseconds, -nan);
00831   TEST_NAN_HANDLING(absl::Microseconds, -nan);
00832   TEST_NAN_HANDLING(absl::Milliseconds, -nan);
00833   TEST_NAN_HANDLING(absl::Seconds, -nan);
00834   TEST_NAN_HANDLING(absl::Minutes, -nan);
00835   TEST_NAN_HANDLING(absl::Hours, -nan);
00836 
00837 #undef TEST_NAN_HANDLING
00838 }
00839 
00840 TEST(Duration, Range) {
00841   const absl::Duration range = ApproxYears(100 * 1e9);
00842   const absl::Duration range_future = range;
00843   const absl::Duration range_past = -range;
00844 
00845   EXPECT_LT(range_future, absl::InfiniteDuration());
00846   EXPECT_GT(range_past, -absl::InfiniteDuration());
00847 
00848   const absl::Duration full_range = range_future - range_past;
00849   EXPECT_GT(full_range, absl::ZeroDuration());
00850   EXPECT_LT(full_range, absl::InfiniteDuration());
00851 
00852   const absl::Duration neg_full_range = range_past - range_future;
00853   EXPECT_LT(neg_full_range, absl::ZeroDuration());
00854   EXPECT_GT(neg_full_range, -absl::InfiniteDuration());
00855 
00856   EXPECT_LT(neg_full_range, full_range);
00857   EXPECT_EQ(neg_full_range, -full_range);
00858 }
00859 
00860 TEST(Duration, RelationalOperators) {
00861 #define TEST_REL_OPS(UNIT)               \
00862   static_assert(UNIT(2) == UNIT(2), ""); \
00863   static_assert(UNIT(1) != UNIT(2), ""); \
00864   static_assert(UNIT(1) < UNIT(2), "");  \
00865   static_assert(UNIT(3) > UNIT(2), "");  \
00866   static_assert(UNIT(1) <= UNIT(2), ""); \
00867   static_assert(UNIT(2) <= UNIT(2), ""); \
00868   static_assert(UNIT(3) >= UNIT(2), ""); \
00869   static_assert(UNIT(2) >= UNIT(2), "");
00870 
00871   TEST_REL_OPS(absl::Nanoseconds);
00872   TEST_REL_OPS(absl::Microseconds);
00873   TEST_REL_OPS(absl::Milliseconds);
00874   TEST_REL_OPS(absl::Seconds);
00875   TEST_REL_OPS(absl::Minutes);
00876   TEST_REL_OPS(absl::Hours);
00877 
00878 #undef TEST_REL_OPS
00879 }
00880 
00881 TEST(Duration, Addition) {
00882 #define TEST_ADD_OPS(UNIT)                  \
00883   do {                                      \
00884     EXPECT_EQ(UNIT(2), UNIT(1) + UNIT(1));  \
00885     EXPECT_EQ(UNIT(1), UNIT(2) - UNIT(1));  \
00886     EXPECT_EQ(UNIT(0), UNIT(2) - UNIT(2));  \
00887     EXPECT_EQ(UNIT(-1), UNIT(1) - UNIT(2)); \
00888     EXPECT_EQ(UNIT(-2), UNIT(0) - UNIT(2)); \
00889     EXPECT_EQ(UNIT(-2), UNIT(1) - UNIT(3)); \
00890     absl::Duration a = UNIT(1);             \
00891     a += UNIT(1);                           \
00892     EXPECT_EQ(UNIT(2), a);                  \
00893     a -= UNIT(1);                           \
00894     EXPECT_EQ(UNIT(1), a);                  \
00895   } while (0)
00896 
00897   TEST_ADD_OPS(absl::Nanoseconds);
00898   TEST_ADD_OPS(absl::Microseconds);
00899   TEST_ADD_OPS(absl::Milliseconds);
00900   TEST_ADD_OPS(absl::Seconds);
00901   TEST_ADD_OPS(absl::Minutes);
00902   TEST_ADD_OPS(absl::Hours);
00903 
00904 #undef TEST_ADD_OPS
00905 
00906   EXPECT_EQ(absl::Seconds(2), absl::Seconds(3) - 2 * absl::Milliseconds(500));
00907   EXPECT_EQ(absl::Seconds(2) + absl::Milliseconds(500),
00908             absl::Seconds(3) - absl::Milliseconds(500));
00909 
00910   EXPECT_EQ(absl::Seconds(1) + absl::Milliseconds(998),
00911             absl::Milliseconds(999) + absl::Milliseconds(999));
00912 
00913   EXPECT_EQ(absl::Milliseconds(-1),
00914             absl::Milliseconds(998) - absl::Milliseconds(999));
00915 
00916   // Tests fractions of a nanoseconds. These are implementation details only.
00917   EXPECT_GT(absl::Nanoseconds(1), absl::Nanoseconds(1) / 2);
00918   EXPECT_EQ(absl::Nanoseconds(1),
00919             absl::Nanoseconds(1) / 2 + absl::Nanoseconds(1) / 2);
00920   EXPECT_GT(absl::Nanoseconds(1) / 4, absl::Nanoseconds(0));
00921   EXPECT_EQ(absl::Nanoseconds(1) / 8, absl::Nanoseconds(0));
00922 
00923   // Tests subtraction that will cause wrap around of the rep_lo_ bits.
00924   absl::Duration d_7_5 = absl::Seconds(7) + absl::Milliseconds(500);
00925   absl::Duration d_3_7 = absl::Seconds(3) + absl::Milliseconds(700);
00926   absl::Duration ans_3_8 = absl::Seconds(3) + absl::Milliseconds(800);
00927   EXPECT_EQ(ans_3_8, d_7_5 - d_3_7);
00928 
00929   // Subtracting min_duration
00930   absl::Duration min_dur = absl::Seconds(kint64min);
00931   EXPECT_EQ(absl::Seconds(0), min_dur - min_dur);
00932   EXPECT_EQ(absl::Seconds(kint64max), absl::Seconds(-1) - min_dur);
00933 }
00934 
00935 TEST(Duration, Negation) {
00936   // By storing negations of various values in constexpr variables we
00937   // verify that the initializers are constant expressions.
00938   constexpr absl::Duration negated_zero_duration = -absl::ZeroDuration();
00939   EXPECT_EQ(negated_zero_duration, absl::ZeroDuration());
00940 
00941   constexpr absl::Duration negated_infinite_duration =
00942       -absl::InfiniteDuration();
00943   EXPECT_NE(negated_infinite_duration, absl::InfiniteDuration());
00944   EXPECT_EQ(-negated_infinite_duration, absl::InfiniteDuration());
00945 
00946   // The public APIs to check if a duration is infinite depend on using
00947   // -InfiniteDuration(), but we're trying to test operator- here, so we
00948   // need to use the lower-level internal query IsInfiniteDuration.
00949   EXPECT_TRUE(
00950       absl::time_internal::IsInfiniteDuration(negated_infinite_duration));
00951 
00952   // The largest Duration is kint64max seconds and kTicksPerSecond - 1 ticks.
00953   // Using the absl::time_internal::MakeDuration API is the cleanest way to
00954   // construct that Duration.
00955   constexpr absl::Duration max_duration = absl::time_internal::MakeDuration(
00956       kint64max, absl::time_internal::kTicksPerSecond - 1);
00957   constexpr absl::Duration negated_max_duration = -max_duration;
00958   // The largest negatable value is one tick above the minimum representable;
00959   // it's the negation of max_duration.
00960   constexpr absl::Duration nearly_min_duration =
00961       absl::time_internal::MakeDuration(kint64min, int64_t{1});
00962   constexpr absl::Duration negated_nearly_min_duration = -nearly_min_duration;
00963 
00964   EXPECT_EQ(negated_max_duration, nearly_min_duration);
00965   EXPECT_EQ(negated_nearly_min_duration, max_duration);
00966   EXPECT_EQ(-(-max_duration), max_duration);
00967 
00968   constexpr absl::Duration min_duration =
00969       absl::time_internal::MakeDuration(kint64min);
00970   constexpr absl::Duration negated_min_duration = -min_duration;
00971   EXPECT_EQ(negated_min_duration, absl::InfiniteDuration());
00972 }
00973 
00974 TEST(Duration, AbsoluteValue) {
00975   EXPECT_EQ(absl::ZeroDuration(), AbsDuration(absl::ZeroDuration()));
00976   EXPECT_EQ(absl::Seconds(1), AbsDuration(absl::Seconds(1)));
00977   EXPECT_EQ(absl::Seconds(1), AbsDuration(absl::Seconds(-1)));
00978 
00979   EXPECT_EQ(absl::InfiniteDuration(), AbsDuration(absl::InfiniteDuration()));
00980   EXPECT_EQ(absl::InfiniteDuration(), AbsDuration(-absl::InfiniteDuration()));
00981 
00982   absl::Duration max_dur =
00983       absl::Seconds(kint64max) + (absl::Seconds(1) - absl::Nanoseconds(1) / 4);
00984   EXPECT_EQ(max_dur, AbsDuration(max_dur));
00985 
00986   absl::Duration min_dur = absl::Seconds(kint64min);
00987   EXPECT_EQ(absl::InfiniteDuration(), AbsDuration(min_dur));
00988   EXPECT_EQ(max_dur, AbsDuration(min_dur + absl::Nanoseconds(1) / 4));
00989 }
00990 
00991 TEST(Duration, Multiplication) {
00992 #define TEST_MUL_OPS(UNIT)                                    \
00993   do {                                                        \
00994     EXPECT_EQ(UNIT(5), UNIT(2) * 2.5);                        \
00995     EXPECT_EQ(UNIT(2), UNIT(5) / 2.5);                        \
00996     EXPECT_EQ(UNIT(-5), UNIT(-2) * 2.5);                      \
00997     EXPECT_EQ(UNIT(-5), -UNIT(2) * 2.5);                      \
00998     EXPECT_EQ(UNIT(-5), UNIT(2) * -2.5);                      \
00999     EXPECT_EQ(UNIT(-2), UNIT(-5) / 2.5);                      \
01000     EXPECT_EQ(UNIT(-2), -UNIT(5) / 2.5);                      \
01001     EXPECT_EQ(UNIT(-2), UNIT(5) / -2.5);                      \
01002     EXPECT_EQ(UNIT(2), UNIT(11) % UNIT(3));                   \
01003     absl::Duration a = UNIT(2);                               \
01004     a *= 2.5;                                                 \
01005     EXPECT_EQ(UNIT(5), a);                                    \
01006     a /= 2.5;                                                 \
01007     EXPECT_EQ(UNIT(2), a);                                    \
01008     a %= UNIT(1);                                             \
01009     EXPECT_EQ(UNIT(0), a);                                    \
01010     absl::Duration big = UNIT(1000000000);                    \
01011     big *= 3;                                                 \
01012     big /= 3;                                                 \
01013     EXPECT_EQ(UNIT(1000000000), big);                         \
01014     EXPECT_EQ(-UNIT(2), -UNIT(2));                            \
01015     EXPECT_EQ(-UNIT(2), UNIT(2) * -1);                        \
01016     EXPECT_EQ(-UNIT(2), -1 * UNIT(2));                        \
01017     EXPECT_EQ(-UNIT(-2), UNIT(2));                            \
01018     EXPECT_EQ(2, UNIT(2) / UNIT(1));                          \
01019     absl::Duration rem;                                       \
01020     EXPECT_EQ(2, absl::IDivDuration(UNIT(2), UNIT(1), &rem)); \
01021     EXPECT_EQ(2.0, absl::FDivDuration(UNIT(2), UNIT(1)));     \
01022   } while (0)
01023 
01024   TEST_MUL_OPS(absl::Nanoseconds);
01025   TEST_MUL_OPS(absl::Microseconds);
01026   TEST_MUL_OPS(absl::Milliseconds);
01027   TEST_MUL_OPS(absl::Seconds);
01028   TEST_MUL_OPS(absl::Minutes);
01029   TEST_MUL_OPS(absl::Hours);
01030 
01031 #undef TEST_MUL_OPS
01032 
01033   // Ensures that multiplication and division by 1 with a maxed-out durations
01034   // doesn't lose precision.
01035   absl::Duration max_dur =
01036       absl::Seconds(kint64max) + (absl::Seconds(1) - absl::Nanoseconds(1) / 4);
01037   absl::Duration min_dur = absl::Seconds(kint64min);
01038   EXPECT_EQ(max_dur, max_dur * 1);
01039   EXPECT_EQ(max_dur, max_dur / 1);
01040   EXPECT_EQ(min_dur, min_dur * 1);
01041   EXPECT_EQ(min_dur, min_dur / 1);
01042 
01043   // Tests division on a Duration with a large number of significant digits.
01044   // Tests when the digits span hi and lo as well as only in hi.
01045   absl::Duration sigfigs = absl::Seconds(2000000000) + absl::Nanoseconds(3);
01046   EXPECT_EQ(absl::Seconds(666666666) + absl::Nanoseconds(666666667) +
01047                 absl::Nanoseconds(1) / 2,
01048             sigfigs / 3);
01049   sigfigs = absl::Seconds(7000000000LL);
01050   EXPECT_EQ(absl::Seconds(2333333333) + absl::Nanoseconds(333333333) +
01051                 absl::Nanoseconds(1) / 4,
01052             sigfigs / 3);
01053 
01054   EXPECT_EQ(absl::Seconds(7) + absl::Milliseconds(500), absl::Seconds(3) * 2.5);
01055   EXPECT_EQ(absl::Seconds(8) * -1 + absl::Milliseconds(300),
01056             (absl::Seconds(2) + absl::Milliseconds(200)) * -3.5);
01057   EXPECT_EQ(-absl::Seconds(8) + absl::Milliseconds(300),
01058             (absl::Seconds(2) + absl::Milliseconds(200)) * -3.5);
01059   EXPECT_EQ(absl::Seconds(1) + absl::Milliseconds(875),
01060             (absl::Seconds(7) + absl::Milliseconds(500)) / 4);
01061   EXPECT_EQ(absl::Seconds(30),
01062             (absl::Seconds(7) + absl::Milliseconds(500)) / 0.25);
01063   EXPECT_EQ(absl::Seconds(3),
01064             (absl::Seconds(7) + absl::Milliseconds(500)) / 2.5);
01065 
01066   // Tests division remainder.
01067   EXPECT_EQ(absl::Nanoseconds(0), absl::Nanoseconds(7) % absl::Nanoseconds(1));
01068   EXPECT_EQ(absl::Nanoseconds(0), absl::Nanoseconds(0) % absl::Nanoseconds(10));
01069   EXPECT_EQ(absl::Nanoseconds(2), absl::Nanoseconds(7) % absl::Nanoseconds(5));
01070   EXPECT_EQ(absl::Nanoseconds(2), absl::Nanoseconds(2) % absl::Nanoseconds(5));
01071 
01072   EXPECT_EQ(absl::Nanoseconds(1), absl::Nanoseconds(10) % absl::Nanoseconds(3));
01073   EXPECT_EQ(absl::Nanoseconds(1),
01074             absl::Nanoseconds(10) % absl::Nanoseconds(-3));
01075   EXPECT_EQ(absl::Nanoseconds(-1),
01076             absl::Nanoseconds(-10) % absl::Nanoseconds(3));
01077   EXPECT_EQ(absl::Nanoseconds(-1),
01078             absl::Nanoseconds(-10) % absl::Nanoseconds(-3));
01079 
01080   EXPECT_EQ(absl::Milliseconds(100),
01081             absl::Seconds(1) % absl::Milliseconds(300));
01082   EXPECT_EQ(
01083       absl::Milliseconds(300),
01084       (absl::Seconds(3) + absl::Milliseconds(800)) % absl::Milliseconds(500));
01085 
01086   EXPECT_EQ(absl::Nanoseconds(1), absl::Nanoseconds(1) % absl::Seconds(1));
01087   EXPECT_EQ(absl::Nanoseconds(-1), absl::Nanoseconds(-1) % absl::Seconds(1));
01088   EXPECT_EQ(0, absl::Nanoseconds(-1) / absl::Seconds(1));  // Actual -1e-9
01089 
01090   // Tests identity a = (a/b)*b + a%b
01091 #define TEST_MOD_IDENTITY(a, b) \
01092   EXPECT_EQ((a), ((a) / (b))*(b) + ((a)%(b)))
01093 
01094   TEST_MOD_IDENTITY(absl::Seconds(0), absl::Seconds(2));
01095   TEST_MOD_IDENTITY(absl::Seconds(1), absl::Seconds(1));
01096   TEST_MOD_IDENTITY(absl::Seconds(1), absl::Seconds(2));
01097   TEST_MOD_IDENTITY(absl::Seconds(2), absl::Seconds(1));
01098 
01099   TEST_MOD_IDENTITY(absl::Seconds(-2), absl::Seconds(1));
01100   TEST_MOD_IDENTITY(absl::Seconds(2), absl::Seconds(-1));
01101   TEST_MOD_IDENTITY(absl::Seconds(-2), absl::Seconds(-1));
01102 
01103   TEST_MOD_IDENTITY(absl::Nanoseconds(0), absl::Nanoseconds(2));
01104   TEST_MOD_IDENTITY(absl::Nanoseconds(1), absl::Nanoseconds(1));
01105   TEST_MOD_IDENTITY(absl::Nanoseconds(1), absl::Nanoseconds(2));
01106   TEST_MOD_IDENTITY(absl::Nanoseconds(2), absl::Nanoseconds(1));
01107 
01108   TEST_MOD_IDENTITY(absl::Nanoseconds(-2), absl::Nanoseconds(1));
01109   TEST_MOD_IDENTITY(absl::Nanoseconds(2), absl::Nanoseconds(-1));
01110   TEST_MOD_IDENTITY(absl::Nanoseconds(-2), absl::Nanoseconds(-1));
01111 
01112   // Mixed seconds + subseconds
01113   absl::Duration mixed_a = absl::Seconds(1) + absl::Nanoseconds(2);
01114   absl::Duration mixed_b = absl::Seconds(1) + absl::Nanoseconds(3);
01115 
01116   TEST_MOD_IDENTITY(absl::Seconds(0), mixed_a);
01117   TEST_MOD_IDENTITY(mixed_a, mixed_a);
01118   TEST_MOD_IDENTITY(mixed_a, mixed_b);
01119   TEST_MOD_IDENTITY(mixed_b, mixed_a);
01120 
01121   TEST_MOD_IDENTITY(-mixed_a, mixed_b);
01122   TEST_MOD_IDENTITY(mixed_a, -mixed_b);
01123   TEST_MOD_IDENTITY(-mixed_a, -mixed_b);
01124 
01125 #undef TEST_MOD_IDENTITY
01126 }
01127 
01128 TEST(Duration, Truncation) {
01129   const absl::Duration d = absl::Nanoseconds(1234567890);
01130   const absl::Duration inf = absl::InfiniteDuration();
01131   for (int unit_sign : {1, -1}) {  // sign shouldn't matter
01132     EXPECT_EQ(absl::Nanoseconds(1234567890),
01133               Trunc(d, unit_sign * absl::Nanoseconds(1)));
01134     EXPECT_EQ(absl::Microseconds(1234567),
01135               Trunc(d, unit_sign * absl::Microseconds(1)));
01136     EXPECT_EQ(absl::Milliseconds(1234),
01137               Trunc(d, unit_sign * absl::Milliseconds(1)));
01138     EXPECT_EQ(absl::Seconds(1), Trunc(d, unit_sign * absl::Seconds(1)));
01139     EXPECT_EQ(inf, Trunc(inf, unit_sign * absl::Seconds(1)));
01140 
01141     EXPECT_EQ(absl::Nanoseconds(-1234567890),
01142               Trunc(-d, unit_sign * absl::Nanoseconds(1)));
01143     EXPECT_EQ(absl::Microseconds(-1234567),
01144               Trunc(-d, unit_sign * absl::Microseconds(1)));
01145     EXPECT_EQ(absl::Milliseconds(-1234),
01146               Trunc(-d, unit_sign * absl::Milliseconds(1)));
01147     EXPECT_EQ(absl::Seconds(-1), Trunc(-d, unit_sign * absl::Seconds(1)));
01148     EXPECT_EQ(-inf, Trunc(-inf, unit_sign * absl::Seconds(1)));
01149   }
01150 }
01151 
01152 TEST(Duration, Flooring) {
01153   const absl::Duration d = absl::Nanoseconds(1234567890);
01154   const absl::Duration inf = absl::InfiniteDuration();
01155   for (int unit_sign : {1, -1}) {  // sign shouldn't matter
01156     EXPECT_EQ(absl::Nanoseconds(1234567890),
01157               absl::Floor(d, unit_sign * absl::Nanoseconds(1)));
01158     EXPECT_EQ(absl::Microseconds(1234567),
01159               absl::Floor(d, unit_sign * absl::Microseconds(1)));
01160     EXPECT_EQ(absl::Milliseconds(1234),
01161               absl::Floor(d, unit_sign * absl::Milliseconds(1)));
01162     EXPECT_EQ(absl::Seconds(1), absl::Floor(d, unit_sign * absl::Seconds(1)));
01163     EXPECT_EQ(inf, absl::Floor(inf, unit_sign * absl::Seconds(1)));
01164 
01165     EXPECT_EQ(absl::Nanoseconds(-1234567890),
01166               absl::Floor(-d, unit_sign * absl::Nanoseconds(1)));
01167     EXPECT_EQ(absl::Microseconds(-1234568),
01168               absl::Floor(-d, unit_sign * absl::Microseconds(1)));
01169     EXPECT_EQ(absl::Milliseconds(-1235),
01170               absl::Floor(-d, unit_sign * absl::Milliseconds(1)));
01171     EXPECT_EQ(absl::Seconds(-2), absl::Floor(-d, unit_sign * absl::Seconds(1)));
01172     EXPECT_EQ(-inf, absl::Floor(-inf, unit_sign * absl::Seconds(1)));
01173   }
01174 }
01175 
01176 TEST(Duration, Ceiling) {
01177   const absl::Duration d = absl::Nanoseconds(1234567890);
01178   const absl::Duration inf = absl::InfiniteDuration();
01179   for (int unit_sign : {1, -1}) {  // // sign shouldn't matter
01180     EXPECT_EQ(absl::Nanoseconds(1234567890),
01181               absl::Ceil(d, unit_sign * absl::Nanoseconds(1)));
01182     EXPECT_EQ(absl::Microseconds(1234568),
01183               absl::Ceil(d, unit_sign * absl::Microseconds(1)));
01184     EXPECT_EQ(absl::Milliseconds(1235),
01185               absl::Ceil(d, unit_sign * absl::Milliseconds(1)));
01186     EXPECT_EQ(absl::Seconds(2), absl::Ceil(d, unit_sign * absl::Seconds(1)));
01187     EXPECT_EQ(inf, absl::Ceil(inf, unit_sign * absl::Seconds(1)));
01188 
01189     EXPECT_EQ(absl::Nanoseconds(-1234567890),
01190               absl::Ceil(-d, unit_sign * absl::Nanoseconds(1)));
01191     EXPECT_EQ(absl::Microseconds(-1234567),
01192               absl::Ceil(-d, unit_sign * absl::Microseconds(1)));
01193     EXPECT_EQ(absl::Milliseconds(-1234),
01194               absl::Ceil(-d, unit_sign * absl::Milliseconds(1)));
01195     EXPECT_EQ(absl::Seconds(-1), absl::Ceil(-d, unit_sign * absl::Seconds(1)));
01196     EXPECT_EQ(-inf, absl::Ceil(-inf, unit_sign * absl::Seconds(1)));
01197   }
01198 }
01199 
01200 TEST(Duration, RoundTripUnits) {
01201   const int kRange = 100000;
01202 
01203 #define ROUND_TRIP_UNIT(U, LOW, HIGH)          \
01204   do {                                         \
01205     for (int64_t i = LOW; i < HIGH; ++i) {     \
01206       absl::Duration d = absl::U(i);           \
01207       if (d == absl::InfiniteDuration())       \
01208         EXPECT_EQ(kint64max, d / absl::U(1));  \
01209       else if (d == -absl::InfiniteDuration()) \
01210         EXPECT_EQ(kint64min, d / absl::U(1));  \
01211       else                                     \
01212         EXPECT_EQ(i, absl::U(i) / absl::U(1)); \
01213     }                                          \
01214   } while (0)
01215 
01216   ROUND_TRIP_UNIT(Nanoseconds, kint64min, kint64min + kRange);
01217   ROUND_TRIP_UNIT(Nanoseconds, -kRange, kRange);
01218   ROUND_TRIP_UNIT(Nanoseconds, kint64max - kRange, kint64max);
01219 
01220   ROUND_TRIP_UNIT(Microseconds, kint64min, kint64min + kRange);
01221   ROUND_TRIP_UNIT(Microseconds, -kRange, kRange);
01222   ROUND_TRIP_UNIT(Microseconds, kint64max - kRange, kint64max);
01223 
01224   ROUND_TRIP_UNIT(Milliseconds, kint64min, kint64min + kRange);
01225   ROUND_TRIP_UNIT(Milliseconds, -kRange, kRange);
01226   ROUND_TRIP_UNIT(Milliseconds, kint64max - kRange, kint64max);
01227 
01228   ROUND_TRIP_UNIT(Seconds, kint64min, kint64min + kRange);
01229   ROUND_TRIP_UNIT(Seconds, -kRange, kRange);
01230   ROUND_TRIP_UNIT(Seconds, kint64max - kRange, kint64max);
01231 
01232   ROUND_TRIP_UNIT(Minutes, kint64min / 60, kint64min / 60 + kRange);
01233   ROUND_TRIP_UNIT(Minutes, -kRange, kRange);
01234   ROUND_TRIP_UNIT(Minutes, kint64max / 60 - kRange, kint64max / 60);
01235 
01236   ROUND_TRIP_UNIT(Hours, kint64min / 3600, kint64min / 3600 + kRange);
01237   ROUND_TRIP_UNIT(Hours, -kRange, kRange);
01238   ROUND_TRIP_UNIT(Hours, kint64max / 3600 - kRange, kint64max / 3600);
01239 
01240 #undef ROUND_TRIP_UNIT
01241 }
01242 
01243 TEST(Duration, TruncConversions) {
01244   // Tests ToTimespec()/DurationFromTimespec()
01245   const struct {
01246     absl::Duration d;
01247     timespec ts;
01248   } to_ts[] = {
01249       {absl::Seconds(1) + absl::Nanoseconds(1), {1, 1}},
01250       {absl::Seconds(1) + absl::Nanoseconds(1) / 2, {1, 0}},
01251       {absl::Seconds(1) + absl::Nanoseconds(0), {1, 0}},
01252       {absl::Seconds(0) + absl::Nanoseconds(0), {0, 0}},
01253       {absl::Seconds(0) - absl::Nanoseconds(1) / 2, {0, 0}},
01254       {absl::Seconds(0) - absl::Nanoseconds(1), {-1, 999999999}},
01255       {absl::Seconds(-1) + absl::Nanoseconds(1), {-1, 1}},
01256       {absl::Seconds(-1) + absl::Nanoseconds(1) / 2, {-1, 1}},
01257       {absl::Seconds(-1) + absl::Nanoseconds(0), {-1, 0}},
01258       {absl::Seconds(-1) - absl::Nanoseconds(1) / 2, {-1, 0}},
01259   };
01260   for (const auto& test : to_ts) {
01261     EXPECT_THAT(absl::ToTimespec(test.d), TimespecMatcher(test.ts));
01262   }
01263   const struct {
01264     timespec ts;
01265     absl::Duration d;
01266   } from_ts[] = {
01267       {{1, 1}, absl::Seconds(1) + absl::Nanoseconds(1)},
01268       {{1, 0}, absl::Seconds(1) + absl::Nanoseconds(0)},
01269       {{0, 0}, absl::Seconds(0) + absl::Nanoseconds(0)},
01270       {{0, -1}, absl::Seconds(0) - absl::Nanoseconds(1)},
01271       {{-1, 999999999}, absl::Seconds(0) - absl::Nanoseconds(1)},
01272       {{-1, 1}, absl::Seconds(-1) + absl::Nanoseconds(1)},
01273       {{-1, 0}, absl::Seconds(-1) + absl::Nanoseconds(0)},
01274       {{-1, -1}, absl::Seconds(-1) - absl::Nanoseconds(1)},
01275       {{-2, 999999999}, absl::Seconds(-1) - absl::Nanoseconds(1)},
01276   };
01277   for (const auto& test : from_ts) {
01278     EXPECT_EQ(test.d, absl::DurationFromTimespec(test.ts));
01279   }
01280 
01281   // Tests ToTimeval()/DurationFromTimeval() (same as timespec above)
01282   const struct {
01283     absl::Duration d;
01284     timeval tv;
01285   } to_tv[] = {
01286       {absl::Seconds(1) + absl::Microseconds(1), {1, 1}},
01287       {absl::Seconds(1) + absl::Microseconds(1) / 2, {1, 0}},
01288       {absl::Seconds(1) + absl::Microseconds(0), {1, 0}},
01289       {absl::Seconds(0) + absl::Microseconds(0), {0, 0}},
01290       {absl::Seconds(0) - absl::Microseconds(1) / 2, {0, 0}},
01291       {absl::Seconds(0) - absl::Microseconds(1), {-1, 999999}},
01292       {absl::Seconds(-1) + absl::Microseconds(1), {-1, 1}},
01293       {absl::Seconds(-1) + absl::Microseconds(1) / 2, {-1, 1}},
01294       {absl::Seconds(-1) + absl::Microseconds(0), {-1, 0}},
01295       {absl::Seconds(-1) - absl::Microseconds(1) / 2, {-1, 0}},
01296   };
01297   for (const auto& test : to_tv) {
01298     EXPECT_THAT(absl::ToTimeval(test.d), TimevalMatcher(test.tv));
01299   }
01300   const struct {
01301     timeval tv;
01302     absl::Duration d;
01303   } from_tv[] = {
01304       {{1, 1}, absl::Seconds(1) + absl::Microseconds(1)},
01305       {{1, 0}, absl::Seconds(1) + absl::Microseconds(0)},
01306       {{0, 0}, absl::Seconds(0) + absl::Microseconds(0)},
01307       {{0, -1}, absl::Seconds(0) - absl::Microseconds(1)},
01308       {{-1, 999999}, absl::Seconds(0) - absl::Microseconds(1)},
01309       {{-1, 1}, absl::Seconds(-1) + absl::Microseconds(1)},
01310       {{-1, 0}, absl::Seconds(-1) + absl::Microseconds(0)},
01311       {{-1, -1}, absl::Seconds(-1) - absl::Microseconds(1)},
01312       {{-2, 999999}, absl::Seconds(-1) - absl::Microseconds(1)},
01313   };
01314   for (const auto& test : from_tv) {
01315     EXPECT_EQ(test.d, absl::DurationFromTimeval(test.tv));
01316   }
01317 }
01318 
01319 TEST(Duration, SmallConversions) {
01320   // Special tests for conversions of small durations.
01321 
01322   EXPECT_EQ(absl::ZeroDuration(), absl::Seconds(0));
01323   // TODO(bww): Is the next one OK?
01324   EXPECT_EQ(absl::ZeroDuration(), absl::Seconds(0.124999999e-9));
01325   EXPECT_EQ(absl::Nanoseconds(1) / 4, absl::Seconds(0.125e-9));
01326   EXPECT_EQ(absl::Nanoseconds(1) / 4, absl::Seconds(0.250e-9));
01327   EXPECT_EQ(absl::Nanoseconds(1) / 2, absl::Seconds(0.375e-9));
01328   EXPECT_EQ(absl::Nanoseconds(1) / 2, absl::Seconds(0.500e-9));
01329   EXPECT_EQ(absl::Nanoseconds(3) / 4, absl::Seconds(0.625e-9));
01330   EXPECT_EQ(absl::Nanoseconds(3) / 4, absl::Seconds(0.750e-9));
01331   EXPECT_EQ(absl::Nanoseconds(1), absl::Seconds(0.875e-9));
01332   EXPECT_EQ(absl::Nanoseconds(1), absl::Seconds(1.000e-9));
01333 
01334   EXPECT_EQ(absl::ZeroDuration(), absl::Seconds(-0.124999999e-9));
01335   EXPECT_EQ(-absl::Nanoseconds(1) / 4, absl::Seconds(-0.125e-9));
01336   EXPECT_EQ(-absl::Nanoseconds(1) / 4, absl::Seconds(-0.250e-9));
01337   EXPECT_EQ(-absl::Nanoseconds(1) / 2, absl::Seconds(-0.375e-9));
01338   EXPECT_EQ(-absl::Nanoseconds(1) / 2, absl::Seconds(-0.500e-9));
01339   EXPECT_EQ(-absl::Nanoseconds(3) / 4, absl::Seconds(-0.625e-9));
01340   EXPECT_EQ(-absl::Nanoseconds(3) / 4, absl::Seconds(-0.750e-9));
01341   EXPECT_EQ(-absl::Nanoseconds(1), absl::Seconds(-0.875e-9));
01342   EXPECT_EQ(-absl::Nanoseconds(1), absl::Seconds(-1.000e-9));
01343 
01344   timespec ts;
01345   ts.tv_sec = 0;
01346   ts.tv_nsec = 0;
01347   EXPECT_THAT(ToTimespec(absl::Nanoseconds(0)), TimespecMatcher(ts));
01348   // TODO(bww): Are the next three OK?
01349   EXPECT_THAT(ToTimespec(absl::Nanoseconds(1) / 4), TimespecMatcher(ts));
01350   EXPECT_THAT(ToTimespec(absl::Nanoseconds(2) / 4), TimespecMatcher(ts));
01351   EXPECT_THAT(ToTimespec(absl::Nanoseconds(3) / 4), TimespecMatcher(ts));
01352   ts.tv_nsec = 1;
01353   EXPECT_THAT(ToTimespec(absl::Nanoseconds(4) / 4), TimespecMatcher(ts));
01354   EXPECT_THAT(ToTimespec(absl::Nanoseconds(5) / 4), TimespecMatcher(ts));
01355   EXPECT_THAT(ToTimespec(absl::Nanoseconds(6) / 4), TimespecMatcher(ts));
01356   EXPECT_THAT(ToTimespec(absl::Nanoseconds(7) / 4), TimespecMatcher(ts));
01357   ts.tv_nsec = 2;
01358   EXPECT_THAT(ToTimespec(absl::Nanoseconds(8) / 4), TimespecMatcher(ts));
01359 
01360   timeval tv;
01361   tv.tv_sec = 0;
01362   tv.tv_usec = 0;
01363   EXPECT_THAT(ToTimeval(absl::Nanoseconds(0)), TimevalMatcher(tv));
01364   // TODO(bww): Is the next one OK?
01365   EXPECT_THAT(ToTimeval(absl::Nanoseconds(999)), TimevalMatcher(tv));
01366   tv.tv_usec = 1;
01367   EXPECT_THAT(ToTimeval(absl::Nanoseconds(1000)), TimevalMatcher(tv));
01368   EXPECT_THAT(ToTimeval(absl::Nanoseconds(1999)), TimevalMatcher(tv));
01369   tv.tv_usec = 2;
01370   EXPECT_THAT(ToTimeval(absl::Nanoseconds(2000)), TimevalMatcher(tv));
01371 }
01372 
01373 void VerifySameAsMul(double time_as_seconds, int* const misses) {
01374   auto direct_seconds = absl::Seconds(time_as_seconds);
01375   auto mul_by_one_second = time_as_seconds * absl::Seconds(1);
01376   if (direct_seconds != mul_by_one_second) {
01377     if (*misses > 10) return;
01378     ASSERT_LE(++(*misses), 10) << "Too many errors, not reporting more.";
01379     EXPECT_EQ(direct_seconds, mul_by_one_second)
01380         << "given double time_as_seconds = " << std::setprecision(17)
01381         << time_as_seconds;
01382   }
01383 }
01384 
01385 // For a variety of interesting durations, we find the exact point
01386 // where one double converts to that duration, and the very next double
01387 // converts to the next duration.  For both of those points, verify that
01388 // Seconds(point) returns the same duration as point * Seconds(1.0)
01389 TEST(Duration, ToDoubleSecondsCheckEdgeCases) {
01390   constexpr uint32_t kTicksPerSecond = absl::time_internal::kTicksPerSecond;
01391   constexpr auto duration_tick = absl::time_internal::MakeDuration(0, 1u);
01392   int misses = 0;
01393   for (int64_t seconds = 0; seconds < 99; ++seconds) {
01394     uint32_t tick_vals[] = {0, +999, +999999, +999999999, kTicksPerSecond - 1,
01395                             0, 1000, 1000000, 1000000000, kTicksPerSecond,
01396                             1, 1001, 1000001, 1000000001, kTicksPerSecond + 1,
01397                             2, 1002, 1000002, 1000000002, kTicksPerSecond + 2,
01398                             3, 1003, 1000003, 1000000003, kTicksPerSecond + 3,
01399                             4, 1004, 1000004, 1000000004, kTicksPerSecond + 4,
01400                             5, 6,    7,       8,          9};
01401     for (uint32_t ticks : tick_vals) {
01402       absl::Duration s_plus_t = absl::Seconds(seconds) + ticks * duration_tick;
01403       for (absl::Duration d : {s_plus_t, -s_plus_t}) {
01404         absl::Duration after_d = d + duration_tick;
01405         EXPECT_NE(d, after_d);
01406         EXPECT_EQ(after_d - d, duration_tick);
01407 
01408         double low_edge = ToDoubleSeconds(d);
01409         EXPECT_EQ(d, absl::Seconds(low_edge));
01410 
01411         double high_edge = ToDoubleSeconds(after_d);
01412         EXPECT_EQ(after_d, absl::Seconds(high_edge));
01413 
01414         for (;;) {
01415           double midpoint = low_edge + (high_edge - low_edge) / 2;
01416           if (midpoint == low_edge || midpoint == high_edge) break;
01417           absl::Duration mid_duration = absl::Seconds(midpoint);
01418           if (mid_duration == d) {
01419             low_edge = midpoint;
01420           } else {
01421             EXPECT_EQ(mid_duration, after_d);
01422             high_edge = midpoint;
01423           }
01424         }
01425         // Now low_edge is the highest double that converts to Duration d,
01426         // and high_edge is the lowest double that converts to Duration after_d.
01427         VerifySameAsMul(low_edge, &misses);
01428         VerifySameAsMul(high_edge, &misses);
01429       }
01430     }
01431   }
01432 }
01433 
01434 TEST(Duration, ToDoubleSecondsCheckRandom) {
01435   std::random_device rd;
01436   std::seed_seq seed({rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd()});
01437   std::mt19937_64 gen(seed);
01438   // We want doubles distributed from 1/8ns up to 2^63, where
01439   // as many values are tested from 1ns to 2ns as from 1sec to 2sec,
01440   // so even distribute along a log-scale of those values, and
01441   // exponentiate before using them.  (9.223377e+18 is just slightly
01442   // out of bounds for absl::Duration.)
01443   std::uniform_real_distribution<double> uniform(std::log(0.125e-9),
01444                                                  std::log(9.223377e+18));
01445   int misses = 0;
01446   for (int i = 0; i < 1000000; ++i) {
01447     double d = std::exp(uniform(gen));
01448     VerifySameAsMul(d, &misses);
01449     VerifySameAsMul(-d, &misses);
01450   }
01451 }
01452 
01453 TEST(Duration, ConversionSaturation) {
01454   absl::Duration d;
01455 
01456   const auto max_timeval_sec =
01457       std::numeric_limits<decltype(timeval::tv_sec)>::max();
01458   const auto min_timeval_sec =
01459       std::numeric_limits<decltype(timeval::tv_sec)>::min();
01460   timeval tv;
01461   tv.tv_sec = max_timeval_sec;
01462   tv.tv_usec = 999998;
01463   d = absl::DurationFromTimeval(tv);
01464   tv = ToTimeval(d);
01465   EXPECT_EQ(max_timeval_sec, tv.tv_sec);
01466   EXPECT_EQ(999998, tv.tv_usec);
01467   d += absl::Microseconds(1);
01468   tv = ToTimeval(d);
01469   EXPECT_EQ(max_timeval_sec, tv.tv_sec);
01470   EXPECT_EQ(999999, tv.tv_usec);
01471   d += absl::Microseconds(1);  // no effect
01472   tv = ToTimeval(d);
01473   EXPECT_EQ(max_timeval_sec, tv.tv_sec);
01474   EXPECT_EQ(999999, tv.tv_usec);
01475 
01476   tv.tv_sec = min_timeval_sec;
01477   tv.tv_usec = 1;
01478   d = absl::DurationFromTimeval(tv);
01479   tv = ToTimeval(d);
01480   EXPECT_EQ(min_timeval_sec, tv.tv_sec);
01481   EXPECT_EQ(1, tv.tv_usec);
01482   d -= absl::Microseconds(1);
01483   tv = ToTimeval(d);
01484   EXPECT_EQ(min_timeval_sec, tv.tv_sec);
01485   EXPECT_EQ(0, tv.tv_usec);
01486   d -= absl::Microseconds(1);  // no effect
01487   tv = ToTimeval(d);
01488   EXPECT_EQ(min_timeval_sec, tv.tv_sec);
01489   EXPECT_EQ(0, tv.tv_usec);
01490 
01491   const auto max_timespec_sec =
01492       std::numeric_limits<decltype(timespec::tv_sec)>::max();
01493   const auto min_timespec_sec =
01494       std::numeric_limits<decltype(timespec::tv_sec)>::min();
01495   timespec ts;
01496   ts.tv_sec = max_timespec_sec;
01497   ts.tv_nsec = 999999998;
01498   d = absl::DurationFromTimespec(ts);
01499   ts = absl::ToTimespec(d);
01500   EXPECT_EQ(max_timespec_sec, ts.tv_sec);
01501   EXPECT_EQ(999999998, ts.tv_nsec);
01502   d += absl::Nanoseconds(1);
01503   ts = absl::ToTimespec(d);
01504   EXPECT_EQ(max_timespec_sec, ts.tv_sec);
01505   EXPECT_EQ(999999999, ts.tv_nsec);
01506   d += absl::Nanoseconds(1);  // no effect
01507   ts = absl::ToTimespec(d);
01508   EXPECT_EQ(max_timespec_sec, ts.tv_sec);
01509   EXPECT_EQ(999999999, ts.tv_nsec);
01510 
01511   ts.tv_sec = min_timespec_sec;
01512   ts.tv_nsec = 1;
01513   d = absl::DurationFromTimespec(ts);
01514   ts = absl::ToTimespec(d);
01515   EXPECT_EQ(min_timespec_sec, ts.tv_sec);
01516   EXPECT_EQ(1, ts.tv_nsec);
01517   d -= absl::Nanoseconds(1);
01518   ts = absl::ToTimespec(d);
01519   EXPECT_EQ(min_timespec_sec, ts.tv_sec);
01520   EXPECT_EQ(0, ts.tv_nsec);
01521   d -= absl::Nanoseconds(1);  // no effect
01522   ts = absl::ToTimespec(d);
01523   EXPECT_EQ(min_timespec_sec, ts.tv_sec);
01524   EXPECT_EQ(0, ts.tv_nsec);
01525 }
01526 
01527 TEST(Duration, FormatDuration) {
01528   // Example from Go's docs.
01529   EXPECT_EQ("72h3m0.5s",
01530             absl::FormatDuration(absl::Hours(72) + absl::Minutes(3) +
01531                                  absl::Milliseconds(500)));
01532   // Go's largest time: 2540400h10m10.000000000s
01533   EXPECT_EQ("2540400h10m10s",
01534             absl::FormatDuration(absl::Hours(2540400) + absl::Minutes(10) +
01535                                  absl::Seconds(10)));
01536 
01537   EXPECT_EQ("0", absl::FormatDuration(absl::ZeroDuration()));
01538   EXPECT_EQ("0", absl::FormatDuration(absl::Seconds(0)));
01539   EXPECT_EQ("0", absl::FormatDuration(absl::Nanoseconds(0)));
01540 
01541   EXPECT_EQ("1ns", absl::FormatDuration(absl::Nanoseconds(1)));
01542   EXPECT_EQ("1us", absl::FormatDuration(absl::Microseconds(1)));
01543   EXPECT_EQ("1ms", absl::FormatDuration(absl::Milliseconds(1)));
01544   EXPECT_EQ("1s", absl::FormatDuration(absl::Seconds(1)));
01545   EXPECT_EQ("1m", absl::FormatDuration(absl::Minutes(1)));
01546   EXPECT_EQ("1h", absl::FormatDuration(absl::Hours(1)));
01547 
01548   EXPECT_EQ("1h1m", absl::FormatDuration(absl::Hours(1) + absl::Minutes(1)));
01549   EXPECT_EQ("1h1s", absl::FormatDuration(absl::Hours(1) + absl::Seconds(1)));
01550   EXPECT_EQ("1m1s", absl::FormatDuration(absl::Minutes(1) + absl::Seconds(1)));
01551 
01552   EXPECT_EQ("1h0.25s",
01553             absl::FormatDuration(absl::Hours(1) + absl::Milliseconds(250)));
01554   EXPECT_EQ("1m0.25s",
01555             absl::FormatDuration(absl::Minutes(1) + absl::Milliseconds(250)));
01556   EXPECT_EQ("1h1m0.25s",
01557             absl::FormatDuration(absl::Hours(1) + absl::Minutes(1) +
01558                                  absl::Milliseconds(250)));
01559   EXPECT_EQ("1h0.0005s",
01560             absl::FormatDuration(absl::Hours(1) + absl::Microseconds(500)));
01561   EXPECT_EQ("1h0.0000005s",
01562             absl::FormatDuration(absl::Hours(1) + absl::Nanoseconds(500)));
01563 
01564   // Subsecond special case.
01565   EXPECT_EQ("1.5ns", absl::FormatDuration(absl::Nanoseconds(1) +
01566                                           absl::Nanoseconds(1) / 2));
01567   EXPECT_EQ("1.25ns", absl::FormatDuration(absl::Nanoseconds(1) +
01568                                            absl::Nanoseconds(1) / 4));
01569   EXPECT_EQ("1ns", absl::FormatDuration(absl::Nanoseconds(1) +
01570                                         absl::Nanoseconds(1) / 9));
01571   EXPECT_EQ("1.2us", absl::FormatDuration(absl::Microseconds(1) +
01572                                           absl::Nanoseconds(200)));
01573   EXPECT_EQ("1.2ms", absl::FormatDuration(absl::Milliseconds(1) +
01574                                           absl::Microseconds(200)));
01575   EXPECT_EQ("1.0002ms", absl::FormatDuration(absl::Milliseconds(1) +
01576                                              absl::Nanoseconds(200)));
01577   EXPECT_EQ("1.00001ms", absl::FormatDuration(absl::Milliseconds(1) +
01578                                               absl::Nanoseconds(10)));
01579   EXPECT_EQ("1.000001ms",
01580             absl::FormatDuration(absl::Milliseconds(1) + absl::Nanoseconds(1)));
01581 
01582   // Negative durations.
01583   EXPECT_EQ("-1ns", absl::FormatDuration(absl::Nanoseconds(-1)));
01584   EXPECT_EQ("-1us", absl::FormatDuration(absl::Microseconds(-1)));
01585   EXPECT_EQ("-1ms", absl::FormatDuration(absl::Milliseconds(-1)));
01586   EXPECT_EQ("-1s", absl::FormatDuration(absl::Seconds(-1)));
01587   EXPECT_EQ("-1m", absl::FormatDuration(absl::Minutes(-1)));
01588   EXPECT_EQ("-1h", absl::FormatDuration(absl::Hours(-1)));
01589 
01590   EXPECT_EQ("-1h1m",
01591             absl::FormatDuration(-(absl::Hours(1) + absl::Minutes(1))));
01592   EXPECT_EQ("-1h1s",
01593             absl::FormatDuration(-(absl::Hours(1) + absl::Seconds(1))));
01594   EXPECT_EQ("-1m1s",
01595             absl::FormatDuration(-(absl::Minutes(1) + absl::Seconds(1))));
01596 
01597   EXPECT_EQ("-1ns", absl::FormatDuration(absl::Nanoseconds(-1)));
01598   EXPECT_EQ("-1.2us", absl::FormatDuration(
01599                           -(absl::Microseconds(1) + absl::Nanoseconds(200))));
01600   EXPECT_EQ("-1.2ms", absl::FormatDuration(
01601                           -(absl::Milliseconds(1) + absl::Microseconds(200))));
01602   EXPECT_EQ("-1.0002ms", absl::FormatDuration(-(absl::Milliseconds(1) +
01603                                                 absl::Nanoseconds(200))));
01604   EXPECT_EQ("-1.00001ms", absl::FormatDuration(-(absl::Milliseconds(1) +
01605                                                  absl::Nanoseconds(10))));
01606   EXPECT_EQ("-1.000001ms", absl::FormatDuration(-(absl::Milliseconds(1) +
01607                                                   absl::Nanoseconds(1))));
01608 
01609   //
01610   // Interesting corner cases.
01611   //
01612 
01613   const absl::Duration qns = absl::Nanoseconds(1) / 4;
01614   const absl::Duration max_dur =
01615       absl::Seconds(kint64max) + (absl::Seconds(1) - qns);
01616   const absl::Duration min_dur = absl::Seconds(kint64min);
01617 
01618   EXPECT_EQ("0.25ns", absl::FormatDuration(qns));
01619   EXPECT_EQ("-0.25ns", absl::FormatDuration(-qns));
01620   EXPECT_EQ("2562047788015215h30m7.99999999975s",
01621             absl::FormatDuration(max_dur));
01622   EXPECT_EQ("-2562047788015215h30m8s", absl::FormatDuration(min_dur));
01623 
01624   // Tests printing full precision from units that print using FDivDuration
01625   EXPECT_EQ("55.00000000025s", absl::FormatDuration(absl::Seconds(55) + qns));
01626   EXPECT_EQ("55.00000025ms",
01627             absl::FormatDuration(absl::Milliseconds(55) + qns));
01628   EXPECT_EQ("55.00025us", absl::FormatDuration(absl::Microseconds(55) + qns));
01629   EXPECT_EQ("55.25ns", absl::FormatDuration(absl::Nanoseconds(55) + qns));
01630 
01631   // Formatting infinity
01632   EXPECT_EQ("inf", absl::FormatDuration(absl::InfiniteDuration()));
01633   EXPECT_EQ("-inf", absl::FormatDuration(-absl::InfiniteDuration()));
01634 
01635   // Formatting approximately +/- 100 billion years
01636   const absl::Duration huge_range = ApproxYears(100000000000);
01637   EXPECT_EQ("876000000000000h", absl::FormatDuration(huge_range));
01638   EXPECT_EQ("-876000000000000h", absl::FormatDuration(-huge_range));
01639 
01640   EXPECT_EQ("876000000000000h0.999999999s",
01641             absl::FormatDuration(huge_range +
01642                                  (absl::Seconds(1) - absl::Nanoseconds(1))));
01643   EXPECT_EQ("876000000000000h0.9999999995s",
01644             absl::FormatDuration(
01645                 huge_range + (absl::Seconds(1) - absl::Nanoseconds(1) / 2)));
01646   EXPECT_EQ("876000000000000h0.99999999975s",
01647             absl::FormatDuration(
01648                 huge_range + (absl::Seconds(1) - absl::Nanoseconds(1) / 4)));
01649 
01650   EXPECT_EQ("-876000000000000h0.999999999s",
01651             absl::FormatDuration(-huge_range -
01652                                  (absl::Seconds(1) - absl::Nanoseconds(1))));
01653   EXPECT_EQ("-876000000000000h0.9999999995s",
01654             absl::FormatDuration(
01655                 -huge_range - (absl::Seconds(1) - absl::Nanoseconds(1) / 2)));
01656   EXPECT_EQ("-876000000000000h0.99999999975s",
01657             absl::FormatDuration(
01658                 -huge_range - (absl::Seconds(1) - absl::Nanoseconds(1) / 4)));
01659 }
01660 
01661 TEST(Duration, ParseDuration) {
01662   absl::Duration d;
01663 
01664   // No specified unit. Should only work for zero and infinity.
01665   EXPECT_TRUE(absl::ParseDuration("0", &d));
01666   EXPECT_EQ(absl::ZeroDuration(), d);
01667   EXPECT_TRUE(absl::ParseDuration("+0", &d));
01668   EXPECT_EQ(absl::ZeroDuration(), d);
01669   EXPECT_TRUE(absl::ParseDuration("-0", &d));
01670   EXPECT_EQ(absl::ZeroDuration(), d);
01671 
01672   EXPECT_TRUE(absl::ParseDuration("inf", &d));
01673   EXPECT_EQ(absl::InfiniteDuration(), d);
01674   EXPECT_TRUE(absl::ParseDuration("+inf", &d));
01675   EXPECT_EQ(absl::InfiniteDuration(), d);
01676   EXPECT_TRUE(absl::ParseDuration("-inf", &d));
01677   EXPECT_EQ(-absl::InfiniteDuration(), d);
01678   EXPECT_FALSE(absl::ParseDuration("infBlah", &d));
01679 
01680   // Illegal input forms.
01681   EXPECT_FALSE(absl::ParseDuration("", &d));
01682   EXPECT_FALSE(absl::ParseDuration("0.0", &d));
01683   EXPECT_FALSE(absl::ParseDuration(".0", &d));
01684   EXPECT_FALSE(absl::ParseDuration(".", &d));
01685   EXPECT_FALSE(absl::ParseDuration("01", &d));
01686   EXPECT_FALSE(absl::ParseDuration("1", &d));
01687   EXPECT_FALSE(absl::ParseDuration("-1", &d));
01688   EXPECT_FALSE(absl::ParseDuration("2", &d));
01689   EXPECT_FALSE(absl::ParseDuration("2 s", &d));
01690   EXPECT_FALSE(absl::ParseDuration(".s", &d));
01691   EXPECT_FALSE(absl::ParseDuration("-.s", &d));
01692   EXPECT_FALSE(absl::ParseDuration("s", &d));
01693   EXPECT_FALSE(absl::ParseDuration(" 2s", &d));
01694   EXPECT_FALSE(absl::ParseDuration("2s ", &d));
01695   EXPECT_FALSE(absl::ParseDuration(" 2s ", &d));
01696   EXPECT_FALSE(absl::ParseDuration("2mt", &d));
01697   EXPECT_FALSE(absl::ParseDuration("1e3s", &d));
01698 
01699   // One unit type.
01700   EXPECT_TRUE(absl::ParseDuration("1ns", &d));
01701   EXPECT_EQ(absl::Nanoseconds(1), d);
01702   EXPECT_TRUE(absl::ParseDuration("1us", &d));
01703   EXPECT_EQ(absl::Microseconds(1), d);
01704   EXPECT_TRUE(absl::ParseDuration("1ms", &d));
01705   EXPECT_EQ(absl::Milliseconds(1), d);
01706   EXPECT_TRUE(absl::ParseDuration("1s", &d));
01707   EXPECT_EQ(absl::Seconds(1), d);
01708   EXPECT_TRUE(absl::ParseDuration("2m", &d));
01709   EXPECT_EQ(absl::Minutes(2), d);
01710   EXPECT_TRUE(absl::ParseDuration("2h", &d));
01711   EXPECT_EQ(absl::Hours(2), d);
01712 
01713   // Huge counts of a unit.
01714   EXPECT_TRUE(absl::ParseDuration("9223372036854775807us", &d));
01715   EXPECT_EQ(absl::Microseconds(9223372036854775807), d);
01716   EXPECT_TRUE(absl::ParseDuration("-9223372036854775807us", &d));
01717   EXPECT_EQ(absl::Microseconds(-9223372036854775807), d);
01718 
01719   // Multiple units.
01720   EXPECT_TRUE(absl::ParseDuration("2h3m4s", &d));
01721   EXPECT_EQ(absl::Hours(2) + absl::Minutes(3) + absl::Seconds(4), d);
01722   EXPECT_TRUE(absl::ParseDuration("3m4s5us", &d));
01723   EXPECT_EQ(absl::Minutes(3) + absl::Seconds(4) + absl::Microseconds(5), d);
01724   EXPECT_TRUE(absl::ParseDuration("2h3m4s5ms6us7ns", &d));
01725   EXPECT_EQ(absl::Hours(2) + absl::Minutes(3) + absl::Seconds(4) +
01726                 absl::Milliseconds(5) + absl::Microseconds(6) +
01727                 absl::Nanoseconds(7),
01728             d);
01729 
01730   // Multiple units out of order.
01731   EXPECT_TRUE(absl::ParseDuration("2us3m4s5h", &d));
01732   EXPECT_EQ(absl::Hours(5) + absl::Minutes(3) + absl::Seconds(4) +
01733                 absl::Microseconds(2),
01734             d);
01735 
01736   // Fractional values of units.
01737   EXPECT_TRUE(absl::ParseDuration("1.5ns", &d));
01738   EXPECT_EQ(1.5 * absl::Nanoseconds(1), d);
01739   EXPECT_TRUE(absl::ParseDuration("1.5us", &d));
01740   EXPECT_EQ(1.5 * absl::Microseconds(1), d);
01741   EXPECT_TRUE(absl::ParseDuration("1.5ms", &d));
01742   EXPECT_EQ(1.5 * absl::Milliseconds(1), d);
01743   EXPECT_TRUE(absl::ParseDuration("1.5s", &d));
01744   EXPECT_EQ(1.5 * absl::Seconds(1), d);
01745   EXPECT_TRUE(absl::ParseDuration("1.5m", &d));
01746   EXPECT_EQ(1.5 * absl::Minutes(1), d);
01747   EXPECT_TRUE(absl::ParseDuration("1.5h", &d));
01748   EXPECT_EQ(1.5 * absl::Hours(1), d);
01749 
01750   // Huge fractional counts of a unit.
01751   EXPECT_TRUE(absl::ParseDuration("0.4294967295s", &d));
01752   EXPECT_EQ(absl::Nanoseconds(429496729) + absl::Nanoseconds(1) / 2, d);
01753   EXPECT_TRUE(absl::ParseDuration("0.429496729501234567890123456789s", &d));
01754   EXPECT_EQ(absl::Nanoseconds(429496729) + absl::Nanoseconds(1) / 2, d);
01755 
01756   // Negative durations.
01757   EXPECT_TRUE(absl::ParseDuration("-1s", &d));
01758   EXPECT_EQ(absl::Seconds(-1), d);
01759   EXPECT_TRUE(absl::ParseDuration("-1m", &d));
01760   EXPECT_EQ(absl::Minutes(-1), d);
01761   EXPECT_TRUE(absl::ParseDuration("-1h", &d));
01762   EXPECT_EQ(absl::Hours(-1), d);
01763 
01764   EXPECT_TRUE(absl::ParseDuration("-1h2s", &d));
01765   EXPECT_EQ(-(absl::Hours(1) + absl::Seconds(2)), d);
01766   EXPECT_FALSE(absl::ParseDuration("1h-2s", &d));
01767   EXPECT_FALSE(absl::ParseDuration("-1h-2s", &d));
01768   EXPECT_FALSE(absl::ParseDuration("-1h -2s", &d));
01769 }
01770 
01771 TEST(Duration, FormatParseRoundTrip) {
01772 #define TEST_PARSE_ROUNDTRIP(d)                \
01773   do {                                         \
01774     std::string s = absl::FormatDuration(d);   \
01775     absl::Duration dur;                        \
01776     EXPECT_TRUE(absl::ParseDuration(s, &dur)); \
01777     EXPECT_EQ(d, dur);                         \
01778   } while (0)
01779 
01780   TEST_PARSE_ROUNDTRIP(absl::Nanoseconds(1));
01781   TEST_PARSE_ROUNDTRIP(absl::Microseconds(1));
01782   TEST_PARSE_ROUNDTRIP(absl::Milliseconds(1));
01783   TEST_PARSE_ROUNDTRIP(absl::Seconds(1));
01784   TEST_PARSE_ROUNDTRIP(absl::Minutes(1));
01785   TEST_PARSE_ROUNDTRIP(absl::Hours(1));
01786   TEST_PARSE_ROUNDTRIP(absl::Hours(1) + absl::Nanoseconds(2));
01787 
01788   TEST_PARSE_ROUNDTRIP(absl::Nanoseconds(-1));
01789   TEST_PARSE_ROUNDTRIP(absl::Microseconds(-1));
01790   TEST_PARSE_ROUNDTRIP(absl::Milliseconds(-1));
01791   TEST_PARSE_ROUNDTRIP(absl::Seconds(-1));
01792   TEST_PARSE_ROUNDTRIP(absl::Minutes(-1));
01793   TEST_PARSE_ROUNDTRIP(absl::Hours(-1));
01794 
01795   TEST_PARSE_ROUNDTRIP(absl::Hours(-1) + absl::Nanoseconds(2));
01796   TEST_PARSE_ROUNDTRIP(absl::Hours(1) + absl::Nanoseconds(-2));
01797   TEST_PARSE_ROUNDTRIP(absl::Hours(-1) + absl::Nanoseconds(-2));
01798 
01799   TEST_PARSE_ROUNDTRIP(absl::Nanoseconds(1) +
01800                        absl::Nanoseconds(1) / 4);  // 1.25ns
01801 
01802   const absl::Duration huge_range = ApproxYears(100000000000);
01803   TEST_PARSE_ROUNDTRIP(huge_range);
01804   TEST_PARSE_ROUNDTRIP(huge_range + (absl::Seconds(1) - absl::Nanoseconds(1)));
01805 
01806 #undef TEST_PARSE_ROUNDTRIP
01807 }
01808 
01809 }  // namespace


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