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_size.h"
12
13 #include <limits>
14
15 #include "test/gtest.h"
16
17 namespace webrtc {
18 namespace test {
19
TEST(DataSizeTest,ConstExpr)20 TEST(DataSizeTest, ConstExpr) {
21 constexpr int64_t kValue = 12345;
22 constexpr DataSize kDataSizeZero = DataSize::Zero();
23 constexpr DataSize kDataSizeInf = DataSize::Infinity();
24 static_assert(kDataSizeZero.IsZero(), "");
25 static_assert(kDataSizeInf.IsInfinite(), "");
26 static_assert(kDataSizeInf.bytes_or(-1) == -1, "");
27 static_assert(kDataSizeInf > kDataSizeZero, "");
28
29 constexpr DataSize kDataSize = DataSize::Bytes(kValue);
30 static_assert(kDataSize.bytes_or(-1) == kValue, "");
31
32 EXPECT_EQ(kDataSize.bytes(), kValue);
33 }
34
TEST(DataSizeTest,GetBackSameValues)35 TEST(DataSizeTest, GetBackSameValues) {
36 const int64_t kValue = 123 * 8;
37 EXPECT_EQ(DataSize::Bytes(kValue).bytes(), kValue);
38 }
39
TEST(DataSizeTest,IdentityChecks)40 TEST(DataSizeTest, IdentityChecks) {
41 const int64_t kValue = 3000;
42 EXPECT_TRUE(DataSize::Zero().IsZero());
43 EXPECT_FALSE(DataSize::Bytes(kValue).IsZero());
44
45 EXPECT_TRUE(DataSize::Infinity().IsInfinite());
46 EXPECT_FALSE(DataSize::Zero().IsInfinite());
47 EXPECT_FALSE(DataSize::Bytes(kValue).IsInfinite());
48
49 EXPECT_FALSE(DataSize::Infinity().IsFinite());
50 EXPECT_TRUE(DataSize::Bytes(kValue).IsFinite());
51 EXPECT_TRUE(DataSize::Zero().IsFinite());
52 }
53
TEST(DataSizeTest,ComparisonOperators)54 TEST(DataSizeTest, ComparisonOperators) {
55 const int64_t kSmall = 450;
56 const int64_t kLarge = 451;
57 const DataSize small = DataSize::Bytes(kSmall);
58 const DataSize large = DataSize::Bytes(kLarge);
59
60 EXPECT_EQ(DataSize::Zero(), DataSize::Bytes(0));
61 EXPECT_EQ(DataSize::Infinity(), DataSize::Infinity());
62 EXPECT_EQ(small, small);
63 EXPECT_LE(small, small);
64 EXPECT_GE(small, small);
65 EXPECT_NE(small, large);
66 EXPECT_LE(small, large);
67 EXPECT_LT(small, large);
68 EXPECT_GE(large, small);
69 EXPECT_GT(large, small);
70 EXPECT_LT(DataSize::Zero(), small);
71 EXPECT_GT(DataSize::Infinity(), large);
72 }
73
TEST(DataSizeTest,ConvertsToAndFromDouble)74 TEST(DataSizeTest, ConvertsToAndFromDouble) {
75 const int64_t kValue = 128;
76 const double kDoubleValue = static_cast<double>(kValue);
77
78 EXPECT_EQ(DataSize::Bytes(kValue).bytes<double>(), kDoubleValue);
79 EXPECT_EQ(DataSize::Bytes(kDoubleValue).bytes(), kValue);
80
81 const double kInfinity = std::numeric_limits<double>::infinity();
82 EXPECT_EQ(DataSize::Infinity().bytes<double>(), kInfinity);
83 EXPECT_TRUE(DataSize::Bytes(kInfinity).IsInfinite());
84 }
85
TEST(DataSizeTest,MathOperations)86 TEST(DataSizeTest, MathOperations) {
87 const int64_t kValueA = 450;
88 const int64_t kValueB = 267;
89 const DataSize size_a = DataSize::Bytes(kValueA);
90 const DataSize size_b = DataSize::Bytes(kValueB);
91 EXPECT_EQ((size_a + size_b).bytes(), kValueA + kValueB);
92 EXPECT_EQ((size_a - size_b).bytes(), kValueA - kValueB);
93
94 const int32_t kInt32Value = 123;
95 const double kFloatValue = 123.0;
96 EXPECT_EQ((size_a * kValueB).bytes(), kValueA * kValueB);
97 EXPECT_EQ((size_a * kInt32Value).bytes(), kValueA * kInt32Value);
98 EXPECT_EQ((size_a * kFloatValue).bytes(), kValueA * kFloatValue);
99
100 EXPECT_EQ((size_a / 10).bytes(), kValueA / 10);
101 EXPECT_EQ(size_a / size_b, static_cast<double>(kValueA) / kValueB);
102
103 DataSize mutable_size = DataSize::Bytes(kValueA);
104 mutable_size += size_b;
105 EXPECT_EQ(mutable_size.bytes(), kValueA + kValueB);
106 mutable_size -= size_a;
107 EXPECT_EQ(mutable_size.bytes(), kValueB);
108 }
109 } // namespace test
110 } // namespace webrtc
111