OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2015 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 #include "webrtc/modules/rtp_rtcp/source/time_util.h" | 10 #include "webrtc/modules/rtp_rtcp/source/time_util.h" |
11 | 11 |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 namespace webrtc { | 14 namespace webrtc { |
15 | 15 |
16 TEST(TimeUtilTest, CompactNtp) { | 16 TEST(TimeUtilTest, CompactNtp) { |
17 const uint32_t kNtpSec = 0x12345678; | 17 const uint32_t kNtpSec = 0x12345678; |
18 const uint32_t kNtpFrac = 0x23456789; | 18 const uint32_t kNtpFrac = 0x23456789; |
19 const NtpTime kNtp(kNtpSec, kNtpFrac); | 19 const NtpTime kNtp(kNtpSec, kNtpFrac); |
20 const uint32_t kNtpMid = 0x56782345; | 20 const uint32_t kNtpMid = 0x56782345; |
21 EXPECT_EQ(kNtpMid, CompactNtp(kNtp)); | 21 EXPECT_EQ(kNtpMid, CompactNtp(kNtp)); |
22 } | 22 } |
23 | 23 |
24 TEST(TimeUtilTest, CompactNtpToMs) { | 24 TEST(TimeUtilTest, CompactNtpRttToMs) { |
25 const NtpTime ntp1(0x12345, 0x23456); | 25 const NtpTime ntp1(0x12345, 0x23456); |
26 const NtpTime ntp2(0x12654, 0x64335); | 26 const NtpTime ntp2(0x12654, 0x64335); |
27 uint32_t ms_diff = ntp2.ToMs() - ntp1.ToMs(); | 27 int64_t ms_diff = ntp2.ToMs() - ntp1.ToMs(); |
28 uint32_t ntp_diff = CompactNtp(ntp2) - CompactNtp(ntp1); | 28 uint32_t ntp_diff = CompactNtp(ntp2) - CompactNtp(ntp1); |
29 | 29 |
30 uint32_t ntp_to_ms_diff = CompactNtpIntervalToMs(ntp_diff); | 30 int64_t ntp_to_ms_diff = CompactNtpRttToMs(ntp_diff); |
31 | 31 |
32 EXPECT_NEAR(ms_diff, ntp_to_ms_diff, 1); | 32 EXPECT_NEAR(ms_diff, ntp_to_ms_diff, 1); |
33 } | 33 } |
34 | 34 |
35 TEST(TimeUtilTest, CompactNtpToMsWithWrap) { | 35 TEST(TimeUtilTest, CompactNtpRttToMsWithWrap) { |
36 const NtpTime ntp1(0x1ffff, 0x23456); | 36 const NtpTime ntp1(0x1ffff, 0x23456); |
37 const NtpTime ntp2(0x20000, 0x64335); | 37 const NtpTime ntp2(0x20000, 0x64335); |
38 uint32_t ms_diff = ntp2.ToMs() - ntp1.ToMs(); | 38 int64_t ms_diff = ntp2.ToMs() - ntp1.ToMs(); |
39 | 39 |
40 // While ntp2 > ntp1, there compact ntp presentation happen to be opposite. | 40 // While ntp2 > ntp1, there compact ntp presentation happen to be opposite. |
41 // That shouldn't be a problem as long as unsigned arithmetic is used. | 41 // That shouldn't be a problem as long as unsigned arithmetic is used. |
42 ASSERT_GT(ntp2.ToMs(), ntp1.ToMs()); | 42 ASSERT_GT(ntp2.ToMs(), ntp1.ToMs()); |
43 ASSERT_LT(CompactNtp(ntp2), CompactNtp(ntp1)); | 43 ASSERT_LT(CompactNtp(ntp2), CompactNtp(ntp1)); |
44 | 44 |
45 uint32_t ntp_diff = CompactNtp(ntp2) - CompactNtp(ntp1); | 45 uint32_t ntp_diff = CompactNtp(ntp2) - CompactNtp(ntp1); |
46 uint32_t ntp_to_ms_diff = CompactNtpIntervalToMs(ntp_diff); | 46 int64_t ntp_to_ms_diff = CompactNtpRttToMs(ntp_diff); |
47 | 47 |
48 EXPECT_NEAR(ms_diff, ntp_to_ms_diff, 1); | 48 EXPECT_NEAR(ms_diff, ntp_to_ms_diff, 1); |
49 } | 49 } |
50 | 50 |
51 TEST(TimeUtilTest, CompactNtpToMsLarge) { | 51 TEST(TimeUtilTest, CompactNtpRttToMsLarge) { |
52 const NtpTime ntp1(0x10000, 0x23456); | 52 const NtpTime ntp1(0x10000, 0x00006); |
53 const NtpTime ntp2(0x1ffff, 0x64335); | 53 const NtpTime ntp2(0x17fff, 0xffff5); |
54 uint32_t ms_diff = ntp2.ToMs() - ntp1.ToMs(); | 54 int64_t ms_diff = ntp2.ToMs() - ntp1.ToMs(); |
55 // Ntp difference close to maximum of ~18 hours should convert correctly too. | 55 // Ntp difference close to 2^15 seconds should convert correctly too. |
56 ASSERT_GT(ms_diff, 18u * 3600 * 1000); | 56 ASSERT_NEAR(ms_diff, ((1 << 15) - 1) * 1000, 1); |
57 uint32_t ntp_diff = CompactNtp(ntp2) - CompactNtp(ntp1); | 57 uint32_t ntp_diff = CompactNtp(ntp2) - CompactNtp(ntp1); |
58 uint32_t ntp_to_ms_diff = CompactNtpIntervalToMs(ntp_diff); | 58 int64_t ntp_to_ms_diff = CompactNtpRttToMs(ntp_diff); |
59 | 59 |
60 EXPECT_NEAR(ms_diff, ntp_to_ms_diff, 1); | 60 EXPECT_NEAR(ms_diff, ntp_to_ms_diff, 1); |
61 } | 61 } |
| 62 |
| 63 TEST(TimeUtilTest, CompactNtpRttToMsNegative) { |
| 64 const NtpTime ntp1(0x20000, 0x23456); |
| 65 const NtpTime ntp2(0x1ffff, 0x64335); |
| 66 int64_t ms_diff = ntp2.ToMs() - ntp1.ToMs(); |
| 67 ASSERT_GT(0, ms_diff); |
| 68 // Ntp difference close to 2^16 seconds should be treated as negative. |
| 69 uint32_t ntp_diff = CompactNtp(ntp2) - CompactNtp(ntp1); |
| 70 int64_t ntp_to_ms_diff = CompactNtpRttToMs(ntp_diff); |
| 71 EXPECT_EQ(1, ntp_to_ms_diff); |
| 72 } |
62 } // namespace webrtc | 73 } // namespace webrtc |
OLD | NEW |