1 /*
2 * Copyright (c) 2018 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 <limits>
12
13 #include "api/units/timestamp.h"
14 #include "test/gtest.h"
15
16 namespace webrtc {
17 namespace test {
TEST(TimestampTest,ConstExpr)18 TEST(TimestampTest, ConstExpr) {
19 constexpr int64_t kValue = 12345;
20 constexpr Timestamp kTimestampInf = Timestamp::PlusInfinity();
21 static_assert(kTimestampInf.IsInfinite(), "");
22 static_assert(kTimestampInf.ms_or(-1) == -1, "");
23
24 constexpr Timestamp kTimestampSeconds = Timestamp::Seconds(kValue);
25 constexpr Timestamp kTimestampMs = Timestamp::Millis(kValue);
26 constexpr Timestamp kTimestampUs = Timestamp::Micros(kValue);
27
28 static_assert(kTimestampSeconds.seconds_or(0) == kValue, "");
29 static_assert(kTimestampMs.ms_or(0) == kValue, "");
30 static_assert(kTimestampUs.us_or(0) == kValue, "");
31
32 static_assert(kTimestampMs > kTimestampUs, "");
33
34 EXPECT_EQ(kTimestampSeconds.seconds(), kValue);
35 EXPECT_EQ(kTimestampMs.ms(), kValue);
36 EXPECT_EQ(kTimestampUs.us(), kValue);
37 }
38
TEST(TimestampTest,GetBackSameValues)39 TEST(TimestampTest, GetBackSameValues) {
40 const int64_t kValue = 499;
41 EXPECT_EQ(Timestamp::Millis(kValue).ms(), kValue);
42 EXPECT_EQ(Timestamp::Micros(kValue).us(), kValue);
43 EXPECT_EQ(Timestamp::Seconds(kValue).seconds(), kValue);
44 }
45
TEST(TimestampTest,GetDifferentPrefix)46 TEST(TimestampTest, GetDifferentPrefix) {
47 const int64_t kValue = 3000000;
48 EXPECT_EQ(Timestamp::Micros(kValue).seconds(), kValue / 1000000);
49 EXPECT_EQ(Timestamp::Millis(kValue).seconds(), kValue / 1000);
50 EXPECT_EQ(Timestamp::Micros(kValue).ms(), kValue / 1000);
51
52 EXPECT_EQ(Timestamp::Millis(kValue).us(), kValue * 1000);
53 EXPECT_EQ(Timestamp::Seconds(kValue).ms(), kValue * 1000);
54 EXPECT_EQ(Timestamp::Seconds(kValue).us(), kValue * 1000000);
55 }
56
TEST(TimestampTest,IdentityChecks)57 TEST(TimestampTest, IdentityChecks) {
58 const int64_t kValue = 3000;
59
60 EXPECT_TRUE(Timestamp::PlusInfinity().IsInfinite());
61 EXPECT_TRUE(Timestamp::MinusInfinity().IsInfinite());
62 EXPECT_FALSE(Timestamp::Millis(kValue).IsInfinite());
63
64 EXPECT_FALSE(Timestamp::PlusInfinity().IsFinite());
65 EXPECT_FALSE(Timestamp::MinusInfinity().IsFinite());
66 EXPECT_TRUE(Timestamp::Millis(kValue).IsFinite());
67
68 EXPECT_TRUE(Timestamp::PlusInfinity().IsPlusInfinity());
69 EXPECT_FALSE(Timestamp::MinusInfinity().IsPlusInfinity());
70
71 EXPECT_TRUE(Timestamp::MinusInfinity().IsMinusInfinity());
72 EXPECT_FALSE(Timestamp::PlusInfinity().IsMinusInfinity());
73 }
74
TEST(TimestampTest,ComparisonOperators)75 TEST(TimestampTest, ComparisonOperators) {
76 const int64_t kSmall = 450;
77 const int64_t kLarge = 451;
78
79 EXPECT_EQ(Timestamp::PlusInfinity(), Timestamp::PlusInfinity());
80 EXPECT_GE(Timestamp::PlusInfinity(), Timestamp::PlusInfinity());
81 EXPECT_GT(Timestamp::PlusInfinity(), Timestamp::Millis(kLarge));
82 EXPECT_EQ(Timestamp::Millis(kSmall), Timestamp::Millis(kSmall));
83 EXPECT_LE(Timestamp::Millis(kSmall), Timestamp::Millis(kSmall));
84 EXPECT_GE(Timestamp::Millis(kSmall), Timestamp::Millis(kSmall));
85 EXPECT_NE(Timestamp::Millis(kSmall), Timestamp::Millis(kLarge));
86 EXPECT_LE(Timestamp::Millis(kSmall), Timestamp::Millis(kLarge));
87 EXPECT_LT(Timestamp::Millis(kSmall), Timestamp::Millis(kLarge));
88 EXPECT_GE(Timestamp::Millis(kLarge), Timestamp::Millis(kSmall));
89 EXPECT_GT(Timestamp::Millis(kLarge), Timestamp::Millis(kSmall));
90 }
91
TEST(TimestampTest,CanBeInititializedFromLargeInt)92 TEST(TimestampTest, CanBeInititializedFromLargeInt) {
93 const int kMaxInt = std::numeric_limits<int>::max();
94 EXPECT_EQ(Timestamp::Seconds(kMaxInt).us(),
95 static_cast<int64_t>(kMaxInt) * 1000000);
96 EXPECT_EQ(Timestamp::Millis(kMaxInt).us(),
97 static_cast<int64_t>(kMaxInt) * 1000);
98 }
99
TEST(TimestampTest,ConvertsToAndFromDouble)100 TEST(TimestampTest, ConvertsToAndFromDouble) {
101 const int64_t kMicros = 17017;
102 const double kMicrosDouble = kMicros;
103 const double kMillisDouble = kMicros * 1e-3;
104 const double kSecondsDouble = kMillisDouble * 1e-3;
105
106 EXPECT_EQ(Timestamp::Micros(kMicros).seconds<double>(), kSecondsDouble);
107 EXPECT_EQ(Timestamp::Seconds(kSecondsDouble).us(), kMicros);
108
109 EXPECT_EQ(Timestamp::Micros(kMicros).ms<double>(), kMillisDouble);
110 EXPECT_EQ(Timestamp::Millis(kMillisDouble).us(), kMicros);
111
112 EXPECT_EQ(Timestamp::Micros(kMicros).us<double>(), kMicrosDouble);
113 EXPECT_EQ(Timestamp::Micros(kMicrosDouble).us(), kMicros);
114
115 const double kPlusInfinity = std::numeric_limits<double>::infinity();
116 const double kMinusInfinity = -kPlusInfinity;
117
118 EXPECT_EQ(Timestamp::PlusInfinity().seconds<double>(), kPlusInfinity);
119 EXPECT_EQ(Timestamp::MinusInfinity().seconds<double>(), kMinusInfinity);
120 EXPECT_EQ(Timestamp::PlusInfinity().ms<double>(), kPlusInfinity);
121 EXPECT_EQ(Timestamp::MinusInfinity().ms<double>(), kMinusInfinity);
122 EXPECT_EQ(Timestamp::PlusInfinity().us<double>(), kPlusInfinity);
123 EXPECT_EQ(Timestamp::MinusInfinity().us<double>(), kMinusInfinity);
124
125 EXPECT_TRUE(Timestamp::Seconds(kPlusInfinity).IsPlusInfinity());
126 EXPECT_TRUE(Timestamp::Seconds(kMinusInfinity).IsMinusInfinity());
127 EXPECT_TRUE(Timestamp::Millis(kPlusInfinity).IsPlusInfinity());
128 EXPECT_TRUE(Timestamp::Millis(kMinusInfinity).IsMinusInfinity());
129 EXPECT_TRUE(Timestamp::Micros(kPlusInfinity).IsPlusInfinity());
130 EXPECT_TRUE(Timestamp::Micros(kMinusInfinity).IsMinusInfinity());
131 }
132
TEST(UnitConversionTest,TimestampAndTimeDeltaMath)133 TEST(UnitConversionTest, TimestampAndTimeDeltaMath) {
134 const int64_t kValueA = 267;
135 const int64_t kValueB = 450;
136 const Timestamp time_a = Timestamp::Millis(kValueA);
137 const Timestamp time_b = Timestamp::Millis(kValueB);
138 const TimeDelta delta_a = TimeDelta::Millis(kValueA);
139 const TimeDelta delta_b = TimeDelta::Millis(kValueB);
140
141 EXPECT_EQ((time_a - time_b), TimeDelta::Millis(kValueA - kValueB));
142 EXPECT_EQ((time_b - delta_a), Timestamp::Millis(kValueB - kValueA));
143 EXPECT_EQ((time_b + delta_a), Timestamp::Millis(kValueB + kValueA));
144
145 Timestamp mutable_time = time_a;
146 mutable_time += delta_b;
147 EXPECT_EQ(mutable_time, time_a + delta_b);
148 mutable_time -= delta_b;
149 EXPECT_EQ(mutable_time, time_a);
150 }
151
TEST(UnitConversionTest,InfinityOperations)152 TEST(UnitConversionTest, InfinityOperations) {
153 const int64_t kValue = 267;
154 const Timestamp finite_time = Timestamp::Millis(kValue);
155 const TimeDelta finite_delta = TimeDelta::Millis(kValue);
156 EXPECT_TRUE((Timestamp::PlusInfinity() + finite_delta).IsInfinite());
157 EXPECT_TRUE((Timestamp::PlusInfinity() - finite_delta).IsInfinite());
158 EXPECT_TRUE((finite_time + TimeDelta::PlusInfinity()).IsInfinite());
159 EXPECT_TRUE((finite_time - TimeDelta::MinusInfinity()).IsInfinite());
160 }
161 } // namespace test
162 } // namespace webrtc
163