Debian Codegraph @

tests/src/test_cpu.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/controlmock.h"
9 #include "common/sensormock.h"
10 #include "core/components/cpu.h"
11 #include "core/info/icpuinfo.h"
12
13 extern template struct trompeloeil::reporter<trompeloeil::specialized>;
14
15 namespace Tests::CPU {
16
17 class CPUInfoStub : public ICPUInfo
18 {
19 public:
20 int physicalId() const override
21 {
22 return 0;
23 }
24
25 std::vector<ICPUInfo::ExecutionUnit> const &executionUnits() const override
26 {
27 return executionUnits_;
28 }
29
30 std::vector<std::string> keys() const override
31 {
32 return std::vector<std::string>{"info_k1"};
33 }
34
35 std::string info(std::string_view) const override
36 {
37 return "info_1";
38 }
39
40 bool hasCapability(std::string_view) const override
41 {
42 return true;
43 }
44
45 void initialize(std::vector<std::unique_ptr<ICPUInfo::IProvider>> const &) override
46 {
47 }
48
49 private:
50 std::vector<ICPUInfo::ExecutionUnit> const executionUnits_{{0, 0, "/cpu0"}};
51 };
52
53 class CPUImporterStub : public ICPU::Importer
54 {
55 public:
56 std::optional<std::reference_wrapper<Importable::Importer>>
57 provideImporter(Item const &) override
58 {
59 return *this;
60 }
61
62 bool provideActive() const override
63 {
64 return false;
65 }
66 };
67
68 class CPUExporterMock : public ICPU::Exporter
69 {
70 public:
71 MAKE_MOCK1(takeActive, void(bool), override);
72 MAKE_MOCK1(takeInfo, void(ICPUInfo const &), override);
73 MAKE_MOCK1(takeSensor, void(ISensor const &), override);
74 MAKE_MOCK1(
75 provideExporter,
76 std::optional<std::reference_wrapper<Exportable::Exporter>>(Item const &),
77 override);
78 };
79
80 TEST_CASE("CPU tests", "[CPU]")
81 {
82 CommandQueueStub ctlCmds;
83
84 auto info = std::make_unique<CPUInfoStub>();
85 auto &infoStub = *info;
86
87 std::vector<std::unique_ptr<IControl>> controls;
88 controls.emplace_back(std::make_unique<ControlMock>());
89 auto &controlMock = static_cast<ControlMock &>(*controls[0]);
90
91 std::vector<std::unique_ptr<ISensor>> sensors;
92 sensors.emplace_back(std::make_unique<SensorMock>());
93 auto &sensorMock = static_cast<SensorMock &>(*sensors[0]);
94
95 ::CPU ts(std::move(info), std::move(controls), std::move(sensors));
96
97 SECTION("Has ICPU ID")
98 {
99 REQUIRE(ts.ID() == ICPU::ItemID);
100 }
101
102 SECTION("Is active by default")
103 {
104 REQUIRE(ts.active());
105
106 SECTION("Can be deactivated")
107 {
108 ts.activate(false);
109
110 REQUIRE_FALSE(ts.active());
111
112 SECTION("Can be activated")
113 {
114 ts.activate(true);
115
116 REQUIRE(ts.active());
117 }
118 }
119 }
120
121 SECTION("Has a unique key in a system component scope")
122 {
123 // 'CPU' + physical id combination seems to be good enough to differentiate
124 // between system components.
125 REQUIRE_THAT(ts.key(), Catch::Matchers::ContainsSubstring("CPU") &&
126 Catch::Matchers::ContainsSubstring("0"));
127 }
128
129 SECTION("Its CPU information can be retrieved")
130 {
131 REQUIRE(&ts.info() == &infoStub);
132 }
133
134 SECTION("CPU description and information can be retrieved")
135 {
136 auto [gpuDesc, infos] = ts.componentInfo();
137 REQUIRE_THAT(gpuDesc, Catch::Matchers::ContainsSubstring("CPU") &&
138 Catch::Matchers::ContainsSubstring("0"));
139 REQUIRE_FALSE(infos.empty());
140 auto &[infoKey, info] = infos[0];
141 REQUIRE(infoKey == "info_k1");
142 REQUIRE(info == "info_1");
143 }
144
145 SECTION("Update its sensors")
146 {
147 std::string const sensorid("sensorid");
148 ALLOW_CALL(sensorMock, ID()).LR_RETURN(sensorid);
149 REQUIRE_CALL(sensorMock, update());
150
151 std::unordered_map<std::string, std::unordered_set<std::string>> ignored;
152 ts.updateSensors(ignored);
153 }
154
155 SECTION("Does not update ignored sensors")
156 {
157 std::string const sensorid("sensorid");
158 std::unordered_map<std::string, std::unordered_set<std::string>> ignored;
159 ignored[ts.key()] = {};
160 ignored[ts.key()].emplace(sensorid);
161
162 ALLOW_CALL(sensorMock, ID()).LR_RETURN(sensorid);
163 FORBID_CALL(sensorMock, update());
164 ts.updateSensors(ignored);
165 }
166
167 SECTION("Pre-init its controls")
168 {
169 REQUIRE_CALL(controlMock, preInit(trompeloeil::_));
170 ts.preInit(ctlCmds);
171 }
172
173 SECTION("Post-init its controls")
174 {
175 REQUIRE_CALL(controlMock, postInit(trompeloeil::_));
176 ts.postInit(ctlCmds);
177 }
178
179 SECTION("Init controls")
180 {
181 REQUIRE_CALL(controlMock, init());
182 ts.init();
183 }
184
185 SECTION("Only clear and sync controls when is active")
186 {
187 ALLOW_CALL(sensorMock, update());
188
189 REQUIRE_CALL(controlMock, clean(trompeloeil::_));
190 REQUIRE_CALL(controlMock, sync(trompeloeil::_));
191 ts.activate(true);
192 ts.sync(ctlCmds);
193
194 FORBID_CALL(controlMock, clean(trompeloeil::_));
195 FORBID_CALL(controlMock, sync(trompeloeil::_));
196 ts.activate(false);
197 ts.sync(ctlCmds);
198 }
199
200 SECTION("Imports its state and controls")
201 {
202 REQUIRE_CALL(controlMock, importWith(trompeloeil::_));
203
204 ts.activate(true);
205 CPUImporterStub i;
206 ts.importWith(i);
207
208 REQUIRE_FALSE(ts.active());
209 }
210
211 SECTION("Exports its state, sensors and controls")
212 {
213 CPUExporterMock e;
214 ALLOW_CALL(e, provideExporter(trompeloeil::_)).LR_RETURN(e);
215 REQUIRE_CALL(e, takeActive(true));
216 REQUIRE_CALL(e, takeInfo(trompeloeil::_)).LR_WITH(&_1 == &infoStub);
217 REQUIRE_CALL(e, takeSensor(trompeloeil::_)).LR_WITH(&_1 == &sensorMock);
218
219 REQUIRE_CALL(controlMock, exportWith(trompeloeil::_));
220
221 ts.activate(true);
222 ts.exportWith(e);
223 }
224 }
225
226 } // namespace Tests::CPU