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 "LongArrayMultiStateCounter.h" |
| 20 | |
| 21 | namespace android { |
| 22 | namespace battery { |
| 23 | |
| 24 | class LongArrayMultiStateCounterTest : public testing::Test {}; |
| 25 | |
| 26 | TEST_F(LongArrayMultiStateCounterTest, stateChange) { |
Dmitri Plotnikov | 12aaf8e | 2021-09-03 19:07:23 -0700 | [diff] [blame^] | 27 | LongArrayMultiStateCounter testCounter(2, std::vector<uint64_t>(4)); |
| 28 | testCounter.updateValue(std::vector<uint64_t>({0, 0, 0, 0}), 1000); |
| 29 | testCounter.setState(0, 1000); |
Dmitri Plotnikov | 5effd85 | 2021-08-11 14:55:58 -0700 | [diff] [blame] | 30 | testCounter.setState(1, 2000); |
| 31 | testCounter.updateValue(std::vector<uint64_t>({100, 200, 300, 400}), 3000); |
| 32 | |
| 33 | // Time was split in half between the two states, so the counts will be split 50:50 too |
| 34 | EXPECT_EQ(std::vector<uint64_t>({50, 100, 150, 200}), testCounter.getCount(0)); |
| 35 | EXPECT_EQ(std::vector<uint64_t>({50, 100, 150, 200}), testCounter.getCount(1)); |
| 36 | } |
| 37 | |
| 38 | TEST_F(LongArrayMultiStateCounterTest, accumulation) { |
Dmitri Plotnikov | 12aaf8e | 2021-09-03 19:07:23 -0700 | [diff] [blame^] | 39 | LongArrayMultiStateCounter testCounter(2, std::vector<uint64_t>(4)); |
| 40 | testCounter.updateValue(std::vector<uint64_t>({0, 0, 0, 0}), 1000); |
| 41 | testCounter.setState(0, 1000); |
Dmitri Plotnikov | 5effd85 | 2021-08-11 14:55:58 -0700 | [diff] [blame] | 42 | testCounter.setState(1, 2000); |
| 43 | testCounter.updateValue(std::vector<uint64_t>({100, 200, 300, 400}), 3000); |
| 44 | testCounter.setState(0, 4000); |
| 45 | testCounter.updateValue(std::vector<uint64_t>({200, 300, 400, 500}), 8000); |
| 46 | |
| 47 | // The first delta is split 50:50: |
| 48 | // 0: {50, 100, 150, 200} |
| 49 | // 1: {50, 100, 150, 200} |
| 50 | // The second delta is split 4:1 |
| 51 | // 0: {80, 80, 80, 80} |
| 52 | // 1: {20, 20, 20, 20} |
| 53 | EXPECT_EQ(std::vector<uint64_t>({130, 180, 230, 280}), testCounter.getCount(0)); |
| 54 | EXPECT_EQ(std::vector<uint64_t>({70, 120, 170, 220}), testCounter.getCount(1)); |
| 55 | } |
| 56 | |
Dmitri Plotnikov | 12aaf8e | 2021-09-03 19:07:23 -0700 | [diff] [blame^] | 57 | TEST_F(LongArrayMultiStateCounterTest, toString) { |
| 58 | LongArrayMultiStateCounter testCounter(2, std::vector<uint64_t>(4)); |
| 59 | testCounter.updateValue(std::vector<uint64_t>({0, 0, 0, 0}), 1000); |
| 60 | testCounter.setState(0, 1000); |
| 61 | testCounter.setState(1, 2000); |
| 62 | testCounter.updateValue(std::vector<uint64_t>({100, 200, 300, 400}), 3000); |
| 63 | |
| 64 | EXPECT_STREQ("[0: {50, 100, 150, 200}, 1: {50, 100, 150, 200}] updated: 3000 currentState: 1", |
| 65 | testCounter.toString().c_str()); |
| 66 | } |
| 67 | |
Dmitri Plotnikov | 5effd85 | 2021-08-11 14:55:58 -0700 | [diff] [blame] | 68 | } // namespace battery |
| 69 | } // namespace android |