1 /*
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
21 namespace android {
22 namespace battery {
23
24 typedef MultiStateCounter<double> DoubleMultiStateCounter;
25
26 template <>
delta(const double & previousValue,const double & newValue,double * outValue) const27 bool DoubleMultiStateCounter::delta(const double& previousValue, const double& newValue,
28 double* outValue) const {
29 *outValue = newValue - previousValue;
30 return *outValue >= 0;
31 }
32
33 template <>
add(double * value1,const double & value2,const uint64_t numerator,const uint64_t denominator) const34 void 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
44 template <>
valueToString(const double & v) const45 std::string DoubleMultiStateCounter::valueToString(const double& v) const {
46 return std::to_string(v);
47 }
48
49 class MultiStateCounterTest : public testing::Test {};
50
TEST_F(MultiStateCounterTest,constructor)51 TEST_F(MultiStateCounterTest, constructor) {
52 DoubleMultiStateCounter testCounter(3, 0);
53 testCounter.updateValue(0, 0);
54 testCounter.setState(1, 0);
55 double delta = testCounter.updateValue(3.14, 3000);
56
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));
60 EXPECT_DOUBLE_EQ(3.14, delta);
61 }
62
TEST_F(MultiStateCounterTest,stateChange)63 TEST_F(MultiStateCounterTest, stateChange) {
64 DoubleMultiStateCounter testCounter(3, 0);
65 testCounter.updateValue(0, 0);
66 testCounter.setState(1, 0);
67 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
TEST_F(MultiStateCounterTest,copyStatesFrom)75 TEST_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
TEST_F(MultiStateCounterTest,setEnabled)91 TEST_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
TEST_F(MultiStateCounterTest,reset)147 TEST_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
TEST_F(MultiStateCounterTest,timeAdjustment_setState)168 TEST_F(MultiStateCounterTest, timeAdjustment_setState) {
169 DoubleMultiStateCounter testCounter(3, 0);
170 testCounter.updateValue(0, 0);
171 testCounter.setState(1, 0);
172 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
TEST_F(MultiStateCounterTest,timeAdjustment_updateValue)189 TEST_F(MultiStateCounterTest, timeAdjustment_updateValue) {
190 DoubleMultiStateCounter testCounter(1, 0);
191 testCounter.updateValue(0, 0);
192 testCounter.setState(0, 0);
193 testCounter.updateValue(6.0, 2000);
194
195 // Time moves back. The delta over the negative interval from 2000 to 1000 is ignored
196 testCounter.updateValue(8.0, 1000);
197 double delta = testCounter.updateValue(11.0, 3000);
198
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));
203
204 // 11.0-8.0
205 EXPECT_DOUBLE_EQ(3.0, delta);
206 }
207
TEST_F(MultiStateCounterTest,updateValue_nonmonotonic)208 TEST_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
TEST_F(MultiStateCounterTest,incrementValue)229 TEST_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
TEST_F(MultiStateCounterTest,addValue)249 TEST_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));
263 }
264
TEST_F(MultiStateCounterTest,toString)265 TEST_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
283 } // namespace battery
284 } // namespace android
285