Debian Codegraph @

tests/src/test_amdfanfixed.cpp

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 // Copyright 2019 Juan Palacios <[email protected]>
3
4 #include <catch2/catch_all.hpp>
5 #include <catch2/trompeloeil.hpp>
6
7 #include "common/commandqueuestub.h"
8 #include "common/uintdatasourcestub.h"
9 #include "core/components/controls/amd/fan/fixed/fanfixed.h"
10
11 extern template struct trompeloeil::reporter<trompeloeil::specialized>;
12
13 namespace Tests::AMD::FanFixed {
14
15 class FanFixedTestAdapter : public ::AMD::FanFixed
16 {
17 public:
18 using ::AMD::FanFixed::FanFixed;
19
20 using ::AMD::FanFixed::cleanControl;
21 using ::AMD::FanFixed::exportControl;
22 using ::AMD::FanFixed::fanStartValue;
23 using ::AMD::FanFixed::fanStop;
24 using ::AMD::FanFixed::importControl;
25 using ::AMD::FanFixed::syncControl;
26 using ::AMD::FanFixed::value;
27 };
28
29 class FanFixedImporterStub : public ::AMD::FanFixed::Importer
30 {
31 public:
32 FanFixedImporterStub()
33 : value_(0)
34 , fanStartValue_(0)
35 , fanStop_(false)
36 {
37 }
38
39 std::optional<std::reference_wrapper<Importable::Importer>>
40 provideImporter(Item const &) override
41 {
42 return *this;
43 }
44
45 bool provideActive() const override
46 {
47 return false;
48 }
49
50 units::concentration::percent_t provideFanFixedValue() const override
51 {
52 return value_;
53 }
54
55 bool provideFanFixedFanStop() const override
56 {
57 return fanStop_;
58 }
59
60 units::concentration::percent_t provideFanFixedFanStartValue() const override
61 {
62 return fanStartValue_;
63 }
64
65 void value(units::concentration::percent_t value)
66 {
67 value_ = value;
68 }
69
70 void fanStop(bool enabled)
71 {
72 fanStop_ = enabled;
73 }
74
75 void fanStartValue(units::concentration::percent_t value)
76 {
77 fanStartValue_ = value;
78 }
79
80 private:
81 units::concentration::percent_t value_;
82 units::concentration::percent_t fanStartValue_;
83 bool fanStop_;
84 };
85
86 class FanFixedExporterMock : public ::AMD::FanFixed::Exporter
87 {
88 public:
89 MAKE_MOCK1(takeFanFixedValue, void(units::concentration::percent_t), override);
90 MAKE_MOCK1(takeFanFixedFanStop, void(bool), override);
91 MAKE_MOCK1(takeFanFixedFanStartValue, void(units::concentration::percent_t),
92 override);
93 MAKE_MOCK1(takeActive, void(bool), override);
94 MAKE_MOCK1(
95 provideExporter,
96 std::optional<std::reference_wrapper<Exportable::Exporter>>(Item const &),
97 override);
98 };
99
100 TEST_CASE("AMD FanFixed tests", "[GPU][AMD][Fan][FanFixed]")
101 {
102 CommandQueueStub ctlCmds;
103
104 SECTION("Has FanFixed ID")
105 {
106 FanFixedTestAdapter ts(std::make_unique<UIntDataSourceStub>(),
107 std::make_unique<UIntDataSourceStub>());
108
109 REQUIRE(ts.ID() == ::AMD::FanFixed::ItemID);
110 }
111
112 SECTION("Is not active by default")
113 {
114 FanFixedTestAdapter ts(std::make_unique<UIntDataSourceStub>(),
115 std::make_unique<UIntDataSourceStub>());
116
117 REQUIRE_FALSE(ts.active());
118 }
119
120 SECTION("Has value == 64 (25%) by default")
121 {
122 FanFixedTestAdapter ts(std::make_unique<UIntDataSourceStub>(),
123 std::make_unique<UIntDataSourceStub>());
124
125 REQUIRE(ts.value() == 64);
126 }
127
128 SECTION("Clamp its value in [0, 255] range")
129 {
130 FanFixedTestAdapter ts(std::make_unique<UIntDataSourceStub>(),
131 std::make_unique<UIntDataSourceStub>());
132
133 ts.value(300);
134 REQUIRE(ts.value() == 255);
135 }
136
137 SECTION("Has fan stop disabled by default")
138 {
139 FanFixedTestAdapter ts(std::make_unique<UIntDataSourceStub>(),
140 std::make_unique<UIntDataSourceStub>());
141
142 REQUIRE_FALSE(ts.fanStop());
143 }
144
145 SECTION("Has fan start value == 54 (21%) by default")
146 {
147 FanFixedTestAdapter ts(std::make_unique<UIntDataSourceStub>(),
148 std::make_unique<UIntDataSourceStub>());
149
150 REQUIRE(ts.fanStartValue() == 54);
151 }
152
153 SECTION("Clamp its fan start value in [0, 255] range")
154 {
155 FanFixedTestAdapter ts(std::make_unique<UIntDataSourceStub>(),
156 std::make_unique<UIntDataSourceStub>());
157
158 ts.fanStartValue(300);
159 REQUIRE(ts.fanStartValue() == 255);
160 }
161
162 SECTION("Does not generate pre-init control commands")
163 {
164 FanFixedTestAdapter ts(std::make_unique<UIntDataSourceStub>(),
165 std::make_unique<UIntDataSourceStub>());
166 ts.preInit(ctlCmds);
167
168 REQUIRE(ctlCmds.commands().empty());
169 }
170
171 SECTION("Does not generate post-init control commands")
172 {
173 FanFixedTestAdapter ts(std::make_unique<UIntDataSourceStub>(),
174 std::make_unique<UIntDataSourceStub>());
175 ts.postInit(ctlCmds);
176
177 REQUIRE(ctlCmds.commands().empty());
178 }
179
180 SECTION("Imports its state")
181 {
182 FanFixedTestAdapter ts(std::make_unique<UIntDataSourceStub>(),
183 std::make_unique<UIntDataSourceStub>());
184 FanFixedImporterStub i;
185 i.value(100);
186 i.fanStartValue(50);
187 i.fanStop(true);
188
189 ts.importWith(i);
190
191 REQUIRE(ts.value() == 255);
192 REQUIRE(ts.fanStartValue() == 128);
193 REQUIRE(ts.fanStop());
194 }
195
196 SECTION("Export its state")
197 {
198 FanFixedTestAdapter ts(std::make_unique<UIntDataSourceStub>(),
199 std::make_unique<UIntDataSourceStub>());
200 ts.value(128);
201 ts.fanStop(true);
202 ts.fanStartValue(128);
203 units::concentration::percent_t percentValue(50);
204
205 FanFixedExporterMock e;
206 REQUIRE_CALL(e, takeFanFixedValue(trompeloeil::_)).LR_WITH(_1 == percentValue);
207 REQUIRE_CALL(e, takeFanFixedFanStop(true));
208 REQUIRE_CALL(e, takeFanFixedFanStartValue(trompeloeil::_))
209 .LR_WITH(_1 == percentValue);
210
211 ts.exportControl(e);
212 }
213
214 SECTION("Does not generate clean control commands")
215 {
216 FanFixedTestAdapter ts(std::make_unique<UIntDataSourceStub>(),
217 std::make_unique<UIntDataSourceStub>());
218 ts.cleanControl(ctlCmds);
219
220 REQUIRE(ctlCmds.commands().empty());
221 }
222
223 SECTION("Does not generate sync control commands when is synced")
224 {
225 FanFixedTestAdapter ts(std::make_unique<UIntDataSourceStub>("pwm_enabled", 1),
226 std::make_unique<UIntDataSourceStub>("pwm", 128));
227 ts.value(128);
228
229 ts.syncControl(ctlCmds);
230 REQUIRE(ctlCmds.commands().empty());
231 }
232
233 SECTION("Does generate sync control commands when...")
234 {
235 SECTION("pwm_enabled is out of sync")
236 {
237 FanFixedTestAdapter ts(
238 std::make_unique<UIntDataSourceStub>("pwm_enabled", 0),
239 std::make_unique<UIntDataSourceStub>("pwm", 128));
240 ts.value(128);
241
242 ts.syncControl(ctlCmds);
243
244 REQUIRE(ctlCmds.commands().size() == 2);
245
246 auto &[cmd0Path, cmd0Value] = ctlCmds.commands().front();
247 REQUIRE(cmd0Path == "pwm_enabled");
248 REQUIRE(cmd0Value == "1");
249
250 auto &[cmd1Path, cmd1Value] = ctlCmds.commands().back();
251 REQUIRE(cmd1Path == "pwm");
252 REQUIRE(cmd1Value == "128");
253 }
254
255 SECTION("pwm is out of sync")
256 {
257 FanFixedTestAdapter ts(
258 std::make_unique<UIntDataSourceStub>("pwm_enabled", 1),
259 std::make_unique<UIntDataSourceStub>("pwm", 0));
260 ts.value(128);
261
262 ts.syncControl(ctlCmds);
263
264 REQUIRE(ctlCmds.commands().size() == 1);
265
266 auto &[cmdPath, cmdValue] = ctlCmds.commands().front();
267 REQUIRE(cmdPath == "pwm");
268 REQUIRE(cmdValue == "128");
269 }
270
271 SECTION("pwm_enabled and pwm are out of sync")
272 {
273 FanFixedTestAdapter ts(
274 std::make_unique<UIntDataSourceStub>("pwm_enabled", 0),
275 std::make_unique<UIntDataSourceStub>("pwm", 0));
276 ts.value(128);
277
278 ts.syncControl(ctlCmds);
279
280 REQUIRE(ctlCmds.commands().size() == 2);
281
282 auto &[cmd0Path, cmd0Value] = ctlCmds.commands().front();
283 REQUIRE(cmd0Path == "pwm_enabled");
284 REQUIRE(cmd0Value == "1");
285
286 auto &[cmd1Path, cmd1Value] = ctlCmds.commands().back();
287 REQUIRE(cmd1Path == "pwm");
288 REQUIRE(cmd1Value == "128");
289 }
290 }
291
292 SECTION("Synced hw value uses fan start value when enabled")
293 {
294 FanFixedTestAdapter ts(std::make_unique<UIntDataSourceStub>("pwm_enabled", 1),
295 std::make_unique<UIntDataSourceStub>("pwm", 20));
296 ts.value(20);
297 ts.fanStartValue(30);
298 ts.fanStop(false);
299
300 ts.syncControl(ctlCmds);
301
302 REQUIRE(ctlCmds.commands().empty());
303
304 ts.fanStop(true);
305 ts.syncControl(ctlCmds);
306
307 REQUIRE(ctlCmds.commands().size() == 1);
308 auto &[cmdPath, cmdValue] = ctlCmds.commands().front();
309 REQUIRE(cmdPath == "pwm");
310 REQUIRE(cmdValue == "0");
311 }
312 }
313
314 } // namespace Tests::AMD::FanFixed