danilchap | b1ac203 | 2015-11-26 09:01:10 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "system_wrappers/include/ntp_time.h" |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 12 | |
Karl Wiberg | 79eb1d9 | 2017-11-08 12:26:07 +0100 | [diff] [blame] | 13 | #include "system_wrappers/include/clock.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 14 | #include "test/gtest.h" |
danilchap | b1ac203 | 2015-11-26 09:01:10 -0800 | [diff] [blame] | 15 | |
| 16 | namespace webrtc { |
| 17 | namespace { |
| 18 | |
| 19 | const uint32_t kNtpSec = 0x12345678; |
| 20 | const uint32_t kNtpFrac = 0x23456789; |
| 21 | |
| 22 | TEST(NtpTimeTest, NoValueMeansInvalid) { |
| 23 | NtpTime ntp; |
| 24 | EXPECT_FALSE(ntp.Valid()); |
| 25 | } |
| 26 | |
| 27 | TEST(NtpTimeTest, CanResetValue) { |
| 28 | NtpTime ntp(kNtpSec, kNtpFrac); |
| 29 | EXPECT_TRUE(ntp.Valid()); |
| 30 | ntp.Reset(); |
| 31 | EXPECT_FALSE(ntp.Valid()); |
| 32 | } |
| 33 | |
| 34 | TEST(NtpTimeTest, CanGetWhatIsSet) { |
| 35 | NtpTime ntp; |
| 36 | ntp.Set(kNtpSec, kNtpFrac); |
| 37 | EXPECT_EQ(kNtpSec, ntp.seconds()); |
| 38 | EXPECT_EQ(kNtpFrac, ntp.fractions()); |
| 39 | } |
| 40 | |
| 41 | TEST(NtpTimeTest, SetIsSameAs2ParameterConstructor) { |
| 42 | NtpTime ntp1(kNtpSec, kNtpFrac); |
| 43 | NtpTime ntp2; |
| 44 | EXPECT_NE(ntp1, ntp2); |
| 45 | |
| 46 | ntp2.Set(kNtpSec, kNtpFrac); |
| 47 | EXPECT_EQ(ntp1, ntp2); |
| 48 | } |
| 49 | |
danilchap | b1ac203 | 2015-11-26 09:01:10 -0800 | [diff] [blame] | 50 | TEST(NtpTimeTest, ToMsMeansToNtpMilliseconds) { |
| 51 | SimulatedClock clock(0x123456789abc); |
| 52 | |
danilchap | 3795376 | 2017-02-09 11:15:25 -0800 | [diff] [blame] | 53 | NtpTime ntp = clock.CurrentNtpTime(); |
danilchap | b1ac203 | 2015-11-26 09:01:10 -0800 | [diff] [blame] | 54 | EXPECT_EQ(ntp.ToMs(), Clock::NtpToMs(ntp.seconds(), ntp.fractions())); |
| 55 | EXPECT_EQ(ntp.ToMs(), clock.CurrentNtpInMilliseconds()); |
| 56 | } |
| 57 | |
danilchap | 27260ce | 2017-02-15 01:18:15 -0800 | [diff] [blame] | 58 | TEST(NtpTimeTest, CanExplicitlyConvertToAndFromUint64) { |
| 59 | uint64_t untyped_time = 0x123456789; |
| 60 | NtpTime time(untyped_time); |
| 61 | EXPECT_EQ(untyped_time, static_cast<uint64_t>(time)); |
| 62 | EXPECT_EQ(NtpTime(0x12345678, 0x90abcdef), NtpTime(0x1234567890abcdef)); |
| 63 | } |
| 64 | |
danilchap | b1ac203 | 2015-11-26 09:01:10 -0800 | [diff] [blame] | 65 | } // namespace |
| 66 | } // namespace webrtc |