// Copyright 2014 the V8 project authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "src/base/platform/time.h" #if V8_OS_MACOSX #include #endif #if V8_OS_POSIX #include #endif #if V8_OS_WIN #include "src/base/win32-headers.h" #endif #include #include "src/base/platform/elapsed-timer.h" #include "src/base/platform/platform.h" #include "testing/gtest/include/gtest/gtest.h" namespace v8 { namespace base { TEST(TimeDelta, ZeroMinMax) { constexpr TimeDelta kZero; static_assert(kZero.IsZero(), ""); constexpr TimeDelta kMax = TimeDelta::Max(); static_assert(kMax.IsMax(), ""); static_assert(kMax == TimeDelta::Max(), ""); EXPECT_GT(kMax, TimeDelta::FromDays(100 * 365)); static_assert(kMax > kZero, ""); constexpr TimeDelta kMin = TimeDelta::Min(); static_assert(kMin.IsMin(), ""); static_assert(kMin == TimeDelta::Min(), ""); EXPECT_LT(kMin, TimeDelta::FromDays(-100 * 365)); static_assert(kMin < kZero, ""); } TEST(TimeDelta, MaxConversions) { // static_assert also confirms constexpr works as intended. constexpr TimeDelta kMax = TimeDelta::Max(); EXPECT_EQ(kMax.InDays(), std::numeric_limits::max()); EXPECT_EQ(kMax.InHours(), std::numeric_limits::max()); EXPECT_EQ(kMax.InMinutes(), std::numeric_limits::max()); EXPECT_EQ(kMax.InSecondsF(), std::numeric_limits::infinity()); EXPECT_EQ(kMax.InSeconds(), std::numeric_limits::max()); EXPECT_EQ(kMax.InMillisecondsF(), std::numeric_limits::infinity()); EXPECT_EQ(kMax.InMilliseconds(), std::numeric_limits::max()); EXPECT_EQ(kMax.InMillisecondsRoundedUp(), std::numeric_limits::max()); // TODO(v8-team): Import overflow support from Chromium's base. // EXPECT_TRUE(TimeDelta::FromDays(std::numeric_limits::max()).IsMax()); // EXPECT_TRUE( // TimeDelta::FromHours(std::numeric_limits::max()).IsMax()); // EXPECT_TRUE( // TimeDelta::FromMinutes(std::numeric_limits::max()).IsMax()); // constexpr int64_t max_int = std::numeric_limits::max(); // constexpr int64_t min_int = std::numeric_limits::min(); // EXPECT_TRUE( // TimeDelta::FromSeconds(max_int / Time::kMicrosecondsPerSecond + 1) // .IsMax()); // EXPECT_TRUE(TimeDelta::FromMilliseconds( // max_int / Time::kMillisecondsPerSecond + 1) // .IsMax()); // EXPECT_TRUE(TimeDelta::FromMicroseconds(max_int).IsMax()); // EXPECT_TRUE( // TimeDelta::FromSeconds(min_int / Time::kMicrosecondsPerSecond - 1) // .IsMin()); // EXPECT_TRUE(TimeDelta::FromMilliseconds( // min_int / Time::kMillisecondsPerSecond - 1) // .IsMin()); // EXPECT_TRUE(TimeDelta::FromMicroseconds(min_int).IsMin()); // EXPECT_TRUE( // TimeDelta::FromMicroseconds(std::numeric_limits::min()) // .IsMin()); } TEST(TimeDelta, NumericOperators) { constexpr int i = 2; EXPECT_EQ(TimeDelta::FromMilliseconds(2000), (TimeDelta::FromMilliseconds(1000) * i)); EXPECT_EQ(TimeDelta::FromMilliseconds(500), (TimeDelta::FromMilliseconds(1000) / i)); EXPECT_EQ(TimeDelta::FromMilliseconds(2000), (TimeDelta::FromMilliseconds(1000) *= i)); EXPECT_EQ(TimeDelta::FromMilliseconds(500), (TimeDelta::FromMilliseconds(1000) /= i)); constexpr int64_t i64 = 2; EXPECT_EQ(TimeDelta::FromMilliseconds(2000), (TimeDelta::FromMilliseconds(1000) * i64)); EXPECT_EQ(TimeDelta::FromMilliseconds(500), (TimeDelta::FromMilliseconds(1000) / i64)); EXPECT_EQ(TimeDelta::FromMilliseconds(2000), (TimeDelta::FromMilliseconds(1000) *= i64)); EXPECT_EQ(TimeDelta::FromMilliseconds(500), (TimeDelta::FromMilliseconds(1000) /= i64)); EXPECT_EQ(TimeDelta::FromMilliseconds(2000), (TimeDelta::FromMilliseconds(1000) * 2)); EXPECT_EQ(TimeDelta::FromMilliseconds(500), (TimeDelta::FromMilliseconds(1000) / 2)); EXPECT_EQ(TimeDelta::FromMilliseconds(2000), (TimeDelta::FromMilliseconds(1000) *= 2)); EXPECT_EQ(TimeDelta::FromMilliseconds(500), (TimeDelta::FromMilliseconds(1000) /= 2)); } // TODO(v8-team): Import support for overflow from Chromium's base. TEST(TimeDelta, DISABLED_Overflows) { // Some sanity checks. static_assert's used were possible to verify constexpr // evaluation at the same time. static_assert(TimeDelta::Max().IsMax(), ""); static_assert(-TimeDelta::Max() < TimeDelta(), ""); static_assert(-TimeDelta::Max() > TimeDelta::Min(), ""); static_assert(TimeDelta() > -TimeDelta::Max(), ""); TimeDelta large_delta = TimeDelta::Max() - TimeDelta::FromMilliseconds(1); TimeDelta large_negative = -large_delta; EXPECT_GT(TimeDelta(), large_negative); EXPECT_FALSE(large_delta.IsMax()); EXPECT_FALSE((-large_negative).IsMin()); const TimeDelta kOneSecond = TimeDelta::FromSeconds(1); // Test +, -, * and / operators. EXPECT_TRUE((large_delta + kOneSecond).IsMax()); EXPECT_TRUE((large_negative + (-kOneSecond)).IsMin()); EXPECT_TRUE((large_negative - kOneSecond).IsMin()); EXPECT_TRUE((large_delta - (-kOneSecond)).IsMax()); EXPECT_TRUE((large_delta * 2).IsMax()); EXPECT_TRUE((large_delta * -2).IsMin()); // Test +=, -=, *= and /= operators. TimeDelta delta = large_delta; delta += kOneSecond; EXPECT_TRUE(delta.IsMax()); delta = large_negative; delta += -kOneSecond; EXPECT_TRUE((delta).IsMin()); delta = large_negative; delta -= kOneSecond; EXPECT_TRUE((delta).IsMin()); delta = large_delta; delta -= -kOneSecond; EXPECT_TRUE(delta.IsMax()); delta = large_delta; delta *= 2; EXPECT_TRUE(delta.IsMax()); // Test operations with Time and TimeTicks. EXPECT_TRUE((large_delta + Time::Now()).IsMax()); EXPECT_TRUE((large_delta + TimeTicks::Now()).IsMax()); EXPECT_TRUE((Time::Now() + large_delta).IsMax()); EXPECT_TRUE((TimeTicks::Now() + large_delta).IsMax()); Time time_now = Time::Now(); EXPECT_EQ(kOneSecond, (time_now + kOneSecond) - time_now); EXPECT_EQ(-kOneSecond, (time_now - kOneSecond) - time_now); TimeTicks ticks_now = TimeTicks::Now(); EXPECT_EQ(-kOneSecond, (ticks_now - kOneSecond) - ticks_now); EXPECT_EQ(kOneSecond, (ticks_now + kOneSecond) - ticks_now); } TEST(TimeDelta, FromAndIn) { EXPECT_EQ(TimeDelta::FromDays(2), TimeDelta::FromHours(48)); EXPECT_EQ(TimeDelta::FromHours(3), TimeDelta::FromMinutes(180)); EXPECT_EQ(TimeDelta::FromMinutes(2), TimeDelta::FromSeconds(120)); EXPECT_EQ(TimeDelta::FromSeconds(2), TimeDelta::FromMilliseconds(2000)); EXPECT_EQ(TimeDelta::FromMilliseconds(2), TimeDelta::FromMicroseconds(2000)); EXPECT_EQ(static_cast(13), TimeDelta::FromDays(13).InDays()); EXPECT_EQ(static_cast(13), TimeDelta::FromHours(13).InHours()); EXPECT_EQ(static_cast(13), TimeDelta::FromMinutes(13).InMinutes()); EXPECT_EQ(static_cast(13), TimeDelta::FromSeconds(13).InSeconds()); EXPECT_DOUBLE_EQ(13.0, TimeDelta::FromSeconds(13).InSecondsF()); EXPECT_EQ(static_cast(13), TimeDelta::FromMilliseconds(13).InMilliseconds()); EXPECT_DOUBLE_EQ(13.0, TimeDelta::FromMilliseconds(13).InMillisecondsF()); EXPECT_EQ(static_cast(13), TimeDelta::FromMicroseconds(13).InMicroseconds()); } #if V8_OS_MACOSX TEST(TimeDelta, MachTimespec) { TimeDelta null = TimeDelta(); EXPECT_EQ(null, TimeDelta::FromMachTimespec(null.ToMachTimespec())); TimeDelta delta1 = TimeDelta::FromMilliseconds(42); EXPECT_EQ(delta1, TimeDelta::FromMachTimespec(delta1.ToMachTimespec())); TimeDelta delta2 = TimeDelta::FromDays(42); EXPECT_EQ(delta2, TimeDelta::FromMachTimespec(delta2.ToMachTimespec())); } #endif TEST(Time, Max) { Time max = Time::Max(); EXPECT_TRUE(max.IsMax()); EXPECT_EQ(max, Time::Max()); EXPECT_GT(max, Time::Now()); EXPECT_GT(max, Time()); } TEST(Time, MaxConversions) { Time t = Time::Max(); EXPECT_EQ(std::numeric_limits::max(), t.ToInternalValue()); // TODO(v8-team): Time::FromJsTime() overflows with infinity. Import support // from Chromium's base. // t = Time::FromJsTime(std::numeric_limits::infinity()); // EXPECT_TRUE(t.IsMax()); // EXPECT_EQ(std::numeric_limits::infinity(), t.ToJsTime()); #if defined(OS_POSIX) struct timeval tval; tval.tv_sec = std::numeric_limits::max(); tval.tv_usec = static_cast(Time::kMicrosecondsPerSecond) - 1; t = Time::FromTimeVal(tval); EXPECT_TRUE(t.IsMax()); tval = t.ToTimeVal(); EXPECT_EQ(std::numeric_limits::max(), tval.tv_sec); EXPECT_EQ(static_cast(Time::kMicrosecondsPerSecond) - 1, tval.tv_usec); #endif #if defined(OS_WIN) FILETIME ftime; ftime.dwHighDateTime = std::numeric_limits::max(); ftime.dwLowDateTime = std::numeric_limits::max(); t = Time::FromFileTime(ftime); EXPECT_TRUE(t.IsMax()); ftime = t.ToFileTime(); EXPECT_EQ(std::numeric_limits::max(), ftime.dwHighDateTime); EXPECT_EQ(std::numeric_limits::max(), ftime.dwLowDateTime); #endif } TEST(Time, JsTime) { Time t = Time::FromJsTime(700000.3); EXPECT_DOUBLE_EQ(700000.3, t.ToJsTime()); } #if V8_OS_POSIX TEST(Time, Timespec) { Time null; EXPECT_TRUE(null.IsNull()); EXPECT_EQ(null, Time::FromTimespec(null.ToTimespec())); Time now = Time::Now(); EXPECT_EQ(now, Time::FromTimespec(now.ToTimespec())); Time now_sys = Time::NowFromSystemTime(); EXPECT_EQ(now_sys, Time::FromTimespec(now_sys.ToTimespec())); Time unix_epoch = Time::UnixEpoch(); EXPECT_EQ(unix_epoch, Time::FromTimespec(unix_epoch.ToTimespec())); Time max = Time::Max(); EXPECT_TRUE(max.IsMax()); EXPECT_EQ(max, Time::FromTimespec(max.ToTimespec())); } TEST(Time, Timeval) { Time null; EXPECT_TRUE(null.IsNull()); EXPECT_EQ(null, Time::FromTimeval(null.ToTimeval())); Time now = Time::Now(); EXPECT_EQ(now, Time::FromTimeval(now.ToTimeval())); Time now_sys = Time::NowFromSystemTime(); EXPECT_EQ(now_sys, Time::FromTimeval(now_sys.ToTimeval())); Time unix_epoch = Time::UnixEpoch(); EXPECT_EQ(unix_epoch, Time::FromTimeval(unix_epoch.ToTimeval())); Time max = Time::Max(); EXPECT_TRUE(max.IsMax()); EXPECT_EQ(max, Time::FromTimeval(max.ToTimeval())); } #endif #if V8_OS_WIN TEST(Time, Filetime) { Time null; EXPECT_TRUE(null.IsNull()); EXPECT_EQ(null, Time::FromFiletime(null.ToFiletime())); Time now = Time::Now(); EXPECT_EQ(now, Time::FromFiletime(now.ToFiletime())); Time now_sys = Time::NowFromSystemTime(); EXPECT_EQ(now_sys, Time::FromFiletime(now_sys.ToFiletime())); Time unix_epoch = Time::UnixEpoch(); EXPECT_EQ(unix_epoch, Time::FromFiletime(unix_epoch.ToFiletime())); Time max = Time::Max(); EXPECT_TRUE(max.IsMax()); EXPECT_EQ(max, Time::FromFiletime(max.ToFiletime())); } #endif namespace { template static void ResolutionTest(T (*Now)(), TimeDelta target_granularity) { // We're trying to measure that intervals increment in a VERY small amount // of time -- according to the specified target granularity. Unfortunately, // if we happen to have a context switch in the middle of our test, the // context switch could easily exceed our limit. So, we iterate on this // several times. As long as we're able to detect the fine-granularity // timers at least once, then the test has succeeded. static const TimeDelta kExpirationTimeout = TimeDelta::FromSeconds(1); ElapsedTimer timer; timer.Start(); TimeDelta delta; do { T start = Now(); T now = start; // Loop until we can detect that the clock has changed. Non-HighRes timers // will increment in chunks, i.e. 15ms. By spinning until we see a clock // change, we detect the minimum time between measurements. do { now = Now(); delta = now - start; } while (now <= start); EXPECT_NE(static_cast(0), delta.InMicroseconds()); } while (delta > target_granularity && !timer.HasExpired(kExpirationTimeout)); EXPECT_LE(delta, target_granularity); } } // namespace TEST(Time, NowResolution) { // We assume that Time::Now() has at least 16ms resolution. static const TimeDelta kTargetGranularity = TimeDelta::FromMilliseconds(16); ResolutionTest