diff options
author | 2021-09-23 14:56:30 -0700 | |
---|---|---|
committer | 2021-09-23 14:56:30 -0700 | |
commit | 8940e5d69574cb4e068758e12fa15794114ff2ff (patch) | |
tree | d9df37abc58c2d097a4fd698e378906d9adbd8cd | |
parent | 5c78b18047eb5d8e532f6dfa482b2c0bf8eb96cb (diff) |
Add MultiStateCounter.addValue and make updateValue return delta
Bug: 197162116
Test: atest libbattery_test
Change-Id: I790ed0b805a88aa6ee9659f8494af8edf693d931
-rw-r--r-- | libs/battery/MultiStateCounter.h | 24 | ||||
-rw-r--r-- | libs/battery/MultiStateCounterTest.cpp | 24 |
2 files changed, 43 insertions, 5 deletions
diff --git a/libs/battery/MultiStateCounter.h b/libs/battery/MultiStateCounter.h index e1ee07ccf1..112fb85051 100644 --- a/libs/battery/MultiStateCounter.h +++ b/libs/battery/MultiStateCounter.h @@ -62,7 +62,13 @@ public: void setValue(state_t state, const T& value); - void updateValue(const T& value, time_t timestamp); + /** + * Updates the value for the current state and returns the delta from the previously + * set value. + */ + const T& updateValue(const T& value, time_t timestamp); + + void addValue(const T& value); void reset(); @@ -161,7 +167,7 @@ void MultiStateCounter<T>::setValue(state_t state, const T& value) { } template <class T> -void MultiStateCounter<T>::updateValue(const T& value, time_t timestamp) { +const T& MultiStateCounter<T>::updateValue(const T& value, time_t timestamp) { // If the counter is disabled, we ignore the update, except when the counter got disabled after // the previous update, in which case we still need to pick up the residual delta. if (isEnabled || lastUpdateTimestamp < lastStateChangeTimestamp) { @@ -195,6 +201,16 @@ void MultiStateCounter<T>::updateValue(const T& value, time_t timestamp) { } lastValue = value; lastUpdateTimestamp = timestamp; + return deltaValue; +} + +template <class T> +void MultiStateCounter<T>::addValue(const T& value) { + if (!isEnabled) { + return; + } + + add(&states[currentState].counter, value, 1 /* numerator */, 1 /* denominator */); } template <class T> @@ -242,7 +258,9 @@ std::string MultiStateCounter<T>::toString() { } else { str << " currentState: none"; } - + if (!isEnabled) { + str << " disabled"; + } return str.str(); } diff --git a/libs/battery/MultiStateCounterTest.cpp b/libs/battery/MultiStateCounterTest.cpp index 319ba76a4f..848fd10d15 100644 --- a/libs/battery/MultiStateCounterTest.cpp +++ b/libs/battery/MultiStateCounterTest.cpp @@ -52,11 +52,12 @@ TEST_F(MultiStateCounterTest, constructor) { DoubleMultiStateCounter testCounter(3, 0); testCounter.updateValue(0, 0); testCounter.setState(1, 0); - testCounter.updateValue(3.14, 3000); + double delta = testCounter.updateValue(3.14, 3000); EXPECT_DOUBLE_EQ(0, testCounter.getCount(0)); EXPECT_DOUBLE_EQ(3.14, testCounter.getCount(1)); EXPECT_DOUBLE_EQ(0, testCounter.getCount(2)); + EXPECT_DOUBLE_EQ(3.14, delta); } TEST_F(MultiStateCounterTest, stateChange) { @@ -177,12 +178,31 @@ TEST_F(MultiStateCounterTest, timeAdjustment_updateValue) { // Time moves back. The negative delta from 2000 to 1000 is ignored testCounter.updateValue(8.0, 1000); - testCounter.updateValue(11.0, 3000); + double delta = testCounter.updateValue(11.0, 3000); // The total accumulated count is: // 6.0 // For the period 0-2000 // +(11.0-8.0) // For the period 1000-3000 EXPECT_DOUBLE_EQ(9.0, testCounter.getCount(0)); + + // 11.0-8.0 + EXPECT_DOUBLE_EQ(3.0, delta); +} + +TEST_F(MultiStateCounterTest, addValue) { + DoubleMultiStateCounter testCounter(1, 0); + testCounter.updateValue(0, 0); + testCounter.setState(0, 0); + testCounter.updateValue(6.0, 2000); + + testCounter.addValue(8.0); + + EXPECT_DOUBLE_EQ(14.0, testCounter.getCount(0)); + + testCounter.setEnabled(false, 3000); + testCounter.addValue(888.0); + + EXPECT_DOUBLE_EQ(14.0, testCounter.getCount(0)); } TEST_F(MultiStateCounterTest, toString) { |