29 #include <gtest/gtest-spi.h>
30 #include <gtest/gtest.h>
39 TEST(SatCounterTest, MaximumValue)
41 const unsigned bits = 3;
42 const unsigned max_value = (1 <<
bits) - 1;
45 for (
int i = 0;
i < 2*max_value;
i++) {
49 ASSERT_EQ(counter, max_value);
55 TEST(SatCounterTest, MinimumValue)
57 const unsigned bits = 3;
60 for (
int i = 0;
i < 2;
i++) {
64 ASSERT_EQ(counter, 0);
70 TEST(SatCounterTest, InitialValue)
72 const unsigned bits = 3;
73 const unsigned initial_value = 4;
75 ASSERT_EQ(counter, initial_value);
78 ASSERT_EQ(counter, initial_value);
84 TEST(SatCounterTest, SaturationPercentile)
86 const unsigned bits = 3;
87 const unsigned max_value = (1 <<
bits) - 1;
91 for (
double value = 0.0; value <= max_value; value++, counter++) {
92 const double saturation = value / max_value;
103 const unsigned bits = 3;
104 const unsigned max_value = (1 <<
bits) - 1;
110 const unsigned diff = counter.
saturate();
111 ASSERT_EQ(diff, max_value - 1);
118 TEST(SatCounterTest, IntComparison)
120 const unsigned bits = 3;
124 ASSERT_EQ(counter++, value++);
125 ASSERT_EQ(counter++, value++);
126 ASSERT_EQ(counter--, value--);
127 ASSERT_EQ(counter++, value++);
128 ASSERT_EQ(counter++, value++);
129 ASSERT_EQ(counter--, value--);
130 ASSERT_EQ(counter++, value++);
131 ASSERT_EQ(counter--, value--);
132 ASSERT_EQ(counter--, value--);
133 ASSERT_EQ(counter++, value++);
134 ASSERT_EQ(counter--, value--);
135 ASSERT_EQ(counter--, value--);
136 ASSERT_EQ(counter, 0);
144 const unsigned bits = 3;
145 const unsigned max_value = (1 <<
bits) - 1;
146 const unsigned initial_value = 1;
152 int value = initial_value;
157 ASSERT_EQ(counter, value);
160 ASSERT_EQ(counter, value);
164 ASSERT_EQ(counter, max_value);
168 ASSERT_EQ(counter, 0);
172 value = initial_value;
175 ASSERT_EQ(counter, value);
176 counter <<= saturated_counter;
178 ASSERT_EQ(counter, max_value);
183 ASSERT_EQ(counter, value);
184 counter >>= saturated_counter;
185 ASSERT_EQ(counter, 0);
192 ASSERT_DEATH(counter >>= -1,
"");
193 ASSERT_DEATH(counter <<= -1,
"");
200 TEST(SatCounterTest, PrePostOperators)
202 const unsigned bits = 3;
203 const unsigned max_value = (1 <<
bits) - 1;
207 for (
int i = 0;
i < 2*max_value;
i++) {
210 ASSERT_EQ(counter_post, value_pre);
213 ASSERT_EQ(counter_pre, max_value);
214 ASSERT_EQ(counter_post, max_value);
216 for (
int i = 0;
i < 2*max_value;
i++) {
219 ASSERT_EQ(counter_post, value_pre);
222 ASSERT_EQ(counter_pre, 0);
223 ASSERT_EQ(counter_post, 0);
231 const unsigned bits = 3;
232 const unsigned max_value = (1 <<
bits) - 1;
233 const unsigned initial_value = 1;
244 deep_copy = counter_copy = counter;
245 ASSERT_EQ(counter_copy_constructor, initial_value + 1);
246 ASSERT_EQ(counter_copy, initial_value + 1);
247 ASSERT_EQ(deep_copy, initial_value + 1);
251 for (
int i = 0;
i < 2*max_value;
i++) {
252 counter_copy_constructor++;
256 ASSERT_EQ(counter, initial_value + 1);
257 ASSERT_EQ(counter_copy_constructor, max_value);
258 ASSERT_EQ(counter_copy, max_value);
259 ASSERT_EQ(deep_copy, max_value);
262 counter_copy_constructor.
reset();
263 counter_copy.
reset();
265 ASSERT_EQ(counter_copy_constructor, initial_value);
266 ASSERT_EQ(counter_copy, initial_value);
267 ASSERT_EQ(deep_copy, initial_value);
270 SatCounter counter_move_constructor(std::move(counter));
271 ASSERT_EQ(counter, 0);
272 ASSERT_EQ(counter_move_constructor, initial_value + 1);
275 counter_move = std::move(counter_move_constructor);
276 ASSERT_EQ(counter_move_constructor, 0);
277 ASSERT_EQ(counter_move, initial_value + 1);
283 TEST(SatCounterTest, AddSubAssignment)
285 const unsigned bits = 3;
286 const unsigned max_value = (1 <<
bits) - 1;
295 ASSERT_EQ(counter, value);
298 ASSERT_EQ(counter, value);
299 counter += max_value;
301 ASSERT_EQ(counter, value);
306 ASSERT_EQ(counter, value);
309 ASSERT_EQ(counter, value);
310 counter -= max_value;
312 ASSERT_EQ(counter, value);
317 ASSERT_EQ(counter, value);
318 counter += saturated_counter;
320 ASSERT_EQ(counter, saturated_counter);
325 ASSERT_EQ(counter, value);
326 counter -= saturated_counter;
327 ASSERT_EQ(counter, 0);
333 TEST(SatCounterTest, NegativeAddSubAssignment)
335 const unsigned bits = 3;
336 const unsigned max_value = (1 <<
bits) - 1;
338 int value = max_value;
343 ASSERT_EQ(counter, value);
346 ASSERT_EQ(counter, value);
347 counter += (int)-max_value;
349 ASSERT_EQ(counter, value);
354 ASSERT_EQ(counter, value);
357 ASSERT_EQ(counter, value);
358 counter -= (int)-max_value;
360 ASSERT_EQ(counter, value);