1 #include "dbus/dbusutil.hpp"
2
3 #include <cstdint>
4 #include <map>
5 #include <string>
6 #include <tuple>
7 #include <unordered_map>
8 #include <utility>
9 #include <vector>
10
11 #include <gmock/gmock.h>
12 #include <gtest/gtest.h>
13
14 namespace pid_control
15 {
16 namespace
17 {
18
19 using ::testing::ContainerEq;
20 using ::testing::Eq;
21 using ::testing::StrEq;
22 using ::testing::UnorderedElementsAreArray;
23
24 class GetSensorPathTest :
25 public ::testing::TestWithParam<
26 std::tuple<std::string, std::string, std::string>>
27 {};
28
TEST_P(GetSensorPathTest,ReturnsExpectedValue)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
37 INSTANTIATE_TEST_SUITE_P(
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",
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")));
50
51 class FindSensorsTest : public ::testing::Test
52 {
53 protected:
54 const std::unordered_map<std::string, std::string> sensors = {
55 {"/abcd/_a", "b"}, {"_a", "c"}, {"/abcd_a", "d"},
56 {"/_a_a", "e"}, {"one/slash", "one"}, {"other_/slash", "other"},
57 };
58
59 std::vector<std::pair<std::string, std::string>> results;
60 };
61
TEST_F(FindSensorsTest,NoMatches)62 TEST_F(FindSensorsTest, NoMatches)
63 {
64 const std::string target = "abcd";
65
66 EXPECT_FALSE(findSensors(sensors, target, results));
67 }
68
TEST_F(FindSensorsTest,OneMatches)69 TEST_F(FindSensorsTest, OneMatches)
70 {
71 const std::string target = "_a";
72
73 EXPECT_TRUE(findSensors(sensors, target, results));
74
75 std::vector<std::pair<std::string, std::string>> expected_results = {
76 {"/abcd/_a", "b"},
77 };
78
79 EXPECT_THAT(results, UnorderedElementsAreArray(expected_results));
80 }
81
TEST_F(FindSensorsTest,MultipleMatches)82 TEST_F(FindSensorsTest, MultipleMatches)
83 {
84 const std::string target = "slash";
85 EXPECT_TRUE(findSensors(sensors, target, results));
86
87 std::vector<std::pair<std::string, std::string>> expected_results = {
88 {"one/slash", "one"},
89 {"other_/slash", "other"},
90 };
91
92 EXPECT_THAT(results, UnorderedElementsAreArray(expected_results));
93 }
94
TEST(GetZoneIndexTest,ZoneAlreadyAssigned)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
TEST(GetZoneIndexTest,ZoneNotYetAssignedZeroBased)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
TEST(SetZoneIndexTest,ZoneAlreadyAssigned)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
TEST(SetZoneIndexTest,ZoneNotYetAssignedEmptyListZeroBased)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
TEST(SetZoneIndexTest,ZoneNotYetAssignedEmptyListNonZeroBased)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
TEST(SetZoneIndexTest,ZoneListNotEmptyAssignsNextIndexZeroBased)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
TEST(SetZoneIndexTest,ZoneListNotEmptyAssignsNextIndexNonZeroBased)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
TEST(SetZoneIndexTest,ZoneListNotEmptyAssignsIntoGap)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
201 } // namespace
202 } // namespace pid_control
203