summaryrefslogtreecommitdiff
path: root/libs/battery/MultiStateCounterTest.cpp
blob: a51a38a6c709dd60b1e27b99fc70b96038bdecc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
 * Copyright (C) 2021 The Android Open Source Project
 * Android BPF library - public API
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <gtest/gtest.h>
#include "MultiStateCounter.h"

namespace android {
namespace battery {

typedef MultiStateCounter<double> DoubleMultiStateCounter;

template <>
bool DoubleMultiStateCounter::delta(const double& previousValue, const double& newValue,
                                    double* outValue) const {
    *outValue = newValue - previousValue;
    return *outValue >= 0;
}

template <>
void DoubleMultiStateCounter::add(double* value1, const double& value2, const uint64_t numerator,
                                  const uint64_t denominator) const {
    if (numerator != denominator) {
        // The caller ensures that denominator != 0
        *value1 += value2 * numerator / denominator;
    } else {
        *value1 += value2;
    }
}

template <>
std::string DoubleMultiStateCounter::valueToString(const double& v) const {
    return std::to_string(v);
}

class MultiStateCounterTest : public testing::Test {};

TEST_F(MultiStateCounterTest, constructor) {
    DoubleMultiStateCounter testCounter(3, 0);
    testCounter.updateValue(0, 0);
    testCounter.setState(1, 0);
    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) {
    DoubleMultiStateCounter testCounter(3, 0);
    testCounter.updateValue(0, 0);
    testCounter.setState(1, 0);
    testCounter.setState(2, 1000);
    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, 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);
    testCounter.setState(1, 0);
    testCounter.setEnabled(false, 1000);
    testCounter.setState(2, 2000);
    testCounter.updateValue(6.0, 3000);

    // In state 1: accumulated 1000 before disabled, that's 6.0 * 1000/3000 = 2.0
    // In state 2: 0, since it is still disabled
    EXPECT_DOUBLE_EQ(0, testCounter.getCount(0));
    EXPECT_DOUBLE_EQ(2.0, testCounter.getCount(1));
    EXPECT_DOUBLE_EQ(0, testCounter.getCount(2));

    // Should have no effect since the counter is disabled
    testCounter.setState(0, 3500);

    // Should have no effect since the counter is disabled
    testCounter.updateValue(10.0, 4000);

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

    testCounter.setState(2, 4500);

    // Enable the counter to partially accumulate deltas for the current state, 2
    testCounter.setEnabled(true, 5000);
    testCounter.setEnabled(false, 6000);
    testCounter.setEnabled(true, 7000);
    testCounter.updateValue(20.0, 8000);

    // The delta is 10.0 over 5000-3000=2000.
    // Counter has been enabled in state 2 for (6000-5000)+(8000-7000) = 2000,
    // so its share is (20.0-10.0) * 2000/(8000-4000) = 5.0
    EXPECT_DOUBLE_EQ(0, testCounter.getCount(0));
    EXPECT_DOUBLE_EQ(2.0, testCounter.getCount(1));
    EXPECT_DOUBLE_EQ(5.0, testCounter.getCount(2));

    testCounter.reset();
    testCounter.setState(0, 0);
    testCounter.updateValue(0, 0);
    testCounter.setState(1, 2000);
    testCounter.setEnabled(false, 3000);
    testCounter.updateValue(200, 5000);

    // 200 over 5000 = 40 per second
    // Counter was in state 0 from 0 to 2000, so 2 sec, so the count should be 40 * 2 = 80
    // It stayed in state 1 from 2000 to 3000, at which point the counter was disabled,
    // so the count for state 1 should be 40 * 1 = 40.
    // The remaining 2 seconds from 3000 to 5000 don't count because the counter was disabled.
    EXPECT_DOUBLE_EQ(80.0, testCounter.getCount(0));
    EXPECT_DOUBLE_EQ(40.0, testCounter.getCount(1));
    EXPECT_DOUBLE_EQ(0, testCounter.getCount(2));
}

TEST_F(MultiStateCounterTest, reset) {
    DoubleMultiStateCounter testCounter(3, 0);
    testCounter.updateValue(0, 0);
    testCounter.setState(1, 0);
    testCounter.updateValue(2.72, 3000);

    testCounter.reset();

    EXPECT_DOUBLE_EQ(0, testCounter.getCount(0));
    EXPECT_DOUBLE_EQ(0, testCounter.getCount(1));
    EXPECT_DOUBLE_EQ(0, testCounter.getCount(2));

    // Assert that we can still continue accumulating after a reset
    testCounter.updateValue(0, 4000);
    testCounter.updateValue(3.14, 5000);

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

TEST_F(MultiStateCounterTest, timeAdjustment_setState) {
    DoubleMultiStateCounter testCounter(3, 0);
    testCounter.updateValue(0, 0);
    testCounter.setState(1, 0);
    testCounter.setState(2, 2000);

    // Time moves back
    testCounter.setState(1, 1000);
    testCounter.updateValue(6.0, 3000);

    EXPECT_DOUBLE_EQ(0, testCounter.getCount(0));

    // We were in state 1 from 0 to 2000, which was erased because the time moved back.
    // Then from 1000 to 3000, so we expect the count to be 6 * (2000/3000)
    EXPECT_DOUBLE_EQ(4.0, testCounter.getCount(1));

    // No time was effectively accumulated for state 2, because the timestamp moved back
    // while we were in state 2.
    EXPECT_DOUBLE_EQ(0, testCounter.getCount(2));
}

TEST_F(MultiStateCounterTest, timeAdjustment_updateValue) {
    DoubleMultiStateCounter testCounter(1, 0);
    testCounter.updateValue(0, 0);
    testCounter.setState(0, 0);
    testCounter.updateValue(6.0, 2000);

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

    // 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, 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, 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);
    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) {
    DoubleMultiStateCounter testCounter(2, 0);

    EXPECT_STREQ("[0: 0.000000, 1: 0.000000] currentState: none", testCounter.toString().c_str());

    testCounter.updateValue(0, 0);
    testCounter.setState(1, 0);
    testCounter.setState(1, 2000);
    EXPECT_STREQ("[0: 0.000000, 1: 0.000000 timeInStateSinceUpdate: 2000]"
                 " updated: 0 currentState: 1 stateChanged: 2000",
                 testCounter.toString().c_str());

    testCounter.updateValue(3.14, 3000);

    EXPECT_STREQ("[0: 0.000000, 1: 3.140000] updated: 3000 currentState: 1",
                 testCounter.toString().c_str());
}

} // namespace battery
} // namespace android