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/thread.h" | 14 #include "webrtc/base/thread.h" |
14 #include "webrtc/base/timeutils.h" | 15 #include "webrtc/base/timeutils.h" |
15 | 16 |
16 namespace rtc { | 17 namespace rtc { |
17 | 18 |
18 TEST(TimeTest, TimeInMs) { | 19 TEST(TimeTest, TimeInMs) { |
19 uint32_t ts_earlier = Time(); | 20 uint32_t ts_earlier = Time(); |
20 Thread::SleepMs(100); | 21 Thread::SleepMs(100); |
21 uint32_t ts_now = Time(); | 22 uint32_t ts_now = Time(); |
22 // Allow for the thread to wakeup ~20ms early. | 23 // Allow for the thread to wakeup ~20ms early. |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 unwrapped_ts += 0x10; | 160 unwrapped_ts += 0x10; |
160 EXPECT_EQ(unwrapped_ts, wraparound_handler_.Unwrap(ts)); | 161 EXPECT_EQ(unwrapped_ts, wraparound_handler_.Unwrap(ts)); |
161 ts = 0xfffffff2; | 162 ts = 0xfffffff2; |
162 unwrapped_ts += 0xfffffff0; | 163 unwrapped_ts += 0xfffffff0; |
163 EXPECT_EQ(unwrapped_ts, wraparound_handler_.Unwrap(ts)); | 164 EXPECT_EQ(unwrapped_ts, wraparound_handler_.Unwrap(ts)); |
164 ts = 0; | 165 ts = 0; |
165 unwrapped_ts += 0xe; | 166 unwrapped_ts += 0xe; |
166 EXPECT_EQ(unwrapped_ts, wraparound_handler_.Unwrap(ts)); | 167 EXPECT_EQ(unwrapped_ts, wraparound_handler_.Unwrap(ts)); |
167 } | 168 } |
168 | 169 |
| 170 class TmToSeconds : public testing::Test { |
| 171 public: |
| 172 TmToSeconds() { |
| 173 // Set use of the test RNG to get deterministic expiration timestamp. |
| 174 rtc::SetRandomTestMode(true); |
| 175 } |
| 176 ~TmToSeconds() { |
| 177 // Put it back for the next test. |
| 178 rtc::SetRandomTestMode(false); |
| 179 } |
| 180 |
| 181 void TestTmToSeconds(int times) { |
| 182 static char mdays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; |
| 183 for (int i = 0; i < times; i++) { |
| 184 |
| 185 // First generate something correct and check that TmToSeconds is happy. |
| 186 int year = rtc::CreateRandomId() % 400 + 1970; |
| 187 |
| 188 bool leap_year = false; |
| 189 if (year % 4 == 0) |
| 190 leap_year = true; |
| 191 if (year % 100 == 0) |
| 192 leap_year = false; |
| 193 if (year % 400 == 0) |
| 194 leap_year = true; |
| 195 |
| 196 std::tm tm; |
| 197 tm.tm_year = year - 1900; // std::tm is year 1900 based. |
| 198 tm.tm_mon = rtc::CreateRandomId() % 12; |
| 199 tm.tm_mday = rtc::CreateRandomId() % mdays[tm.tm_mon] + 1; |
| 200 tm.tm_hour = rtc::CreateRandomId() % 24; |
| 201 tm.tm_min = rtc::CreateRandomId() % 60; |
| 202 tm.tm_sec = rtc::CreateRandomId() % 60; |
| 203 int64_t t = rtc::TmToSeconds(tm); |
| 204 EXPECT_TRUE(t >= 0); |
| 205 |
| 206 // Now damage a random field and check that TmToSeconds is unhappy. |
| 207 switch (rtc::CreateRandomId() % 11) { |
| 208 case 0: |
| 209 tm.tm_year = 1969 - 1900; |
| 210 break; |
| 211 case 1: |
| 212 tm.tm_mon = -1; |
| 213 break; |
| 214 case 2: |
| 215 tm.tm_mon = 12; |
| 216 break; |
| 217 case 3: |
| 218 tm.tm_mday = 0; |
| 219 break; |
| 220 case 4: |
| 221 tm.tm_mday = mdays[tm.tm_mon] + (leap_year && tm.tm_mon == 1) + 1; |
| 222 break; |
| 223 case 5: |
| 224 tm.tm_hour = -1; |
| 225 break; |
| 226 case 6: |
| 227 tm.tm_hour = 24; |
| 228 break; |
| 229 case 7: |
| 230 tm.tm_min = -1; |
| 231 break; |
| 232 case 8: |
| 233 tm.tm_min = 60; |
| 234 break; |
| 235 case 9: |
| 236 tm.tm_sec = -1; |
| 237 break; |
| 238 case 10: |
| 239 tm.tm_sec = 60; |
| 240 break; |
| 241 } |
| 242 EXPECT_EQ(rtc::TmToSeconds(tm), -1); |
| 243 } |
| 244 // Check consistency with the system gmtime_r. With time_t, we can only |
| 245 // portably test dates until 2038, which is achieved by the % 0x80000000. |
| 246 for (int i = 0; i < times; i++) { |
| 247 time_t t = rtc::CreateRandomId() % 0x80000000; |
| 248 #if defined(WEBRTC_WIN) |
| 249 std::tm* tm = std::gmtime(&t); |
| 250 EXPECT_TRUE(tm); |
| 251 EXPECT_TRUE(rtc::TmToSeconds(*tm) == t); |
| 252 #else |
| 253 std::tm tm; |
| 254 EXPECT_TRUE(gmtime_r(&t, &tm)); |
| 255 EXPECT_TRUE(rtc::TmToSeconds(tm) == t); |
| 256 #endif |
| 257 } |
| 258 } |
| 259 }; |
| 260 |
| 261 TEST_F(TmToSeconds, TestTmToSeconds) { |
| 262 TestTmToSeconds(100000); |
| 263 } |
| 264 |
169 } // namespace rtc | 265 } // namespace rtc |
OLD | NEW |