1 /*
2 * Copyright 2018 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 // TODO(b/129481165): remove the #pragma below and fix conversion issues
18 #pragma clang diagnostic push
19 #pragma clang diagnostic ignored "-Wconversion"
20
21 #undef LOG_TAG
22 #define LOG_TAG "SchedulerUnittests"
23
24 #include <gmock/gmock.h>
25 #include <gtest/gtest.h>
26
27 #include <array>
28
29 #include "Scheduler/SchedulerUtils.h"
30
31 namespace android {
32 namespace scheduler {
33
34 class SchedulerUtilsTest : public testing::Test {
35 public:
36 SchedulerUtilsTest() = default;
37 ~SchedulerUtilsTest() override = default;
38 };
39
40 namespace {
TEST_F(SchedulerUtilsTest,calculate_mean)41 TEST_F(SchedulerUtilsTest, calculate_mean) {
42 std::array<int64_t, 30> testArray{};
43 // Calling the function on empty array returns 0.
44 EXPECT_EQ(0, calculate_mean(testArray));
45
46 testArray[0] = 33;
47 EXPECT_EQ(1, calculate_mean(testArray));
48 testArray[1] = 33;
49 testArray[2] = 33;
50 EXPECT_EQ(3, calculate_mean(testArray));
51 testArray[3] = 42;
52 EXPECT_EQ(4, calculate_mean(testArray));
53 testArray[4] = 33;
54 EXPECT_EQ(5, calculate_mean(testArray));
55 testArray[5] = 42;
56 EXPECT_EQ(7, calculate_mean(testArray));
57 for (int i = 6; i < 30; i++) {
58 testArray[i] = 33;
59 }
60 EXPECT_EQ(33, calculate_mean(testArray));
61 }
62
TEST_F(SchedulerUtilsTest,calculate_median)63 TEST_F(SchedulerUtilsTest, calculate_median) {
64 std::vector<int64_t> testVector;
65 // Calling the function on empty vector returns 0.
66 EXPECT_EQ(0, calculate_median(&testVector));
67
68 testVector.push_back(33);
69 EXPECT_EQ(33, calculate_median(&testVector));
70 testVector.push_back(33);
71 testVector.push_back(33);
72 EXPECT_EQ(33, calculate_median(&testVector));
73 testVector.push_back(42);
74 EXPECT_EQ(33, calculate_median(&testVector));
75 testVector.push_back(33);
76 EXPECT_EQ(33, calculate_median(&testVector));
77 testVector.push_back(42);
78 EXPECT_EQ(33, calculate_median(&testVector));
79 testVector.push_back(42);
80 EXPECT_EQ(33, calculate_median(&testVector));
81 testVector.push_back(42);
82 EXPECT_EQ(42, calculate_median(&testVector));
83 testVector.push_back(60);
84 EXPECT_EQ(42, calculate_median(&testVector));
85 testVector.push_back(60);
86 EXPECT_EQ(42, calculate_median(&testVector));
87 testVector.push_back(33);
88 EXPECT_EQ(42, calculate_median(&testVector));
89 testVector.push_back(33);
90 EXPECT_EQ(42, calculate_median(&testVector));
91 testVector.push_back(33);
92 EXPECT_EQ(33, calculate_median(&testVector));
93 }
94
TEST_F(SchedulerUtilsTest,calculate_mode)95 TEST_F(SchedulerUtilsTest, calculate_mode) {
96 std::vector<int64_t> testVector;
97 // Calling the function on empty vector returns 0.
98 EXPECT_EQ(0, calculate_mode(testVector));
99
100 testVector.push_back(33);
101 EXPECT_EQ(33, calculate_mode(testVector));
102 testVector.push_back(33);
103 testVector.push_back(33);
104 EXPECT_EQ(33, calculate_mode(testVector));
105 testVector.push_back(42);
106 EXPECT_EQ(33, calculate_mode(testVector));
107 testVector.push_back(33);
108 EXPECT_EQ(33, calculate_mode(testVector));
109 testVector.push_back(42);
110 EXPECT_EQ(33, calculate_mode(testVector));
111 testVector.push_back(42);
112 EXPECT_EQ(33, calculate_mode(testVector));
113 testVector.push_back(42);
114 EXPECT_EQ(33, calculate_mode(testVector));
115 testVector.push_back(60);
116 EXPECT_EQ(33, calculate_mode(testVector));
117 testVector.push_back(60);
118 EXPECT_EQ(33, calculate_mode(testVector));
119 testVector.push_back(33);
120 // 5 occurences of 33.
121 EXPECT_EQ(33, calculate_mode(testVector));
122 testVector.push_back(42);
123 // 5 occurences of 33, 5 occurences of 42. We choose the first one.
124 EXPECT_EQ(33, calculate_mode(testVector));
125 testVector.push_back(42);
126 // 5 occurences of 33, 6 occurences of 42.
127 EXPECT_EQ(42, calculate_mode(testVector));
128 testVector.push_back(42);
129 // 5 occurences of 33, 7 occurences of 42.
130 EXPECT_EQ(42, calculate_mode(testVector));
131 }
132
133 } // namespace
134 } // namespace scheduler
135 } // namespace android
136 // TODO(b/129481165): remove the #pragma below and fix conversion issues
137 #pragma clang diagnostic pop // ignored "-Wconversion"
138