blob: f45273fb94950f722b11842d5ed1a2473b30c0e6 [file] [log] [blame]
danilchapb1ac2032015-11-26 09:01:10 -08001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "system_wrappers/include/ntp_time.h"
Jonas Olssona4d87372019-07-05 19:08:33 +020012
Karl Wiberg79eb1d92017-11-08 12:26:07 +010013#include "system_wrappers/include/clock.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "test/gtest.h"
danilchapb1ac2032015-11-26 09:01:10 -080015
16namespace webrtc {
17namespace {
18
19const uint32_t kNtpSec = 0x12345678;
20const uint32_t kNtpFrac = 0x23456789;
21
22TEST(NtpTimeTest, NoValueMeansInvalid) {
23 NtpTime ntp;
24 EXPECT_FALSE(ntp.Valid());
25}
26
27TEST(NtpTimeTest, CanResetValue) {
28 NtpTime ntp(kNtpSec, kNtpFrac);
29 EXPECT_TRUE(ntp.Valid());
30 ntp.Reset();
31 EXPECT_FALSE(ntp.Valid());
32}
33
34TEST(NtpTimeTest, CanGetWhatIsSet) {
35 NtpTime ntp;
36 ntp.Set(kNtpSec, kNtpFrac);
37 EXPECT_EQ(kNtpSec, ntp.seconds());
38 EXPECT_EQ(kNtpFrac, ntp.fractions());
39}
40
41TEST(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
danilchapb1ac2032015-11-26 09:01:10 -080050TEST(NtpTimeTest, ToMsMeansToNtpMilliseconds) {
51 SimulatedClock clock(0x123456789abc);
52
danilchap37953762017-02-09 11:15:25 -080053 NtpTime ntp = clock.CurrentNtpTime();
danilchapb1ac2032015-11-26 09:01:10 -080054 EXPECT_EQ(ntp.ToMs(), Clock::NtpToMs(ntp.seconds(), ntp.fractions()));
55 EXPECT_EQ(ntp.ToMs(), clock.CurrentNtpInMilliseconds());
56}
57
danilchap27260ce2017-02-15 01:18:15 -080058TEST(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
danilchapb1ac2032015-11-26 09:01:10 -080065} // namespace
66} // namespace webrtc