blob: 09d1b5c076d0845ba6cf6a71235a833974c66422 [file] [log] [blame]
Patrick Venture8b4478c2020-10-06 08:30:27 -07001#include "dbus/dbusutil.hpp"
2
3#include <string>
4#include <tuple>
Patrick Venture1a7c49f2020-10-06 15:49:27 -07005#include <unordered_map>
6#include <utility>
7#include <vector>
Patrick Venture8b4478c2020-10-06 08:30:27 -07008
9#include <gmock/gmock.h>
10#include <gtest/gtest.h>
11
12namespace pid_control
13{
14namespace
15{
16
Patrick Ventureb8cfc642020-10-07 08:30:22 -070017using ::testing::ContainerEq;
18using ::testing::Eq;
Patrick Venture8b4478c2020-10-06 08:30:27 -070019using ::testing::StrEq;
Patrick Venture1a7c49f2020-10-06 15:49:27 -070020using ::testing::UnorderedElementsAreArray;
Patrick Venture8b4478c2020-10-06 08:30:27 -070021
22class GetSensorPathTest :
23 public ::testing::TestWithParam<
24 std::tuple<std::string, std::string, std::string>>
25{};
26
27TEST_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
Harvey.Wua1ae4fa2022-10-28 17:38:35 +080035INSTANTIATE_TEST_SUITE_P(
Patrick Venture8b4478c2020-10-06 08:30:27 -070036 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",
Josh Lehan23e22b92022-11-12 22:37:58 -080043 "/xyz/openbmc_project/sensors/temperature/123"),
44 std::make_tuple("power", "9000",
45 "/xyz/openbmc_project/sensors/power/9000"),
46 std::make_tuple("powersum", "total",
47 "/xyz/openbmc_project/sensors/power/total")));
Patrick Venture8b4478c2020-10-06 08:30:27 -070048
Patrick Venture1a7c49f2020-10-06 15:49:27 -070049class FindSensorsTest : public ::testing::Test
50{
51 protected:
52 const std::unordered_map<std::string, std::string> sensors = {
Jae Hyun Yoo7a8d5a12020-10-21 17:38:56 -070053 {"/abcd/_a", "b"}, {"_a", "c"}, {"/abcd_a", "d"},
54 {"/_a_a", "e"}, {"one/slash", "one"}, {"other_/slash", "other"},
Patrick Venture1a7c49f2020-10-06 15:49:27 -070055 };
56
57 std::vector<std::pair<std::string, std::string>> results;
58};
59
60TEST_F(FindSensorsTest, NoMatches)
61{
62 const std::string target = "abcd";
63
64 EXPECT_FALSE(findSensors(sensors, target, results));
65}
66
67TEST_F(FindSensorsTest, OneMatches)
68{
Jae Hyun Yoo7a8d5a12020-10-21 17:38:56 -070069 const std::string target = "_a";
Patrick Venture1a7c49f2020-10-06 15:49:27 -070070
71 EXPECT_TRUE(findSensors(sensors, target, results));
72
73 std::vector<std::pair<std::string, std::string>> expected_results = {
Jae Hyun Yoo7a8d5a12020-10-21 17:38:56 -070074 {"/abcd/_a", "b"},
Patrick Venture1a7c49f2020-10-06 15:49:27 -070075 };
76
77 EXPECT_THAT(results, UnorderedElementsAreArray(expected_results));
78}
79
80TEST_F(FindSensorsTest, MultipleMatches)
81{
Jae Hyun Yoo7a8d5a12020-10-21 17:38:56 -070082 const std::string target = "slash";
Patrick Venture1a7c49f2020-10-06 15:49:27 -070083 EXPECT_TRUE(findSensors(sensors, target, results));
84
85 std::vector<std::pair<std::string, std::string>> expected_results = {
Jae Hyun Yoo7a8d5a12020-10-21 17:38:56 -070086 {"one/slash", "one"},
87 {"other_/slash", "other"},
Patrick Venture1a7c49f2020-10-06 15:49:27 -070088 };
89
90 EXPECT_THAT(results, UnorderedElementsAreArray(expected_results));
91}
92
Patrick Ventureb8cfc642020-10-07 08:30:22 -070093TEST(GetZoneIndexTest, ZoneAlreadyAssigned)
94{
95 std::map<std::string, int64_t> zones = {
96 {"a", 0},
97 };
98 const std::map<std::string, int64_t> expected_zones = {
99 {"a", 0},
100 };
101
102 EXPECT_THAT(getZoneIndex("a", zones), Eq(0));
103 EXPECT_THAT(zones, ContainerEq(expected_zones));
104}
105
106TEST(GetZoneIndexTest, ZoneNotYetAssignedZeroBased)
107{
108 /* This calls into setZoneIndex, but is a case hit by getZoneIndex. */
109 std::map<std::string, int64_t> zones;
110 const std::map<std::string, int64_t> expected_zones = {
111 {"a", 0},
112 };
113
114 EXPECT_THAT(getZoneIndex("a", zones), Eq(0));
115 EXPECT_THAT(zones, ContainerEq(expected_zones));
116}
117
118TEST(SetZoneIndexTest, ZoneAlreadyAssigned)
119{
120 std::map<std::string, int64_t> zones = {
121 {"a", 0},
122 };
123 const std::map<std::string, int64_t> expected_zones = {
124 {"a", 0},
125 };
126
127 EXPECT_THAT(setZoneIndex("a", zones, 0), Eq(0));
128 EXPECT_THAT(zones, ContainerEq(expected_zones));
129}
130
131TEST(SetZoneIndexTest, ZoneNotYetAssignedEmptyListZeroBased)
132{
133 constexpr int64_t index = 0;
134 std::map<std::string, int64_t> zones;
135 const std::map<std::string, int64_t> expected_zones = {
136 {"a", index},
137 };
138
139 EXPECT_THAT(setZoneIndex("a", zones, index), Eq(index));
140 EXPECT_THAT(zones, ContainerEq(expected_zones));
141}
142
143TEST(SetZoneIndexTest, ZoneNotYetAssignedEmptyListNonZeroBased)
144{
145 constexpr int64_t index = 5;
146 std::map<std::string, int64_t> zones;
147 const std::map<std::string, int64_t> expected_zones = {
148 {"a", index},
149 };
150
151 EXPECT_THAT(setZoneIndex("a", zones, index), Eq(index));
152 EXPECT_THAT(zones, ContainerEq(expected_zones));
153}
154
155TEST(SetZoneIndexTest, ZoneListNotEmptyAssignsNextIndexZeroBased)
156{
157 std::map<std::string, int64_t> zones = {
158 {"a", 0},
159 };
160 const std::map<std::string, int64_t> expected_zones = {
161 {"a", 0},
162 {"b", 1},
163 };
164
165 EXPECT_THAT(setZoneIndex("b", zones, 0), Eq(1));
166 EXPECT_THAT(zones, ContainerEq(expected_zones));
167}
168
169TEST(SetZoneIndexTest, ZoneListNotEmptyAssignsNextIndexNonZeroBased)
170{
171 std::map<std::string, int64_t> zones = {
172 {"a", 0},
173 };
174 const std::map<std::string, int64_t> expected_zones = {
175 {"a", 0},
176 {"b", 5},
177 };
178
179 EXPECT_THAT(setZoneIndex("b", zones, 5), Eq(5));
180 EXPECT_THAT(zones, ContainerEq(expected_zones));
181}
182
183TEST(SetZoneIndexTest, ZoneListNotEmptyAssignsIntoGap)
184{
185 std::map<std::string, int64_t> zones = {
186 {"a", 0},
187 {"b", 5},
188 };
189 const std::map<std::string, int64_t> expected_zones = {
190 {"a", 0},
191 {"c", 1},
192 {"b", 5},
193 };
194
195 EXPECT_THAT(setZoneIndex("c", zones, 0), Eq(1));
196 EXPECT_THAT(zones, ContainerEq(expected_zones));
197}
198
Patrick Venture8b4478c2020-10-06 08:30:27 -0700199} // namespace
200} // namespace pid_control