summaryrefslogtreecommitdiff
path: root/src/base
diff options
context:
space:
mode:
Diffstat (limited to 'src/base')
-rw-r--r--src/base/sat_counter.hh12
-rw-r--r--src/base/sat_counter.test.cc17
2 files changed, 29 insertions, 0 deletions
diff --git a/src/base/sat_counter.hh b/src/base/sat_counter.hh
index 3de9486db..6aeca681d 100644
--- a/src/base/sat_counter.hh
+++ b/src/base/sat_counter.hh
@@ -235,6 +235,18 @@ class SatCounter
*/
bool isSaturated() const { return counter == maxVal; }
+ /**
+ * Saturate the counter.
+ *
+ * @return The value added to the counter to reach saturation.
+ */
+ uint8_t saturate()
+ {
+ const uint8_t diff = maxVal - counter;
+ counter = maxVal;
+ return diff;
+ }
+
private:
uint8_t initialVal;
uint8_t maxVal;
diff --git a/src/base/sat_counter.test.cc b/src/base/sat_counter.test.cc
index dbdaf0ae6..817f2c7ac 100644
--- a/src/base/sat_counter.test.cc
+++ b/src/base/sat_counter.test.cc
@@ -97,6 +97,23 @@ TEST(SatCounterTest, SaturationPercentile)
}
/**
+ * Test abrupt saturation.
+ */
+TEST(SatCounterTest, Saturate)
+{
+ const unsigned bits = 3;
+ const unsigned max_value = (1 << bits) - 1;
+ SatCounter counter(bits);
+ counter++;
+ ASSERT_FALSE(counter.isSaturated());
+
+ // Make sure the value added is what was missing to saturate
+ const unsigned diff = counter.saturate();
+ ASSERT_EQ(diff, max_value - 1);
+ ASSERT_TRUE(counter.isSaturated());
+}
+
+/**
* Test back and forth against an int.
*/
TEST(SatCounterTest, IntComparison)