blob: 1c74e3fd143a9338591b9e95ae3281220a65d0bd [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 "LongArrayMultiStateCounter.h"
20
21namespace android {
22namespace battery {
23
24class LongArrayMultiStateCounterTest : public testing::Test {};
25
26TEST_F(LongArrayMultiStateCounterTest, stateChange) {
Dmitri Plotnikov0a6dc402024-01-19 14:33:20 -080027 LongArrayMultiStateCounter testCounter(2, Uint64Array(4));
28 testCounter.updateValue(Uint64ArrayRW({0, 0, 0, 0}), 1000);
Dmitri Plotnikov12aaf8e2021-09-03 19:07:23 -070029 testCounter.setState(0, 1000);
Dmitri Plotnikov5effd852021-08-11 14:55:58 -070030 testCounter.setState(1, 2000);
Dmitri Plotnikov0a6dc402024-01-19 14:33:20 -080031 testCounter.updateValue(Uint64ArrayRW({100, 200, 300, 400}), 3000);
Dmitri Plotnikov5effd852021-08-11 14:55:58 -070032
33 // Time was split in half between the two states, so the counts will be split 50:50 too
Dmitri Plotnikov0a6dc402024-01-19 14:33:20 -080034 EXPECT_EQ(Uint64ArrayRW({50, 100, 150, 200}), testCounter.getCount(0));
35 EXPECT_EQ(Uint64ArrayRW({50, 100, 150, 200}), testCounter.getCount(1));
Dmitri Plotnikov5effd852021-08-11 14:55:58 -070036}
37
38TEST_F(LongArrayMultiStateCounterTest, accumulation) {
Dmitri Plotnikov0a6dc402024-01-19 14:33:20 -080039 LongArrayMultiStateCounter testCounter(2, Uint64Array(4));
40 testCounter.updateValue(Uint64ArrayRW({0, 0, 0, 0}), 1000);
Dmitri Plotnikov12aaf8e2021-09-03 19:07:23 -070041 testCounter.setState(0, 1000);
Dmitri Plotnikov5effd852021-08-11 14:55:58 -070042 testCounter.setState(1, 2000);
Dmitri Plotnikov0a6dc402024-01-19 14:33:20 -080043 testCounter.updateValue(Uint64ArrayRW({100, 200, 300, 400}), 3000);
Dmitri Plotnikov5effd852021-08-11 14:55:58 -070044 testCounter.setState(0, 4000);
Dmitri Plotnikov0a6dc402024-01-19 14:33:20 -080045 testCounter.updateValue(Uint64ArrayRW({200, 300, 400, 500}), 8000);
Dmitri Plotnikov5effd852021-08-11 14:55:58 -070046
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}
Dmitri Plotnikov0a6dc402024-01-19 14:33:20 -080053 EXPECT_EQ(Uint64ArrayRW({130, 180, 230, 280}), testCounter.getCount(0));
54 EXPECT_EQ(Uint64ArrayRW({70, 120, 170, 220}), testCounter.getCount(1));
Dmitri Plotnikov5effd852021-08-11 14:55:58 -070055}
56
Dmitri Plotnikov12aaf8e2021-09-03 19:07:23 -070057TEST_F(LongArrayMultiStateCounterTest, toString) {
Dmitri Plotnikov0a6dc402024-01-19 14:33:20 -080058 LongArrayMultiStateCounter testCounter(2, Uint64Array(4));
59 testCounter.updateValue(Uint64ArrayRW({0, 0, 0, 0}), 1000);
Dmitri Plotnikov12aaf8e2021-09-03 19:07:23 -070060 testCounter.setState(0, 1000);
61 testCounter.setState(1, 2000);
Dmitri Plotnikov0a6dc402024-01-19 14:33:20 -080062 testCounter.updateValue(Uint64ArrayRW({100, 200, 300, 400}), 3000);
Dmitri Plotnikov12aaf8e2021-09-03 19:07:23 -070063
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 Plotnikov5effd852021-08-11 14:55:58 -070068} // namespace battery
69} // namespace android