*/
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;
ASSERT_TRUE(counter.isSaturated());
}
+/**
+ * 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.
*/