Loading libs/battery/MultiStateCounter.h +22 −4 Original line number Diff line number Diff line Loading @@ -63,12 +63,24 @@ public: void setValue(state_t state, const T& value); /** * Updates the value for the current state and returns the delta from the previously * set value. * Updates the value by distributing the delta from the previously set value * among states according to their respective time-in-state. * Returns the delta from the previously set value. */ const T& updateValue(const T& value, time_t timestamp); void addValue(const T& value); /** * Updates the value by distributing the specified increment among states according * to their respective time-in-state. */ void incrementValue(const T& increment, time_t timestamp); /** * Adds the specified increment to the value for the current state, without affecting * the last updated value or timestamp. Ignores partial time-in-state: the entirety of * the increment is given to the current state. */ void addValue(const T& increment); void reset(); Loading Loading @@ -215,12 +227,18 @@ const T& MultiStateCounter<T>::updateValue(const T& value, time_t timestamp) { return *returnValue; } template <class T> void MultiStateCounter<T>::incrementValue(const T& increment, time_t timestamp) { T newValue = lastValue; add(&newValue, increment, 1 /* numerator */, 1 /* denominator */); updateValue(newValue, timestamp); } template <class T> void MultiStateCounter<T>::addValue(const T& value) { if (!isEnabled) { return; } add(&states[currentState].counter, value, 1 /* numerator */, 1 /* denominator */); } Loading libs/battery/MultiStateCounterTest.cpp +20 −0 Original line number Diff line number Diff line Loading @@ -210,6 +210,26 @@ TEST_F(MultiStateCounterTest, updateValue_nonmonotonic) { EXPECT_DOUBLE_EQ(3.0, delta); } TEST_F(MultiStateCounterTest, incrementValue) { DoubleMultiStateCounter testCounter(2, 0); testCounter.updateValue(0, 0); testCounter.setState(0, 0); testCounter.updateValue(6.0, 2000); testCounter.setState(1, 3000); testCounter.incrementValue(8.0, 6000); // The total accumulated count is: // 6.0 // For the period 0-2000 // +(8.0 * 0.25) // For the period 3000-4000 EXPECT_DOUBLE_EQ(8.0, testCounter.getCount(0)); // 0 // For the period 0-3000 // +(8.0 * 0.75) // For the period 3000-4000 EXPECT_DOUBLE_EQ(6.0, testCounter.getCount(1)); } TEST_F(MultiStateCounterTest, addValue) { DoubleMultiStateCounter testCounter(1, 0); testCounter.updateValue(0, 0); Loading Loading
libs/battery/MultiStateCounter.h +22 −4 Original line number Diff line number Diff line Loading @@ -63,12 +63,24 @@ public: void setValue(state_t state, const T& value); /** * Updates the value for the current state and returns the delta from the previously * set value. * Updates the value by distributing the delta from the previously set value * among states according to their respective time-in-state. * Returns the delta from the previously set value. */ const T& updateValue(const T& value, time_t timestamp); void addValue(const T& value); /** * Updates the value by distributing the specified increment among states according * to their respective time-in-state. */ void incrementValue(const T& increment, time_t timestamp); /** * Adds the specified increment to the value for the current state, without affecting * the last updated value or timestamp. Ignores partial time-in-state: the entirety of * the increment is given to the current state. */ void addValue(const T& increment); void reset(); Loading Loading @@ -215,12 +227,18 @@ const T& MultiStateCounter<T>::updateValue(const T& value, time_t timestamp) { return *returnValue; } template <class T> void MultiStateCounter<T>::incrementValue(const T& increment, time_t timestamp) { T newValue = lastValue; add(&newValue, increment, 1 /* numerator */, 1 /* denominator */); updateValue(newValue, timestamp); } template <class T> void MultiStateCounter<T>::addValue(const T& value) { if (!isEnabled) { return; } add(&states[currentState].counter, value, 1 /* numerator */, 1 /* denominator */); } Loading
libs/battery/MultiStateCounterTest.cpp +20 −0 Original line number Diff line number Diff line Loading @@ -210,6 +210,26 @@ TEST_F(MultiStateCounterTest, updateValue_nonmonotonic) { EXPECT_DOUBLE_EQ(3.0, delta); } TEST_F(MultiStateCounterTest, incrementValue) { DoubleMultiStateCounter testCounter(2, 0); testCounter.updateValue(0, 0); testCounter.setState(0, 0); testCounter.updateValue(6.0, 2000); testCounter.setState(1, 3000); testCounter.incrementValue(8.0, 6000); // The total accumulated count is: // 6.0 // For the period 0-2000 // +(8.0 * 0.25) // For the period 3000-4000 EXPECT_DOUBLE_EQ(8.0, testCounter.getCount(0)); // 0 // For the period 0-3000 // +(8.0 * 0.75) // For the period 3000-4000 EXPECT_DOUBLE_EQ(6.0, testCounter.getCount(1)); } TEST_F(MultiStateCounterTest, addValue) { DoubleMultiStateCounter testCounter(1, 0); testCounter.updateValue(0, 0); Loading