blob: 76938e969a37a9e35f32624bc80b3df2f24509a5 [file] [log] [blame]
Patrick Venture8b4478c2020-10-06 08:30:27 -07001#include "dbus/dbusutil.hpp"
2
Alexander Hansendc6a0502025-10-28 14:11:08 +01003#include <xyz/openbmc_project/Sensor/Value/common.hpp>
4
Ed Tanousf8b6e552025-06-27 13:27:50 -07005#include <cstdint>
6#include <map>
Patrick Venture8b4478c2020-10-06 08:30:27 -07007#include <string>
8#include <tuple>
Patrick Venture1a7c49f2020-10-06 15:49:27 -07009#include <unordered_map>
10#include <utility>
11#include <vector>
Patrick Venture8b4478c2020-10-06 08:30:27 -070012
13#include <gmock/gmock.h>
14#include <gtest/gtest.h>
15
16namespace pid_control
17{
18namespace
19{
20
Alexander Hansendc6a0502025-10-28 14:11:08 +010021using SensorValue = sdbusplus::common::xyz::openbmc_project::sensor::Value;
22
Patrick Ventureb8cfc642020-10-07 08:30:22 -070023using ::testing::ContainerEq;
24using ::testing::Eq;
Patrick Venture8b4478c2020-10-06 08:30:27 -070025using ::testing::StrEq;
Patrick Venture1a7c49f2020-10-06 15:49:27 -070026using ::testing::UnorderedElementsAreArray;
Patrick Venture8b4478c2020-10-06 08:30:27 -070027
28class GetSensorPathTest :
29 public ::testing::TestWithParam<
30 std::tuple<std::string, std::string, std::string>>
31{};
32
33TEST_P(GetSensorPathTest, ReturnsExpectedValue)
34{
35 // type, id, output
36 const auto& params = GetParam();
37 EXPECT_THAT(getSensorPath(std::get<0>(params), std::get<1>(params)),
38 StrEq(std::get<2>(params)));
39}
40
Harvey.Wua1ae4fa2022-10-28 17:38:35 +080041INSTANTIATE_TEST_SUITE_P(
Patrick Venture8b4478c2020-10-06 08:30:27 -070042 GetSensorPathTests, GetSensorPathTest,
43 ::testing::Values(
Alexander Hansendc6a0502025-10-28 14:11:08 +010044 std::make_tuple("fan", "0",
45 std::format("{}/{}/0",
46 SensorValue::namespace_path::value,
47 SensorValue::namespace_path::fan_tach)),
48 std::make_tuple("as", "we",
49 std::format("{}/unknown/we",
50 SensorValue::namespace_path::value)),
Patrick Venture8b4478c2020-10-06 08:30:27 -070051 std::make_tuple("margin", "9",
Alexander Hansendc6a0502025-10-28 14:11:08 +010052 std::format("{}/{}/9",
53 SensorValue::namespace_path::value,
54 SensorValue::namespace_path::temperature)),
Patrick Venture8b4478c2020-10-06 08:30:27 -070055 std::make_tuple("temp", "123",
Alexander Hansendc6a0502025-10-28 14:11:08 +010056 std::format("{}/{}/123",
57 SensorValue::namespace_path::value,
58 SensorValue::namespace_path::temperature)),
Josh Lehan23e22b92022-11-12 22:37:58 -080059 std::make_tuple("power", "9000",
Alexander Hansendc6a0502025-10-28 14:11:08 +010060 std::format("{}/{}/9000",
61 SensorValue::namespace_path::value,
62 SensorValue::namespace_path::power)),
Josh Lehan23e22b92022-11-12 22:37:58 -080063 std::make_tuple("powersum", "total",
Alexander Hansendc6a0502025-10-28 14:11:08 +010064 std::format("{}/{}/total",
65 SensorValue::namespace_path::value,
66 SensorValue::namespace_path::power))));
Patrick Venture8b4478c2020-10-06 08:30:27 -070067
Patrick Venture1a7c49f2020-10-06 15:49:27 -070068class FindSensorsTest : public ::testing::Test
69{
70 protected:
71 const std::unordered_map<std::string, std::string> sensors = {
Jae Hyun Yoo7a8d5a12020-10-21 17:38:56 -070072 {"/abcd/_a", "b"}, {"_a", "c"}, {"/abcd_a", "d"},
73 {"/_a_a", "e"}, {"one/slash", "one"}, {"other_/slash", "other"},
Patrick Venture1a7c49f2020-10-06 15:49:27 -070074 };
75
76 std::vector<std::pair<std::string, std::string>> results;
77};
78
79TEST_F(FindSensorsTest, NoMatches)
80{
81 const std::string target = "abcd";
82
83 EXPECT_FALSE(findSensors(sensors, target, results));
84}
85
86TEST_F(FindSensorsTest, OneMatches)
87{
Jae Hyun Yoo7a8d5a12020-10-21 17:38:56 -070088 const std::string target = "_a";
Patrick Venture1a7c49f2020-10-06 15:49:27 -070089
90 EXPECT_TRUE(findSensors(sensors, target, results));
91
92 std::vector<std::pair<std::string, std::string>> expected_results = {
Jae Hyun Yoo7a8d5a12020-10-21 17:38:56 -070093 {"/abcd/_a", "b"},
Patrick Venture1a7c49f2020-10-06 15:49:27 -070094 };
95
96 EXPECT_THAT(results, UnorderedElementsAreArray(expected_results));
97}
98
99TEST_F(FindSensorsTest, MultipleMatches)
100{
Jae Hyun Yoo7a8d5a12020-10-21 17:38:56 -0700101 const std::string target = "slash";
Patrick Venture1a7c49f2020-10-06 15:49:27 -0700102 EXPECT_TRUE(findSensors(sensors, target, results));
103
104 std::vector<std::pair<std::string, std::string>> expected_results = {
Jae Hyun Yoo7a8d5a12020-10-21 17:38:56 -0700105 {"one/slash", "one"},
106 {"other_/slash", "other"},
Patrick Venture1a7c49f2020-10-06 15:49:27 -0700107 };
108
109 EXPECT_THAT(results, UnorderedElementsAreArray(expected_results));
110}
111
Patrick Ventureb8cfc642020-10-07 08:30:22 -0700112TEST(GetZoneIndexTest, ZoneAlreadyAssigned)
113{
114 std::map<std::string, int64_t> zones = {
115 {"a", 0},
116 };
117 const std::map<std::string, int64_t> expected_zones = {
118 {"a", 0},
119 };
120
121 EXPECT_THAT(getZoneIndex("a", zones), Eq(0));
122 EXPECT_THAT(zones, ContainerEq(expected_zones));
123}
124
125TEST(GetZoneIndexTest, ZoneNotYetAssignedZeroBased)
126{
127 /* This calls into setZoneIndex, but is a case hit by getZoneIndex. */
128 std::map<std::string, int64_t> zones;
129 const std::map<std::string, int64_t> expected_zones = {
130 {"a", 0},
131 };
132
133 EXPECT_THAT(getZoneIndex("a", zones), Eq(0));
134 EXPECT_THAT(zones, ContainerEq(expected_zones));
135}
136
137TEST(SetZoneIndexTest, ZoneAlreadyAssigned)
138{
139 std::map<std::string, int64_t> zones = {
140 {"a", 0},
141 };
142 const std::map<std::string, int64_t> expected_zones = {
143 {"a", 0},
144 };
145
146 EXPECT_THAT(setZoneIndex("a", zones, 0), Eq(0));
147 EXPECT_THAT(zones, ContainerEq(expected_zones));
148}
149
150TEST(SetZoneIndexTest, ZoneNotYetAssignedEmptyListZeroBased)
151{
152 constexpr int64_t index = 0;
153 std::map<std::string, int64_t> zones;
154 const std::map<std::string, int64_t> expected_zones = {
155 {"a", index},
156 };
157
158 EXPECT_THAT(setZoneIndex("a", zones, index), Eq(index));
159 EXPECT_THAT(zones, ContainerEq(expected_zones));
160}
161
162TEST(SetZoneIndexTest, ZoneNotYetAssignedEmptyListNonZeroBased)
163{
164 constexpr int64_t index = 5;
165 std::map<std::string, int64_t> zones;
166 const std::map<std::string, int64_t> expected_zones = {
167 {"a", index},
168 };
169
170 EXPECT_THAT(setZoneIndex("a", zones, index), Eq(index));
171 EXPECT_THAT(zones, ContainerEq(expected_zones));
172}
173
174TEST(SetZoneIndexTest, ZoneListNotEmptyAssignsNextIndexZeroBased)
175{
176 std::map<std::string, int64_t> zones = {
177 {"a", 0},
178 };
179 const std::map<std::string, int64_t> expected_zones = {
180 {"a", 0},
181 {"b", 1},
182 };
183
184 EXPECT_THAT(setZoneIndex("b", zones, 0), Eq(1));
185 EXPECT_THAT(zones, ContainerEq(expected_zones));
186}
187
188TEST(SetZoneIndexTest, ZoneListNotEmptyAssignsNextIndexNonZeroBased)
189{
190 std::map<std::string, int64_t> zones = {
191 {"a", 0},
192 };
193 const std::map<std::string, int64_t> expected_zones = {
194 {"a", 0},
195 {"b", 5},
196 };
197
198 EXPECT_THAT(setZoneIndex("b", zones, 5), Eq(5));
199 EXPECT_THAT(zones, ContainerEq(expected_zones));
200}
201
202TEST(SetZoneIndexTest, ZoneListNotEmptyAssignsIntoGap)
203{
204 std::map<std::string, int64_t> zones = {
205 {"a", 0},
206 {"b", 5},
207 };
208 const std::map<std::string, int64_t> expected_zones = {
209 {"a", 0},
210 {"c", 1},
211 {"b", 5},
212 };
213
214 EXPECT_THAT(setZoneIndex("c", zones, 0), Eq(1));
215 EXPECT_THAT(zones, ContainerEq(expected_zones));
216}
217
Patrick Venture8b4478c2020-10-06 08:30:27 -0700218} // namespace
219} // namespace pid_control