blob: 87c80c53d3bd7e295bbe22299b1aff593c7bd0ea [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 Plotnikov5effd852021-08-11 14:55:58 -070055 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}
61
62TEST_F(MultiStateCounterTest, stateChange) {
Dmitri Plotnikov12aaf8e2021-09-03 19:07:23 -070063 DoubleMultiStateCounter testCounter(3, 0);
64 testCounter.updateValue(0, 0);
65 testCounter.setState(1, 0);
Dmitri Plotnikov5effd852021-08-11 14:55:58 -070066 testCounter.setState(2, 1000);
67 testCounter.updateValue(6.0, 3000);
68
69 EXPECT_DOUBLE_EQ(0, testCounter.getCount(0));
70 EXPECT_DOUBLE_EQ(2.0, testCounter.getCount(1));
71 EXPECT_DOUBLE_EQ(4.0, testCounter.getCount(2));
72}
73
74TEST_F(MultiStateCounterTest, timeAdjustment_setState) {
Dmitri Plotnikov12aaf8e2021-09-03 19:07:23 -070075 DoubleMultiStateCounter testCounter(3, 0);
76 testCounter.updateValue(0, 0);
77 testCounter.setState(1, 0);
Dmitri Plotnikov5effd852021-08-11 14:55:58 -070078 testCounter.setState(2, 2000);
79
80 // Time moves back
81 testCounter.setState(1, 1000);
82 testCounter.updateValue(6.0, 3000);
83
84 EXPECT_DOUBLE_EQ(0, testCounter.getCount(0));
85
86 // We were in state 1 from 0 to 2000, which was erased because the time moved back.
87 // Then from 1000 to 3000, so we expect the count to be 6 * (2000/3000)
88 EXPECT_DOUBLE_EQ(4.0, testCounter.getCount(1));
89
90 // No time was effectively accumulated for state 2, because the timestamp moved back
91 // while we were in state 2.
92 EXPECT_DOUBLE_EQ(0, testCounter.getCount(2));
93}
94
95TEST_F(MultiStateCounterTest, timeAdjustment_updateValue) {
Dmitri Plotnikov12aaf8e2021-09-03 19:07:23 -070096 DoubleMultiStateCounter testCounter(1, 0);
97 testCounter.updateValue(0, 0);
98 testCounter.setState(0, 0);
Dmitri Plotnikov5effd852021-08-11 14:55:58 -070099 testCounter.updateValue(6.0, 2000);
100
101 // Time moves back. The negative delta from 2000 to 1000 is ignored
102 testCounter.updateValue(8.0, 1000);
103 testCounter.updateValue(11.0, 3000);
104
105 // The total accumulated count is:
106 // 6.0 // For the period 0-2000
107 // +(11.0-8.0) // For the period 1000-3000
108 EXPECT_DOUBLE_EQ(9.0, testCounter.getCount(0));
109}
110
Dmitri Plotnikov12aaf8e2021-09-03 19:07:23 -0700111TEST_F(MultiStateCounterTest, toString) {
112 DoubleMultiStateCounter testCounter(2, 0);
113
114 EXPECT_STREQ("[0: 0.000000, 1: 0.000000] currentState: none", testCounter.toString().c_str());
115
116 testCounter.updateValue(0, 0);
117 testCounter.setState(1, 0);
118 testCounter.setState(1, 2000);
119 EXPECT_STREQ("[0: 0.000000, 1: 0.000000 timeInStateSinceUpdate: 2000]"
120 " updated: 0 currentState: 1 stateChanged: 2000",
121 testCounter.toString().c_str());
122
123 testCounter.updateValue(3.14, 3000);
124
125 EXPECT_STREQ("[0: 0.000000, 1: 3.140000] updated: 3000 currentState: 1",
126 testCounter.toString().c_str());
127}
128
Dmitri Plotnikov5effd852021-08-11 14:55:58 -0700129} // namespace battery
130} // namespace android