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) { |
| 27 | LongArrayMultiStateCounter testCounter(2, 0, std::vector<uint64_t>(4), 1000); |
| 28 | testCounter.setState(1, 2000); |
| 29 | testCounter.updateValue(std::vector<uint64_t>({100, 200, 300, 400}), 3000); |
| 30 | |
| 31 | // Time was split in half between the two states, so the counts will be split 50:50 too |
| 32 | EXPECT_EQ(std::vector<uint64_t>({50, 100, 150, 200}), testCounter.getCount(0)); |
| 33 | EXPECT_EQ(std::vector<uint64_t>({50, 100, 150, 200}), testCounter.getCount(1)); |
| 34 | } |
| 35 | |
| 36 | TEST_F(LongArrayMultiStateCounterTest, accumulation) { |
| 37 | LongArrayMultiStateCounter testCounter(2, 0, std::vector<uint64_t>(4), 1000); |
| 38 | testCounter.setState(1, 2000); |
| 39 | testCounter.updateValue(std::vector<uint64_t>({100, 200, 300, 400}), 3000); |
| 40 | testCounter.setState(0, 4000); |
| 41 | testCounter.updateValue(std::vector<uint64_t>({200, 300, 400, 500}), 8000); |
| 42 | |
| 43 | // The first delta is split 50:50: |
| 44 | // 0: {50, 100, 150, 200} |
| 45 | // 1: {50, 100, 150, 200} |
| 46 | // The second delta is split 4:1 |
| 47 | // 0: {80, 80, 80, 80} |
| 48 | // 1: {20, 20, 20, 20} |
| 49 | EXPECT_EQ(std::vector<uint64_t>({130, 180, 230, 280}), testCounter.getCount(0)); |
| 50 | EXPECT_EQ(std::vector<uint64_t>({70, 120, 170, 220}), testCounter.getCount(1)); |
| 51 | } |
| 52 | |
| 53 | } // namespace battery |
| 54 | } // namespace android |