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 | 18 |
19 TEST(TimeTest, TimeInMs) { | 19 TEST(TimeTest, TimeInMs) { |
20 uint32_t ts_earlier = Time(); | 20 int64_t ts_earlier = Time(); |
21 Thread::SleepMs(100); | 21 Thread::SleepMs(100); |
22 uint32_t ts_now = Time(); | 22 int64_t ts_now = Time(); |
23 // Allow for the thread to wakeup ~20ms early. | 23 // Allow for the thread to wakeup ~20ms early. |
24 EXPECT_GE(ts_now, ts_earlier + 80); | 24 EXPECT_GE(ts_now, ts_earlier + 80); |
25 // Make sure the Time is not returning in smaller unit like microseconds. | 25 // Make sure the Time is not returning in smaller unit like microseconds. |
26 EXPECT_LT(ts_now, ts_earlier + 1000); | 26 EXPECT_LT(ts_now, ts_earlier + 1000); |
27 } | 27 } |
28 | 28 |
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 // Earlier of two times | |
53 EXPECT_EQ(ts_earlier, TimeMin(ts_earlier, ts_earlier)); | |
54 EXPECT_EQ(ts_earlier, TimeMin(ts_earlier, ts_now)); | |
55 EXPECT_EQ(ts_earlier, TimeMin(ts_earlier, ts_later)); | |
56 EXPECT_EQ(ts_earlier, TimeMin(ts_now, ts_earlier)); | |
57 EXPECT_EQ(ts_earlier, TimeMin(ts_later, ts_earlier)); | |
58 | |
59 // Later of two times | |
60 EXPECT_EQ(ts_earlier, TimeMax(ts_earlier, ts_earlier)); | |
61 EXPECT_EQ(ts_now, TimeMax(ts_earlier, ts_now)); | |
62 EXPECT_EQ(ts_later, TimeMax(ts_earlier, ts_later)); | |
63 EXPECT_EQ(ts_now, TimeMax(ts_now, ts_earlier)); | |
64 EXPECT_EQ(ts_later, TimeMax(ts_later, ts_earlier)); | |
65 } | |
66 | |
67 TEST(TimeTest, Intervals) { | 29 TEST(TimeTest, Intervals) { |
68 TimeStamp ts_earlier = Time(); | 30 int64_t ts_earlier = Time(); |
69 TimeStamp ts_later = TimeAfter(500); | 31 int64_t ts_later = TimeAfter(500); |
70 | 32 |
71 // We can't depend on ts_later and ts_earlier to be exactly 500 apart | 33 // We can't depend on ts_later and ts_earlier to be exactly 500 apart |
72 // since time elapses between the calls to Time() and TimeAfter(500) | 34 // since time elapses between the calls to Time() and TimeAfter(500) |
73 EXPECT_LE(500, TimeDiff(ts_later, ts_earlier)); | 35 EXPECT_LE(500, TimeDiff(ts_later, ts_earlier)); |
74 EXPECT_GE(-500, TimeDiff(ts_earlier, ts_later)); | 36 EXPECT_GE(-500, TimeDiff(ts_earlier, ts_later)); |
75 | 37 |
76 // Time has elapsed since ts_earlier | 38 // Time has elapsed since ts_earlier |
77 EXPECT_GE(TimeSince(ts_earlier), 0); | 39 EXPECT_GE(TimeSince(ts_earlier), 0); |
78 | 40 |
79 // ts_earlier is earlier than now, so TimeUntil ts_earlier is -ve | 41 // ts_earlier is earlier than now, so TimeUntil ts_earlier is -ve |
80 EXPECT_LE(TimeUntil(ts_earlier), 0); | 42 EXPECT_LE(TimeUntil(ts_earlier), 0); |
81 | 43 |
82 // ts_later likely hasn't happened yet, so TimeSince could be -ve | 44 // ts_later likely hasn't happened yet, so TimeSince could be -ve |
83 // but within 500 | 45 // but within 500 |
84 EXPECT_GE(TimeSince(ts_later), -500); | 46 EXPECT_GE(TimeSince(ts_later), -500); |
85 | 47 |
86 // TimeUntil ts_later is at most 500 | 48 // TimeUntil ts_later is at most 500 |
87 EXPECT_LE(TimeUntil(ts_later), 500); | 49 EXPECT_LE(TimeUntil(ts_later), 500); |
88 } | 50 } |
89 | 51 |
90 TEST(TimeTest, BoundaryComparison) { | |
91 // Obtain two different times, in known order | |
92 TimeStamp ts_earlier = static_cast<TimeStamp>(-50); | |
93 TimeStamp ts_later = ts_earlier + 100; | |
94 EXPECT_NE(ts_earlier, ts_later); | |
95 | |
96 // Common comparisons | |
97 EXPECT_TRUE( TimeIsLaterOrEqual(ts_earlier, ts_later)); | |
98 EXPECT_TRUE( TimeIsLater( ts_earlier, ts_later)); | |
99 EXPECT_FALSE(TimeIsLaterOrEqual(ts_later, ts_earlier)); | |
100 EXPECT_FALSE(TimeIsLater( ts_later, ts_earlier)); | |
101 | |
102 // Earlier of two times | |
103 EXPECT_EQ(ts_earlier, TimeMin(ts_earlier, ts_earlier)); | |
104 EXPECT_EQ(ts_earlier, TimeMin(ts_earlier, ts_later)); | |
105 EXPECT_EQ(ts_earlier, TimeMin(ts_later, ts_earlier)); | |
106 | |
107 // Later of two times | |
108 EXPECT_EQ(ts_earlier, TimeMax(ts_earlier, ts_earlier)); | |
109 EXPECT_EQ(ts_later, TimeMax(ts_earlier, ts_later)); | |
110 EXPECT_EQ(ts_later, TimeMax(ts_later, ts_earlier)); | |
111 | |
112 // Interval | |
113 EXPECT_EQ(100, TimeDiff(ts_later, ts_earlier)); | |
114 EXPECT_EQ(-100, TimeDiff(ts_earlier, ts_later)); | |
115 } | |
116 | |
117 TEST(TimeTest, TestTimeDiff64) { | 52 TEST(TimeTest, TestTimeDiff64) { |
118 int64_t ts_diff = 100; | 53 int64_t ts_diff = 100; |
119 int64_t ts_earlier = rtc::Time64(); | 54 int64_t ts_earlier = rtc::Time64(); |
120 int64_t ts_later = ts_earlier + ts_diff; | 55 int64_t ts_later = ts_earlier + ts_diff; |
121 EXPECT_EQ(ts_diff, rtc::TimeDiff(ts_later, ts_earlier)); | 56 EXPECT_EQ(ts_diff, rtc::TimeDiff(ts_later, ts_earlier)); |
122 EXPECT_EQ(-ts_diff, rtc::TimeDiff(ts_earlier, ts_later)); | 57 EXPECT_EQ(-ts_diff, rtc::TimeDiff(ts_earlier, ts_later)); |
123 } | 58 } |
124 | 59 |
125 class TimestampWrapAroundHandlerTest : public testing::Test { | 60 class TimestampWrapAroundHandlerTest : public testing::Test { |
126 public: | 61 public: |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 #endif | 199 #endif |
265 } | 200 } |
266 } | 201 } |
267 }; | 202 }; |
268 | 203 |
269 TEST_F(TmToSeconds, TestTmToSeconds) { | 204 TEST_F(TmToSeconds, TestTmToSeconds) { |
270 TestTmToSeconds(100000); | 205 TestTmToSeconds(100000); |
271 } | 206 } |
272 | 207 |
273 } // namespace rtc | 208 } // namespace rtc |
OLD | NEW |