Daniel Carvalho has uploaded this change for review. (
https://gem5-review.googlesource.com/c/public/gem5/+/42763 )
Change subject: base: Add fatal tests to sat_counter
......................................................................
base: Add fatal tests to sat_counter
Add tests to make sure that the fatal conditions are
triggered when met.
This commit also moves shift-related death tests to
their own DeathTests.
Change-Id: Ia610636fe8cf636401e2b2ed623bf20b41147ea4
Signed-off-by: Daniel R. Carvalho <[email protected]>
---
M src/base/sat_counter.hh
M src/base/sat_counter.test.cc
2 files changed, 63 insertions(+), 7 deletions(-)
diff --git a/src/base/sat_counter.hh b/src/base/sat_counter.hh
index 4849c2a..cf60fdc 100644
--- a/src/base/sat_counter.hh
+++ b/src/base/sat_counter.hh
@@ -78,7 +78,7 @@
fatal_if(bits > 8*sizeof(T),
"Number of bits exceeds counter size");
fatal_if(initial_val > maxVal,
- "Saturating counter's Initial value exceeds max value.");
+ "Saturating counter's initial value exceeds max value.");
}
/**
diff --git a/src/base/sat_counter.test.cc b/src/base/sat_counter.test.cc
index 19f792e..02b6a87 100644
--- a/src/base/sat_counter.test.cc
+++ b/src/base/sat_counter.test.cc
@@ -31,9 +31,44 @@
#include <utility>
+#include "base/gtest/logging.hh"
#include "base/sat_counter.hh"
/**
+ * Test that an error is triggered when the number of bits exceeds the
+ * counter's capacity.
+ */
+TEST(SatCounterDeathTest, BitCountExceeds)
+{
+#ifdef NDEBUG
+ GTEST_SKIP() << "Skipping as assertions are "
+ "stripped out of fast builds";
+#endif
+
+ gtestLogOutput.str("");
+ EXPECT_ANY_THROW(SatCounter8 counter(9));
+ ASSERT_NE(gtestLogOutput.str().find("Number of bits exceeds counter
size"),
+ std::string::npos);
+}
+
+/**
+ * Test that an error is triggered when the initial value is higher than
the
+ * maximum possible value.
+ */
+TEST(SatCounterDeathTest, InitialValueExceeds)
+{
+#ifdef NDEBUG
+ GTEST_SKIP() << "Skipping as assertions are "
+ "stripped out of fast builds";
+#endif
+
+ gtestLogOutput.str("");
+ EXPECT_ANY_THROW(SatCounter8 counter(7, 128));
+ ASSERT_NE(gtestLogOutput.str().find("initial value exceeds max value"),
+ std::string::npos);
+}
+
+/**
* Test if the maximum value is indeed the maximum value reachable.
*/
TEST(SatCounterTest, MaximumValue)
@@ -183,15 +218,36 @@
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. As these tests depend on asserts
failing,
- // these tests are only functional if `TRACING_ON == 1`, when gem5 is
- // compiled as `debug` or `opt`.
- #if TRACING_ON
+/**
+ * Make sure the counters cannot be right-shifted by negative numbers,
since
+ * that is undefined behaviour
+ */
+TEST(SatCounterDeathTest, RightShiftNegative)
+{
+#ifdef NDEBUG
+ GTEST_SKIP() << "Skipping as assertions are "
+ "stripped out of fast builds";
+#endif
+
+ SatCounter8 counter(8);
ASSERT_DEATH(counter >>= -1, "");
+}
+
+/**
+ * Make sure the counters cannot be left-shifted by negative numbers, since
+ * that is undefined behaviour
+ */
+TEST(SatCounterDeathTest, LeftShiftNegative)
+{
+#ifdef NDEBUG
+ GTEST_SKIP() << "Skipping as assertions are "
+ "stripped out of fast builds";
+#endif
+
+ SatCounter8 counter(8);
ASSERT_DEATH(counter <<= -1, "");
- #endif
}
/**
--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/42763
To unsubscribe, or for help writing mail filters, visit
https://gem5-review.googlesource.com/settings
Gerrit-Project: public/gem5
Gerrit-Branch: develop
Gerrit-Change-Id: Ia610636fe8cf636401e2b2ed623bf20b41147ea4
Gerrit-Change-Number: 42763
Gerrit-PatchSet: 1
Gerrit-Owner: Daniel Carvalho <[email protected]>
Gerrit-MessageType: newchange
_______________________________________________
gem5-dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s