Patrick Venture | 8b4478c | 2020-10-06 08:30:27 -0700 | [diff] [blame] | 1 | #include "dbus/dbusutil.hpp" |
| 2 | |
| 3 | #include <string> |
| 4 | #include <tuple> |
Patrick Venture | 1a7c49f | 2020-10-06 15:49:27 -0700 | [diff] [blame] | 5 | #include <unordered_map> |
| 6 | #include <utility> |
| 7 | #include <vector> |
Patrick Venture | 8b4478c | 2020-10-06 08:30:27 -0700 | [diff] [blame] | 8 | |
| 9 | #include <gmock/gmock.h> |
| 10 | #include <gtest/gtest.h> |
| 11 | |
| 12 | namespace pid_control |
| 13 | { |
| 14 | namespace |
| 15 | { |
| 16 | |
Patrick Venture | b8cfc64 | 2020-10-07 08:30:22 -0700 | [diff] [blame^] | 17 | using ::testing::ContainerEq; |
| 18 | using ::testing::Eq; |
Patrick Venture | 8b4478c | 2020-10-06 08:30:27 -0700 | [diff] [blame] | 19 | using ::testing::StrEq; |
Patrick Venture | 1a7c49f | 2020-10-06 15:49:27 -0700 | [diff] [blame] | 20 | using ::testing::UnorderedElementsAreArray; |
Patrick Venture | 8b4478c | 2020-10-06 08:30:27 -0700 | [diff] [blame] | 21 | |
| 22 | class GetSensorPathTest : |
| 23 | public ::testing::TestWithParam< |
| 24 | std::tuple<std::string, std::string, std::string>> |
| 25 | {}; |
| 26 | |
| 27 | TEST_P(GetSensorPathTest, ReturnsExpectedValue) |
| 28 | { |
| 29 | // type, id, output |
| 30 | const auto& params = GetParam(); |
| 31 | EXPECT_THAT(getSensorPath(std::get<0>(params), std::get<1>(params)), |
| 32 | StrEq(std::get<2>(params))); |
| 33 | } |
| 34 | |
| 35 | INSTANTIATE_TEST_CASE_P( |
| 36 | GetSensorPathTests, GetSensorPathTest, |
| 37 | ::testing::Values( |
| 38 | std::make_tuple("fan", "0", "/xyz/openbmc_project/sensors/fan_tach/0"), |
| 39 | std::make_tuple("as", "we", "/xyz/openbmc_project/sensors/unknown/we"), |
| 40 | std::make_tuple("margin", "9", |
| 41 | "/xyz/openbmc_project/sensors/temperature/9"), |
| 42 | std::make_tuple("temp", "123", |
| 43 | "/xyz/openbmc_project/sensors/temperature/123"))); |
| 44 | |
Patrick Venture | 1a7c49f | 2020-10-06 15:49:27 -0700 | [diff] [blame] | 45 | class FindSensorsTest : public ::testing::Test |
| 46 | { |
| 47 | protected: |
| 48 | const std::unordered_map<std::string, std::string> sensors = { |
| 49 | {"path_a", "b"}, |
| 50 | {"apple", "juice"}, |
| 51 | {"other_le", "thing"}, |
| 52 | }; |
| 53 | |
| 54 | std::vector<std::pair<std::string, std::string>> results; |
| 55 | }; |
| 56 | |
| 57 | TEST_F(FindSensorsTest, NoMatches) |
| 58 | { |
| 59 | const std::string target = "abcd"; |
| 60 | |
| 61 | EXPECT_FALSE(findSensors(sensors, target, results)); |
| 62 | } |
| 63 | |
| 64 | TEST_F(FindSensorsTest, OneMatches) |
| 65 | { |
| 66 | const std::string target = "a"; |
| 67 | |
| 68 | EXPECT_TRUE(findSensors(sensors, target, results)); |
| 69 | |
| 70 | std::vector<std::pair<std::string, std::string>> expected_results = { |
| 71 | {"path_a", "b"}, |
| 72 | }; |
| 73 | |
| 74 | EXPECT_THAT(results, UnorderedElementsAreArray(expected_results)); |
| 75 | } |
| 76 | |
| 77 | TEST_F(FindSensorsTest, MultipleMatches) |
| 78 | { |
| 79 | const std::string target = "le"; |
| 80 | EXPECT_TRUE(findSensors(sensors, target, results)); |
| 81 | |
| 82 | std::vector<std::pair<std::string, std::string>> expected_results = { |
| 83 | {"apple", "juice"}, |
| 84 | {"other_le", "thing"}, |
| 85 | }; |
| 86 | |
| 87 | EXPECT_THAT(results, UnorderedElementsAreArray(expected_results)); |
| 88 | } |
| 89 | |
Patrick Venture | b8cfc64 | 2020-10-07 08:30:22 -0700 | [diff] [blame^] | 90 | TEST(GetZoneIndexTest, ZoneAlreadyAssigned) |
| 91 | { |
| 92 | std::map<std::string, int64_t> zones = { |
| 93 | {"a", 0}, |
| 94 | }; |
| 95 | const std::map<std::string, int64_t> expected_zones = { |
| 96 | {"a", 0}, |
| 97 | }; |
| 98 | |
| 99 | EXPECT_THAT(getZoneIndex("a", zones), Eq(0)); |
| 100 | EXPECT_THAT(zones, ContainerEq(expected_zones)); |
| 101 | } |
| 102 | |
| 103 | TEST(GetZoneIndexTest, ZoneNotYetAssignedZeroBased) |
| 104 | { |
| 105 | /* This calls into setZoneIndex, but is a case hit by getZoneIndex. */ |
| 106 | std::map<std::string, int64_t> zones; |
| 107 | const std::map<std::string, int64_t> expected_zones = { |
| 108 | {"a", 0}, |
| 109 | }; |
| 110 | |
| 111 | EXPECT_THAT(getZoneIndex("a", zones), Eq(0)); |
| 112 | EXPECT_THAT(zones, ContainerEq(expected_zones)); |
| 113 | } |
| 114 | |
| 115 | TEST(SetZoneIndexTest, ZoneAlreadyAssigned) |
| 116 | { |
| 117 | std::map<std::string, int64_t> zones = { |
| 118 | {"a", 0}, |
| 119 | }; |
| 120 | const std::map<std::string, int64_t> expected_zones = { |
| 121 | {"a", 0}, |
| 122 | }; |
| 123 | |
| 124 | EXPECT_THAT(setZoneIndex("a", zones, 0), Eq(0)); |
| 125 | EXPECT_THAT(zones, ContainerEq(expected_zones)); |
| 126 | } |
| 127 | |
| 128 | TEST(SetZoneIndexTest, ZoneNotYetAssignedEmptyListZeroBased) |
| 129 | { |
| 130 | constexpr int64_t index = 0; |
| 131 | std::map<std::string, int64_t> zones; |
| 132 | const std::map<std::string, int64_t> expected_zones = { |
| 133 | {"a", index}, |
| 134 | }; |
| 135 | |
| 136 | EXPECT_THAT(setZoneIndex("a", zones, index), Eq(index)); |
| 137 | EXPECT_THAT(zones, ContainerEq(expected_zones)); |
| 138 | } |
| 139 | |
| 140 | TEST(SetZoneIndexTest, ZoneNotYetAssignedEmptyListNonZeroBased) |
| 141 | { |
| 142 | constexpr int64_t index = 5; |
| 143 | std::map<std::string, int64_t> zones; |
| 144 | const std::map<std::string, int64_t> expected_zones = { |
| 145 | {"a", index}, |
| 146 | }; |
| 147 | |
| 148 | EXPECT_THAT(setZoneIndex("a", zones, index), Eq(index)); |
| 149 | EXPECT_THAT(zones, ContainerEq(expected_zones)); |
| 150 | } |
| 151 | |
| 152 | TEST(SetZoneIndexTest, ZoneListNotEmptyAssignsNextIndexZeroBased) |
| 153 | { |
| 154 | std::map<std::string, int64_t> zones = { |
| 155 | {"a", 0}, |
| 156 | }; |
| 157 | const std::map<std::string, int64_t> expected_zones = { |
| 158 | {"a", 0}, |
| 159 | {"b", 1}, |
| 160 | }; |
| 161 | |
| 162 | EXPECT_THAT(setZoneIndex("b", zones, 0), Eq(1)); |
| 163 | EXPECT_THAT(zones, ContainerEq(expected_zones)); |
| 164 | } |
| 165 | |
| 166 | TEST(SetZoneIndexTest, ZoneListNotEmptyAssignsNextIndexNonZeroBased) |
| 167 | { |
| 168 | std::map<std::string, int64_t> zones = { |
| 169 | {"a", 0}, |
| 170 | }; |
| 171 | const std::map<std::string, int64_t> expected_zones = { |
| 172 | {"a", 0}, |
| 173 | {"b", 5}, |
| 174 | }; |
| 175 | |
| 176 | EXPECT_THAT(setZoneIndex("b", zones, 5), Eq(5)); |
| 177 | EXPECT_THAT(zones, ContainerEq(expected_zones)); |
| 178 | } |
| 179 | |
| 180 | TEST(SetZoneIndexTest, ZoneListNotEmptyAssignsIntoGap) |
| 181 | { |
| 182 | std::map<std::string, int64_t> zones = { |
| 183 | {"a", 0}, |
| 184 | {"b", 5}, |
| 185 | }; |
| 186 | const std::map<std::string, int64_t> expected_zones = { |
| 187 | {"a", 0}, |
| 188 | {"c", 1}, |
| 189 | {"b", 5}, |
| 190 | }; |
| 191 | |
| 192 | EXPECT_THAT(setZoneIndex("c", zones, 0), Eq(1)); |
| 193 | EXPECT_THAT(zones, ContainerEq(expected_zones)); |
| 194 | } |
| 195 | |
Patrick Venture | 8b4478c | 2020-10-06 08:30:27 -0700 | [diff] [blame] | 196 | } // namespace |
| 197 | } // namespace pid_control |