OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/base/common.h" | 11 #include "webrtc/base/common.h" |
12 #include "webrtc/base/gunit.h" | 12 #include "webrtc/base/gunit.h" |
13 #include "webrtc/base/helpers.h" | 13 #include "webrtc/base/helpers.h" |
14 #include "webrtc/base/thread.h" | 14 #include "webrtc/base/thread.h" |
15 #include "webrtc/base/timeutils.h" | 15 #include "webrtc/base/timeutils.h" |
16 | 16 |
17 namespace rtc { | 17 namespace rtc { |
18 typedef int64_t TimeStamp; | |
pthatcher1
2016/04/11 20:56:59
Why not just search and replace TimeStamp with uin
honghaiz3
2016/04/18 23:39:04
Done.
| |
18 | 19 |
19 TEST(TimeTest, TimeInMs) { | 20 TEST(TimeTest, TimeInMs) { |
20 uint32_t ts_earlier = Time(); | 21 TimeStamp ts_earlier = Time(); |
21 Thread::SleepMs(100); | 22 Thread::SleepMs(100); |
22 uint32_t ts_now = Time(); | 23 TimeStamp ts_now = Time(); |
23 // Allow for the thread to wakeup ~20ms early. | 24 // Allow for the thread to wakeup ~20ms early. |
24 EXPECT_GE(ts_now, ts_earlier + 80); | 25 EXPECT_GE(ts_now, ts_earlier + 80); |
25 // Make sure the Time is not returning in smaller unit like microseconds. | 26 // Make sure the Time is not returning in smaller unit like microseconds. |
26 EXPECT_LT(ts_now, ts_earlier + 1000); | 27 EXPECT_LT(ts_now, ts_earlier + 1000); |
27 } | 28 } |
28 | 29 |
29 TEST(TimeTest, Comparison) { | |
30 // Obtain two different times, in known order | |
31 TimeStamp ts_earlier = Time(); | |
32 Thread::SleepMs(100); | |
33 TimeStamp ts_now = Time(); | |
34 EXPECT_NE(ts_earlier, ts_now); | |
35 | |
36 // Common comparisons | |
37 EXPECT_TRUE( TimeIsLaterOrEqual(ts_earlier, ts_now)); | |
38 EXPECT_TRUE( TimeIsLater( ts_earlier, ts_now)); | |
39 EXPECT_FALSE(TimeIsLaterOrEqual(ts_now, ts_earlier)); | |
40 EXPECT_FALSE(TimeIsLater( ts_now, ts_earlier)); | |
41 | |
42 // Edge cases | |
43 EXPECT_TRUE( TimeIsLaterOrEqual(ts_earlier, ts_earlier)); | |
44 EXPECT_FALSE(TimeIsLater( ts_earlier, ts_earlier)); | |
45 | |
46 // Obtain a third time | |
47 TimeStamp ts_later = TimeAfter(100); | |
48 EXPECT_NE(ts_now, ts_later); | |
49 EXPECT_TRUE( TimeIsLater(ts_now, ts_later)); | |
50 EXPECT_TRUE( TimeIsLater(ts_earlier, ts_later)); | |
51 | |
52 // Common comparisons | |
53 EXPECT_TRUE( TimeIsBetween(ts_earlier, ts_now, ts_later)); | |
54 EXPECT_FALSE(TimeIsBetween(ts_earlier, ts_later, ts_now)); | |
55 EXPECT_FALSE(TimeIsBetween(ts_now, ts_earlier, ts_later)); | |
56 EXPECT_TRUE( TimeIsBetween(ts_now, ts_later, ts_earlier)); | |
57 EXPECT_TRUE( TimeIsBetween(ts_later, ts_earlier, ts_now)); | |
58 EXPECT_FALSE(TimeIsBetween(ts_later, ts_now, ts_earlier)); | |
59 | |
60 // Edge cases | |
61 EXPECT_TRUE( TimeIsBetween(ts_earlier, ts_earlier, ts_earlier)); | |
62 EXPECT_TRUE( TimeIsBetween(ts_earlier, ts_earlier, ts_later)); | |
63 EXPECT_TRUE( TimeIsBetween(ts_earlier, ts_later, ts_later)); | |
64 | |
65 // Earlier of two times | |
66 EXPECT_EQ(ts_earlier, TimeMin(ts_earlier, ts_earlier)); | |
67 EXPECT_EQ(ts_earlier, TimeMin(ts_earlier, ts_now)); | |
68 EXPECT_EQ(ts_earlier, TimeMin(ts_earlier, ts_later)); | |
69 EXPECT_EQ(ts_earlier, TimeMin(ts_now, ts_earlier)); | |
70 EXPECT_EQ(ts_earlier, TimeMin(ts_later, ts_earlier)); | |
71 | |
72 // Later of two times | |
73 EXPECT_EQ(ts_earlier, TimeMax(ts_earlier, ts_earlier)); | |
74 EXPECT_EQ(ts_now, TimeMax(ts_earlier, ts_now)); | |
75 EXPECT_EQ(ts_later, TimeMax(ts_earlier, ts_later)); | |
76 EXPECT_EQ(ts_now, TimeMax(ts_now, ts_earlier)); | |
77 EXPECT_EQ(ts_later, TimeMax(ts_later, ts_earlier)); | |
78 } | |
79 | |
80 TEST(TimeTest, Intervals) { | 30 TEST(TimeTest, Intervals) { |
81 TimeStamp ts_earlier = Time(); | 31 TimeStamp ts_earlier = Time(); |
82 TimeStamp ts_later = TimeAfter(500); | 32 TimeStamp ts_later = TimeAfter(500); |
83 | 33 |
84 // We can't depend on ts_later and ts_earlier to be exactly 500 apart | 34 // We can't depend on ts_later and ts_earlier to be exactly 500 apart |
85 // since time elapses between the calls to Time() and TimeAfter(500) | 35 // since time elapses between the calls to Time() and TimeAfter(500) |
86 EXPECT_LE(500, TimeDiff(ts_later, ts_earlier)); | 36 EXPECT_LE(500, TimeDiff(ts_later, ts_earlier)); |
87 EXPECT_GE(-500, TimeDiff(ts_earlier, ts_later)); | 37 EXPECT_GE(-500, TimeDiff(ts_earlier, ts_later)); |
88 | 38 |
89 // Time has elapsed since ts_earlier | 39 // Time has elapsed since ts_earlier |
90 EXPECT_GE(TimeSince(ts_earlier), 0); | 40 EXPECT_GE(TimeSince(ts_earlier), 0); |
91 | 41 |
92 // ts_earlier is earlier than now, so TimeUntil ts_earlier is -ve | 42 // ts_earlier is earlier than now, so TimeUntil ts_earlier is -ve |
93 EXPECT_LE(TimeUntil(ts_earlier), 0); | 43 EXPECT_LE(TimeUntil(ts_earlier), 0); |
94 | 44 |
95 // ts_later likely hasn't happened yet, so TimeSince could be -ve | 45 // ts_later likely hasn't happened yet, so TimeSince could be -ve |
96 // but within 500 | 46 // but within 500 |
97 EXPECT_GE(TimeSince(ts_later), -500); | 47 EXPECT_GE(TimeSince(ts_later), -500); |
98 | 48 |
99 // TimeUntil ts_later is at most 500 | 49 // TimeUntil ts_later is at most 500 |
100 EXPECT_LE(TimeUntil(ts_later), 500); | 50 EXPECT_LE(TimeUntil(ts_later), 500); |
101 } | 51 } |
102 | 52 |
103 TEST(TimeTest, BoundaryComparison) { | |
104 // Obtain two different times, in known order | |
105 TimeStamp ts_earlier = static_cast<TimeStamp>(-50); | |
106 TimeStamp ts_later = ts_earlier + 100; | |
107 EXPECT_NE(ts_earlier, ts_later); | |
108 | |
109 // Common comparisons | |
110 EXPECT_TRUE( TimeIsLaterOrEqual(ts_earlier, ts_later)); | |
111 EXPECT_TRUE( TimeIsLater( ts_earlier, ts_later)); | |
112 EXPECT_FALSE(TimeIsLaterOrEqual(ts_later, ts_earlier)); | |
113 EXPECT_FALSE(TimeIsLater( ts_later, ts_earlier)); | |
114 | |
115 // Earlier of two times | |
116 EXPECT_EQ(ts_earlier, TimeMin(ts_earlier, ts_earlier)); | |
117 EXPECT_EQ(ts_earlier, TimeMin(ts_earlier, ts_later)); | |
118 EXPECT_EQ(ts_earlier, TimeMin(ts_later, ts_earlier)); | |
119 | |
120 // Later of two times | |
121 EXPECT_EQ(ts_earlier, TimeMax(ts_earlier, ts_earlier)); | |
122 EXPECT_EQ(ts_later, TimeMax(ts_earlier, ts_later)); | |
123 EXPECT_EQ(ts_later, TimeMax(ts_later, ts_earlier)); | |
124 | |
125 // Interval | |
126 EXPECT_EQ(100, TimeDiff(ts_later, ts_earlier)); | |
127 EXPECT_EQ(-100, TimeDiff(ts_earlier, ts_later)); | |
128 } | |
129 | |
130 TEST(TimeTest, DISABLED_CurrentTmTime) { | 53 TEST(TimeTest, DISABLED_CurrentTmTime) { |
131 struct tm tm; | 54 struct tm tm; |
132 int microseconds; | 55 int microseconds; |
133 | 56 |
134 time_t before = ::time(NULL); | 57 time_t before = ::time(NULL); |
135 CurrentTmTime(&tm, µseconds); | 58 CurrentTmTime(&tm, µseconds); |
136 time_t after = ::time(NULL); | 59 time_t after = ::time(NULL); |
137 | 60 |
138 // Assert that 'tm' represents a time between 'before' and 'after'. | 61 // Assert that 'tm' represents a time between 'before' and 'after'. |
139 // mktime() uses local time, so we have to compensate for that. | 62 // mktime() uses local time, so we have to compensate for that. |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
294 #endif | 217 #endif |
295 } | 218 } |
296 } | 219 } |
297 }; | 220 }; |
298 | 221 |
299 TEST_F(TmToSeconds, TestTmToSeconds) { | 222 TEST_F(TmToSeconds, TestTmToSeconds) { |
300 TestTmToSeconds(100000); | 223 TestTmToSeconds(100000); |
301 } | 224 } |
302 | 225 |
303 } // namespace rtc | 226 } // namespace rtc |
OLD | NEW |