1 /*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "chre/util/system/stats_container.h"
18 #include "gtest/gtest.h"
19
TEST(StatsContainer,MeanBasicTest)20 TEST(StatsContainer, MeanBasicTest) {
21 chre::StatsContainer<uint8_t> testContainer;
22
23 ASSERT_EQ(testContainer.getMean(), 0);
24
25 testContainer.addValue(10);
26 testContainer.addValue(20);
27 ASSERT_EQ(testContainer.getMean(), 15);
28
29 testContainer.addValue(40);
30 ASSERT_EQ(testContainer.getMean(), (10 + 20 + 40) / 3);
31 }
32
TEST(StatsContainer,UINTMeanOverflowTest)33 TEST(StatsContainer, UINTMeanOverflowTest) {
34 chre::StatsContainer<uint8_t> testContainer;
35
36 testContainer.addValue(200);
37 testContainer.addValue(100);
38 ASSERT_EQ(testContainer.getMean(), 150);
39 }
40
TEST(StatsContainer,AddSmallerValueThanMeanCheck)41 TEST(StatsContainer, AddSmallerValueThanMeanCheck) {
42 chre::StatsContainer<uint16_t> testContainer;
43
44 testContainer.addValue(10);
45 testContainer.addValue(20);
46 testContainer.addValue(30);
47 ASSERT_EQ(testContainer.getMean(), 20);
48
49 testContainer.addValue(4);
50 ASSERT_EQ(testContainer.getMean(), 16);
51 }
52
TEST(StatsContainer,AddBiggerValueThanMeanCheck)53 TEST(StatsContainer, AddBiggerValueThanMeanCheck) {
54 chre::StatsContainer<uint16_t> testContainer;
55
56 testContainer.addValue(10);
57 testContainer.addValue(20);
58 testContainer.addValue(30);
59 ASSERT_EQ(testContainer.getMean(), 20);
60
61 testContainer.addValue(40);
62 ASSERT_EQ(testContainer.getMean(), 25);
63 }
64
TEST(StatsContainer,OverAverageWindowCheck)65 TEST(StatsContainer, OverAverageWindowCheck) {
66 uint64_t maxCount = 3;
67 chre::StatsContainer<uint16_t> testContainer(maxCount);
68
69 testContainer.addValue(10);
70 testContainer.addValue(20);
71 testContainer.addValue(30);
72 ASSERT_EQ(testContainer.getMean(), 20);
73
74 testContainer.addValue(40);
75
76 /**
77 * Only check if StatsContainer still works after have more element than its
78 * averageWindow. Does not check the correctness of the estimated value
79 */
80 ASSERT_GT(testContainer.getMean(), 20);
81 }