xref: /aosp_15_r20/external/openscreen/cast/streaming/expanded_value_base_unittest.cc (revision 3f982cf4871df8771c9d4abe6e9a6f8d829b2736)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "cast/streaming/expanded_value_base.h"
6 
7 #include "gtest/gtest.h"
8 
9 namespace openscreen {
10 namespace cast {
11 
12 namespace {
13 
14 // A basic subclass of ExpandedValueBase to use for testing.
15 class TestValue : public ExpandedValueBase<int64_t, TestValue> {
16  public:
TestValue(int64_t value)17   explicit TestValue(int64_t value) : ExpandedValueBase(value) {}
18 };
19 
20 }  // namespace
21 
22 // Tests the various scenarios of truncating and then re-expanding values, and
23 // confirming the correct behavior.  Note that, while the code below just tests
24 // truncation/expansion to/from 8 bits, the 16- and 32-bit cases are implicitly
25 // confirmed because the Expand() method uses the compiler to derive all of its
26 // constants (based on the type of its argument).
TEST(ExpandedValueBaseTest,TruncationAndExpansion)27 TEST(ExpandedValueBaseTest, TruncationAndExpansion) {
28   // Test that expansion works when the reference is always equal to the value
29   // that was truncated.
30   for (int64_t i = -512; i <= 512; ++i) {
31     const TestValue original_value(i);
32     const uint8_t truncated = original_value.lower_8_bits();
33     const TestValue reference(i);
34     ASSERT_EQ(original_value, reference.Expand(truncated)) << "i=" << i;
35   }
36 
37   // Test that expansion works when the reference is always one less than the
38   // value that was truncated.
39   for (int64_t i = -512; i <= 512; ++i) {
40     const TestValue original_value(i);
41     const uint8_t truncated = original_value.lower_8_bits();
42     const TestValue reference(i - 1);
43     ASSERT_EQ(original_value, reference.Expand(truncated)) << "i=" << i;
44   }
45 
46   // Test that expansion works when the reference is always one greater than the
47   // value that was truncated.
48   for (int64_t i = -512; i <= 512; ++i) {
49     const TestValue original_value(i);
50     const uint8_t truncated = original_value.lower_8_bits();
51     const TestValue reference(i + 1);
52     ASSERT_EQ(original_value, reference.Expand(truncated)) << "i=" << i;
53   }
54 
55   // Test cases where the difference between the original value and the fixed
56   // reference is within the range [-128,+127].  The truncated value should
57   // always be re-expanded to the original value.
58   for (int64_t bias = -5; bias <= 5; ++bias) {
59     for (int64_t i = -128; i <= 127; ++i) {
60       const TestValue original_value(bias + i);
61       const uint8_t truncated = original_value.lower_8_bits();
62       const TestValue reference(bias);
63       ASSERT_EQ(original_value, reference.Expand(truncated))
64           << "bias=" << bias << ", i=" << i;
65     }
66   }
67 
68   // Test cases where the difference between the original value and the fixed
69   // reference is within the range [+128,+255].  When the truncated value is
70   // re-expanded, it should be 256 less than the original value.
71   for (int64_t bias = -5; bias <= 5; ++bias) {
72     for (int64_t i = 128; i <= 255; ++i) {
73       // Example: Let |original_value| be 192.  Then, the truncated 8-bit value
74       // will be 0xc0.  When a |reference| of zero is asked to expand 0xc0 back
75       // to the original value, it should produce -64 since -64 is closer to
76       // |reference| than 192.
77       const TestValue original_value(bias + i);
78       const uint8_t truncated = original_value.lower_8_bits();
79       const TestValue reexpanded_value(bias + i - 256);
80       ASSERT_EQ(reexpanded_value.lower_8_bits(), truncated);
81       const TestValue reference(bias);
82       ASSERT_EQ(reexpanded_value, reference.Expand(truncated))
83           << "bias=" << bias << ", i=" << i;
84     }
85   }
86 
87   // Test cases where the difference between the original value and the fixed
88   // reference is within the range [-256,-129].  When the truncated value is
89   // re-expanded, it should be 256 more than the original value.
90   for (int64_t bias = -5; bias <= 5; ++bias) {
91     for (int64_t i = -256; i <= -129; ++i) {
92       // Example: Let |original_value| be -192.  Then, the truncated 8-bit value
93       // will be 0x40.  When a |reference| of zero is asked to expand 0x40 back
94       // to the original value, it should produce 64 since 64 is closer to the
95       // |reference| than -192.
96       const TestValue original_value(bias + i);
97       const uint8_t truncated = original_value.lower_8_bits();
98       const TestValue reexpanded_value(bias + i + 256);
99       ASSERT_EQ(reexpanded_value.lower_8_bits(), truncated);
100       const TestValue reference(bias);
101       ASSERT_EQ(reexpanded_value, reference.Expand(truncated))
102           << "bias=" << bias << ", i=" << i;
103     }
104   }
105 }
106 
107 }  // namespace cast
108 }  // namespace openscreen
109