1/*
2 * Copyright (C) 2016 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include <wtf/ClockType.h>
28#include <wtf/MonotonicTime.h>
29#include <wtf/Seconds.h>
30#include <wtf/StringPrintStream.h>
31#include <wtf/TimeWithDynamicClockType.h>
32#include <wtf/WallTime.h>
33
34namespace WTF {
35
36std::basic_ostream<char>& operator<<(std::basic_ostream<char>& out, Seconds value)
37{
38 out << toCString(value).data();
39 return out;
40}
41
42std::basic_ostream<char>& operator<<(std::basic_ostream<char>& out, WallTime value)
43{
44 out << toCString(value).data();
45 return out;
46}
47
48std::basic_ostream<char>& operator<<(std::basic_ostream<char>& out, MonotonicTime value)
49{
50 out << toCString(value).data();
51 return out;
52}
53
54std::basic_ostream<char>& operator<<(std::basic_ostream<char>& out, TimeWithDynamicClockType value)
55{
56 out << toCString(value).data();
57 return out;
58}
59
60} // namespace WTF
61
62namespace TestWebKitAPI {
63
64namespace {
65
66Seconds s(double value)
67{
68 return Seconds(value);
69}
70
71WallTime wt(double value)
72{
73 return WallTime::fromRawSeconds(value);
74}
75
76MonotonicTime mt(double value)
77{
78 return MonotonicTime::fromRawSeconds(value);
79}
80
81TimeWithDynamicClockType dt(double value, ClockType type)
82{
83 return TimeWithDynamicClockType::fromRawSeconds(value, type);
84}
85
86TimeWithDynamicClockType dtw(double value)
87{
88 return dt(value, ClockType::Wall);
89}
90
91TimeWithDynamicClockType dtm(double value)
92{
93 return dt(value, ClockType::Monotonic);
94}
95
96} // anonymous namespace
97
98TEST(WTF_Time, units)
99{
100 EXPECT_EQ(s(60), Seconds::fromMinutes(1));
101 EXPECT_EQ(s(0.001), Seconds::fromMilliseconds(1));
102 EXPECT_EQ(s(0.000001), Seconds::fromMicroseconds(1));
103 EXPECT_EQ(s(0.0000005), Seconds::fromNanoseconds(500));
104
105 EXPECT_EQ(s(120).minutes(), 2);
106 EXPECT_EQ(s(2).seconds(), 2);
107 EXPECT_EQ(s(2).milliseconds(), 2000);
108 EXPECT_EQ(s(2).microseconds(), 2000000);
109 EXPECT_EQ(s(2).nanoseconds(), 2000000000);
110}
111
112TEST(WTF_Time, plus)
113{
114 EXPECT_EQ(s(6), s(1) + s(5));
115 EXPECT_EQ(s(6), s(5) + s(1));
116 EXPECT_EQ(wt(6), s(1) + wt(5));
117 EXPECT_EQ(wt(6), s(5) + wt(1));
118 EXPECT_EQ(wt(6), wt(1) + s(5));
119 EXPECT_EQ(wt(6), wt(5) + s(1));
120 EXPECT_EQ(mt(6), s(1) + mt(5));
121 EXPECT_EQ(mt(6), s(5) + mt(1));
122 EXPECT_EQ(mt(6), mt(1) + s(5));
123 EXPECT_EQ(mt(6), mt(5) + s(1));
124 EXPECT_EQ(dtw(6), s(1) + dtw(5));
125 EXPECT_EQ(dtw(6), s(5) + dtw(1));
126 EXPECT_EQ(dtw(6), dtw(1) + s(5));
127 EXPECT_EQ(dtw(6), dtw(5) + s(1));
128 EXPECT_EQ(dtm(6), s(1) + dtm(5));
129 EXPECT_EQ(dtm(6), s(5) + dtm(1));
130 EXPECT_EQ(dtm(6), dtm(1) + s(5));
131 EXPECT_EQ(dtm(6), dtm(5) + s(1));
132}
133
134TEST(WTF_Time, minus)
135{
136 EXPECT_EQ(s(-4), s(1) - s(5));
137 EXPECT_EQ(s(4), s(5) - s(1));
138 EXPECT_EQ(wt(-4), s(1) - wt(5));
139 EXPECT_EQ(wt(4), s(5) - wt(1));
140 EXPECT_EQ(wt(-4), wt(1) - s(5));
141 EXPECT_EQ(wt(4), wt(5) - s(1));
142 EXPECT_EQ(mt(-4), s(1) - mt(5));
143 EXPECT_EQ(mt(4), s(5) - mt(1));
144 EXPECT_EQ(mt(-4), mt(1) - s(5));
145 EXPECT_EQ(mt(4), mt(5) - s(1));
146 EXPECT_EQ(dtw(-4), s(1) - dtw(5));
147 EXPECT_EQ(dtw(4), s(5) - dtw(1));
148 EXPECT_EQ(dtw(-4), dtw(1) - s(5));
149 EXPECT_EQ(dtw(4), dtw(5) - s(1));
150 EXPECT_EQ(dtm(-4), s(1) - dtm(5));
151 EXPECT_EQ(dtm(4), s(5) - dtm(1));
152 EXPECT_EQ(dtm(-4), dtm(1) - s(5));
153 EXPECT_EQ(dtm(4), dtm(5) - s(1));
154}
155
156TEST(WTF_Time, negate)
157{
158 EXPECT_EQ(s(-7), -s(7));
159 EXPECT_EQ(s(7), -s(-7));
160 EXPECT_EQ(wt(-7), -wt(7));
161 EXPECT_EQ(wt(7), -wt(-7));
162 EXPECT_EQ(mt(-7), -mt(7));
163 EXPECT_EQ(mt(7), -mt(-7));
164 EXPECT_EQ(dtw(-7), -dtw(7));
165 EXPECT_EQ(dtw(7), -dtw(-7));
166 EXPECT_EQ(dtm(-7), -dtm(7));
167 EXPECT_EQ(dtm(7), -dtm(-7));
168}
169
170TEST(WTF_Time, times)
171{
172 EXPECT_EQ(s(15), s(3) * 5);
173}
174
175TEST(WTF_Time, divide)
176{
177 EXPECT_EQ(s(3), s(15) / 5);
178}
179
180TEST(WTF_Time, less)
181{
182 EXPECT_FALSE(s(2) < s(1));
183 EXPECT_FALSE(s(2) < s(2));
184 EXPECT_TRUE(s(2) < s(3));
185 EXPECT_FALSE(wt(2) < wt(1));
186 EXPECT_FALSE(wt(2) < wt(2));
187 EXPECT_TRUE(wt(2) < wt(3));
188 EXPECT_FALSE(mt(2) < mt(1));
189 EXPECT_FALSE(mt(2) < mt(2));
190 EXPECT_TRUE(mt(2) < mt(3));
191 EXPECT_FALSE(dtw(2) < dtw(1));
192 EXPECT_FALSE(dtw(2) < dtw(2));
193 EXPECT_TRUE(dtw(2) < dtw(3));
194 EXPECT_FALSE(dtm(2) < dtm(1));
195 EXPECT_FALSE(dtm(2) < dtm(2));
196 EXPECT_TRUE(dtm(2) < dtm(3));
197}
198
199TEST(WTF_Time, lessEqual)
200{
201 EXPECT_FALSE(s(2) <= s(1));
202 EXPECT_TRUE(s(2) <= s(2));
203 EXPECT_TRUE(s(2) <= s(3));
204 EXPECT_FALSE(wt(2) <= wt(1));
205 EXPECT_TRUE(wt(2) <= wt(2));
206 EXPECT_TRUE(wt(2) <= wt(3));
207 EXPECT_FALSE(mt(2) <= mt(1));
208 EXPECT_TRUE(mt(2) <= mt(2));
209 EXPECT_TRUE(mt(2) <= mt(3));
210 EXPECT_FALSE(dtw(2) <= dtw(1));
211 EXPECT_TRUE(dtw(2) <= dtw(2));
212 EXPECT_TRUE(dtw(2) <= dtw(3));
213 EXPECT_FALSE(dtm(2) <= dtm(1));
214 EXPECT_TRUE(dtm(2) <= dtm(2));
215 EXPECT_TRUE(dtm(2) <= dtm(3));
216}
217
218TEST(WTF_Time, greater)
219{
220 EXPECT_TRUE(s(2) > s(1));
221 EXPECT_FALSE(s(2) > s(2));
222 EXPECT_FALSE(s(2) > s(3));
223 EXPECT_TRUE(wt(2) > wt(1));
224 EXPECT_FALSE(wt(2) > wt(2));
225 EXPECT_FALSE(wt(2) > wt(3));
226 EXPECT_TRUE(mt(2) > mt(1));
227 EXPECT_FALSE(mt(2) > mt(2));
228 EXPECT_FALSE(mt(2) > mt(3));
229 EXPECT_TRUE(dtw(2) > dtw(1));
230 EXPECT_FALSE(dtw(2) > dtw(2));
231 EXPECT_FALSE(dtw(2) > dtw(3));
232 EXPECT_TRUE(dtm(2) > dtm(1));
233 EXPECT_FALSE(dtm(2) > dtm(2));
234 EXPECT_FALSE(dtm(2) > dtm(3));
235}
236
237TEST(WTF_Time, greaterEqual)
238{
239 EXPECT_TRUE(s(2) >= s(1));
240 EXPECT_TRUE(s(2) >= s(2));
241 EXPECT_FALSE(s(2) >= s(3));
242 EXPECT_TRUE(wt(2) >= wt(1));
243 EXPECT_TRUE(wt(2) >= wt(2));
244 EXPECT_FALSE(wt(2) >= wt(3));
245 EXPECT_TRUE(mt(2) >= mt(1));
246 EXPECT_TRUE(mt(2) >= mt(2));
247 EXPECT_FALSE(mt(2) >= mt(3));
248 EXPECT_TRUE(dtw(2) >= dtw(1));
249 EXPECT_TRUE(dtw(2) >= dtw(2));
250 EXPECT_FALSE(dtw(2) >= dtw(3));
251 EXPECT_TRUE(dtm(2) >= dtm(1));
252 EXPECT_TRUE(dtm(2) >= dtm(2));
253 EXPECT_FALSE(dtm(2) >= dtm(3));
254}
255
256TEST(WTF_Time, equal)
257{
258 EXPECT_FALSE(s(2) == s(1));
259 EXPECT_TRUE(s(2) == s(2));
260 EXPECT_FALSE(s(2) == s(3));
261 EXPECT_FALSE(wt(2) == wt(1));
262 EXPECT_TRUE(wt(2) == wt(2));
263 EXPECT_FALSE(wt(2) == wt(3));
264 EXPECT_FALSE(mt(2) == mt(1));
265 EXPECT_TRUE(mt(2) == mt(2));
266 EXPECT_FALSE(mt(2) == mt(3));
267 EXPECT_FALSE(dtw(2) == dtw(1));
268 EXPECT_TRUE(dtw(2) == dtw(2));
269 EXPECT_FALSE(dtw(2) == dtw(3));
270 EXPECT_FALSE(dtm(2) == dtm(1));
271 EXPECT_TRUE(dtm(2) == dtm(2));
272 EXPECT_FALSE(dtm(2) == dtm(3));
273}
274
275TEST(WTF_Time, notEqual)
276{
277 EXPECT_TRUE(s(2) != s(1));
278 EXPECT_FALSE(s(2) != s(2));
279 EXPECT_TRUE(s(2) != s(3));
280 EXPECT_TRUE(wt(2) != wt(1));
281 EXPECT_FALSE(wt(2) != wt(2));
282 EXPECT_TRUE(wt(2) != wt(3));
283 EXPECT_TRUE(mt(2) != mt(1));
284 EXPECT_FALSE(mt(2) != mt(2));
285 EXPECT_TRUE(mt(2) != mt(3));
286 EXPECT_TRUE(dtw(2) != dtw(1));
287 EXPECT_FALSE(dtw(2) != dtw(2));
288 EXPECT_TRUE(dtw(2) != dtw(3));
289 EXPECT_TRUE(dtm(2) != dtm(1));
290 EXPECT_FALSE(dtm(2) != dtm(2));
291 EXPECT_TRUE(dtm(2) != dtm(3));
292}
293
294TEST(WTF_Time, literals)
295{
296 EXPECT_TRUE(s(120) == 2_min);
297 EXPECT_TRUE(s(2) == 2_s);
298 EXPECT_TRUE(s(2) == 2000_ms);
299 EXPECT_TRUE(s(2) - 1000_ms == s(1));
300 EXPECT_TRUE(2_s - s(1) == 1000_ms);
301
302 EXPECT_TRUE(Seconds::fromMinutes(2) == 2_min);
303 EXPECT_TRUE(Seconds(2) == 2_s);
304 EXPECT_TRUE(Seconds::fromMilliseconds(2) == 2_ms);
305 EXPECT_TRUE(Seconds::fromMicroseconds(2) == 2_us);
306 EXPECT_TRUE(Seconds::fromNanoseconds(2) == 2_ns);
307
308 EXPECT_TRUE(Seconds::fromMinutes(2.5) == 2.5_min);
309 EXPECT_TRUE(Seconds(2.5) == 2.5_s);
310 EXPECT_TRUE(Seconds::fromMilliseconds(2.5) == 2.5_ms);
311 EXPECT_TRUE(Seconds::fromMicroseconds(2.5) == 2.5_us);
312 EXPECT_TRUE(Seconds::fromNanoseconds(2.5) == 2.5_ns);
313}
314
315TEST(WTF_Time, clamp)
316{
317 Seconds positiveInfinity = Seconds::infinity();
318 EXPECT_TRUE(positiveInfinity.secondsAs<int32_t>() == INT32_MAX);
319 EXPECT_TRUE(positiveInfinity.secondsAs<uint32_t>() == UINT32_MAX);
320 EXPECT_TRUE(positiveInfinity.secondsAs<int64_t>() == INT64_MAX);
321 EXPECT_TRUE(positiveInfinity.secondsAs<uint64_t>() == UINT64_MAX);
322 EXPECT_TRUE(positiveInfinity.millisecondsAs<int32_t>() == INT32_MAX);
323 EXPECT_TRUE(positiveInfinity.millisecondsAs<uint32_t>() == UINT32_MAX);
324 EXPECT_TRUE(positiveInfinity.millisecondsAs<int64_t>() == INT64_MAX);
325 EXPECT_TRUE(positiveInfinity.millisecondsAs<uint64_t>() == UINT64_MAX);
326 EXPECT_TRUE(positiveInfinity.microsecondsAs<int32_t>() == INT32_MAX);
327 EXPECT_TRUE(positiveInfinity.microsecondsAs<uint32_t>() == UINT32_MAX);
328 EXPECT_TRUE(positiveInfinity.microsecondsAs<int64_t>() == INT64_MAX);
329 EXPECT_TRUE(positiveInfinity.microsecondsAs<uint64_t>() == UINT64_MAX);
330 EXPECT_TRUE(positiveInfinity.nanosecondsAs<int32_t>() == INT32_MAX);
331 EXPECT_TRUE(positiveInfinity.nanosecondsAs<uint32_t>() == UINT32_MAX);
332 EXPECT_TRUE(positiveInfinity.nanosecondsAs<int64_t>() == INT64_MAX);
333 EXPECT_TRUE(positiveInfinity.nanosecondsAs<uint64_t>() == UINT64_MAX);
334
335 Seconds negativeInfinity = -Seconds::infinity();
336 EXPECT_TRUE(negativeInfinity.secondsAs<int32_t>() == INT32_MIN);
337 EXPECT_TRUE(negativeInfinity.secondsAs<uint32_t>() == 0);
338 EXPECT_TRUE(negativeInfinity.secondsAs<int64_t>() == INT64_MIN);
339 EXPECT_TRUE(negativeInfinity.secondsAs<uint64_t>() == 0);
340 EXPECT_TRUE(negativeInfinity.millisecondsAs<int32_t>() == INT32_MIN);
341 EXPECT_TRUE(negativeInfinity.millisecondsAs<uint32_t>() == 0);
342 EXPECT_TRUE(negativeInfinity.millisecondsAs<int64_t>() == INT64_MIN);
343 EXPECT_TRUE(negativeInfinity.millisecondsAs<uint64_t>() == 0);
344 EXPECT_TRUE(negativeInfinity.microsecondsAs<int32_t>() == INT32_MIN);
345 EXPECT_TRUE(negativeInfinity.microsecondsAs<uint32_t>() == 0);
346 EXPECT_TRUE(negativeInfinity.microsecondsAs<int64_t>() == INT64_MIN);
347 EXPECT_TRUE(negativeInfinity.microsecondsAs<uint64_t>() == 0);
348 EXPECT_TRUE(negativeInfinity.nanosecondsAs<int32_t>() == INT32_MIN);
349 EXPECT_TRUE(negativeInfinity.nanosecondsAs<uint32_t>() == 0);
350 EXPECT_TRUE(negativeInfinity.nanosecondsAs<int64_t>() == INT64_MIN);
351 EXPECT_TRUE(negativeInfinity.nanosecondsAs<uint64_t>() == 0);
352}
353
354// Test MonotonicTime constexpr features. If they are not calculated in constexpr,
355// they invokes global constructors and becomes compile errors.
356static const MonotonicTime NaN = MonotonicTime::nan();
357static const MonotonicTime Infinity = MonotonicTime::infinity();
358static const MonotonicTime Zero = MonotonicTime::fromRawSeconds(0);
359static const MonotonicTime One = Zero + Seconds(1);
360static const MonotonicTime NegativeOne = Zero - Seconds(1);
361static const bool ZeroIsFalse = !!Zero;
362static const bool Equal = Zero == Zero;
363static const bool NotEqual = Zero != One;
364static const bool LessThan = Zero < One;
365static const bool GreaterThan = One > Zero;
366static const bool LessThanOrEqual = Zero <= Zero;
367static const bool GreaterThanOrEqual = Zero >= Zero;
368
369TEST(WTF_Time, constexprMonotonicTime)
370{
371 EXPECT_TRUE(std::isnan(NaN));
372 EXPECT_TRUE(std::isinf(Infinity));
373 EXPECT_TRUE(Zero.secondsSinceEpoch().value() == 0.0);
374 EXPECT_TRUE(One.secondsSinceEpoch().value() == 1.0);
375 EXPECT_TRUE(NegativeOne.secondsSinceEpoch().value() == -1.0);
376 EXPECT_FALSE(ZeroIsFalse);
377 EXPECT_TRUE(Equal);
378 EXPECT_TRUE(NotEqual);
379 EXPECT_TRUE(LessThan);
380 EXPECT_TRUE(GreaterThan);
381 EXPECT_TRUE(LessThanOrEqual);
382 EXPECT_TRUE(GreaterThanOrEqual);
383}
384
385
386} // namespace TestWebKitAPI
387
388