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 
11 #include "system_wrappers/include/ntp_time.h"
12 #include "system_wrappers/include/clock.h"
13 #include "test/gtest.h"
14 
15 namespace webrtc {
16 namespace {
17 
18 const uint32_t kNtpSec = 0x12345678;
19 const uint32_t kNtpFrac = 0x23456789;
20 
TEST(NtpTimeTest,NoValueMeansInvalid)21 TEST(NtpTimeTest, NoValueMeansInvalid) {
22   NtpTime ntp;
23   EXPECT_FALSE(ntp.Valid());
24 }
25 
TEST(NtpTimeTest,CanResetValue)26 TEST(NtpTimeTest, CanResetValue) {
27   NtpTime ntp(kNtpSec, kNtpFrac);
28   EXPECT_TRUE(ntp.Valid());
29   ntp.Reset();
30   EXPECT_FALSE(ntp.Valid());
31 }
32 
TEST(NtpTimeTest,CanGetWhatIsSet)33 TEST(NtpTimeTest, CanGetWhatIsSet) {
34   NtpTime ntp;
35   ntp.Set(kNtpSec, kNtpFrac);
36   EXPECT_EQ(kNtpSec, ntp.seconds());
37   EXPECT_EQ(kNtpFrac, ntp.fractions());
38 }
39 
TEST(NtpTimeTest,SetIsSameAs2ParameterConstructor)40 TEST(NtpTimeTest, SetIsSameAs2ParameterConstructor) {
41   NtpTime ntp1(kNtpSec, kNtpFrac);
42   NtpTime ntp2;
43   EXPECT_NE(ntp1, ntp2);
44 
45   ntp2.Set(kNtpSec, kNtpFrac);
46   EXPECT_EQ(ntp1, ntp2);
47 }
48 
TEST(NtpTimeTest,ToMsMeansToNtpMilliseconds)49 TEST(NtpTimeTest, ToMsMeansToNtpMilliseconds) {
50   SimulatedClock clock(0x123456789abc);
51 
52   NtpTime ntp = clock.CurrentNtpTime();
53   EXPECT_EQ(ntp.ToMs(), Clock::NtpToMs(ntp.seconds(), ntp.fractions()));
54   EXPECT_EQ(ntp.ToMs(), clock.CurrentNtpInMilliseconds());
55 }
56 
TEST(NtpTimeTest,CanExplicitlyConvertToAndFromUint64)57 TEST(NtpTimeTest, CanExplicitlyConvertToAndFromUint64) {
58   uint64_t untyped_time = 0x123456789;
59   NtpTime time(untyped_time);
60   EXPECT_EQ(untyped_time, static_cast<uint64_t>(time));
61   EXPECT_EQ(NtpTime(0x12345678, 0x90abcdef), NtpTime(0x1234567890abcdef));
62 }
63 
64 }  // namespace
65 }  // namespace webrtc
66