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