Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3ecdfeaf authored by Dmitri Plotnikov's avatar Dmitri Plotnikov Committed by Android (Google) Code Review
Browse files

Merge "Reset time-since-update if the tracked value is nonmonotonic"

parents 162176e3 4b238fb0
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -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;
                }
            }
        }
    }
+22 −1
Original line number Diff line number Diff line
@@ -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);

@@ -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);