169TEST(SatCounterTest, IntComparison)
171 const unsigned bits = 3;
175 ASSERT_EQ(counter++, value++);
176 ASSERT_EQ(counter++, value++);
177 ASSERT_EQ(counter--, value--);
178 ASSERT_EQ(counter++, value++);
179 ASSERT_EQ(counter++, value++);
180 ASSERT_EQ(counter--, value--);
181 ASSERT_EQ(counter++, value++);
182 ASSERT_EQ(counter--, value--);
183 ASSERT_EQ(counter--, value--);
184 ASSERT_EQ(counter++, value++);
185 ASSERT_EQ(counter--, value--);
186 ASSERT_EQ(counter--, value--);
187 ASSERT_EQ(counter, 0);
195 const unsigned bits = 3;
196 const unsigned max_value = (1 <<
bits) - 1;
197 const unsigned initial_value = 1;
203 int value = initial_value;
208 ASSERT_EQ(counter, value);
211 ASSERT_EQ(counter, value);
215 ASSERT_EQ(counter, max_value);
219 ASSERT_EQ(counter, 0);
223 value = initial_value;
226 ASSERT_EQ(counter, value);
227 counter <<= saturated_counter;
229 ASSERT_EQ(counter, max_value);
234 ASSERT_EQ(counter, value);
235 counter >>= saturated_counter;
236 ASSERT_EQ(counter, 0);
272TEST(SatCounterTest, PrePostOperators)
274 const unsigned bits = 3;
275 const unsigned max_value = (1 <<
bits) - 1;
279 for (
int i = 0;
i < 2*max_value;
i++) {
282 ASSERT_EQ(counter_post, value_pre);
285 ASSERT_EQ(counter_pre, max_value);
286 ASSERT_EQ(counter_post, max_value);
288 for (
int i = 0;
i < 2*max_value;
i++) {
291 ASSERT_EQ(counter_post, value_pre);
294 ASSERT_EQ(counter_pre, 0);
295 ASSERT_EQ(counter_post, 0);
303 const unsigned bits = 3;
304 const unsigned max_value = (1 <<
bits) - 1;
305 const unsigned initial_value = 1;
316 deep_copy = counter_copy = counter;
317 ASSERT_EQ(counter_copy_constructor, initial_value + 1);
318 ASSERT_EQ(counter_copy, initial_value + 1);
319 ASSERT_EQ(deep_copy, initial_value + 1);
323 for (
int i = 0;
i < 2*max_value;
i++) {
324 counter_copy_constructor++;
328 ASSERT_EQ(counter, initial_value + 1);
329 ASSERT_EQ(counter_copy_constructor, max_value);
330 ASSERT_EQ(counter_copy, max_value);
331 ASSERT_EQ(deep_copy, max_value);
334 counter_copy_constructor.
reset();
335 counter_copy.
reset();
337 ASSERT_EQ(counter_copy_constructor, initial_value);
338 ASSERT_EQ(counter_copy, initial_value);
339 ASSERT_EQ(deep_copy, initial_value);
342 SatCounter8 counter_move_constructor(std::move(counter));
343 ASSERT_EQ(counter, 0);
344 ASSERT_EQ(counter_move_constructor, initial_value + 1);
347 counter_move = std::move(counter_move_constructor);
348 ASSERT_EQ(counter_move_constructor, 0);
349 ASSERT_EQ(counter_move, initial_value + 1);
355TEST(SatCounterTest, AddSubAssignment)
357 const unsigned bits = 3;
358 const unsigned max_value = (1 <<
bits) - 1;
367 ASSERT_EQ(counter, value);
370 ASSERT_EQ(counter, value);
371 counter += max_value;
373 ASSERT_EQ(counter, value);
378 ASSERT_EQ(counter, value);
381 ASSERT_EQ(counter, value);
382 counter -= max_value;
384 ASSERT_EQ(counter, value);
389 ASSERT_EQ(counter, value);
390 counter += saturated_counter;
392 ASSERT_EQ(counter, saturated_counter);
397 ASSERT_EQ(counter, value);
398 counter -= saturated_counter;
399 ASSERT_EQ(counter, 0);
405TEST(SatCounterTest, NegativeAddSubAssignment)
407 const unsigned bits = 3;
408 const unsigned max_value = (1 <<
bits) - 1;
410 int value = max_value;
415 ASSERT_EQ(counter, value);
418 ASSERT_EQ(counter, value);
419 counter += (int)-max_value;
421 ASSERT_EQ(counter, value);
426 ASSERT_EQ(counter, value);
429 ASSERT_EQ(counter, value);
430 counter -= (int)-max_value;
432 ASSERT_EQ(counter, value);
438 const uint16_t bits_16 = 9;
439 const uint16_t max_value_16 = (1 << bits_16) - 1;
444 ASSERT_EQ(counter_16, 1);
446 ASSERT_EQ(counter_16, 2);
447 counter_16 += 2 * max_value_16;
448 ASSERT_EQ(counter_16, max_value_16);
450 ASSERT_EQ(counter_16, max_value_16);
452 ASSERT_EQ(counter_16, max_value_16);
456 ASSERT_EQ(counter_16, max_value_16 - 1);
458 ASSERT_EQ(counter_16, (max_value_16 - 1) >> 1);
459 counter_16 -= 2 * max_value_16;
460 ASSERT_EQ(counter_16, 0);
462 ASSERT_EQ(counter_16, 0);
464 ASSERT_EQ(counter_16, 0);
470 const uint32_t bits_32 = 17;
471 const uint32_t max_value_32 = (1 << bits_32) - 1;
476 ASSERT_EQ(counter_32, 1);
478 ASSERT_EQ(counter_32, 2);
479 counter_32 += 2 * max_value_32;
480 ASSERT_EQ(counter_32, max_value_32);
482 ASSERT_EQ(counter_32, max_value_32);
484 ASSERT_EQ(counter_32, max_value_32);
488 ASSERT_EQ(counter_32, max_value_32 - 1);
490 ASSERT_EQ(counter_32, (max_value_32 - 1) >> 1);
491 counter_32 -= 2 * max_value_32;
492 ASSERT_EQ(counter_32, 0);
494 ASSERT_EQ(counter_32, 0);
496 ASSERT_EQ(counter_32, 0);
502 const uint64_t bits_64 = 33;
503 const uint64_t max_value_64 = (1ULL << bits_64) - 1;
508 ASSERT_EQ(counter_64, 1);
510 ASSERT_EQ(counter_64, 2);
511 counter_64 += max_value_64;
512 ASSERT_EQ(counter_64, max_value_64);
514 ASSERT_EQ(counter_64, max_value_64);
516 ASSERT_EQ(counter_64, max_value_64);
520 ASSERT_EQ(counter_64, max_value_64 - 1);
522 ASSERT_EQ(counter_64, (max_value_64 - 1) >> 1);
523 counter_64 -= max_value_64;
524 ASSERT_EQ(counter_64, 0);
526 ASSERT_EQ(counter_64, 0);
528 ASSERT_EQ(counter_64, 0);
constexpr T bits(T val, unsigned first, unsigned last)
Extract the bitfield from position 'first' to 'last' (inclusive) from 'val' and right justify it.