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

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

Merge "Add method copyStatesFrom" into main

parents 5013f984 756365be
Loading
Loading
Loading
Loading
+22 −0
Original line number Diff line number Diff line
@@ -62,6 +62,12 @@ public:

    void setState(state_t state, time_t timestamp);

    /**
     * Copies the current state and accumulated times-in-state from the source. Resets
     * the accumulated value.
     */
    void copyStatesFrom(const MultiStateCounter<T>& source);

    void setValue(state_t state, const T& value);

    /**
@@ -192,6 +198,22 @@ void MultiStateCounter<T>::setState(state_t state, time_t timestamp) {
    lastStateChangeTimestamp = timestamp;
}

template <class T>
void MultiStateCounter<T>::copyStatesFrom(const MultiStateCounter<T>& source) {
    if (stateCount != source.stateCount) {
        ALOGE("State count mismatch: %u vs. %u\n", stateCount, source.stateCount);
        return;
    }

    currentState = source.currentState;
    for (int i = 0; i < stateCount; i++) {
        states[i].timeInStateSinceUpdate = source.states[i].timeInStateSinceUpdate;
        states[i].counter = emptyValue;
    }
    lastStateChangeTimestamp = source.lastStateChangeTimestamp;
    lastUpdateTimestamp = source.lastUpdateTimestamp;
}

template <class T>
void MultiStateCounter<T>::setValue(state_t state, const T& value) {
    states[state].counter = value;
+16 −0
Original line number Diff line number Diff line
@@ -72,6 +72,22 @@ TEST_F(MultiStateCounterTest, stateChange) {
    EXPECT_DOUBLE_EQ(4.0, testCounter.getCount(2));
}

TEST_F(MultiStateCounterTest, copyStatesFrom) {
    DoubleMultiStateCounter sourceCounter(3, 0);

    sourceCounter.updateValue(0, 0);
    sourceCounter.setState(1, 0);
    sourceCounter.setState(2, 1000);

    DoubleMultiStateCounter testCounter(3, 0);
    testCounter.copyStatesFrom(sourceCounter);
    testCounter.updateValue(6.0, 3000);

    EXPECT_DOUBLE_EQ(0, testCounter.getCount(0));
    EXPECT_DOUBLE_EQ(2.0, testCounter.getCount(1));
    EXPECT_DOUBLE_EQ(4.0, testCounter.getCount(2));
}

TEST_F(MultiStateCounterTest, setEnabled) {
    DoubleMultiStateCounter testCounter(3, 0);
    testCounter.updateValue(0, 0);