Sebastian Jansson | 26b5e35 | 2019-06-07 11:05:31 +0200 | [diff] [blame] | 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 { |
| 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 | |
| 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 | |
| 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 | } |
| 40 | |
| 41 | TEST(FrequencyTest, IdentityChecks) { |
| 42 | const int64_t kValue = 31; |
| 43 | EXPECT_TRUE(Frequency::Zero().IsZero()); |
| 44 | EXPECT_FALSE(Frequency::hertz(kValue).IsZero()); |
| 45 | |
| 46 | EXPECT_TRUE(Frequency::PlusInfinity().IsInfinite()); |
| 47 | EXPECT_TRUE(Frequency::MinusInfinity().IsInfinite()); |
| 48 | EXPECT_FALSE(Frequency::Zero().IsInfinite()); |
| 49 | EXPECT_FALSE(Frequency::hertz(kValue).IsInfinite()); |
| 50 | |
| 51 | EXPECT_FALSE(Frequency::PlusInfinity().IsFinite()); |
| 52 | EXPECT_FALSE(Frequency::MinusInfinity().IsFinite()); |
| 53 | EXPECT_TRUE(Frequency::hertz(kValue).IsFinite()); |
| 54 | EXPECT_TRUE(Frequency::Zero().IsFinite()); |
| 55 | |
| 56 | EXPECT_TRUE(Frequency::PlusInfinity().IsPlusInfinity()); |
| 57 | EXPECT_FALSE(Frequency::MinusInfinity().IsPlusInfinity()); |
| 58 | |
| 59 | EXPECT_TRUE(Frequency::MinusInfinity().IsMinusInfinity()); |
| 60 | EXPECT_FALSE(Frequency::PlusInfinity().IsMinusInfinity()); |
| 61 | } |
| 62 | |
| 63 | TEST(FrequencyTest, ComparisonOperators) { |
| 64 | const int64_t kSmall = 42; |
| 65 | const int64_t kLarge = 45; |
| 66 | const Frequency small = Frequency::hertz(kSmall); |
| 67 | const Frequency large = Frequency::hertz(kLarge); |
| 68 | |
| 69 | EXPECT_EQ(Frequency::Zero(), Frequency::hertz(0)); |
| 70 | EXPECT_EQ(Frequency::PlusInfinity(), Frequency::PlusInfinity()); |
| 71 | EXPECT_EQ(small, Frequency::hertz(kSmall)); |
| 72 | EXPECT_LE(small, Frequency::hertz(kSmall)); |
| 73 | EXPECT_GE(small, Frequency::hertz(kSmall)); |
| 74 | EXPECT_NE(small, Frequency::hertz(kLarge)); |
| 75 | EXPECT_LE(small, Frequency::hertz(kLarge)); |
| 76 | EXPECT_LT(small, Frequency::hertz(kLarge)); |
| 77 | EXPECT_GE(large, Frequency::hertz(kSmall)); |
| 78 | EXPECT_GT(large, Frequency::hertz(kSmall)); |
| 79 | EXPECT_LT(Frequency::Zero(), small); |
| 80 | |
| 81 | EXPECT_GT(Frequency::PlusInfinity(), large); |
| 82 | EXPECT_LT(Frequency::MinusInfinity(), Frequency::Zero()); |
| 83 | } |
| 84 | |
| 85 | TEST(FrequencyTest, Clamping) { |
| 86 | const Frequency upper = Frequency::hertz(800); |
| 87 | const Frequency lower = Frequency::hertz(100); |
| 88 | const Frequency under = Frequency::hertz(100); |
| 89 | const Frequency inside = Frequency::hertz(500); |
| 90 | const Frequency over = Frequency::hertz(1000); |
| 91 | EXPECT_EQ(under.Clamped(lower, upper), lower); |
| 92 | EXPECT_EQ(inside.Clamped(lower, upper), inside); |
| 93 | EXPECT_EQ(over.Clamped(lower, upper), upper); |
| 94 | |
| 95 | Frequency mutable_frequency = lower; |
| 96 | mutable_frequency.Clamp(lower, upper); |
| 97 | EXPECT_EQ(mutable_frequency, lower); |
| 98 | mutable_frequency = inside; |
| 99 | mutable_frequency.Clamp(lower, upper); |
| 100 | EXPECT_EQ(mutable_frequency, inside); |
| 101 | mutable_frequency = over; |
| 102 | mutable_frequency.Clamp(lower, upper); |
| 103 | EXPECT_EQ(mutable_frequency, upper); |
| 104 | } |
| 105 | |
| 106 | TEST(FrequencyTest, MathOperations) { |
| 107 | const int64_t kValueA = 457; |
| 108 | const int64_t kValueB = 260; |
| 109 | const Frequency frequency_a = Frequency::hertz(kValueA); |
| 110 | const Frequency frequency_b = Frequency::hertz(kValueB); |
| 111 | EXPECT_EQ((frequency_a + frequency_b).hertz<int64_t>(), kValueA + kValueB); |
| 112 | EXPECT_EQ((frequency_a - frequency_b).hertz<int64_t>(), kValueA - kValueB); |
| 113 | |
| 114 | EXPECT_EQ((Frequency::hertz(kValueA) * kValueB).hertz<int64_t>(), |
| 115 | kValueA * kValueB); |
| 116 | |
| 117 | EXPECT_EQ((frequency_b / 10).hertz<int64_t>(), kValueB / 10); |
| 118 | EXPECT_EQ(frequency_b / frequency_a, static_cast<double>(kValueB) / kValueA); |
| 119 | |
| 120 | Frequency mutable_frequency = Frequency::hertz(kValueA); |
| 121 | mutable_frequency += Frequency::hertz(kValueB); |
| 122 | EXPECT_EQ(mutable_frequency, Frequency::hertz(kValueA + kValueB)); |
| 123 | mutable_frequency -= Frequency::hertz(kValueB); |
| 124 | EXPECT_EQ(mutable_frequency, Frequency::hertz(kValueA)); |
| 125 | } |
| 126 | TEST(FrequencyTest, Rounding) { |
| 127 | const Frequency freq_high = Frequency::hertz(23.976); |
| 128 | EXPECT_EQ(freq_high.hertz(), 24); |
| 129 | EXPECT_EQ(freq_high.RoundDownTo(Frequency::hertz(1)), Frequency::hertz(23)); |
| 130 | EXPECT_EQ(freq_high.RoundTo(Frequency::hertz(1)), Frequency::hertz(24)); |
| 131 | EXPECT_EQ(freq_high.RoundUpTo(Frequency::hertz(1)), Frequency::hertz(24)); |
| 132 | |
| 133 | const Frequency freq_low = Frequency::hertz(23.4); |
| 134 | EXPECT_EQ(freq_low.hertz(), 23); |
| 135 | EXPECT_EQ(freq_low.RoundDownTo(Frequency::hertz(1)), Frequency::hertz(23)); |
| 136 | EXPECT_EQ(freq_low.RoundTo(Frequency::hertz(1)), Frequency::hertz(23)); |
| 137 | EXPECT_EQ(freq_low.RoundUpTo(Frequency::hertz(1)), Frequency::hertz(24)); |
| 138 | } |
| 139 | |
| 140 | TEST(FrequencyTest, InfinityOperations) { |
| 141 | const double kValue = 267; |
| 142 | const Frequency finite = Frequency::hertz(kValue); |
| 143 | EXPECT_TRUE((Frequency::PlusInfinity() + finite).IsPlusInfinity()); |
| 144 | EXPECT_TRUE((Frequency::PlusInfinity() - finite).IsPlusInfinity()); |
| 145 | EXPECT_TRUE((finite + Frequency::PlusInfinity()).IsPlusInfinity()); |
| 146 | EXPECT_TRUE((finite - Frequency::MinusInfinity()).IsPlusInfinity()); |
| 147 | |
| 148 | EXPECT_TRUE((Frequency::MinusInfinity() + finite).IsMinusInfinity()); |
| 149 | EXPECT_TRUE((Frequency::MinusInfinity() - finite).IsMinusInfinity()); |
| 150 | EXPECT_TRUE((finite + Frequency::MinusInfinity()).IsMinusInfinity()); |
| 151 | EXPECT_TRUE((finite - Frequency::PlusInfinity()).IsMinusInfinity()); |
| 152 | } |
| 153 | |
| 154 | TEST(UnitConversionTest, TimeDeltaAndFrequency) { |
| 155 | EXPECT_EQ(1 / Frequency::hertz(50), TimeDelta::ms(20)); |
| 156 | EXPECT_EQ(1 / TimeDelta::ms(20), Frequency::hertz(50)); |
Sebastian Jansson | 1cf15bf | 2020-01-29 10:56:25 +0100 | [diff] [blame^] | 157 | EXPECT_EQ(Frequency::kHz(200) * TimeDelta::ms(2), 400.0); |
Sebastian Jansson | 26b5e35 | 2019-06-07 11:05:31 +0200 | [diff] [blame] | 158 | } |
| 159 | } // namespace test |
| 160 | } // namespace webrtc |