blob: 7ae303b3b074536e6595df8eb70e07d778cb2f21 [file] [log] [blame]
Patrick Venture8b4478c2020-10-06 08:30:27 -07001#include "dbus/dbusutil.hpp"
2
Ed Tanousf8b6e552025-06-27 13:27:50 -07003#include <cstdint>
4#include <map>
Patrick Venture8b4478c2020-10-06 08:30:27 -07005#include <string>
6#include <tuple>
Patrick Venture1a7c49f2020-10-06 15:49:27 -07007#include <unordered_map>
8#include <utility>
9#include <vector>
Patrick Venture8b4478c2020-10-06 08:30:27 -070010
11#include <gmock/gmock.h>
12#include <gtest/gtest.h>
13
14namespace pid_control
15{
16namespace
17{
18
Patrick Ventureb8cfc642020-10-07 08:30:22 -070019using ::testing::ContainerEq;
20using ::testing::Eq;
Patrick Venture8b4478c2020-10-06 08:30:27 -070021using ::testing::StrEq;
Patrick Venture1a7c49f2020-10-06 15:49:27 -070022using ::testing::UnorderedElementsAreArray;
Patrick Venture8b4478c2020-10-06 08:30:27 -070023
24class GetSensorPathTest :
25 public ::testing::TestWithParam<
26 std::tuple<std::string, std::string, std::string>>
27{};
28
29TEST_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.Wua1ae4fa2022-10-28 17:38:35 +080037INSTANTIATE_TEST_SUITE_P(
Patrick Venture8b4478c2020-10-06 08:30:27 -070038 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 Lehan23e22b92022-11-12 22:37:58 -080045 "/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 Venture8b4478c2020-10-06 08:30:27 -070050
Patrick Venture1a7c49f2020-10-06 15:49:27 -070051class FindSensorsTest : public ::testing::Test
52{
53 protected:
54 const std::unordered_map<std::string, std::string> sensors = {
Jae Hyun Yoo7a8d5a12020-10-21 17:38:56 -070055 {"/abcd/_a", "b"}, {"_a", "c"}, {"/abcd_a", "d"},
56 {"/_a_a", "e"}, {"one/slash", "one"}, {"other_/slash", "other"},
Patrick Venture1a7c49f2020-10-06 15:49:27 -070057 };
58
59 std::vector<std::pair<std::string, std::string>> results;
60};
61
62TEST_F(FindSensorsTest, NoMatches)
63{
64 const std::string target = "abcd";
65
66 EXPECT_FALSE(findSensors(sensors, target, results));
67}
68
69TEST_F(FindSensorsTest, OneMatches)
70{
Jae Hyun Yoo7a8d5a12020-10-21 17:38:56 -070071 const std::string target = "_a";
Patrick Venture1a7c49f2020-10-06 15:49:27 -070072
73 EXPECT_TRUE(findSensors(sensors, target, results));
74
75 std::vector<std::pair<std::string, std::string>> expected_results = {
Jae Hyun Yoo7a8d5a12020-10-21 17:38:56 -070076 {"/abcd/_a", "b"},
Patrick Venture1a7c49f2020-10-06 15:49:27 -070077 };
78
79 EXPECT_THAT(results, UnorderedElementsAreArray(expected_results));
80}
81
82TEST_F(FindSensorsTest, MultipleMatches)
83{
Jae Hyun Yoo7a8d5a12020-10-21 17:38:56 -070084 const std::string target = "slash";
Patrick Venture1a7c49f2020-10-06 15:49:27 -070085 EXPECT_TRUE(findSensors(sensors, target, results));
86
87 std::vector<std::pair<std::string, std::string>> expected_results = {
Jae Hyun Yoo7a8d5a12020-10-21 17:38:56 -070088 {"one/slash", "one"},
89 {"other_/slash", "other"},
Patrick Venture1a7c49f2020-10-06 15:49:27 -070090 };
91
92 EXPECT_THAT(results, UnorderedElementsAreArray(expected_results));
93}
94
Patrick Ventureb8cfc642020-10-07 08:30:22 -070095TEST(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
108TEST(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
120TEST(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
133TEST(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
145TEST(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
157TEST(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
171TEST(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
185TEST(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 Venture8b4478c2020-10-06 08:30:27 -0700201} // namespace
202} // namespace pid_control