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/data_rate.h"
12
13 #include "rtc_base/logging.h"
14 #include "test/gtest.h"
15
16 namespace webrtc {
17 namespace test {
18
TEST(DataRateTest,CompilesWithChecksAndLogs)19 TEST(DataRateTest, CompilesWithChecksAndLogs) {
20 DataRate a = DataRate::KilobitsPerSec(300);
21 DataRate b = DataRate::KilobitsPerSec(210);
22 RTC_CHECK_GT(a, b);
23 RTC_LOG(LS_INFO) << a;
24 }
25
TEST(DataRateTest,ConstExpr)26 TEST(DataRateTest, ConstExpr) {
27 constexpr int64_t kValue = 12345;
28 constexpr DataRate kDataRateZero = DataRate::Zero();
29 constexpr DataRate kDataRateInf = DataRate::Infinity();
30 static_assert(kDataRateZero.IsZero(), "");
31 static_assert(kDataRateInf.IsInfinite(), "");
32 static_assert(kDataRateInf.bps_or(-1) == -1, "");
33 static_assert(kDataRateInf > kDataRateZero, "");
34
35 constexpr DataRate kDataRateBps = DataRate::BitsPerSec(kValue);
36 constexpr DataRate kDataRateKbps = DataRate::KilobitsPerSec(kValue);
37 static_assert(kDataRateBps.bps<double>() == kValue, "");
38 static_assert(kDataRateBps.bps_or(0) == kValue, "");
39 static_assert(kDataRateKbps.kbps_or(0) == kValue, "");
40 }
41
TEST(DataRateTest,GetBackSameValues)42 TEST(DataRateTest, GetBackSameValues) {
43 const int64_t kValue = 123 * 8;
44 EXPECT_EQ(DataRate::BitsPerSec(kValue).bps(), kValue);
45 EXPECT_EQ(DataRate::KilobitsPerSec(kValue).kbps(), kValue);
46 }
47
TEST(DataRateTest,GetDifferentPrefix)48 TEST(DataRateTest, GetDifferentPrefix) {
49 const int64_t kValue = 123 * 8000;
50 EXPECT_EQ(DataRate::BitsPerSec(kValue).kbps(), kValue / 1000);
51 }
52
TEST(DataRateTest,IdentityChecks)53 TEST(DataRateTest, IdentityChecks) {
54 const int64_t kValue = 3000;
55 EXPECT_TRUE(DataRate::Zero().IsZero());
56 EXPECT_FALSE(DataRate::BitsPerSec(kValue).IsZero());
57
58 EXPECT_TRUE(DataRate::Infinity().IsInfinite());
59 EXPECT_FALSE(DataRate::Zero().IsInfinite());
60 EXPECT_FALSE(DataRate::BitsPerSec(kValue).IsInfinite());
61
62 EXPECT_FALSE(DataRate::Infinity().IsFinite());
63 EXPECT_TRUE(DataRate::BitsPerSec(kValue).IsFinite());
64 EXPECT_TRUE(DataRate::Zero().IsFinite());
65 }
66
TEST(DataRateTest,ComparisonOperators)67 TEST(DataRateTest, ComparisonOperators) {
68 const int64_t kSmall = 450;
69 const int64_t kLarge = 451;
70 const DataRate small = DataRate::BitsPerSec(kSmall);
71 const DataRate large = DataRate::BitsPerSec(kLarge);
72
73 EXPECT_EQ(DataRate::Zero(), DataRate::BitsPerSec(0));
74 EXPECT_EQ(DataRate::Infinity(), DataRate::Infinity());
75 EXPECT_EQ(small, small);
76 EXPECT_LE(small, small);
77 EXPECT_GE(small, small);
78 EXPECT_NE(small, large);
79 EXPECT_LE(small, large);
80 EXPECT_LT(small, large);
81 EXPECT_GE(large, small);
82 EXPECT_GT(large, small);
83 EXPECT_LT(DataRate::Zero(), small);
84 EXPECT_GT(DataRate::Infinity(), large);
85 }
86
TEST(DataRateTest,ConvertsToAndFromDouble)87 TEST(DataRateTest, ConvertsToAndFromDouble) {
88 const int64_t kValue = 128;
89 const double kDoubleValue = static_cast<double>(kValue);
90 const double kDoubleKbps = kValue * 1e-3;
91 const double kFloatKbps = static_cast<float>(kDoubleKbps);
92
93 EXPECT_EQ(DataRate::BitsPerSec(kValue).bps<double>(), kDoubleValue);
94 EXPECT_EQ(DataRate::BitsPerSec(kValue).kbps<double>(), kDoubleKbps);
95 EXPECT_EQ(DataRate::BitsPerSec(kValue).kbps<float>(), kFloatKbps);
96 EXPECT_EQ(DataRate::BitsPerSec(kDoubleValue).bps(), kValue);
97 EXPECT_EQ(DataRate::KilobitsPerSec(kDoubleKbps).bps(), kValue);
98
99 const double kInfinity = std::numeric_limits<double>::infinity();
100 EXPECT_EQ(DataRate::Infinity().bps<double>(), kInfinity);
101 EXPECT_TRUE(DataRate::BitsPerSec(kInfinity).IsInfinite());
102 EXPECT_TRUE(DataRate::KilobitsPerSec(kInfinity).IsInfinite());
103 }
TEST(DataRateTest,Clamping)104 TEST(DataRateTest, Clamping) {
105 const DataRate upper = DataRate::KilobitsPerSec(800);
106 const DataRate lower = DataRate::KilobitsPerSec(100);
107 const DataRate under = DataRate::KilobitsPerSec(100);
108 const DataRate inside = DataRate::KilobitsPerSec(500);
109 const DataRate over = DataRate::KilobitsPerSec(1000);
110 EXPECT_EQ(under.Clamped(lower, upper), lower);
111 EXPECT_EQ(inside.Clamped(lower, upper), inside);
112 EXPECT_EQ(over.Clamped(lower, upper), upper);
113
114 DataRate mutable_rate = lower;
115 mutable_rate.Clamp(lower, upper);
116 EXPECT_EQ(mutable_rate, lower);
117 mutable_rate = inside;
118 mutable_rate.Clamp(lower, upper);
119 EXPECT_EQ(mutable_rate, inside);
120 mutable_rate = over;
121 mutable_rate.Clamp(lower, upper);
122 EXPECT_EQ(mutable_rate, upper);
123 }
124
TEST(DataRateTest,MathOperations)125 TEST(DataRateTest, MathOperations) {
126 const int64_t kValueA = 450;
127 const int64_t kValueB = 267;
128 const DataRate rate_a = DataRate::BitsPerSec(kValueA);
129 const DataRate rate_b = DataRate::BitsPerSec(kValueB);
130 const int32_t kInt32Value = 123;
131 const double kFloatValue = 123.0;
132
133 EXPECT_EQ((rate_a + rate_b).bps(), kValueA + kValueB);
134 EXPECT_EQ((rate_a - rate_b).bps(), kValueA - kValueB);
135
136 EXPECT_EQ((rate_a * kValueB).bps(), kValueA * kValueB);
137 EXPECT_EQ((rate_a * kInt32Value).bps(), kValueA * kInt32Value);
138 EXPECT_EQ((rate_a * kFloatValue).bps(), kValueA * kFloatValue);
139
140 EXPECT_EQ(rate_a / rate_b, static_cast<double>(kValueA) / kValueB);
141
142 EXPECT_EQ((rate_a / 10).bps(), kValueA / 10);
143 EXPECT_NEAR((rate_a / 0.5).bps(), kValueA * 2, 1);
144
145 DataRate mutable_rate = DataRate::BitsPerSec(kValueA);
146 mutable_rate += rate_b;
147 EXPECT_EQ(mutable_rate.bps(), kValueA + kValueB);
148 mutable_rate -= rate_a;
149 EXPECT_EQ(mutable_rate.bps(), kValueB);
150 }
151
TEST(UnitConversionTest,DataRateAndDataSizeAndTimeDelta)152 TEST(UnitConversionTest, DataRateAndDataSizeAndTimeDelta) {
153 const int64_t kSeconds = 5;
154 const int64_t kBitsPerSecond = 440;
155 const int64_t kBytes = 44000;
156 const TimeDelta delta_a = TimeDelta::Seconds(kSeconds);
157 const DataRate rate_b = DataRate::BitsPerSec(kBitsPerSecond);
158 const DataSize size_c = DataSize::Bytes(kBytes);
159 EXPECT_EQ((delta_a * rate_b).bytes(), kSeconds * kBitsPerSecond / 8);
160 EXPECT_EQ((rate_b * delta_a).bytes(), kSeconds * kBitsPerSecond / 8);
161 EXPECT_EQ((size_c / delta_a).bps(), kBytes * 8 / kSeconds);
162 EXPECT_EQ((size_c / rate_b).seconds(), kBytes * 8 / kBitsPerSecond);
163 }
164
TEST(UnitConversionTest,DataRateAndDataSizeAndFrequency)165 TEST(UnitConversionTest, DataRateAndDataSizeAndFrequency) {
166 const int64_t kHertz = 30;
167 const int64_t kBitsPerSecond = 96000;
168 const int64_t kBytes = 1200;
169 const Frequency freq_a = Frequency::Hertz(kHertz);
170 const DataRate rate_b = DataRate::BitsPerSec(kBitsPerSecond);
171 const DataSize size_c = DataSize::Bytes(kBytes);
172 EXPECT_EQ((freq_a * size_c).bps(), kHertz * kBytes * 8);
173 EXPECT_EQ((size_c * freq_a).bps(), kHertz * kBytes * 8);
174 EXPECT_EQ((rate_b / size_c).hertz<int64_t>(), kBitsPerSecond / kBytes / 8);
175 EXPECT_EQ((rate_b / freq_a).bytes(), kBitsPerSecond / kHertz / 8);
176 }
177
TEST(UnitConversionDeathTest,DivisionFailsOnLargeSize)178 TEST(UnitConversionDeathTest, DivisionFailsOnLargeSize) {
179 // Note that the failure is expected since the current implementation is
180 // implementated in a way that does not support division of large sizes. If
181 // the implementation is changed, this test can safely be removed.
182 const int64_t kJustSmallEnoughForDivision =
183 std::numeric_limits<int64_t>::max() / 8000000;
184 const DataSize large_size = DataSize::Bytes(kJustSmallEnoughForDivision);
185 const DataRate data_rate = DataRate::KilobitsPerSec(100);
186 const TimeDelta time_delta = TimeDelta::Millis(100);
187 EXPECT_TRUE((large_size / data_rate).IsFinite());
188 EXPECT_TRUE((large_size / time_delta).IsFinite());
189 #if GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) && RTC_DCHECK_IS_ON
190 const int64_t kToolargeForDivision = kJustSmallEnoughForDivision + 1;
191 const DataSize too_large_size = DataSize::Bytes(kToolargeForDivision);
192 EXPECT_DEATH(too_large_size / data_rate, "");
193 EXPECT_DEATH(too_large_size / time_delta, "");
194 #endif // GTEST_HAS_DEATH_TEST && !!defined(WEBRTC_ANDROID) && RTC_DCHECK_IS_ON
195 }
196 } // namespace test
197 } // namespace webrtc
198