blob: a51a38a6c709dd60b1e27b99fc70b96038bdecc3 [file] [log] [blame]
Dmitri Plotnikov5effd852021-08-11 14:55:58 -07001/*
2 * Copyright (C) 2021 The Android Open Source Project
3 * Android BPF library - public API
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include <gtest/gtest.h>
19#include "MultiStateCounter.h"
20
21namespace android {
22namespace battery {
23
24typedef MultiStateCounter<double> DoubleMultiStateCounter;
25
26template <>
27bool DoubleMultiStateCounter::delta(const double& previousValue, const double& newValue,
28 double* outValue) const {
29 *outValue = newValue - previousValue;
30 return *outValue >= 0;
31}
32
33template <>
34void DoubleMultiStateCounter::add(double* value1, const double& value2, const uint64_t numerator,
35 const uint64_t denominator) const {
36 if (numerator != denominator) {
37 // The caller ensures that denominator != 0
38 *value1 += value2 * numerator / denominator;
39 } else {
40 *value1 += value2;
41 }
42}
43
44template <>
45std::string DoubleMultiStateCounter::valueToString(const double& v) const {
46 return std::to_string(v);
47}
48
49class MultiStateCounterTest : public testing::Test {};
50
51TEST_F(MultiStateCounterTest, constructor) {
Dmitri Plotnikov12aaf8e2021-09-03 19:07:23 -070052 DoubleMultiStateCounter testCounter(3, 0);
53 testCounter.updateValue(0, 0);
54 testCounter.setState(1, 0);
Dmitri Plotnikov8940e5d2021-09-23 14:56:30 -070055 double delta = testCounter.updateValue(3.14, 3000);
Dmitri Plotnikov5effd852021-08-11 14:55:58 -070056
57 EXPECT_DOUBLE_EQ(0, testCounter.getCount(0));
58 EXPECT_DOUBLE_EQ(3.14, testCounter.getCount(1));
59 EXPECT_DOUBLE_EQ(0, testCounter.getCount(2));
Dmitri Plotnikov8940e5d2021-09-23 14:56:30 -070060 EXPECT_DOUBLE_EQ(3.14, delta);
Dmitri Plotnikov5effd852021-08-11 14:55:58 -070061}
62
63TEST_F(MultiStateCounterTest, stateChange) {
Dmitri Plotnikov12aaf8e2021-09-03 19:07:23 -070064 DoubleMultiStateCounter testCounter(3, 0);
65 testCounter.updateValue(0, 0);
66 testCounter.setState(1, 0);
Dmitri Plotnikov5effd852021-08-11 14:55:58 -070067 testCounter.setState(2, 1000);
68 testCounter.updateValue(6.0, 3000);
69
70 EXPECT_DOUBLE_EQ(0, testCounter.getCount(0));
71 EXPECT_DOUBLE_EQ(2.0, testCounter.getCount(1));
72 EXPECT_DOUBLE_EQ(4.0, testCounter.getCount(2));
73}
74
Dmitri Plotnikov756365b2024-03-25 17:39:59 -070075TEST_F(MultiStateCounterTest, copyStatesFrom) {
76 DoubleMultiStateCounter sourceCounter(3, 0);
77
78 sourceCounter.updateValue(0, 0);
79 sourceCounter.setState(1, 0);
80 sourceCounter.setState(2, 1000);
81
82 DoubleMultiStateCounter testCounter(3, 0);
83 testCounter.copyStatesFrom(sourceCounter);
84 testCounter.updateValue(6.0, 3000);
85
86 EXPECT_DOUBLE_EQ(0, testCounter.getCount(0));
87 EXPECT_DOUBLE_EQ(2.0, testCounter.getCount(1));
88 EXPECT_DOUBLE_EQ(4.0, testCounter.getCount(2));
89}
90
Dmitri Plotnikov99d7c712021-09-03 19:07:23 -070091TEST_F(MultiStateCounterTest, setEnabled) {
92 DoubleMultiStateCounter testCounter(3, 0);
93 testCounter.updateValue(0, 0);
94 testCounter.setState(1, 0);
95 testCounter.setEnabled(false, 1000);
96 testCounter.setState(2, 2000);
97 testCounter.updateValue(6.0, 3000);
98
99 // In state 1: accumulated 1000 before disabled, that's 6.0 * 1000/3000 = 2.0
100 // In state 2: 0, since it is still disabled
101 EXPECT_DOUBLE_EQ(0, testCounter.getCount(0));
102 EXPECT_DOUBLE_EQ(2.0, testCounter.getCount(1));
103 EXPECT_DOUBLE_EQ(0, testCounter.getCount(2));
104
105 // Should have no effect since the counter is disabled
106 testCounter.setState(0, 3500);
107
108 // Should have no effect since the counter is disabled
109 testCounter.updateValue(10.0, 4000);
110
111 EXPECT_DOUBLE_EQ(0, testCounter.getCount(0));
112 EXPECT_DOUBLE_EQ(2.0, testCounter.getCount(1));
113 EXPECT_DOUBLE_EQ(0, testCounter.getCount(2));
114
115 testCounter.setState(2, 4500);
116
117 // Enable the counter to partially accumulate deltas for the current state, 2
118 testCounter.setEnabled(true, 5000);
119 testCounter.setEnabled(false, 6000);
120 testCounter.setEnabled(true, 7000);
121 testCounter.updateValue(20.0, 8000);
122
123 // The delta is 10.0 over 5000-3000=2000.
124 // Counter has been enabled in state 2 for (6000-5000)+(8000-7000) = 2000,
125 // so its share is (20.0-10.0) * 2000/(8000-4000) = 5.0
126 EXPECT_DOUBLE_EQ(0, testCounter.getCount(0));
127 EXPECT_DOUBLE_EQ(2.0, testCounter.getCount(1));
128 EXPECT_DOUBLE_EQ(5.0, testCounter.getCount(2));
129
130 testCounter.reset();
131 testCounter.setState(0, 0);
132 testCounter.updateValue(0, 0);
133 testCounter.setState(1, 2000);
134 testCounter.setEnabled(false, 3000);
135 testCounter.updateValue(200, 5000);
136
137 // 200 over 5000 = 40 per second
138 // Counter was in state 0 from 0 to 2000, so 2 sec, so the count should be 40 * 2 = 80
139 // It stayed in state 1 from 2000 to 3000, at which point the counter was disabled,
140 // so the count for state 1 should be 40 * 1 = 40.
141 // The remaining 2 seconds from 3000 to 5000 don't count because the counter was disabled.
142 EXPECT_DOUBLE_EQ(80.0, testCounter.getCount(0));
143 EXPECT_DOUBLE_EQ(40.0, testCounter.getCount(1));
144 EXPECT_DOUBLE_EQ(0, testCounter.getCount(2));
145}
146
147TEST_F(MultiStateCounterTest, reset) {
148 DoubleMultiStateCounter testCounter(3, 0);
149 testCounter.updateValue(0, 0);
150 testCounter.setState(1, 0);
151 testCounter.updateValue(2.72, 3000);
152
153 testCounter.reset();
154
155 EXPECT_DOUBLE_EQ(0, testCounter.getCount(0));
156 EXPECT_DOUBLE_EQ(0, testCounter.getCount(1));
157 EXPECT_DOUBLE_EQ(0, testCounter.getCount(2));
158
159 // Assert that we can still continue accumulating after a reset
160 testCounter.updateValue(0, 4000);
161 testCounter.updateValue(3.14, 5000);
162
163 EXPECT_DOUBLE_EQ(0, testCounter.getCount(0));
164 EXPECT_DOUBLE_EQ(3.14, testCounter.getCount(1));
165 EXPECT_DOUBLE_EQ(0, testCounter.getCount(2));
166}
167
Dmitri Plotnikov5effd852021-08-11 14:55:58 -0700168TEST_F(MultiStateCounterTest, timeAdjustment_setState) {
Dmitri Plotnikov12aaf8e2021-09-03 19:07:23 -0700169 DoubleMultiStateCounter testCounter(3, 0);
170 testCounter.updateValue(0, 0);
171 testCounter.setState(1, 0);
Dmitri Plotnikov5effd852021-08-11 14:55:58 -0700172 testCounter.setState(2, 2000);
173
174 // Time moves back
175 testCounter.setState(1, 1000);
176 testCounter.updateValue(6.0, 3000);
177
178 EXPECT_DOUBLE_EQ(0, testCounter.getCount(0));
179
180 // We were in state 1 from 0 to 2000, which was erased because the time moved back.
181 // Then from 1000 to 3000, so we expect the count to be 6 * (2000/3000)
182 EXPECT_DOUBLE_EQ(4.0, testCounter.getCount(1));
183
184 // No time was effectively accumulated for state 2, because the timestamp moved back
185 // while we were in state 2.
186 EXPECT_DOUBLE_EQ(0, testCounter.getCount(2));
187}
188
189TEST_F(MultiStateCounterTest, timeAdjustment_updateValue) {
Dmitri Plotnikov12aaf8e2021-09-03 19:07:23 -0700190 DoubleMultiStateCounter testCounter(1, 0);
191 testCounter.updateValue(0, 0);
192 testCounter.setState(0, 0);
Dmitri Plotnikov5effd852021-08-11 14:55:58 -0700193 testCounter.updateValue(6.0, 2000);
194
Dmitri Plotnikov4b238fb2021-11-09 11:52:08 -0800195 // Time moves back. The delta over the negative interval from 2000 to 1000 is ignored
Dmitri Plotnikov5effd852021-08-11 14:55:58 -0700196 testCounter.updateValue(8.0, 1000);
Dmitri Plotnikov8940e5d2021-09-23 14:56:30 -0700197 double delta = testCounter.updateValue(11.0, 3000);
Dmitri Plotnikov5effd852021-08-11 14:55:58 -0700198
199 // The total accumulated count is:
200 // 6.0 // For the period 0-2000
201 // +(11.0-8.0) // For the period 1000-3000
202 EXPECT_DOUBLE_EQ(9.0, testCounter.getCount(0));
Dmitri Plotnikov8940e5d2021-09-23 14:56:30 -0700203
204 // 11.0-8.0
205 EXPECT_DOUBLE_EQ(3.0, delta);
206}
207
Dmitri Plotnikov4b238fb2021-11-09 11:52:08 -0800208TEST_F(MultiStateCounterTest, updateValue_nonmonotonic) {
209 DoubleMultiStateCounter testCounter(2, 0);
210 testCounter.updateValue(0, 0);
211 testCounter.setState(0, 0);
212 testCounter.updateValue(6.0, 2000);
213
214 // Value goes down. The negative delta from 6.0 to 4.0 is ignored
215 testCounter.updateValue(4.0, 3000);
216
217 // Value goes up again. The positive delta from 4.0 to 7.0 is accumulated.
218 double delta = testCounter.updateValue(7.0, 4000);
219
220 // The total accumulated count is:
221 // 6.0 // For the period 0-2000
222 // +(7.0-4.0) // For the period 3000-4000
223 EXPECT_DOUBLE_EQ(9.0, testCounter.getCount(0));
224
225 // 7.0-4.0
226 EXPECT_DOUBLE_EQ(3.0, delta);
227}
228
Dmitri Plotnikov40dcce22021-12-10 15:38:17 -0800229TEST_F(MultiStateCounterTest, incrementValue) {
230 DoubleMultiStateCounter testCounter(2, 0);
231 testCounter.updateValue(0, 0);
232 testCounter.setState(0, 0);
233 testCounter.updateValue(6.0, 2000);
234
235 testCounter.setState(1, 3000);
236
237 testCounter.incrementValue(8.0, 6000);
238
239 // The total accumulated count is:
240 // 6.0 // For the period 0-2000
241 // +(8.0 * 0.25) // For the period 3000-4000
242 EXPECT_DOUBLE_EQ(8.0, testCounter.getCount(0));
243
244 // 0 // For the period 0-3000
245 // +(8.0 * 0.75) // For the period 3000-4000
246 EXPECT_DOUBLE_EQ(6.0, testCounter.getCount(1));
247}
248
Dmitri Plotnikov8940e5d2021-09-23 14:56:30 -0700249TEST_F(MultiStateCounterTest, addValue) {
250 DoubleMultiStateCounter testCounter(1, 0);
251 testCounter.updateValue(0, 0);
252 testCounter.setState(0, 0);
253 testCounter.updateValue(6.0, 2000);
254
255 testCounter.addValue(8.0);
256
257 EXPECT_DOUBLE_EQ(14.0, testCounter.getCount(0));
258
259 testCounter.setEnabled(false, 3000);
260 testCounter.addValue(888.0);
261
262 EXPECT_DOUBLE_EQ(14.0, testCounter.getCount(0));
Dmitri Plotnikov5effd852021-08-11 14:55:58 -0700263}
264
Dmitri Plotnikov12aaf8e2021-09-03 19:07:23 -0700265TEST_F(MultiStateCounterTest, toString) {
266 DoubleMultiStateCounter testCounter(2, 0);
267
268 EXPECT_STREQ("[0: 0.000000, 1: 0.000000] currentState: none", testCounter.toString().c_str());
269
270 testCounter.updateValue(0, 0);
271 testCounter.setState(1, 0);
272 testCounter.setState(1, 2000);
273 EXPECT_STREQ("[0: 0.000000, 1: 0.000000 timeInStateSinceUpdate: 2000]"
274 " updated: 0 currentState: 1 stateChanged: 2000",
275 testCounter.toString().c_str());
276
277 testCounter.updateValue(3.14, 3000);
278
279 EXPECT_STREQ("[0: 0.000000, 1: 3.140000] updated: 3000 currentState: 1",
280 testCounter.toString().c_str());
281}
282
Dmitri Plotnikov5effd852021-08-11 14:55:58 -0700283} // namespace battery
284} // namespace android