Loading libs/battery/MultiStateCounter.h +8 −0 Original line number Diff line number Diff line Loading @@ -195,10 +195,18 @@ const T& MultiStateCounter<T>::updateValue(const T& value, time_t timestamp) { << ", which is lower than the previous value " << valueToString(lastValue) << "\n"; ALOGE("%s", str.str().c_str()); for (int i = 0; i < stateCount; i++) { states[i].timeInStateSinceUpdate = 0; } } } else if (timestamp < lastUpdateTimestamp) { ALOGE("updateValue is called with an earlier timestamp: %lu, previous: %lu\n", (unsigned long)timestamp, (unsigned long)lastUpdateTimestamp); for (int i = 0; i < stateCount; i++) { states[i].timeInStateSinceUpdate = 0; } } } } Loading libs/battery/MultiStateCounterTest.cpp +22 −1 Original line number Diff line number Diff line Loading @@ -176,7 +176,7 @@ TEST_F(MultiStateCounterTest, timeAdjustment_updateValue) { testCounter.setState(0, 0); testCounter.updateValue(6.0, 2000); // Time moves back. The negative delta from 2000 to 1000 is ignored // Time moves back. The delta over the negative interval from 2000 to 1000 is ignored testCounter.updateValue(8.0, 1000); double delta = testCounter.updateValue(11.0, 3000); Loading @@ -189,6 +189,27 @@ TEST_F(MultiStateCounterTest, timeAdjustment_updateValue) { EXPECT_DOUBLE_EQ(3.0, delta); } TEST_F(MultiStateCounterTest, updateValue_nonmonotonic) { DoubleMultiStateCounter testCounter(2, 0); testCounter.updateValue(0, 0); testCounter.setState(0, 0); testCounter.updateValue(6.0, 2000); // Value goes down. The negative delta from 6.0 to 4.0 is ignored testCounter.updateValue(4.0, 3000); // Value goes up again. The positive delta from 4.0 to 7.0 is accumulated. double delta = testCounter.updateValue(7.0, 4000); // The total accumulated count is: // 6.0 // For the period 0-2000 // +(7.0-4.0) // For the period 3000-4000 EXPECT_DOUBLE_EQ(9.0, testCounter.getCount(0)); // 7.0-4.0 EXPECT_DOUBLE_EQ(3.0, delta); } TEST_F(MultiStateCounterTest, addValue) { DoubleMultiStateCounter testCounter(1, 0); testCounter.updateValue(0, 0); Loading Loading
libs/battery/MultiStateCounter.h +8 −0 Original line number Diff line number Diff line Loading @@ -195,10 +195,18 @@ const T& MultiStateCounter<T>::updateValue(const T& value, time_t timestamp) { << ", which is lower than the previous value " << valueToString(lastValue) << "\n"; ALOGE("%s", str.str().c_str()); for (int i = 0; i < stateCount; i++) { states[i].timeInStateSinceUpdate = 0; } } } else if (timestamp < lastUpdateTimestamp) { ALOGE("updateValue is called with an earlier timestamp: %lu, previous: %lu\n", (unsigned long)timestamp, (unsigned long)lastUpdateTimestamp); for (int i = 0; i < stateCount; i++) { states[i].timeInStateSinceUpdate = 0; } } } } Loading
libs/battery/MultiStateCounterTest.cpp +22 −1 Original line number Diff line number Diff line Loading @@ -176,7 +176,7 @@ TEST_F(MultiStateCounterTest, timeAdjustment_updateValue) { testCounter.setState(0, 0); testCounter.updateValue(6.0, 2000); // Time moves back. The negative delta from 2000 to 1000 is ignored // Time moves back. The delta over the negative interval from 2000 to 1000 is ignored testCounter.updateValue(8.0, 1000); double delta = testCounter.updateValue(11.0, 3000); Loading @@ -189,6 +189,27 @@ TEST_F(MultiStateCounterTest, timeAdjustment_updateValue) { EXPECT_DOUBLE_EQ(3.0, delta); } TEST_F(MultiStateCounterTest, updateValue_nonmonotonic) { DoubleMultiStateCounter testCounter(2, 0); testCounter.updateValue(0, 0); testCounter.setState(0, 0); testCounter.updateValue(6.0, 2000); // Value goes down. The negative delta from 6.0 to 4.0 is ignored testCounter.updateValue(4.0, 3000); // Value goes up again. The positive delta from 4.0 to 7.0 is accumulated. double delta = testCounter.updateValue(7.0, 4000); // The total accumulated count is: // 6.0 // For the period 0-2000 // +(7.0-4.0) // For the period 3000-4000 EXPECT_DOUBLE_EQ(9.0, testCounter.getCount(0)); // 7.0-4.0 EXPECT_DOUBLE_EQ(3.0, delta); } TEST_F(MultiStateCounterTest, addValue) { DoubleMultiStateCounter testCounter(1, 0); testCounter.updateValue(0, 0); Loading