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,setEnabled)75 TEST_F(MultiStateCounterTest, setEnabled) {
76 DoubleMultiStateCounter testCounter(3, 0);
77 testCounter.updateValue(0, 0);
78 testCounter.setState(1, 0);
79 testCounter.setEnabled(false, 1000);
80 testCounter.setState(2, 2000);
81 testCounter.updateValue(6.0, 3000);
82
83 // In state 1: accumulated 1000 before disabled, that's 6.0 * 1000/3000 = 2.0
84 // In state 2: 0, since it is still disabled
85 EXPECT_DOUBLE_EQ(0, testCounter.getCount(0));
86 EXPECT_DOUBLE_EQ(2.0, testCounter.getCount(1));
87 EXPECT_DOUBLE_EQ(0, testCounter.getCount(2));
88
89 // Should have no effect since the counter is disabled
90 testCounter.setState(0, 3500);
91
92 // Should have no effect since the counter is disabled
93 testCounter.updateValue(10.0, 4000);
94
95 EXPECT_DOUBLE_EQ(0, testCounter.getCount(0));
96 EXPECT_DOUBLE_EQ(2.0, testCounter.getCount(1));
97 EXPECT_DOUBLE_EQ(0, testCounter.getCount(2));
98
99 testCounter.setState(2, 4500);
100
101 // Enable the counter to partially accumulate deltas for the current state, 2
102 testCounter.setEnabled(true, 5000);
103 testCounter.setEnabled(false, 6000);
104 testCounter.setEnabled(true, 7000);
105 testCounter.updateValue(20.0, 8000);
106
107 // The delta is 10.0 over 5000-3000=2000.
108 // Counter has been enabled in state 2 for (6000-5000)+(8000-7000) = 2000,
109 // so its share is (20.0-10.0) * 2000/(8000-4000) = 5.0
110 EXPECT_DOUBLE_EQ(0, testCounter.getCount(0));
111 EXPECT_DOUBLE_EQ(2.0, testCounter.getCount(1));
112 EXPECT_DOUBLE_EQ(5.0, testCounter.getCount(2));
113
114 testCounter.reset();
115 testCounter.setState(0, 0);
116 testCounter.updateValue(0, 0);
117 testCounter.setState(1, 2000);
118 testCounter.setEnabled(false, 3000);
119 testCounter.updateValue(200, 5000);
120
121 // 200 over 5000 = 40 per second
122 // Counter was in state 0 from 0 to 2000, so 2 sec, so the count should be 40 * 2 = 80
123 // It stayed in state 1 from 2000 to 3000, at which point the counter was disabled,
124 // so the count for state 1 should be 40 * 1 = 40.
125 // The remaining 2 seconds from 3000 to 5000 don't count because the counter was disabled.
126 EXPECT_DOUBLE_EQ(80.0, testCounter.getCount(0));
127 EXPECT_DOUBLE_EQ(40.0, testCounter.getCount(1));
128 EXPECT_DOUBLE_EQ(0, testCounter.getCount(2));
129 }
130
TEST_F(MultiStateCounterTest,reset)131 TEST_F(MultiStateCounterTest, reset) {
132 DoubleMultiStateCounter testCounter(3, 0);
133 testCounter.updateValue(0, 0);
134 testCounter.setState(1, 0);
135 testCounter.updateValue(2.72, 3000);
136
137 testCounter.reset();
138
139 EXPECT_DOUBLE_EQ(0, testCounter.getCount(0));
140 EXPECT_DOUBLE_EQ(0, testCounter.getCount(1));
141 EXPECT_DOUBLE_EQ(0, testCounter.getCount(2));
142
143 // Assert that we can still continue accumulating after a reset
144 testCounter.updateValue(0, 4000);
145 testCounter.updateValue(3.14, 5000);
146
147 EXPECT_DOUBLE_EQ(0, testCounter.getCount(0));
148 EXPECT_DOUBLE_EQ(3.14, testCounter.getCount(1));
149 EXPECT_DOUBLE_EQ(0, testCounter.getCount(2));
150 }
151
TEST_F(MultiStateCounterTest,timeAdjustment_setState)152 TEST_F(MultiStateCounterTest, timeAdjustment_setState) {
153 DoubleMultiStateCounter testCounter(3, 0);
154 testCounter.updateValue(0, 0);
155 testCounter.setState(1, 0);
156 testCounter.setState(2, 2000);
157
158 // Time moves back
159 testCounter.setState(1, 1000);
160 testCounter.updateValue(6.0, 3000);
161
162 EXPECT_DOUBLE_EQ(0, testCounter.getCount(0));
163
164 // We were in state 1 from 0 to 2000, which was erased because the time moved back.
165 // Then from 1000 to 3000, so we expect the count to be 6 * (2000/3000)
166 EXPECT_DOUBLE_EQ(4.0, testCounter.getCount(1));
167
168 // No time was effectively accumulated for state 2, because the timestamp moved back
169 // while we were in state 2.
170 EXPECT_DOUBLE_EQ(0, testCounter.getCount(2));
171 }
172
TEST_F(MultiStateCounterTest,timeAdjustment_updateValue)173 TEST_F(MultiStateCounterTest, timeAdjustment_updateValue) {
174 DoubleMultiStateCounter testCounter(1, 0);
175 testCounter.updateValue(0, 0);
176 testCounter.setState(0, 0);
177 testCounter.updateValue(6.0, 2000);
178
179 // Time moves back. The delta over the negative interval from 2000 to 1000 is ignored
180 testCounter.updateValue(8.0, 1000);
181 double delta = testCounter.updateValue(11.0, 3000);
182
183 // The total accumulated count is:
184 // 6.0 // For the period 0-2000
185 // +(11.0-8.0) // For the period 1000-3000
186 EXPECT_DOUBLE_EQ(9.0, testCounter.getCount(0));
187
188 // 11.0-8.0
189 EXPECT_DOUBLE_EQ(3.0, delta);
190 }
191
TEST_F(MultiStateCounterTest,updateValue_nonmonotonic)192 TEST_F(MultiStateCounterTest, updateValue_nonmonotonic) {
193 DoubleMultiStateCounter testCounter(2, 0);
194 testCounter.updateValue(0, 0);
195 testCounter.setState(0, 0);
196 testCounter.updateValue(6.0, 2000);
197
198 // Value goes down. The negative delta from 6.0 to 4.0 is ignored
199 testCounter.updateValue(4.0, 3000);
200
201 // Value goes up again. The positive delta from 4.0 to 7.0 is accumulated.
202 double delta = testCounter.updateValue(7.0, 4000);
203
204 // The total accumulated count is:
205 // 6.0 // For the period 0-2000
206 // +(7.0-4.0) // For the period 3000-4000
207 EXPECT_DOUBLE_EQ(9.0, testCounter.getCount(0));
208
209 // 7.0-4.0
210 EXPECT_DOUBLE_EQ(3.0, delta);
211 }
212
TEST_F(MultiStateCounterTest,incrementValue)213 TEST_F(MultiStateCounterTest, incrementValue) {
214 DoubleMultiStateCounter testCounter(2, 0);
215 testCounter.updateValue(0, 0);
216 testCounter.setState(0, 0);
217 testCounter.updateValue(6.0, 2000);
218
219 testCounter.setState(1, 3000);
220
221 testCounter.incrementValue(8.0, 6000);
222
223 // The total accumulated count is:
224 // 6.0 // For the period 0-2000
225 // +(8.0 * 0.25) // For the period 3000-4000
226 EXPECT_DOUBLE_EQ(8.0, testCounter.getCount(0));
227
228 // 0 // For the period 0-3000
229 // +(8.0 * 0.75) // For the period 3000-4000
230 EXPECT_DOUBLE_EQ(6.0, testCounter.getCount(1));
231 }
232
TEST_F(MultiStateCounterTest,addValue)233 TEST_F(MultiStateCounterTest, addValue) {
234 DoubleMultiStateCounter testCounter(1, 0);
235 testCounter.updateValue(0, 0);
236 testCounter.setState(0, 0);
237 testCounter.updateValue(6.0, 2000);
238
239 testCounter.addValue(8.0);
240
241 EXPECT_DOUBLE_EQ(14.0, testCounter.getCount(0));
242
243 testCounter.setEnabled(false, 3000);
244 testCounter.addValue(888.0);
245
246 EXPECT_DOUBLE_EQ(14.0, testCounter.getCount(0));
247 }
248
TEST_F(MultiStateCounterTest,toString)249 TEST_F(MultiStateCounterTest, toString) {
250 DoubleMultiStateCounter testCounter(2, 0);
251
252 EXPECT_STREQ("[0: 0.000000, 1: 0.000000] currentState: none", testCounter.toString().c_str());
253
254 testCounter.updateValue(0, 0);
255 testCounter.setState(1, 0);
256 testCounter.setState(1, 2000);
257 EXPECT_STREQ("[0: 0.000000, 1: 0.000000 timeInStateSinceUpdate: 2000]"
258 " updated: 0 currentState: 1 stateChanged: 2000",
259 testCounter.toString().c_str());
260
261 testCounter.updateValue(3.14, 3000);
262
263 EXPECT_STREQ("[0: 0.000000, 1: 3.140000] updated: 3000 currentState: 1",
264 testCounter.toString().c_str());
265 }
266
267 } // namespace battery
268 } // namespace android
269