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