Index: media/cast/common/expanded_value_base_unittest.cc |
diff --git a/media/cast/common/expanded_value_base_unittest.cc b/media/cast/common/expanded_value_base_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..58563aa24efeb503a02bf04f8ed2cfed0eae3a43 |
--- /dev/null |
+++ b/media/cast/common/expanded_value_base_unittest.cc |
@@ -0,0 +1,92 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "media/cast/common/expanded_value_base.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace media { |
+namespace cast { |
+ |
+namespace { |
+ |
+// A basic subclass of ExpandedValueBase to use for testing. |
+class TestValue : public ExpandedValueBase<int64_t, TestValue> { |
+ public: |
+ explicit TestValue(int64_t value) : ExpandedValueBase(value) {} |
+}; |
+ |
+} // namespace |
+ |
+// Tests the various scenarios of truncating and then re-expanding values, and |
+// confirming the correct behavior. Note that, while the code below just tests |
+// truncation/expansion to/from 8 bits, the 16- and 32-bit cases are implicitly |
+// confirmed because the Expand() method uses the compiler to derive all of its |
+// constants (based on the type of its argument). |
+TEST(ExpandedValueBaseTest, TruncationAndExpansion) { |
+ // Test that expansion works when the reference is always equal to the value |
+ // that was truncated. |
+ for (int64_t i = -512; i <= 512; ++i) { |
+ const TestValue original_value(i); |
+ const uint8_t truncated = original_value.lower_8_bits(); |
+ const TestValue reference(i); |
+ ASSERT_EQ(original_value, reference.Expand(truncated)) << "i=" << i; |
+ } |
+ |
+ // Test that expansion works when the reference is always one less than the |
+ // value that was truncated. |
+ for (int64_t i = -512; i <= 512; ++i) { |
+ const TestValue original_value(i); |
+ const uint8_t truncated = original_value.lower_8_bits(); |
+ const TestValue reference(i - 1); |
+ ASSERT_EQ(original_value, reference.Expand(truncated)) << "i=" << i; |
+ } |
+ |
+ // Test that expansion works when the reference is always one greater than the |
+ // value that was truncated. |
+ for (int64_t i = -512; i <= 512; ++i) { |
+ const TestValue original_value(i); |
+ const uint8_t truncated = original_value.lower_8_bits(); |
+ const TestValue reference(i + 1); |
+ ASSERT_EQ(original_value, reference.Expand(truncated)) << "i=" << i; |
+ } |
+ |
+ // Test the cases where a difference of [-128,+127] around a fixed refererence |
+ // does not trigger any wrap-around adjustments during expansion. |
+ for (int64_t bias = -5; bias <= 5; ++bias) { |
+ for (int64_t i = -128; i <= 127; ++i) { |
+ const TestValue original_value(bias + i); |
+ const uint8_t truncated = original_value.lower_8_bits(); |
+ const TestValue reference(bias); |
+ ASSERT_EQ(original_value, reference.Expand(truncated)) << "bias=" << bias |
+ << ", i=" << i; |
+ } |
+ } |
+ |
+ // Test the cases where a difference of [+128,+255] around a fixed reference |
+ // always triggers backwards wrap-around adjustments. |
+ for (int64_t bias = -5; bias <= 5; ++bias) { |
+ for (int64_t i = 128; i <= 255; ++i) { |
+ const TestValue original_value(bias + i - 256); |
Irfan
2015/12/09 21:24:27
am a little confused by the need for -256. perhaps
miu
2015/12/10 00:38:36
I clarified with comments and a little extra code.
|
+ const uint8_t truncated = original_value.lower_8_bits(); |
+ const TestValue reference(bias); |
+ ASSERT_EQ(original_value, reference.Expand(truncated)) << "bias=" << bias |
+ << ", i=" << i; |
+ } |
+ } |
+ |
+ // Test the cases where a difference of [-256,-129] around a fixed reference |
+ // always triggers forwards wrap-around adjustments. |
+ for (int64_t bias = -5; bias <= 5; ++bias) { |
+ for (int64_t i = -256; i <= -129; ++i) { |
+ const TestValue original_value(bias + i + 256); |
+ const uint8_t truncated = original_value.lower_8_bits(); |
+ const TestValue reference(bias); |
+ ASSERT_EQ(original_value, reference.Expand(truncated)) << "bias=" << bias |
+ << ", i=" << i; |
+ } |
+ } |
+} |
+ |
+} // namespace cast |
+} // namespace media |