danilchap | 1227e8b | 2015-12-21 11:06:50 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | #include "webrtc/modules/rtp_rtcp/source/time_util.h" |
| 11 | |
kwiberg | 77eab70 | 2016-09-28 17:42:01 -0700 | [diff] [blame] | 12 | #include "webrtc/test/gtest.h" |
danilchap | 1227e8b | 2015-12-21 11:06:50 -0800 | [diff] [blame] | 13 | |
| 14 | namespace webrtc { |
| 15 | |
| 16 | TEST(TimeUtilTest, CompactNtp) { |
| 17 | const uint32_t kNtpSec = 0x12345678; |
| 18 | const uint32_t kNtpFrac = 0x23456789; |
| 19 | const NtpTime kNtp(kNtpSec, kNtpFrac); |
| 20 | const uint32_t kNtpMid = 0x56782345; |
| 21 | EXPECT_EQ(kNtpMid, CompactNtp(kNtp)); |
| 22 | } |
| 23 | |
Danil Chapovalov | c1e55c7 | 2016-03-09 15:14:35 +0100 | [diff] [blame] | 24 | TEST(TimeUtilTest, CompactNtpRttToMs) { |
danilchap | 1227e8b | 2015-12-21 11:06:50 -0800 | [diff] [blame] | 25 | const NtpTime ntp1(0x12345, 0x23456); |
| 26 | const NtpTime ntp2(0x12654, 0x64335); |
Danil Chapovalov | c1e55c7 | 2016-03-09 15:14:35 +0100 | [diff] [blame] | 27 | int64_t ms_diff = ntp2.ToMs() - ntp1.ToMs(); |
danilchap | 1227e8b | 2015-12-21 11:06:50 -0800 | [diff] [blame] | 28 | uint32_t ntp_diff = CompactNtp(ntp2) - CompactNtp(ntp1); |
| 29 | |
Danil Chapovalov | c1e55c7 | 2016-03-09 15:14:35 +0100 | [diff] [blame] | 30 | int64_t ntp_to_ms_diff = CompactNtpRttToMs(ntp_diff); |
danilchap | 1227e8b | 2015-12-21 11:06:50 -0800 | [diff] [blame] | 31 | |
| 32 | EXPECT_NEAR(ms_diff, ntp_to_ms_diff, 1); |
| 33 | } |
| 34 | |
Danil Chapovalov | c1e55c7 | 2016-03-09 15:14:35 +0100 | [diff] [blame] | 35 | TEST(TimeUtilTest, CompactNtpRttToMsWithWrap) { |
danilchap | 1227e8b | 2015-12-21 11:06:50 -0800 | [diff] [blame] | 36 | const NtpTime ntp1(0x1ffff, 0x23456); |
| 37 | const NtpTime ntp2(0x20000, 0x64335); |
Danil Chapovalov | c1e55c7 | 2016-03-09 15:14:35 +0100 | [diff] [blame] | 38 | int64_t ms_diff = ntp2.ToMs() - ntp1.ToMs(); |
danilchap | 1227e8b | 2015-12-21 11:06:50 -0800 | [diff] [blame] | 39 | |
| 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. |
| 42 | ASSERT_GT(ntp2.ToMs(), ntp1.ToMs()); |
| 43 | ASSERT_LT(CompactNtp(ntp2), CompactNtp(ntp1)); |
| 44 | |
| 45 | uint32_t ntp_diff = CompactNtp(ntp2) - CompactNtp(ntp1); |
Danil Chapovalov | c1e55c7 | 2016-03-09 15:14:35 +0100 | [diff] [blame] | 46 | int64_t ntp_to_ms_diff = CompactNtpRttToMs(ntp_diff); |
danilchap | 1227e8b | 2015-12-21 11:06:50 -0800 | [diff] [blame] | 47 | |
| 48 | EXPECT_NEAR(ms_diff, ntp_to_ms_diff, 1); |
| 49 | } |
| 50 | |
Danil Chapovalov | c1e55c7 | 2016-03-09 15:14:35 +0100 | [diff] [blame] | 51 | TEST(TimeUtilTest, CompactNtpRttToMsLarge) { |
| 52 | const NtpTime ntp1(0x10000, 0x00006); |
| 53 | const NtpTime ntp2(0x17fff, 0xffff5); |
| 54 | int64_t ms_diff = ntp2.ToMs() - ntp1.ToMs(); |
| 55 | // Ntp difference close to 2^15 seconds should convert correctly too. |
| 56 | ASSERT_NEAR(ms_diff, ((1 << 15) - 1) * 1000, 1); |
danilchap | 1227e8b | 2015-12-21 11:06:50 -0800 | [diff] [blame] | 57 | uint32_t ntp_diff = CompactNtp(ntp2) - CompactNtp(ntp1); |
Danil Chapovalov | c1e55c7 | 2016-03-09 15:14:35 +0100 | [diff] [blame] | 58 | int64_t ntp_to_ms_diff = CompactNtpRttToMs(ntp_diff); |
danilchap | 1227e8b | 2015-12-21 11:06:50 -0800 | [diff] [blame] | 59 | |
| 60 | EXPECT_NEAR(ms_diff, ntp_to_ms_diff, 1); |
| 61 | } |
Danil Chapovalov | c1e55c7 | 2016-03-09 15:14:35 +0100 | [diff] [blame] | 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 | } |
danilchap | 1227e8b | 2015-12-21 11:06:50 -0800 | [diff] [blame] | 73 | } // namespace webrtc |