Dmitri Plotnikov | 5effd85 | 2021-08-11 14:55:58 -0700 | [diff] [blame] | 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 <> |
| 27 | 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 <> |
| 34 | 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 <> |
| 45 | std::string DoubleMultiStateCounter::valueToString(const double& v) const { |
| 46 | return std::to_string(v); |
| 47 | } |
| 48 | |
| 49 | class MultiStateCounterTest : public testing::Test {}; |
| 50 | |
| 51 | TEST_F(MultiStateCounterTest, constructor) { |
Dmitri Plotnikov | 12aaf8e | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 52 | DoubleMultiStateCounter testCounter(3, 0); |
| 53 | testCounter.updateValue(0, 0); |
| 54 | testCounter.setState(1, 0); |
Dmitri Plotnikov | 5effd85 | 2021-08-11 14:55:58 -0700 | [diff] [blame] | 55 | 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 | |
| 62 | TEST_F(MultiStateCounterTest, stateChange) { |
Dmitri Plotnikov | 12aaf8e | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 63 | DoubleMultiStateCounter testCounter(3, 0); |
| 64 | testCounter.updateValue(0, 0); |
| 65 | testCounter.setState(1, 0); |
Dmitri Plotnikov | 5effd85 | 2021-08-11 14:55:58 -0700 | [diff] [blame] | 66 | 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 | |
Dmitri Plotnikov | 99d7c71 | 2021-09-03 19:07:23 -0700 | [diff] [blame^] | 74 | TEST_F(MultiStateCounterTest, setEnabled) { |
| 75 | DoubleMultiStateCounter testCounter(3, 0); |
| 76 | testCounter.updateValue(0, 0); |
| 77 | testCounter.setState(1, 0); |
| 78 | testCounter.setEnabled(false, 1000); |
| 79 | testCounter.setState(2, 2000); |
| 80 | testCounter.updateValue(6.0, 3000); |
| 81 | |
| 82 | // In state 1: accumulated 1000 before disabled, that's 6.0 * 1000/3000 = 2.0 |
| 83 | // In state 2: 0, since it is still disabled |
| 84 | EXPECT_DOUBLE_EQ(0, testCounter.getCount(0)); |
| 85 | EXPECT_DOUBLE_EQ(2.0, testCounter.getCount(1)); |
| 86 | EXPECT_DOUBLE_EQ(0, testCounter.getCount(2)); |
| 87 | |
| 88 | // Should have no effect since the counter is disabled |
| 89 | testCounter.setState(0, 3500); |
| 90 | |
| 91 | // Should have no effect since the counter is disabled |
| 92 | testCounter.updateValue(10.0, 4000); |
| 93 | |
| 94 | EXPECT_DOUBLE_EQ(0, testCounter.getCount(0)); |
| 95 | EXPECT_DOUBLE_EQ(2.0, testCounter.getCount(1)); |
| 96 | EXPECT_DOUBLE_EQ(0, testCounter.getCount(2)); |
| 97 | |
| 98 | testCounter.setState(2, 4500); |
| 99 | |
| 100 | // Enable the counter to partially accumulate deltas for the current state, 2 |
| 101 | testCounter.setEnabled(true, 5000); |
| 102 | testCounter.setEnabled(false, 6000); |
| 103 | testCounter.setEnabled(true, 7000); |
| 104 | testCounter.updateValue(20.0, 8000); |
| 105 | |
| 106 | // The delta is 10.0 over 5000-3000=2000. |
| 107 | // Counter has been enabled in state 2 for (6000-5000)+(8000-7000) = 2000, |
| 108 | // so its share is (20.0-10.0) * 2000/(8000-4000) = 5.0 |
| 109 | EXPECT_DOUBLE_EQ(0, testCounter.getCount(0)); |
| 110 | EXPECT_DOUBLE_EQ(2.0, testCounter.getCount(1)); |
| 111 | EXPECT_DOUBLE_EQ(5.0, testCounter.getCount(2)); |
| 112 | |
| 113 | testCounter.reset(); |
| 114 | testCounter.setState(0, 0); |
| 115 | testCounter.updateValue(0, 0); |
| 116 | testCounter.setState(1, 2000); |
| 117 | testCounter.setEnabled(false, 3000); |
| 118 | testCounter.updateValue(200, 5000); |
| 119 | |
| 120 | // 200 over 5000 = 40 per second |
| 121 | // Counter was in state 0 from 0 to 2000, so 2 sec, so the count should be 40 * 2 = 80 |
| 122 | // It stayed in state 1 from 2000 to 3000, at which point the counter was disabled, |
| 123 | // so the count for state 1 should be 40 * 1 = 40. |
| 124 | // The remaining 2 seconds from 3000 to 5000 don't count because the counter was disabled. |
| 125 | EXPECT_DOUBLE_EQ(80.0, testCounter.getCount(0)); |
| 126 | EXPECT_DOUBLE_EQ(40.0, testCounter.getCount(1)); |
| 127 | EXPECT_DOUBLE_EQ(0, testCounter.getCount(2)); |
| 128 | } |
| 129 | |
| 130 | TEST_F(MultiStateCounterTest, reset) { |
| 131 | DoubleMultiStateCounter testCounter(3, 0); |
| 132 | testCounter.updateValue(0, 0); |
| 133 | testCounter.setState(1, 0); |
| 134 | testCounter.updateValue(2.72, 3000); |
| 135 | |
| 136 | testCounter.reset(); |
| 137 | |
| 138 | EXPECT_DOUBLE_EQ(0, testCounter.getCount(0)); |
| 139 | EXPECT_DOUBLE_EQ(0, testCounter.getCount(1)); |
| 140 | EXPECT_DOUBLE_EQ(0, testCounter.getCount(2)); |
| 141 | |
| 142 | // Assert that we can still continue accumulating after a reset |
| 143 | testCounter.updateValue(0, 4000); |
| 144 | testCounter.updateValue(3.14, 5000); |
| 145 | |
| 146 | EXPECT_DOUBLE_EQ(0, testCounter.getCount(0)); |
| 147 | EXPECT_DOUBLE_EQ(3.14, testCounter.getCount(1)); |
| 148 | EXPECT_DOUBLE_EQ(0, testCounter.getCount(2)); |
| 149 | } |
| 150 | |
Dmitri Plotnikov | 5effd85 | 2021-08-11 14:55:58 -0700 | [diff] [blame] | 151 | TEST_F(MultiStateCounterTest, timeAdjustment_setState) { |
Dmitri Plotnikov | 12aaf8e | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 152 | DoubleMultiStateCounter testCounter(3, 0); |
| 153 | testCounter.updateValue(0, 0); |
| 154 | testCounter.setState(1, 0); |
Dmitri Plotnikov | 5effd85 | 2021-08-11 14:55:58 -0700 | [diff] [blame] | 155 | testCounter.setState(2, 2000); |
| 156 | |
| 157 | // Time moves back |
| 158 | testCounter.setState(1, 1000); |
| 159 | testCounter.updateValue(6.0, 3000); |
| 160 | |
| 161 | EXPECT_DOUBLE_EQ(0, testCounter.getCount(0)); |
| 162 | |
| 163 | // We were in state 1 from 0 to 2000, which was erased because the time moved back. |
| 164 | // Then from 1000 to 3000, so we expect the count to be 6 * (2000/3000) |
| 165 | EXPECT_DOUBLE_EQ(4.0, testCounter.getCount(1)); |
| 166 | |
| 167 | // No time was effectively accumulated for state 2, because the timestamp moved back |
| 168 | // while we were in state 2. |
| 169 | EXPECT_DOUBLE_EQ(0, testCounter.getCount(2)); |
| 170 | } |
| 171 | |
| 172 | TEST_F(MultiStateCounterTest, timeAdjustment_updateValue) { |
Dmitri Plotnikov | 12aaf8e | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 173 | DoubleMultiStateCounter testCounter(1, 0); |
| 174 | testCounter.updateValue(0, 0); |
| 175 | testCounter.setState(0, 0); |
Dmitri Plotnikov | 5effd85 | 2021-08-11 14:55:58 -0700 | [diff] [blame] | 176 | testCounter.updateValue(6.0, 2000); |
| 177 | |
| 178 | // Time moves back. The negative delta from 2000 to 1000 is ignored |
| 179 | testCounter.updateValue(8.0, 1000); |
| 180 | testCounter.updateValue(11.0, 3000); |
| 181 | |
| 182 | // The total accumulated count is: |
| 183 | // 6.0 // For the period 0-2000 |
| 184 | // +(11.0-8.0) // For the period 1000-3000 |
| 185 | EXPECT_DOUBLE_EQ(9.0, testCounter.getCount(0)); |
| 186 | } |
| 187 | |
Dmitri Plotnikov | 12aaf8e | 2021-09-03 19:07:23 -0700 | [diff] [blame] | 188 | TEST_F(MultiStateCounterTest, toString) { |
| 189 | DoubleMultiStateCounter testCounter(2, 0); |
| 190 | |
| 191 | EXPECT_STREQ("[0: 0.000000, 1: 0.000000] currentState: none", testCounter.toString().c_str()); |
| 192 | |
| 193 | testCounter.updateValue(0, 0); |
| 194 | testCounter.setState(1, 0); |
| 195 | testCounter.setState(1, 2000); |
| 196 | EXPECT_STREQ("[0: 0.000000, 1: 0.000000 timeInStateSinceUpdate: 2000]" |
| 197 | " updated: 0 currentState: 1 stateChanged: 2000", |
| 198 | testCounter.toString().c_str()); |
| 199 | |
| 200 | testCounter.updateValue(3.14, 3000); |
| 201 | |
| 202 | EXPECT_STREQ("[0: 0.000000, 1: 3.140000] updated: 3000 currentState: 1", |
| 203 | testCounter.toString().c_str()); |
| 204 | } |
| 205 | |
Dmitri Plotnikov | 5effd85 | 2021-08-11 14:55:58 -0700 | [diff] [blame] | 206 | } // namespace battery |
| 207 | } // namespace android |