Home | History | Annotate | Download | only in cctest
      1 // Copyright 2013 the V8 project authors. All rights reserved.
      2 // Redistribution and use in source and binary forms, with or without
      3 // modification, are permitted provided that the following conditions are
      4 // met:
      5 //
      6 //     * Redistributions of source code must retain the above copyright
      7 //       notice, this list of conditions and the following disclaimer.
      8 //     * Redistributions in binary form must reproduce the above
      9 //       copyright notice, this list of conditions and the following
     10 //       disclaimer in the documentation and/or other materials provided
     11 //       with the distribution.
     12 //     * Neither the name of Google Inc. nor the names of its
     13 //       contributors may be used to endorse or promote products derived
     14 //       from this software without specific prior written permission.
     15 //
     16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27 
     28 #include "src/v8.h"
     29 
     30 #if V8_OS_POSIX
     31 #include <sys/time.h>  // NOLINT
     32 #endif
     33 
     34 #include "test/cctest/cctest.h"
     35 #if V8_OS_WIN
     36 #include "src/base/win32-headers.h"
     37 #endif
     38 
     39 using namespace v8::internal;
     40 
     41 
     42 TEST(TimeDeltaFromAndIn) {
     43   CHECK(TimeDelta::FromDays(2) == TimeDelta::FromHours(48));
     44   CHECK(TimeDelta::FromHours(3) == TimeDelta::FromMinutes(180));
     45   CHECK(TimeDelta::FromMinutes(2) == TimeDelta::FromSeconds(120));
     46   CHECK(TimeDelta::FromSeconds(2) == TimeDelta::FromMilliseconds(2000));
     47   CHECK(TimeDelta::FromMilliseconds(2) == TimeDelta::FromMicroseconds(2000));
     48   CHECK_EQ(static_cast<int>(13), TimeDelta::FromDays(13).InDays());
     49   CHECK_EQ(static_cast<int>(13), TimeDelta::FromHours(13).InHours());
     50   CHECK_EQ(static_cast<int>(13), TimeDelta::FromMinutes(13).InMinutes());
     51   CHECK_EQ(static_cast<int64_t>(13), TimeDelta::FromSeconds(13).InSeconds());
     52   CHECK_EQ(13.0, TimeDelta::FromSeconds(13).InSecondsF());
     53   CHECK_EQ(static_cast<int64_t>(13),
     54            TimeDelta::FromMilliseconds(13).InMilliseconds());
     55   CHECK_EQ(13.0, TimeDelta::FromMilliseconds(13).InMillisecondsF());
     56   CHECK_EQ(static_cast<int64_t>(13),
     57            TimeDelta::FromMicroseconds(13).InMicroseconds());
     58 }
     59 
     60 
     61 #if V8_OS_MACOSX
     62 TEST(TimeDeltaFromMachTimespec) {
     63   TimeDelta null = TimeDelta();
     64   CHECK(null == TimeDelta::FromMachTimespec(null.ToMachTimespec()));
     65   TimeDelta delta1 = TimeDelta::FromMilliseconds(42);
     66   CHECK(delta1 == TimeDelta::FromMachTimespec(delta1.ToMachTimespec()));
     67   TimeDelta delta2 = TimeDelta::FromDays(42);
     68   CHECK(delta2 == TimeDelta::FromMachTimespec(delta2.ToMachTimespec()));
     69 }
     70 #endif
     71 
     72 
     73 TEST(TimeJsTime) {
     74   Time t = Time::FromJsTime(700000.3);
     75   CHECK_EQ(700000.3, t.ToJsTime());
     76 }
     77 
     78 
     79 #if V8_OS_POSIX
     80 TEST(TimeFromTimespec) {
     81   Time null;
     82   CHECK(null.IsNull());
     83   CHECK(null == Time::FromTimespec(null.ToTimespec()));
     84   Time now = Time::Now();
     85   CHECK(now == Time::FromTimespec(now.ToTimespec()));
     86   Time now_sys = Time::NowFromSystemTime();
     87   CHECK(now_sys == Time::FromTimespec(now_sys.ToTimespec()));
     88   Time unix_epoch = Time::UnixEpoch();
     89   CHECK(unix_epoch == Time::FromTimespec(unix_epoch.ToTimespec()));
     90   Time max = Time::Max();
     91   CHECK(max.IsMax());
     92   CHECK(max == Time::FromTimespec(max.ToTimespec()));
     93 }
     94 
     95 
     96 TEST(TimeFromTimeval) {
     97   Time null;
     98   CHECK(null.IsNull());
     99   CHECK(null == Time::FromTimeval(null.ToTimeval()));
    100   Time now = Time::Now();
    101   CHECK(now == Time::FromTimeval(now.ToTimeval()));
    102   Time now_sys = Time::NowFromSystemTime();
    103   CHECK(now_sys == Time::FromTimeval(now_sys.ToTimeval()));
    104   Time unix_epoch = Time::UnixEpoch();
    105   CHECK(unix_epoch == Time::FromTimeval(unix_epoch.ToTimeval()));
    106   Time max = Time::Max();
    107   CHECK(max.IsMax());
    108   CHECK(max == Time::FromTimeval(max.ToTimeval()));
    109 }
    110 #endif
    111 
    112 
    113 #if V8_OS_WIN
    114 TEST(TimeFromFiletime) {
    115   Time null;
    116   CHECK(null.IsNull());
    117   CHECK(null == Time::FromFiletime(null.ToFiletime()));
    118   Time now = Time::Now();
    119   CHECK(now == Time::FromFiletime(now.ToFiletime()));
    120   Time now_sys = Time::NowFromSystemTime();
    121   CHECK(now_sys == Time::FromFiletime(now_sys.ToFiletime()));
    122   Time unix_epoch = Time::UnixEpoch();
    123   CHECK(unix_epoch == Time::FromFiletime(unix_epoch.ToFiletime()));
    124   Time max = Time::Max();
    125   CHECK(max.IsMax());
    126   CHECK(max == Time::FromFiletime(max.ToFiletime()));
    127 }
    128 #endif
    129 
    130 
    131 TEST(TimeTicksIsMonotonic) {
    132   TimeTicks previous_normal_ticks;
    133   TimeTicks previous_highres_ticks;
    134   ElapsedTimer timer;
    135   timer.Start();
    136   while (!timer.HasExpired(TimeDelta::FromMilliseconds(100))) {
    137     TimeTicks normal_ticks = TimeTicks::Now();
    138     TimeTicks highres_ticks = TimeTicks::HighResolutionNow();
    139     CHECK_GE(normal_ticks, previous_normal_ticks);
    140     CHECK_GE((normal_ticks - previous_normal_ticks).InMicroseconds(), 0);
    141     CHECK_GE(highres_ticks, previous_highres_ticks);
    142     CHECK_GE((highres_ticks - previous_highres_ticks).InMicroseconds(), 0);
    143     previous_normal_ticks = normal_ticks;
    144     previous_highres_ticks = highres_ticks;
    145   }
    146 }
    147 
    148 
    149 template <typename T>
    150 static void ResolutionTest(T (*Now)(), TimeDelta target_granularity) {
    151   // We're trying to measure that intervals increment in a VERY small amount
    152   // of time -- according to the specified target granularity. Unfortunately,
    153   // if we happen to have a context switch in the middle of our test, the
    154   // context switch could easily exceed our limit. So, we iterate on this
    155   // several times. As long as we're able to detect the fine-granularity
    156   // timers at least once, then the test has succeeded.
    157   static const TimeDelta kExpirationTimeout = TimeDelta::FromSeconds(1);
    158   ElapsedTimer timer;
    159   timer.Start();
    160   TimeDelta delta;
    161   do {
    162     T start = Now();
    163     T now = start;
    164     // Loop until we can detect that the clock has changed. Non-HighRes timers
    165     // will increment in chunks, i.e. 15ms. By spinning until we see a clock
    166     // change, we detect the minimum time between measurements.
    167     do {
    168       now = Now();
    169       delta = now - start;
    170     } while (now <= start);
    171     CHECK_NE(static_cast<int64_t>(0), delta.InMicroseconds());
    172   } while (delta > target_granularity && !timer.HasExpired(kExpirationTimeout));
    173   CHECK_LE(delta, target_granularity);
    174 }
    175 
    176 
    177 TEST(TimeNowResolution) {
    178   // We assume that Time::Now() has at least 16ms resolution.
    179   static const TimeDelta kTargetGranularity = TimeDelta::FromMilliseconds(16);
    180   ResolutionTest<Time>(&Time::Now, kTargetGranularity);
    181 }
    182 
    183 
    184 TEST(TimeTicksNowResolution) {
    185   // We assume that TimeTicks::Now() has at least 16ms resolution.
    186   static const TimeDelta kTargetGranularity = TimeDelta::FromMilliseconds(16);
    187   ResolutionTest<TimeTicks>(&TimeTicks::Now, kTargetGranularity);
    188 }
    189 
    190 
    191 TEST(TimeTicksHighResolutionNowResolution) {
    192   if (!TimeTicks::IsHighResolutionClockWorking()) return;
    193 
    194   // We assume that TimeTicks::HighResolutionNow() has sub-ms resolution.
    195   static const TimeDelta kTargetGranularity = TimeDelta::FromMilliseconds(1);
    196   ResolutionTest<TimeTicks>(&TimeTicks::HighResolutionNow, kTargetGranularity);
    197 }
    198