Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(50)

Side by Side Diff: webrtc/base/mod_ops_unittest.cc

Issue 1786043004: Implemented more general version of ForwardDiff/RevereseDiff. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Feedback fixes Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/base/mod_ops.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "webrtc/base/mod_ops.h" 12 #include "webrtc/base/mod_ops.h"
13 13
14 namespace webrtc { 14 namespace webrtc {
15 class TestModOps : public ::testing::Test { 15 class TestModOps : public ::testing::Test {
16 protected: 16 protected:
17 // Can't use std::numeric_limits<unsigned long>::max() since 17 // Can't use std::numeric_limits<unsigned long>::max() since
18 // MSVC doesn't support constexpr. 18 // MSVC doesn't support constexpr.
19 static const unsigned long ulmax = ~0ul; // NOLINT 19 static const unsigned long ulmax = ~0ul; // NOLINT
20 }; 20 };
21 21
22 TEST_F(TestModOps, Add) { 22 TEST_F(TestModOps, Add) {
23 const int D = 100; 23 const int D = 100;
24 EXPECT_EQ(1u, Add<D>(0, 1)); 24 ASSERT_EQ(1u, Add<D>(0, 1));
25 EXPECT_EQ(0u, Add<D>(0, D)); 25 ASSERT_EQ(0u, Add<D>(0, D));
26 for (int i = 0; i < D; ++i) 26 for (int i = 0; i < D; ++i)
27 EXPECT_EQ(0u, Add<D>(i, D - i)); 27 ASSERT_EQ(0u, Add<D>(i, D - i));
28 28
29 int t = 37; 29 int t = 37;
30 uint8_t a = t; 30 uint8_t a = t;
31 for (int i = 0; i < 256; ++i) { 31 for (int i = 0; i < 256; ++i) {
32 EXPECT_EQ(a, static_cast<uint8_t>(t)); 32 ASSERT_EQ(a, static_cast<uint8_t>(t));
33 t = Add<256>(t, 1); 33 t = Add<256>(t, 1);
34 ++a; 34 ++a;
35 } 35 }
36 } 36 }
37 37
38 TEST_F(TestModOps, AddLarge) { 38 TEST_F(TestModOps, AddLarge) {
39 // NOLINTNEXTLINE 39 // NOLINTNEXTLINE
40 const unsigned long D = ulmax - 10ul; // NOLINT 40 const unsigned long D = ulmax - 10ul; // NOLINT
41 unsigned long l = D - 1ul; // NOLINT 41 unsigned long l = D - 1ul; // NOLINT
42 EXPECT_EQ(D - 2ul, Add<D>(l, l)); 42 ASSERT_EQ(D - 2ul, Add<D>(l, l));
43 EXPECT_EQ(9ul, Add<D>(l, ulmax)); 43 ASSERT_EQ(9ul, Add<D>(l, ulmax));
44 EXPECT_EQ(10ul, Add<D>(0ul, ulmax)); 44 ASSERT_EQ(10ul, Add<D>(0ul, ulmax));
45 } 45 }
46 46
47 TEST_F(TestModOps, Subtract) { 47 TEST_F(TestModOps, Subtract) {
48 const int D = 100; 48 const int D = 100;
49 EXPECT_EQ(99u, Subtract<D>(0, 1)); 49 ASSERT_EQ(99u, Subtract<D>(0, 1));
50 EXPECT_EQ(0u, Subtract<D>(0, D)); 50 ASSERT_EQ(0u, Subtract<D>(0, D));
51 for (int i = 0; i < D; ++i) 51 for (int i = 0; i < D; ++i)
52 EXPECT_EQ(0u, Subtract<D>(i, D + i)); 52 ASSERT_EQ(0u, Subtract<D>(i, D + i));
53 53
54 int t = 37; 54 int t = 37;
55 uint8_t a = t; 55 uint8_t a = t;
56 for (int i = 0; i < 256; ++i) { 56 for (int i = 0; i < 256; ++i) {
57 EXPECT_EQ(a, static_cast<uint8_t>(t)); 57 ASSERT_EQ(a, static_cast<uint8_t>(t));
58 t = Subtract<256>(t, 1); 58 t = Subtract<256>(t, 1);
59 --a; 59 --a;
60 } 60 }
61 } 61 }
62 62
63 TEST_F(TestModOps, SubtractLarge) { 63 TEST_F(TestModOps, SubtractLarge) {
64 // NOLINTNEXTLINE 64 // NOLINTNEXTLINE
65 const unsigned long D = ulmax - 10ul; // NOLINT 65 const unsigned long D = ulmax - 10ul; // NOLINT
66 unsigned long l = D - 1ul; // NOLINT 66 unsigned long l = D - 1ul; // NOLINT
67 EXPECT_EQ(0ul, Subtract<D>(l, l)); 67 ASSERT_EQ(0ul, Subtract<D>(l, l));
68 EXPECT_EQ(D - 11ul, Subtract<D>(l, ulmax)); 68 ASSERT_EQ(D - 11ul, Subtract<D>(l, ulmax));
69 EXPECT_EQ(D - 10ul, Subtract<D>(0ul, ulmax)); 69 ASSERT_EQ(D - 10ul, Subtract<D>(0ul, ulmax));
70 } 70 }
71 71
72 TEST_F(TestModOps, ForwardDiff) { 72 TEST_F(TestModOps, ForwardDiff) {
73 EXPECT_EQ(0u, ForwardDiff(4711u, 4711u)); 73 ASSERT_EQ(0u, ForwardDiff(4711u, 4711u));
74 74
75 uint8_t x = 0; 75 uint8_t x = 0;
76 uint8_t y = 255; 76 uint8_t y = 255;
77 for (int i = 0; i < 256; ++i) { 77 for (int i = 0; i < 256; ++i) {
78 EXPECT_EQ(255u, ForwardDiff(x, y)); 78 ASSERT_EQ(255u, ForwardDiff(x, y));
79 ++x; 79 ++x;
80 ++y; 80 ++y;
81 } 81 }
82 82
83 int yi = 255; 83 int yi = 255;
84 for (int i = 0; i < 256; ++i) { 84 for (int i = 0; i < 256; ++i) {
85 EXPECT_EQ(255u, ForwardDiff<uint8_t>(x, yi)); 85 ASSERT_EQ(255u, ForwardDiff<uint8_t>(x, yi));
86 ++x; 86 ++x;
87 ++yi; 87 ++yi;
88 } 88 }
89 } 89 }
90 90
91 TEST_F(TestModOps, ReverseDiff) { 91 TEST_F(TestModOps, ReverseDiff) {
92 EXPECT_EQ(0u, ReverseDiff(4711u, 4711u)); 92 ASSERT_EQ(0u, ReverseDiff(4711u, 4711u));
93 93
94 uint8_t x = 0; 94 uint8_t x = 0;
95 uint8_t y = 255; 95 uint8_t y = 255;
96 for (int i = 0; i < 256; ++i) { 96 for (int i = 0; i < 256; ++i) {
97 EXPECT_EQ(1u, ReverseDiff(x, y)); 97 ASSERT_EQ(1u, ReverseDiff(x, y));
98 ++x; 98 ++x;
99 ++y; 99 ++y;
100 } 100 }
101 101
102 int yi = 255; 102 int yi = 255;
103 for (int i = 0; i < 256; ++i) { 103 for (int i = 0; i < 256; ++i) {
104 EXPECT_EQ(1u, ReverseDiff<uint8_t>(x, yi)); 104 ASSERT_EQ(1u, ReverseDiff<uint8_t>(x, yi));
105 ++x; 105 ++x;
106 ++yi; 106 ++yi;
107 } 107 }
108 } 108 }
109 109
110 TEST_F(TestModOps, AheadOrAt) { 110 TEST_F(TestModOps, AheadOrAt) {
111 uint8_t x = 0; 111 uint8_t x = 0;
112 uint8_t y = 0; 112 uint8_t y = 0;
113 EXPECT_TRUE(AheadOrAt(x, y)); 113 ASSERT_TRUE(AheadOrAt(x, y));
114 ++x; 114 ++x;
115 EXPECT_TRUE(AheadOrAt(x, y)); 115 ASSERT_TRUE(AheadOrAt(x, y));
116 EXPECT_FALSE(AheadOrAt(y, x)); 116 ASSERT_FALSE(AheadOrAt(y, x));
117 for (int i = 0; i < 256; ++i) { 117 for (int i = 0; i < 256; ++i) {
118 EXPECT_TRUE(AheadOrAt(x, y)); 118 ASSERT_TRUE(AheadOrAt(x, y));
119 ++x; 119 ++x;
120 ++y; 120 ++y;
121 } 121 }
122 122
123 x = 128; 123 x = 128;
124 y = 0; 124 y = 0;
125 EXPECT_TRUE(AheadOrAt(x, y)); 125 ASSERT_TRUE(AheadOrAt(x, y));
126 EXPECT_FALSE(AheadOrAt(y, x)); 126 ASSERT_FALSE(AheadOrAt(y, x));
127 127
128 x = 129; 128 x = 129;
129 EXPECT_FALSE(AheadOrAt(x, y)); 129 ASSERT_FALSE(AheadOrAt(x, y));
130 EXPECT_TRUE(AheadOrAt(y, x)); 130 ASSERT_TRUE(AheadOrAt(y, x));
131 EXPECT_TRUE(AheadOrAt<uint16_t>(x, y)); 131 ASSERT_TRUE(AheadOrAt<uint16_t>(x, y));
132 EXPECT_FALSE(AheadOrAt<uint16_t>(y, x)); 132 ASSERT_FALSE(AheadOrAt<uint16_t>(y, x));
133 } 133 }
134 134
135 TEST_F(TestModOps, AheadOf) { 135 TEST_F(TestModOps, AheadOf) {
136 uint8_t x = 0; 136 uint8_t x = 0;
137 uint8_t y = 0; 137 uint8_t y = 0;
138 EXPECT_FALSE(AheadOf(x, y)); 138 ASSERT_FALSE(AheadOf(x, y));
139 ++x; 139 ++x;
140 EXPECT_TRUE(AheadOf(x, y)); 140 ASSERT_TRUE(AheadOf(x, y));
141 EXPECT_FALSE(AheadOf(y, x)); 141 ASSERT_FALSE(AheadOf(y, x));
142 for (int i = 0; i < 256; ++i) { 142 for (int i = 0; i < 256; ++i) {
143 EXPECT_TRUE(AheadOf(x, y)); 143 ASSERT_TRUE(AheadOf(x, y));
144 ++x; 144 ++x;
145 ++y; 145 ++y;
146 } 146 }
147 147
148 x = 128; 148 x = 128;
149 y = 0; 149 y = 0;
150 for (int i = 0; i < 128; ++i) { 150 for (int i = 0; i < 128; ++i) {
151 EXPECT_TRUE(AheadOf(x, y)); 151 ASSERT_TRUE(AheadOf(x, y));
152 EXPECT_FALSE(AheadOf(y, x)); 152 ASSERT_FALSE(AheadOf(y, x));
153 x++; 153 x++;
154 y++; 154 y++;
155 } 155 }
156 156
157 for (int i = 0; i < 128; ++i) { 157 for (int i = 0; i < 128; ++i) {
158 EXPECT_FALSE(AheadOf(x, y)); 158 ASSERT_FALSE(AheadOf(x, y));
159 EXPECT_TRUE(AheadOf(y, x)); 159 ASSERT_TRUE(AheadOf(y, x));
160 x++; 160 x++;
161 y++; 161 y++;
162 } 162 }
163 163
164 x = 129; 164 x = 129;
165 y = 0; 165 y = 0;
166 EXPECT_FALSE(AheadOf(x, y)); 166 ASSERT_FALSE(AheadOf(x, y));
167 EXPECT_TRUE(AheadOf(y, x)); 167 ASSERT_TRUE(AheadOf(y, x));
168 EXPECT_TRUE(AheadOf<uint16_t>(x, y)); 168 ASSERT_TRUE(AheadOf<uint16_t>(x, y));
169 EXPECT_FALSE(AheadOf<uint16_t>(y, x)); 169 ASSERT_FALSE(AheadOf<uint16_t>(y, x));
170 }
171
172 TEST_F(TestModOps, ForwardDiffWithDivisor) {
173 const uint8_t kDivisor = 211;
174
175 for (uint8_t i = 0; i < kDivisor - 1; ++i) {
176 ASSERT_EQ(0, (ForwardDiff<uint8_t, kDivisor>(i, i)));
177 ASSERT_EQ(1, (ForwardDiff<uint8_t, kDivisor>(i, i + 1)));
178 ASSERT_EQ(kDivisor - 1, (ForwardDiff<uint8_t, kDivisor>(i + 1, i)));
179 }
180
181 for (uint8_t i = 1; i < kDivisor; ++i) {
182 ASSERT_EQ(i, (ForwardDiff<uint8_t, kDivisor>(0, i)));
183 ASSERT_EQ(kDivisor - i, (ForwardDiff<uint8_t, kDivisor>(i, 0)));
184 }
185 }
186
187 TEST_F(TestModOps, ReverseDiffWithDivisor) {
188 const uint8_t kDivisor = 241;
189
190 for (uint8_t i = 0; i < kDivisor - 1; ++i) {
191 ASSERT_EQ(0, (ReverseDiff<uint8_t, kDivisor>(i, i)));
192 ASSERT_EQ(kDivisor - 1, (ReverseDiff<uint8_t, kDivisor>(i, i + 1)));
193 ASSERT_EQ(1, (ReverseDiff<uint8_t, kDivisor>(i + 1, i)));
194 }
195
196 for (uint8_t i = 1; i < kDivisor; ++i) {
197 ASSERT_EQ(kDivisor - i, (ReverseDiff<uint8_t, kDivisor>(0, i)));
198 ASSERT_EQ(i, (ReverseDiff<uint8_t, kDivisor>(i, 0)));
199 }
170 } 200 }
171 201
172 } // namespace webrtc 202 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/base/mod_ops.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698