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 "api/units/time_delta.h"
12
13 #include <limits>
14
15 #include "test/gtest.h"
16
17 namespace webrtc {
18 namespace test {
TEST(TimeDeltaTest,ConstExpr)19 TEST(TimeDeltaTest, ConstExpr) {
20 constexpr int64_t kValue = -12345;
21 constexpr TimeDelta kTimeDeltaZero = TimeDelta::Zero();
22 constexpr TimeDelta kTimeDeltaPlusInf = TimeDelta::PlusInfinity();
23 constexpr TimeDelta kTimeDeltaMinusInf = TimeDelta::MinusInfinity();
24 static_assert(kTimeDeltaZero.IsZero(), "");
25 static_assert(kTimeDeltaPlusInf.IsPlusInfinity(), "");
26 static_assert(kTimeDeltaMinusInf.IsMinusInfinity(), "");
27 static_assert(kTimeDeltaPlusInf.ms_or(-1) == -1, "");
28
29 static_assert(kTimeDeltaPlusInf > kTimeDeltaZero, "");
30
31 constexpr TimeDelta kTimeDeltaSeconds = TimeDelta::Seconds(kValue);
32 constexpr TimeDelta kTimeDeltaMs = TimeDelta::Millis(kValue);
33 constexpr TimeDelta kTimeDeltaUs = TimeDelta::Micros(kValue);
34
35 static_assert(kTimeDeltaSeconds.seconds_or(0) == kValue, "");
36 static_assert(kTimeDeltaMs.ms_or(0) == kValue, "");
37 static_assert(kTimeDeltaUs.us_or(0) == kValue, "");
38 }
39
TEST(TimeDeltaTest,GetBackSameValues)40 TEST(TimeDeltaTest, GetBackSameValues) {
41 const int64_t kValue = 499;
42 for (int sign = -1; sign <= 1; ++sign) {
43 int64_t value = kValue * sign;
44 EXPECT_EQ(TimeDelta::Millis(value).ms(), value);
45 EXPECT_EQ(TimeDelta::Micros(value).us(), value);
46 EXPECT_EQ(TimeDelta::Seconds(value).seconds(), value);
47 EXPECT_EQ(TimeDelta::Seconds(value).seconds(), value);
48 }
49 EXPECT_EQ(TimeDelta::Zero().us(), 0);
50 }
51
TEST(TimeDeltaTest,GetDifferentPrefix)52 TEST(TimeDeltaTest, GetDifferentPrefix) {
53 const int64_t kValue = 3000000;
54 EXPECT_EQ(TimeDelta::Micros(kValue).seconds(), kValue / 1000000);
55 EXPECT_EQ(TimeDelta::Millis(kValue).seconds(), kValue / 1000);
56 EXPECT_EQ(TimeDelta::Micros(kValue).ms(), kValue / 1000);
57
58 EXPECT_EQ(TimeDelta::Millis(kValue).us(), kValue * 1000);
59 EXPECT_EQ(TimeDelta::Seconds(kValue).ms(), kValue * 1000);
60 EXPECT_EQ(TimeDelta::Seconds(kValue).us(), kValue * 1000000);
61 }
62
TEST(TimeDeltaTest,IdentityChecks)63 TEST(TimeDeltaTest, IdentityChecks) {
64 const int64_t kValue = 3000;
65 EXPECT_TRUE(TimeDelta::Zero().IsZero());
66 EXPECT_FALSE(TimeDelta::Millis(kValue).IsZero());
67
68 EXPECT_TRUE(TimeDelta::PlusInfinity().IsInfinite());
69 EXPECT_TRUE(TimeDelta::MinusInfinity().IsInfinite());
70 EXPECT_FALSE(TimeDelta::Zero().IsInfinite());
71 EXPECT_FALSE(TimeDelta::Millis(-kValue).IsInfinite());
72 EXPECT_FALSE(TimeDelta::Millis(kValue).IsInfinite());
73
74 EXPECT_FALSE(TimeDelta::PlusInfinity().IsFinite());
75 EXPECT_FALSE(TimeDelta::MinusInfinity().IsFinite());
76 EXPECT_TRUE(TimeDelta::Millis(-kValue).IsFinite());
77 EXPECT_TRUE(TimeDelta::Millis(kValue).IsFinite());
78 EXPECT_TRUE(TimeDelta::Zero().IsFinite());
79
80 EXPECT_TRUE(TimeDelta::PlusInfinity().IsPlusInfinity());
81 EXPECT_FALSE(TimeDelta::MinusInfinity().IsPlusInfinity());
82
83 EXPECT_TRUE(TimeDelta::MinusInfinity().IsMinusInfinity());
84 EXPECT_FALSE(TimeDelta::PlusInfinity().IsMinusInfinity());
85 }
86
TEST(TimeDeltaTest,ComparisonOperators)87 TEST(TimeDeltaTest, ComparisonOperators) {
88 const int64_t kSmall = 450;
89 const int64_t kLarge = 451;
90 const TimeDelta small = TimeDelta::Millis(kSmall);
91 const TimeDelta large = TimeDelta::Millis(kLarge);
92
93 EXPECT_EQ(TimeDelta::Zero(), TimeDelta::Millis(0));
94 EXPECT_EQ(TimeDelta::PlusInfinity(), TimeDelta::PlusInfinity());
95 EXPECT_EQ(small, TimeDelta::Millis(kSmall));
96 EXPECT_LE(small, TimeDelta::Millis(kSmall));
97 EXPECT_GE(small, TimeDelta::Millis(kSmall));
98 EXPECT_NE(small, TimeDelta::Millis(kLarge));
99 EXPECT_LE(small, TimeDelta::Millis(kLarge));
100 EXPECT_LT(small, TimeDelta::Millis(kLarge));
101 EXPECT_GE(large, TimeDelta::Millis(kSmall));
102 EXPECT_GT(large, TimeDelta::Millis(kSmall));
103 EXPECT_LT(TimeDelta::Zero(), small);
104 EXPECT_GT(TimeDelta::Zero(), TimeDelta::Millis(-kSmall));
105 EXPECT_GT(TimeDelta::Zero(), TimeDelta::Millis(-kSmall));
106
107 EXPECT_GT(TimeDelta::PlusInfinity(), large);
108 EXPECT_LT(TimeDelta::MinusInfinity(), TimeDelta::Zero());
109 }
110
TEST(TimeDeltaTest,Clamping)111 TEST(TimeDeltaTest, Clamping) {
112 const TimeDelta upper = TimeDelta::Millis(800);
113 const TimeDelta lower = TimeDelta::Millis(100);
114 const TimeDelta under = TimeDelta::Millis(100);
115 const TimeDelta inside = TimeDelta::Millis(500);
116 const TimeDelta over = TimeDelta::Millis(1000);
117 EXPECT_EQ(under.Clamped(lower, upper), lower);
118 EXPECT_EQ(inside.Clamped(lower, upper), inside);
119 EXPECT_EQ(over.Clamped(lower, upper), upper);
120
121 TimeDelta mutable_delta = lower;
122 mutable_delta.Clamp(lower, upper);
123 EXPECT_EQ(mutable_delta, lower);
124 mutable_delta = inside;
125 mutable_delta.Clamp(lower, upper);
126 EXPECT_EQ(mutable_delta, inside);
127 mutable_delta = over;
128 mutable_delta.Clamp(lower, upper);
129 EXPECT_EQ(mutable_delta, upper);
130 }
131
TEST(TimeDeltaTest,CanBeInititializedFromLargeInt)132 TEST(TimeDeltaTest, CanBeInititializedFromLargeInt) {
133 const int kMaxInt = std::numeric_limits<int>::max();
134 EXPECT_EQ(TimeDelta::Seconds(kMaxInt).us(),
135 static_cast<int64_t>(kMaxInt) * 1000000);
136 EXPECT_EQ(TimeDelta::Millis(kMaxInt).us(),
137 static_cast<int64_t>(kMaxInt) * 1000);
138 }
139
TEST(TimeDeltaTest,ConvertsToAndFromDouble)140 TEST(TimeDeltaTest, ConvertsToAndFromDouble) {
141 const int64_t kMicros = 17017;
142 const double kNanosDouble = kMicros * 1e3;
143 const double kMicrosDouble = kMicros;
144 const double kMillisDouble = kMicros * 1e-3;
145 const double kSecondsDouble = kMillisDouble * 1e-3;
146
147 EXPECT_EQ(TimeDelta::Micros(kMicros).seconds<double>(), kSecondsDouble);
148 EXPECT_EQ(TimeDelta::Seconds(kSecondsDouble).us(), kMicros);
149
150 EXPECT_EQ(TimeDelta::Micros(kMicros).ms<double>(), kMillisDouble);
151 EXPECT_EQ(TimeDelta::Millis(kMillisDouble).us(), kMicros);
152
153 EXPECT_EQ(TimeDelta::Micros(kMicros).us<double>(), kMicrosDouble);
154 EXPECT_EQ(TimeDelta::Micros(kMicrosDouble).us(), kMicros);
155
156 EXPECT_NEAR(TimeDelta::Micros(kMicros).ns<double>(), kNanosDouble, 1);
157
158 const double kPlusInfinity = std::numeric_limits<double>::infinity();
159 const double kMinusInfinity = -kPlusInfinity;
160
161 EXPECT_EQ(TimeDelta::PlusInfinity().seconds<double>(), kPlusInfinity);
162 EXPECT_EQ(TimeDelta::MinusInfinity().seconds<double>(), kMinusInfinity);
163 EXPECT_EQ(TimeDelta::PlusInfinity().ms<double>(), kPlusInfinity);
164 EXPECT_EQ(TimeDelta::MinusInfinity().ms<double>(), kMinusInfinity);
165 EXPECT_EQ(TimeDelta::PlusInfinity().us<double>(), kPlusInfinity);
166 EXPECT_EQ(TimeDelta::MinusInfinity().us<double>(), kMinusInfinity);
167 EXPECT_EQ(TimeDelta::PlusInfinity().ns<double>(), kPlusInfinity);
168 EXPECT_EQ(TimeDelta::MinusInfinity().ns<double>(), kMinusInfinity);
169
170 EXPECT_TRUE(TimeDelta::Seconds(kPlusInfinity).IsPlusInfinity());
171 EXPECT_TRUE(TimeDelta::Seconds(kMinusInfinity).IsMinusInfinity());
172 EXPECT_TRUE(TimeDelta::Millis(kPlusInfinity).IsPlusInfinity());
173 EXPECT_TRUE(TimeDelta::Millis(kMinusInfinity).IsMinusInfinity());
174 EXPECT_TRUE(TimeDelta::Micros(kPlusInfinity).IsPlusInfinity());
175 EXPECT_TRUE(TimeDelta::Micros(kMinusInfinity).IsMinusInfinity());
176 }
177
TEST(TimeDeltaTest,MathOperations)178 TEST(TimeDeltaTest, MathOperations) {
179 const int64_t kValueA = 267;
180 const int64_t kValueB = 450;
181 const TimeDelta delta_a = TimeDelta::Millis(kValueA);
182 const TimeDelta delta_b = TimeDelta::Millis(kValueB);
183 EXPECT_EQ((delta_a + delta_b).ms(), kValueA + kValueB);
184 EXPECT_EQ((delta_a - delta_b).ms(), kValueA - kValueB);
185
186 const int32_t kInt32Value = 123;
187 const double kFloatValue = 123.0;
188 EXPECT_EQ((TimeDelta::Micros(kValueA) * kValueB).us(), kValueA * kValueB);
189 EXPECT_EQ((TimeDelta::Micros(kValueA) * kInt32Value).us(),
190 kValueA * kInt32Value);
191 EXPECT_EQ((TimeDelta::Micros(kValueA) * kFloatValue).us(),
192 kValueA * kFloatValue);
193
194 EXPECT_EQ((delta_b / 10).ms(), kValueB / 10);
195 EXPECT_EQ(delta_b / delta_a, static_cast<double>(kValueB) / kValueA);
196
197 EXPECT_EQ(TimeDelta::Micros(-kValueA).Abs().us(), kValueA);
198 EXPECT_EQ(TimeDelta::Micros(kValueA).Abs().us(), kValueA);
199
200 TimeDelta mutable_delta = TimeDelta::Millis(kValueA);
201 mutable_delta += TimeDelta::Millis(kValueB);
202 EXPECT_EQ(mutable_delta, TimeDelta::Millis(kValueA + kValueB));
203 mutable_delta -= TimeDelta::Millis(kValueB);
204 EXPECT_EQ(mutable_delta, TimeDelta::Millis(kValueA));
205 }
206
TEST(TimeDeltaTest,InfinityOperations)207 TEST(TimeDeltaTest, InfinityOperations) {
208 const int64_t kValue = 267;
209 const TimeDelta finite = TimeDelta::Millis(kValue);
210 EXPECT_TRUE((TimeDelta::PlusInfinity() + finite).IsPlusInfinity());
211 EXPECT_TRUE((TimeDelta::PlusInfinity() - finite).IsPlusInfinity());
212 EXPECT_TRUE((finite + TimeDelta::PlusInfinity()).IsPlusInfinity());
213 EXPECT_TRUE((finite - TimeDelta::MinusInfinity()).IsPlusInfinity());
214
215 EXPECT_TRUE((TimeDelta::MinusInfinity() + finite).IsMinusInfinity());
216 EXPECT_TRUE((TimeDelta::MinusInfinity() - finite).IsMinusInfinity());
217 EXPECT_TRUE((finite + TimeDelta::MinusInfinity()).IsMinusInfinity());
218 EXPECT_TRUE((finite - TimeDelta::PlusInfinity()).IsMinusInfinity());
219 }
220 } // namespace test
221 } // namespace webrtc
222