Loading libs/battery/MultiStateCounter.h +21 −3 Original line number Diff line number Diff line Loading @@ -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(); Loading Loading @@ -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) { Loading Loading @@ -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> Loading Loading @@ -242,7 +258,9 @@ std::string MultiStateCounter<T>::toString() { } else { str << " currentState: none"; } if (!isEnabled) { str << " disabled"; } return str.str(); } Loading libs/battery/MultiStateCounterTest.cpp +22 −2 Original line number Diff line number Diff line Loading @@ -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) { Loading Loading @@ -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) { Loading Loading
libs/battery/MultiStateCounter.h +21 −3 Original line number Diff line number Diff line Loading @@ -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(); Loading Loading @@ -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) { Loading Loading @@ -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> Loading Loading @@ -242,7 +258,9 @@ std::string MultiStateCounter<T>::toString() { } else { str << " currentState: none"; } if (!isEnabled) { str << " disabled"; } return str.str(); } Loading
libs/battery/MultiStateCounterTest.cpp +22 −2 Original line number Diff line number Diff line Loading @@ -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) { Loading Loading @@ -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) { Loading