blob: 848fd10d156bbba3cb5f2c263ea884d78985c0fe [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 Plotnikov99d7c712021-09-03 19:07:23 -070075TEST_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
131TEST_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
Dmitri Plotnikov5effd852021-08-11 14:55:58 -0700152TEST_F(MultiStateCounterTest, timeAdjustment_setState) {
Dmitri Plotnikov12aaf8e2021-09-03 19:07:23 -0700153 DoubleMultiStateCounter testCounter(3, 0);
154 testCounter.updateValue(0, 0);
155 testCounter.setState(1, 0);
Dmitri Plotnikov5effd852021-08-11 14:55:58 -0700156 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
173TEST_F(MultiStateCounterTest, timeAdjustment_updateValue) {
Dmitri Plotnikov12aaf8e2021-09-03 19:07:23 -0700174 DoubleMultiStateCounter testCounter(1, 0);
175 testCounter.updateValue(0, 0);
176 testCounter.setState(0, 0);
Dmitri Plotnikov5effd852021-08-11 14:55:58 -0700177 testCounter.updateValue(6.0, 2000);
178
179 // Time moves back. The negative delta from 2000 to 1000 is ignored
180 testCounter.updateValue(8.0, 1000);
Dmitri Plotnikov8940e5d2021-09-23 14:56:30 -0700181 double delta = testCounter.updateValue(11.0, 3000);
Dmitri Plotnikov5effd852021-08-11 14:55:58 -0700182
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));
Dmitri Plotnikov8940e5d2021-09-23 14:56:30 -0700187
188 // 11.0-8.0
189 EXPECT_DOUBLE_EQ(3.0, delta);
190}
191
192TEST_F(MultiStateCounterTest, addValue) {
193 DoubleMultiStateCounter testCounter(1, 0);
194 testCounter.updateValue(0, 0);
195 testCounter.setState(0, 0);
196 testCounter.updateValue(6.0, 2000);
197
198 testCounter.addValue(8.0);
199
200 EXPECT_DOUBLE_EQ(14.0, testCounter.getCount(0));
201
202 testCounter.setEnabled(false, 3000);
203 testCounter.addValue(888.0);
204
205 EXPECT_DOUBLE_EQ(14.0, testCounter.getCount(0));
Dmitri Plotnikov5effd852021-08-11 14:55:58 -0700206}
207
Dmitri Plotnikov12aaf8e2021-09-03 19:07:23 -0700208TEST_F(MultiStateCounterTest, toString) {
209 DoubleMultiStateCounter testCounter(2, 0);
210
211 EXPECT_STREQ("[0: 0.000000, 1: 0.000000] currentState: none", testCounter.toString().c_str());
212
213 testCounter.updateValue(0, 0);
214 testCounter.setState(1, 0);
215 testCounter.setState(1, 2000);
216 EXPECT_STREQ("[0: 0.000000, 1: 0.000000 timeInStateSinceUpdate: 2000]"
217 " updated: 0 currentState: 1 stateChanged: 2000",
218 testCounter.toString().c_str());
219
220 testCounter.updateValue(3.14, 3000);
221
222 EXPECT_STREQ("[0: 0.000000, 1: 3.140000] updated: 3000 currentState: 1",
223 testCounter.toString().c_str());
224}
225
Dmitri Plotnikov5effd852021-08-11 14:55:58 -0700226} // namespace battery
227} // namespace android