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) { |
| 52 | DoubleMultiStateCounter testCounter(3, 1, 0, 1000); |
| 53 | testCounter.setState(1, 2000); |
| 54 | testCounter.updateValue(3.14, 3000); |
| 55 | |
| 56 | EXPECT_DOUBLE_EQ(0, testCounter.getCount(0)); |
| 57 | EXPECT_DOUBLE_EQ(3.14, testCounter.getCount(1)); |
| 58 | EXPECT_DOUBLE_EQ(0, testCounter.getCount(2)); |
| 59 | } |
| 60 | |
| 61 | TEST_F(MultiStateCounterTest, stateChange) { |
| 62 | DoubleMultiStateCounter testCounter(3, 1, 0, 0); |
| 63 | testCounter.setState(2, 1000); |
| 64 | testCounter.updateValue(6.0, 3000); |
| 65 | |
| 66 | EXPECT_DOUBLE_EQ(0, testCounter.getCount(0)); |
| 67 | EXPECT_DOUBLE_EQ(2.0, testCounter.getCount(1)); |
| 68 | EXPECT_DOUBLE_EQ(4.0, testCounter.getCount(2)); |
| 69 | } |
| 70 | |
| 71 | TEST_F(MultiStateCounterTest, timeAdjustment_setState) { |
| 72 | DoubleMultiStateCounter testCounter(3, 1, 0, 0); |
| 73 | testCounter.setState(2, 2000); |
| 74 | |
| 75 | // Time moves back |
| 76 | testCounter.setState(1, 1000); |
| 77 | testCounter.updateValue(6.0, 3000); |
| 78 | |
| 79 | EXPECT_DOUBLE_EQ(0, testCounter.getCount(0)); |
| 80 | |
| 81 | // We were in state 1 from 0 to 2000, which was erased because the time moved back. |
| 82 | // Then from 1000 to 3000, so we expect the count to be 6 * (2000/3000) |
| 83 | EXPECT_DOUBLE_EQ(4.0, testCounter.getCount(1)); |
| 84 | |
| 85 | // No time was effectively accumulated for state 2, because the timestamp moved back |
| 86 | // while we were in state 2. |
| 87 | EXPECT_DOUBLE_EQ(0, testCounter.getCount(2)); |
| 88 | } |
| 89 | |
| 90 | TEST_F(MultiStateCounterTest, timeAdjustment_updateValue) { |
| 91 | DoubleMultiStateCounter testCounter(1, 0, 0, 0); |
| 92 | testCounter.updateValue(6.0, 2000); |
| 93 | |
| 94 | // Time moves back. The negative delta from 2000 to 1000 is ignored |
| 95 | testCounter.updateValue(8.0, 1000); |
| 96 | testCounter.updateValue(11.0, 3000); |
| 97 | |
| 98 | // The total accumulated count is: |
| 99 | // 6.0 // For the period 0-2000 |
| 100 | // +(11.0-8.0) // For the period 1000-3000 |
| 101 | EXPECT_DOUBLE_EQ(9.0, testCounter.getCount(0)); |
| 102 | } |
| 103 | |
| 104 | } // namespace battery |
| 105 | } // namespace android |