OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (c) 2016 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 "webrtc/base/mod_ops.h" | |
12 #include "webrtc/test/gtest.h" | |
13 | |
14 namespace webrtc { | |
15 class TestModOps : public ::testing::Test { | |
16 protected: | |
17 // Can't use std::numeric_limits<unsigned long>::max() since | |
18 // MSVC doesn't support constexpr. | |
19 static const unsigned long ulmax = ~0ul; // NOLINT | |
20 }; | |
21 | |
22 TEST_F(TestModOps, Add) { | |
23 const int D = 100; | |
24 ASSERT_EQ(1u, Add<D>(0, 1)); | |
25 ASSERT_EQ(0u, Add<D>(0, D)); | |
26 for (int i = 0; i < D; ++i) | |
27 ASSERT_EQ(0u, Add<D>(i, D - i)); | |
28 | |
29 int t = 37; | |
30 uint8_t a = t; | |
31 for (int i = 0; i < 256; ++i) { | |
32 ASSERT_EQ(a, static_cast<uint8_t>(t)); | |
33 t = Add<256>(t, 1); | |
34 ++a; | |
35 } | |
36 } | |
37 | |
38 TEST_F(TestModOps, AddLarge) { | |
39 const unsigned long D = ulmax - 10ul; // NOLINT | |
40 unsigned long l = D - 1ul; // NOLINT | |
41 ASSERT_EQ(D - 2ul, Add<D>(l, l)); | |
42 ASSERT_EQ(9ul, Add<D>(l, ulmax)); | |
43 ASSERT_EQ(10ul, Add<D>(0ul, ulmax)); | |
44 } | |
45 | |
46 TEST_F(TestModOps, Subtract) { | |
47 const int D = 100; | |
48 ASSERT_EQ(99u, Subtract<D>(0, 1)); | |
49 ASSERT_EQ(0u, Subtract<D>(0, D)); | |
50 for (int i = 0; i < D; ++i) | |
51 ASSERT_EQ(0u, Subtract<D>(i, D + i)); | |
52 | |
53 int t = 37; | |
54 uint8_t a = t; | |
55 for (int i = 0; i < 256; ++i) { | |
56 ASSERT_EQ(a, static_cast<uint8_t>(t)); | |
57 t = Subtract<256>(t, 1); | |
58 --a; | |
59 } | |
60 } | |
61 | |
62 TEST_F(TestModOps, SubtractLarge) { | |
63 // NOLINTNEXTLINE | |
64 const unsigned long D = ulmax - 10ul; // NOLINT | |
65 unsigned long l = D - 1ul; // NOLINT | |
66 ASSERT_EQ(0ul, Subtract<D>(l, l)); | |
67 ASSERT_EQ(D - 11ul, Subtract<D>(l, ulmax)); | |
68 ASSERT_EQ(D - 10ul, Subtract<D>(0ul, ulmax)); | |
69 } | |
70 | |
71 TEST_F(TestModOps, ForwardDiff) { | |
72 ASSERT_EQ(0u, ForwardDiff(4711u, 4711u)); | |
73 | |
74 uint8_t x = 0; | |
75 uint8_t y = 255; | |
76 for (int i = 0; i < 256; ++i) { | |
77 ASSERT_EQ(255u, ForwardDiff(x, y)); | |
78 ++x; | |
79 ++y; | |
80 } | |
81 | |
82 int yi = 255; | |
83 for (int i = 0; i < 256; ++i) { | |
84 ASSERT_EQ(255u, ForwardDiff<uint8_t>(x, yi)); | |
85 ++x; | |
86 ++yi; | |
87 } | |
88 } | |
89 | |
90 TEST_F(TestModOps, ReverseDiff) { | |
91 ASSERT_EQ(0u, ReverseDiff(4711u, 4711u)); | |
92 | |
93 uint8_t x = 0; | |
94 uint8_t y = 255; | |
95 for (int i = 0; i < 256; ++i) { | |
96 ASSERT_EQ(1u, ReverseDiff(x, y)); | |
97 ++x; | |
98 ++y; | |
99 } | |
100 | |
101 int yi = 255; | |
102 for (int i = 0; i < 256; ++i) { | |
103 ASSERT_EQ(1u, ReverseDiff<uint8_t>(x, yi)); | |
104 ++x; | |
105 ++yi; | |
106 } | |
107 } | |
108 | |
109 TEST_F(TestModOps, MinDiff) { | |
110 for (uint16_t i = 0; i < 256; ++i) { | |
111 ASSERT_EQ(0, MinDiff<uint8_t>(i, i)); | |
112 ASSERT_EQ(1, MinDiff<uint8_t>(i - 1, i)); | |
113 ASSERT_EQ(1, MinDiff<uint8_t>(i + 1, i)); | |
114 } | |
115 | |
116 for (uint8_t i = 0; i < 128; ++i) | |
117 ASSERT_EQ(i, MinDiff<uint8_t>(0, i)); | |
118 | |
119 for (uint8_t i = 0; i < 128; ++i) | |
120 ASSERT_EQ(128 - i, MinDiff<uint8_t>(0, 128 + i)); | |
121 } | |
122 | |
123 TEST_F(TestModOps, MinDiffWitDivisor) { | |
124 ASSERT_EQ(5u, (MinDiff<uint8_t, 11>(0, 5))); | |
125 ASSERT_EQ(5u, (MinDiff<uint8_t, 11>(0, 6))); | |
126 ASSERT_EQ(5u, (MinDiff<uint8_t, 11>(5, 0))); | |
127 ASSERT_EQ(5u, (MinDiff<uint8_t, 11>(6, 0))); | |
128 | |
129 const uint16_t D = 4711; | |
130 | |
131 for (uint16_t i = 0; i < D / 2; ++i) | |
132 ASSERT_EQ(i, (MinDiff<uint16_t, D>(0, i))); | |
133 | |
134 ASSERT_EQ(D / 2, (MinDiff<uint16_t, D>(0, D / 2))); | |
135 | |
136 for (uint16_t i = 0; i < D / 2; ++i) | |
137 ASSERT_EQ(D / 2 - i, (MinDiff<uint16_t, D>(0, D / 2 - i))); | |
138 } | |
139 | |
140 } // namespace webrtc | |
OLD | NEW |