diff options
author | Daniel R. Carvalho <odanrc@yahoo.com.br> | 2019-12-15 16:24:15 +0100 |
---|---|---|
committer | Daniel Carvalho <odanrc@yahoo.com.br> | 2019-12-21 10:58:08 +0000 |
commit | 8ba0ba4871907aaa08c862534033f2922a3d2ba7 (patch) | |
tree | 84653473bf4039d0cba9065fbdefdca43ac45ac5 /src/base/sat_counter.test.cc | |
parent | c3fecd4520555dfaf6a3836f791e638de1412e28 (diff) | |
download | gem5-8ba0ba4871907aaa08c862534033f2922a3d2ba7.tar.xz |
base: Fix negative op-assign of SatCounter
The value of the add and subtract assignment operations can be negative,
and this was not being handled properly previously. Regarding shift
assignment, the standard says it is undefined behaviour if a negative
number is given, so add assertions for these cases.
Change-Id: I2f1e4143c6385caa80fb25f84ca8edb0ca7e62b7
Signed-off-by: Daniel R. Carvalho <odanrc@yahoo.com.br>
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/23664
Reviewed-by: Bobby R. Bruce <bbruce@ucdavis.edu>
Reviewed-by: Jason Lowe-Power <jason@lowepower.com>
Maintainer: Jason Lowe-Power <jason@lowepower.com>
Tested-by: kokoro <noreply+kokoro@google.com>
Diffstat (limited to 'src/base/sat_counter.test.cc')
-rw-r--r-- | src/base/sat_counter.test.cc | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/base/sat_counter.test.cc b/src/base/sat_counter.test.cc index 817f2c7ac..d9a377f9f 100644 --- a/src/base/sat_counter.test.cc +++ b/src/base/sat_counter.test.cc @@ -28,6 +28,7 @@ * Authors: Daniel Carvalho */ +#include <gtest/gtest-spi.h> #include <gtest/gtest.h> #include <utility> @@ -184,6 +185,11 @@ TEST(SatCounterTest, Shift) ASSERT_EQ(counter, value); counter >>= saturated_counter; ASSERT_EQ(counter, 0); + + // Make sure the counters cannot be shifted by negative numbers, since + // that is undefined behaviour + ASSERT_DEATH(counter >>= -1, ""); + ASSERT_DEATH(counter <<= -1, ""); } /** @@ -319,3 +325,36 @@ TEST(SatCounterTest, AddSubAssignment) ASSERT_EQ(counter, 0); } +/** + * Test add-assignment and subtract assignment using negative numbers. + */ +TEST(SatCounterTest, NegativeAddSubAssignment) +{ + const unsigned bits = 3; + const unsigned max_value = (1 << bits) - 1; + SatCounter counter(bits, max_value); + int value = max_value; + + // Test add-assignment for a few negative values until zero is reached + counter += -2; + value += -2; + ASSERT_EQ(counter, value); + counter += -3; + value += -3; + ASSERT_EQ(counter, value); + counter += (int)-max_value; + value = 0; + ASSERT_EQ(counter, value); + + // Test subtract-assignment for a few negative values until saturation + counter -= -2; + value -= -2; + ASSERT_EQ(counter, value); + counter -= -3; + value -= -3; + ASSERT_EQ(counter, value); + counter -= (int)-max_value; + value = max_value; + ASSERT_EQ(counter, value); +} + |