The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.

Please select the desired protocol below to get the URL.

This URL has Read-Only access.

Statistics
| Branch: | Revision:

main_repo / deps / v8 / test / cctest / test-time.cc @ f230a1cf

History | View | Annotate | Download (7.31 KB)

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 <cstdlib>
29

    
30
#include "v8.h"
31

    
32
#include "cctest.h"
33
#if V8_OS_WIN
34
#include "win32-headers.h"
35
#endif
36

    
37
using namespace v8::internal;
38

    
39

    
40
TEST(TimeDeltaFromAndIn) {
41
  CHECK(TimeDelta::FromDays(2) == TimeDelta::FromHours(48));
42
  CHECK(TimeDelta::FromHours(3) == TimeDelta::FromMinutes(180));
43
  CHECK(TimeDelta::FromMinutes(2) == TimeDelta::FromSeconds(120));
44
  CHECK(TimeDelta::FromSeconds(2) == TimeDelta::FromMilliseconds(2000));
45
  CHECK(TimeDelta::FromMilliseconds(2) == TimeDelta::FromMicroseconds(2000));
46
  CHECK_EQ(static_cast<int>(13), TimeDelta::FromDays(13).InDays());
47
  CHECK_EQ(static_cast<int>(13), TimeDelta::FromHours(13).InHours());
48
  CHECK_EQ(static_cast<int>(13), TimeDelta::FromMinutes(13).InMinutes());
49
  CHECK_EQ(static_cast<int64_t>(13), TimeDelta::FromSeconds(13).InSeconds());
50
  CHECK_EQ(13.0, TimeDelta::FromSeconds(13).InSecondsF());
51
  CHECK_EQ(static_cast<int64_t>(13),
52
           TimeDelta::FromMilliseconds(13).InMilliseconds());
53
  CHECK_EQ(13.0, TimeDelta::FromMilliseconds(13).InMillisecondsF());
54
  CHECK_EQ(static_cast<int64_t>(13),
55
           TimeDelta::FromMicroseconds(13).InMicroseconds());
56
}
57

    
58

    
59
#if V8_OS_MACOSX
60
TEST(TimeDeltaFromMachTimespec) {
61
  TimeDelta null = TimeDelta();
62
  CHECK(null == TimeDelta::FromMachTimespec(null.ToMachTimespec()));
63
  TimeDelta delta1 = TimeDelta::FromMilliseconds(42);
64
  CHECK(delta1 == TimeDelta::FromMachTimespec(delta1.ToMachTimespec()));
65
  TimeDelta delta2 = TimeDelta::FromDays(42);
66
  CHECK(delta2 == TimeDelta::FromMachTimespec(delta2.ToMachTimespec()));
67
}
68
#endif
69

    
70

    
71
TEST(TimeJsTime) {
72
  Time t = Time::FromJsTime(700000.3);
73
  CHECK_EQ(700000.3, t.ToJsTime());
74
}
75

    
76

    
77
#if V8_OS_POSIX
78
TEST(TimeFromTimespec) {
79
  Time null;
80
  CHECK(null.IsNull());
81
  CHECK(null == Time::FromTimespec(null.ToTimespec()));
82
  Time now = Time::Now();
83
  CHECK(now == Time::FromTimespec(now.ToTimespec()));
84
  Time now_sys = Time::NowFromSystemTime();
85
  CHECK(now_sys == Time::FromTimespec(now_sys.ToTimespec()));
86
  Time unix_epoch = Time::UnixEpoch();
87
  CHECK(unix_epoch == Time::FromTimespec(unix_epoch.ToTimespec()));
88
  Time max = Time::Max();
89
  CHECK(max.IsMax());
90
  CHECK(max == Time::FromTimespec(max.ToTimespec()));
91
}
92

    
93

    
94
TEST(TimeFromTimeval) {
95
  Time null;
96
  CHECK(null.IsNull());
97
  CHECK(null == Time::FromTimeval(null.ToTimeval()));
98
  Time now = Time::Now();
99
  CHECK(now == Time::FromTimeval(now.ToTimeval()));
100
  Time now_sys = Time::NowFromSystemTime();
101
  CHECK(now_sys == Time::FromTimeval(now_sys.ToTimeval()));
102
  Time unix_epoch = Time::UnixEpoch();
103
  CHECK(unix_epoch == Time::FromTimeval(unix_epoch.ToTimeval()));
104
  Time max = Time::Max();
105
  CHECK(max.IsMax());
106
  CHECK(max == Time::FromTimeval(max.ToTimeval()));
107
}
108
#endif
109

    
110

    
111
#if V8_OS_WIN
112
TEST(TimeFromFiletime) {
113
  Time null;
114
  CHECK(null.IsNull());
115
  CHECK(null == Time::FromFiletime(null.ToFiletime()));
116
  Time now = Time::Now();
117
  CHECK(now == Time::FromFiletime(now.ToFiletime()));
118
  Time now_sys = Time::NowFromSystemTime();
119
  CHECK(now_sys == Time::FromFiletime(now_sys.ToFiletime()));
120
  Time unix_epoch = Time::UnixEpoch();
121
  CHECK(unix_epoch == Time::FromFiletime(unix_epoch.ToFiletime()));
122
  Time max = Time::Max();
123
  CHECK(max.IsMax());
124
  CHECK(max == Time::FromFiletime(max.ToFiletime()));
125
}
126
#endif
127

    
128

    
129
TEST(TimeTicksIsMonotonic) {
130
  TimeTicks previous_normal_ticks;
131
  TimeTicks previous_highres_ticks;
132
  ElapsedTimer timer;
133
  timer.Start();
134
  while (!timer.HasExpired(TimeDelta::FromMilliseconds(100))) {
135
    TimeTicks normal_ticks = TimeTicks::Now();
136
    TimeTicks highres_ticks = TimeTicks::HighResolutionNow();
137
    CHECK_GE(normal_ticks, previous_normal_ticks);
138
    CHECK_GE((normal_ticks - previous_normal_ticks).InMicroseconds(), 0);
139
    CHECK_GE(highres_ticks, previous_highres_ticks);
140
    CHECK_GE((highres_ticks - previous_highres_ticks).InMicroseconds(), 0);
141
    previous_normal_ticks = normal_ticks;
142
    previous_highres_ticks = highres_ticks;
143
  }
144
}
145

    
146

    
147
template <typename T>
148
static void ResolutionTest(T (*Now)(), TimeDelta target_granularity) {
149
  // We're trying to measure that intervals increment in a VERY small amount
150
  // of time -- according to the specified target granularity. Unfortunately,
151
  // if we happen to have a context switch in the middle of our test, the
152
  // context switch could easily exceed our limit. So, we iterate on this
153
  // several times. As long as we're able to detect the fine-granularity
154
  // timers at least once, then the test has succeeded.
155
  static const TimeDelta kExpirationTimeout = TimeDelta::FromSeconds(1);
156
  ElapsedTimer timer;
157
  timer.Start();
158
  TimeDelta delta;
159
  do {
160
    T start = Now();
161
    T now = start;
162
    // Loop until we can detect that the clock has changed. Non-HighRes timers
163
    // will increment in chunks, i.e. 15ms. By spinning until we see a clock
164
    // change, we detect the minimum time between measurements.
165
    do {
166
      now = Now();
167
      delta = now - start;
168
    } while (now <= start);
169
    CHECK_NE(static_cast<int64_t>(0), delta.InMicroseconds());
170
  } while (delta > target_granularity && !timer.HasExpired(kExpirationTimeout));
171
  CHECK_LE(delta, target_granularity);
172
}
173

    
174

    
175
TEST(TimeNowResolution) {
176
  // We assume that Time::Now() has at least 16ms resolution.
177
  static const TimeDelta kTargetGranularity = TimeDelta::FromMilliseconds(16);
178
  ResolutionTest<Time>(&Time::Now, kTargetGranularity);
179
}
180

    
181

    
182
TEST(TimeTicksNowResolution) {
183
  // We assume that TimeTicks::Now() has at least 16ms resolution.
184
  static const TimeDelta kTargetGranularity = TimeDelta::FromMilliseconds(16);
185
  ResolutionTest<TimeTicks>(&TimeTicks::Now, kTargetGranularity);
186
}
187

    
188

    
189
TEST(TimeTicksHighResolutionNowResolution) {
190
  if (!TimeTicks::IsHighResolutionClockWorking()) return;
191

    
192
  // We assume that TimeTicks::HighResolutionNow() has sub-ms resolution.
193
  static const TimeDelta kTargetGranularity = TimeDelta::FromMilliseconds(1);
194
  ResolutionTest<TimeTicks>(&TimeTicks::HighResolutionNow, kTargetGranularity);
195
}