1 /*
2 * Copyright (c) 2019 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 #include "api/units/frequency.h"
11
12 #include <limits>
13
14 #include "test/gtest.h"
15
16 namespace webrtc {
17 namespace test {
TEST(FrequencyTest,ConstExpr)18 TEST(FrequencyTest, ConstExpr) {
19 constexpr Frequency kFrequencyZero = Frequency::Zero();
20 constexpr Frequency kFrequencyPlusInf = Frequency::PlusInfinity();
21 constexpr Frequency kFrequencyMinusInf = Frequency::MinusInfinity();
22 static_assert(kFrequencyZero.IsZero(), "");
23 static_assert(kFrequencyPlusInf.IsPlusInfinity(), "");
24 static_assert(kFrequencyMinusInf.IsMinusInfinity(), "");
25
26 static_assert(kFrequencyPlusInf > kFrequencyZero, "");
27 }
28
TEST(FrequencyTest,GetBackSameValues)29 TEST(FrequencyTest, GetBackSameValues) {
30 const int64_t kValue = 31;
31 EXPECT_EQ(Frequency::Hertz(kValue).hertz<int64_t>(), kValue);
32 EXPECT_EQ(Frequency::Zero().hertz<int64_t>(), 0);
33 }
34
TEST(FrequencyTest,GetDifferentPrefix)35 TEST(FrequencyTest, GetDifferentPrefix) {
36 const int64_t kValue = 30000;
37 EXPECT_EQ(Frequency::MilliHertz(kValue).hertz<int64_t>(), kValue / 1000);
38 EXPECT_EQ(Frequency::Hertz(kValue).millihertz(), kValue * 1000);
39 EXPECT_EQ(Frequency::KiloHertz(kValue).hertz(), kValue * 1000);
40 }
41
TEST(FrequencyTest,IdentityChecks)42 TEST(FrequencyTest, IdentityChecks) {
43 const int64_t kValue = 31;
44 EXPECT_TRUE(Frequency::Zero().IsZero());
45 EXPECT_FALSE(Frequency::Hertz(kValue).IsZero());
46
47 EXPECT_TRUE(Frequency::PlusInfinity().IsInfinite());
48 EXPECT_TRUE(Frequency::MinusInfinity().IsInfinite());
49 EXPECT_FALSE(Frequency::Zero().IsInfinite());
50 EXPECT_FALSE(Frequency::Hertz(kValue).IsInfinite());
51
52 EXPECT_FALSE(Frequency::PlusInfinity().IsFinite());
53 EXPECT_FALSE(Frequency::MinusInfinity().IsFinite());
54 EXPECT_TRUE(Frequency::Hertz(kValue).IsFinite());
55 EXPECT_TRUE(Frequency::Zero().IsFinite());
56
57 EXPECT_TRUE(Frequency::PlusInfinity().IsPlusInfinity());
58 EXPECT_FALSE(Frequency::MinusInfinity().IsPlusInfinity());
59
60 EXPECT_TRUE(Frequency::MinusInfinity().IsMinusInfinity());
61 EXPECT_FALSE(Frequency::PlusInfinity().IsMinusInfinity());
62 }
63
TEST(FrequencyTest,ComparisonOperators)64 TEST(FrequencyTest, ComparisonOperators) {
65 const int64_t kSmall = 42;
66 const int64_t kLarge = 45;
67 const Frequency small = Frequency::Hertz(kSmall);
68 const Frequency large = Frequency::Hertz(kLarge);
69
70 EXPECT_EQ(Frequency::Zero(), Frequency::Hertz(0));
71 EXPECT_EQ(Frequency::PlusInfinity(), Frequency::PlusInfinity());
72 EXPECT_EQ(small, Frequency::Hertz(kSmall));
73 EXPECT_LE(small, Frequency::Hertz(kSmall));
74 EXPECT_GE(small, Frequency::Hertz(kSmall));
75 EXPECT_NE(small, Frequency::Hertz(kLarge));
76 EXPECT_LE(small, Frequency::Hertz(kLarge));
77 EXPECT_LT(small, Frequency::Hertz(kLarge));
78 EXPECT_GE(large, Frequency::Hertz(kSmall));
79 EXPECT_GT(large, Frequency::Hertz(kSmall));
80 EXPECT_LT(Frequency::Zero(), small);
81
82 EXPECT_GT(Frequency::PlusInfinity(), large);
83 EXPECT_LT(Frequency::MinusInfinity(), Frequency::Zero());
84 }
85
TEST(FrequencyTest,Clamping)86 TEST(FrequencyTest, Clamping) {
87 const Frequency upper = Frequency::Hertz(800);
88 const Frequency lower = Frequency::Hertz(100);
89 const Frequency under = Frequency::Hertz(100);
90 const Frequency inside = Frequency::Hertz(500);
91 const Frequency over = Frequency::Hertz(1000);
92 EXPECT_EQ(under.Clamped(lower, upper), lower);
93 EXPECT_EQ(inside.Clamped(lower, upper), inside);
94 EXPECT_EQ(over.Clamped(lower, upper), upper);
95
96 Frequency mutable_frequency = lower;
97 mutable_frequency.Clamp(lower, upper);
98 EXPECT_EQ(mutable_frequency, lower);
99 mutable_frequency = inside;
100 mutable_frequency.Clamp(lower, upper);
101 EXPECT_EQ(mutable_frequency, inside);
102 mutable_frequency = over;
103 mutable_frequency.Clamp(lower, upper);
104 EXPECT_EQ(mutable_frequency, upper);
105 }
106
TEST(FrequencyTest,MathOperations)107 TEST(FrequencyTest, MathOperations) {
108 const int64_t kValueA = 457;
109 const int64_t kValueB = 260;
110 const Frequency frequency_a = Frequency::Hertz(kValueA);
111 const Frequency frequency_b = Frequency::Hertz(kValueB);
112 EXPECT_EQ((frequency_a + frequency_b).hertz<int64_t>(), kValueA + kValueB);
113 EXPECT_EQ((frequency_a - frequency_b).hertz<int64_t>(), kValueA - kValueB);
114
115 EXPECT_EQ((Frequency::Hertz(kValueA) * kValueB).hertz<int64_t>(),
116 kValueA * kValueB);
117
118 EXPECT_EQ((frequency_b / 10).hertz<int64_t>(), kValueB / 10);
119 EXPECT_EQ(frequency_b / frequency_a, static_cast<double>(kValueB) / kValueA);
120
121 Frequency mutable_frequency = Frequency::Hertz(kValueA);
122 mutable_frequency += Frequency::Hertz(kValueB);
123 EXPECT_EQ(mutable_frequency, Frequency::Hertz(kValueA + kValueB));
124 mutable_frequency -= Frequency::Hertz(kValueB);
125 EXPECT_EQ(mutable_frequency, Frequency::Hertz(kValueA));
126 }
TEST(FrequencyTest,Rounding)127 TEST(FrequencyTest, Rounding) {
128 const Frequency freq_high = Frequency::Hertz(23.976);
129 EXPECT_EQ(freq_high.hertz(), 24);
130 EXPECT_EQ(freq_high.RoundDownTo(Frequency::Hertz(1)), Frequency::Hertz(23));
131 EXPECT_EQ(freq_high.RoundTo(Frequency::Hertz(1)), Frequency::Hertz(24));
132 EXPECT_EQ(freq_high.RoundUpTo(Frequency::Hertz(1)), Frequency::Hertz(24));
133
134 const Frequency freq_low = Frequency::Hertz(23.4);
135 EXPECT_EQ(freq_low.hertz(), 23);
136 EXPECT_EQ(freq_low.RoundDownTo(Frequency::Hertz(1)), Frequency::Hertz(23));
137 EXPECT_EQ(freq_low.RoundTo(Frequency::Hertz(1)), Frequency::Hertz(23));
138 EXPECT_EQ(freq_low.RoundUpTo(Frequency::Hertz(1)), Frequency::Hertz(24));
139 }
140
TEST(FrequencyTest,InfinityOperations)141 TEST(FrequencyTest, InfinityOperations) {
142 const double kValue = 267;
143 const Frequency finite = Frequency::Hertz(kValue);
144 EXPECT_TRUE((Frequency::PlusInfinity() + finite).IsPlusInfinity());
145 EXPECT_TRUE((Frequency::PlusInfinity() - finite).IsPlusInfinity());
146 EXPECT_TRUE((finite + Frequency::PlusInfinity()).IsPlusInfinity());
147 EXPECT_TRUE((finite - Frequency::MinusInfinity()).IsPlusInfinity());
148
149 EXPECT_TRUE((Frequency::MinusInfinity() + finite).IsMinusInfinity());
150 EXPECT_TRUE((Frequency::MinusInfinity() - finite).IsMinusInfinity());
151 EXPECT_TRUE((finite + Frequency::MinusInfinity()).IsMinusInfinity());
152 EXPECT_TRUE((finite - Frequency::PlusInfinity()).IsMinusInfinity());
153 }
154
TEST(UnitConversionTest,TimeDeltaAndFrequency)155 TEST(UnitConversionTest, TimeDeltaAndFrequency) {
156 EXPECT_EQ(1 / Frequency::Hertz(50), TimeDelta::Millis(20));
157 EXPECT_EQ(1 / TimeDelta::Millis(20), Frequency::Hertz(50));
158 EXPECT_EQ(Frequency::KiloHertz(200) * TimeDelta::Millis(2), 400.0);
159 }
160 } // namespace test
161 } // namespace webrtc
162