Repository navigation
Expand file tree
/
Copy pathComputerPrograms_UnitTestRunner.cpp
More file actions
433 lines (373 loc) · 13.4 KB
/
Copy pathComputerPrograms_UnitTestRunner.cpp
File metadata and controls
433 lines (373 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/* Unit Test Runner
*
* From: https://github.com/PokemonAutomation/
*
*/
#include "Common/Cpp/ListenerSet.h"
#include "Common/Cpp/ScopeExit.h"
#include "Common/Cpp/PrettyPrint.h"
#include "Common/Cpp/TestRunners/TestImageDistortionManager.h"
#include "Common/Cpp/TestRunners/UnitTestDatabase.h"
#include "Common/Qt/QtThreadPool_Tests.h"
#include "Kernels/Kernels_Tests.h"
#include "CommonFramework/AudioPipeline/AudioInfo_Tests.h"
#include "CommonFramework/AudioPipeline/Tools/ChannelMixer.h"
#include "CommonFramework/AudioPipeline/SinkPipeline_Tests.h"
#include "CommonFramework/AudioPipeline/IO/AudioFileLoader_Tests.h"
#include "CommonFramework/AudioPipeline/Spectrum/FFTStreamer.h"
#include "CommonFramework/GlobalAutoPaths.h"
#include "CommonFramework/ProgramStats/StatsTracking.h"
#include "CommonFramework/Tools/GlobalThreadPools.h"
#include "CommonFramework/Windows/LabControl.h"
#include "CommonTools/VisualDetectors/BlackBorderDetector.h"
#include "CommonTools/OCR/OCR_Tests.h"
#include "NintendoSwitch/Inference/NintendoSwitch_CheckOnlineDetector.h"
#include "NintendoSwitch/Inference/NintendoSwitch_FailedToConnectDetector.h"
#include "NintendoSwitch/Inference/NintendoSwitch_UpdatePopupDetector.h"
#include "NintendoSwitch/Programs/Typewriter/NintendoSwitch_Typewriter_Tests.h"
#include "Pokemon/Navigation/Pokemon_Navigation_Tests.h"
#include "VideoGames/PokemonHome/PokemonHome_Tests.h"
#include "VideoGames/PokemonSwSh/PokemonSwSh_Tests.h"
#include "VideoGames/PokemonLA/PokemonLA_Tests.h"
#include "VideoGames/PokemonSV/PokemonSV_Tests.h"
#include "VideoGames/PokemonLZA/PokemonLZA_Tests.h"
#include "VideoGames/PokemonFRLG/PokemonFRLG_Tests.h"
#include "VideoGames/PokemonRSE/PokemonRSE_Tests.h"
#include "ComputerPrograms_UnitTestRunner.h"
namespace PokemonAutomation{
namespace ComputerPrograms{
UnitTestDatabase make_UNIT_TESTS_ALL(){
UnitTestDatabase ret;
add_tests_QtEventThreadPool(ret, GlobalThreadPools::computation_normal());
add_tests_BlackBorderDetector(ret);
add_tests_ListenerSet(ret);
add_tests_AudioInfo(ret);
add_tests_ChannelMixerMatrix(ret);
add_tests_FFTStreamer(ret);
add_tests_SinkPipeline(ret);
add_tests_AudioFileLoader(ret);
add_tests_LabControl(ret);
OCR::add_tests(ret);
Kernels::add_tests(ret);
Pokemon::add_tests_LegPlanner(ret);
Pokemon::add_tests_LegWalker(ret);
NintendoSwitch::add_tests_CheckOnlineDetector(ret);
NintendoSwitch::add_tests_FailedToConnectDetector(ret);
NintendoSwitch::add_tests_UpdatePopupDetector(ret);
NintendoSwitch::Typewriter::add_tests_TypewriterTextMapping(ret);
NintendoSwitch::PokemonFRLG::add_tests(ret);
NintendoSwitch::PokemonRSE::add_tests(ret);
NintendoSwitch::PokemonHome::add_tests(ret);
NintendoSwitch::PokemonSwSh::add_tests(ret);
NintendoSwitch::PokemonLA::add_tests(ret);
NintendoSwitch::PokemonSV::add_tests(ret);
NintendoSwitch::PokemonLZA::add_tests(ret);
return ret;
}
const UnitTestDatabase& UNIT_TESTS_ALL(){
static UnitTestDatabase database = make_UNIT_TESTS_ALL();
return database;
}
StringSelectDatabase make_TEST_DATABASE(){
StringSelectDatabase database;
for (auto& test : UNIT_TESTS_ALL()){
database.add_entry(StringSelectEntry(test.first, test.first));
}
return database;
}
const StringSelectDatabase& TEST_DATABASE(){
static StringSelectDatabase DATABASE = make_TEST_DATABASE();
return DATABASE;
}
UnitTestRunner_Descriptor::UnitTestRunner_Descriptor()
: ComputerProgramDescriptor(
"UnitTestRunner",
"Nintendo Switch",
"Unit Test Runner",
"",
"UI to run unit tests.",
PanelQualityStanding::DEVELOPER
)
{}
class UnitTestRunner_Descriptor::Stats : public StatsTracker{
public:
Stats()
: tests(m_stats["Tests"])
, passed(m_stats["Passed"])
, failed(m_stats["Failed"])
, skipped(m_stats["Skipped"])
, oom(m_stats["OOM"])
{
m_display_order.emplace_back("Tests");
m_display_order.emplace_back("Passed");
m_display_order.emplace_back("Failed");
m_display_order.emplace_back("Skipped");
m_display_order.emplace_back("OOM");
}
std::atomic<uint64_t>& tests;
std::atomic<uint64_t>& passed;
std::atomic<uint64_t>& failed;
std::atomic<uint64_t>& skipped;
std::atomic<uint64_t>& oom;
};
std::unique_ptr<StatsTracker> UnitTestRunner_Descriptor::make_stats() const{
return std::unique_ptr<StatsTracker>(new Stats());
}
UnitTestRunner::~UnitTestRunner(){
SUBSTRING_TEST.remove_listener(*this);
RUN_MODE.remove_listener(*this);
}
UnitTestRunner::UnitTestRunner()
: RESOURCE_LABEL("<b>Test Resources Path:</b>")
, RESOURCE_PATH(
false,
LockMode::READ_ONLY,
UNIT_TEST_RESOURCE_PATH(),
""
)
, RUN_MODE(
"<b>Run Mode:</b>",
{
{RunMode::RUN_ONE, "run-one", "Run One Test"},
{RunMode::RUN_SUBSTRING_MATCH, "run-substring-match", "Run Matching Substrings"},
{RunMode::RUN_ALL, "run-all", "Run All Tests"},
},
LockMode::LOCK_WHILE_RUNNING,
RunMode::RUN_SUBSTRING_MATCH
)
, SINGLE_TEST_LABEL("<b>Test Name:</b>")
, SINGLE_TEST(
TEST_DATABASE(),
LockMode::LOCK_WHILE_RUNNING,
0
)
, SUBSTRING_TEST_LABEL("<b>Run All Tests Containing This Substring:</b>")
, SUBSTRING_TEST(false, LockMode::LOCK_WHILE_RUNNING, "", "PokemonSwSh")
, TESTS_TO_RUN(
"<b>Tests to Run:</b>",
LockMode::READ_ONLY,
"", ""
)
, PASSED_TESTS(
"<b>Passing Tests:</b>",
LockMode::READ_ONLY,
"", ""
)
, FAILED_TESTS(
"<b>Failing Tests:</b>",
LockMode::READ_ONLY,
"", ""
)
{
PA_ADD_STATIC(RESOURCE_LABEL);
PA_ADD_STATIC(RESOURCE_PATH);
PA_ADD_OPTION(RUN_MODE);
PA_ADD_STATIC(SINGLE_TEST_LABEL);
PA_ADD_OPTION(SINGLE_TEST);
PA_ADD_STATIC(SUBSTRING_TEST_LABEL);
PA_ADD_OPTION(SUBSTRING_TEST);
PA_ADD_STATIC(TESTS_TO_RUN);
PA_ADD_OPTION(PASSED_TESTS);
PA_ADD_OPTION(FAILED_TESTS);
UnitTestRunner::on_config_value_changed(this);
RUN_MODE.add_listener(*this);
SUBSTRING_TEST.add_listener(*this);
}
void UnitTestRunner::on_config_value_changed(void* object){
if (object == &RUN_MODE){
SINGLE_TEST_LABEL.set_visibility(ConfigOptionState::HIDDEN);
SINGLE_TEST.set_visibility(ConfigOptionState::HIDDEN);
SUBSTRING_TEST_LABEL.set_visibility(ConfigOptionState::HIDDEN);
SUBSTRING_TEST.set_visibility(ConfigOptionState::HIDDEN);
TESTS_TO_RUN.set_visibility(ConfigOptionState::HIDDEN);
switch ((RunMode)RUN_MODE){
case RunMode::RUN_ONE:
SINGLE_TEST_LABEL.set_visibility(ConfigOptionState::ENABLED);
SINGLE_TEST.set_visibility(ConfigOptionState::ENABLED);
break;
case RunMode::RUN_SUBSTRING_MATCH:
SUBSTRING_TEST_LABEL.set_visibility(ConfigOptionState::ENABLED);
SUBSTRING_TEST.set_visibility(ConfigOptionState::ENABLED);
TESTS_TO_RUN.set_visibility(ConfigOptionState::ENABLED);
break;
case RunMode::RUN_ALL:
break;
}
return;
}
if (object == &SUBSTRING_TEST){
std::string name = SUBSTRING_TEST;
TESTS_TO_RUN.set("");
size_t count = 0;
for (const auto& test : UNIT_TESTS_ALL()){
if (test.first.contains(name)){
count++;
if (count <= 1000){
TESTS_TO_RUN.append(test.first + "\n");
}
}
}
if (count > 1000){
TESTS_TO_RUN.append("<" + tostr_u_commas(count - 1000) + " more...>\n");
}
TESTS_TO_RUN.set_label("<b>Tests to Run: " + tostr_u_commas(count) + "</b>");
}
}
void UnitTestRunner::program(ProgramEnvironment& env, CancellableScope& scope){
m_total_failed.store(0, std::memory_order_release);
PASSED_TESTS.set("");
FAILED_TESTS.set("");
m_env = &env;
ScopeExit run_on_exit([this]{
m_env = nullptr;
});
PokemonAutomation::UnitTestRunner runner(
&scope,
env.logger(),
GlobalThreadPools::computation_normal()
);
runner.add_listener(*this);
const UnitTestDatabase& all_tests = UNIT_TESTS_ALL();
switch ((RunMode)RUN_MODE){
case RunMode::RUN_ONE:{
auto iter = all_tests.find(SINGLE_TEST.slug());
if (iter == all_tests.end()){
throw InternalProgramError(
&env.logger(),
PA_CURRENT_FUNCTION,
"Test does not exist: " + SINGLE_TEST.display_name()
);
}
runner.add_test(iter->second);
break;
}
case RunMode::RUN_SUBSTRING_MATCH:{
std::string name = SUBSTRING_TEST;
for (const auto& test : all_tests){
if (test.first.contains(name)){
runner.add_test(test.second);
}
}
break;
}
case RunMode::RUN_ALL:{
for (const auto& test : all_tests){
runner.add_test(test.second);
}
break;
}
}
runner.run();
}
void UnitTestRunner::on_test_finished(
std::shared_ptr<const UnitTest> test,
UnitTestResult result
){
if (m_env == nullptr){
return;
}
UnitTestRunner_Descriptor::Stats& stats = m_env->current_stats<UnitTestRunner_Descriptor::Stats>();
stats.tests++;
switch (result.result){
case UnitTestResult::NOT_RUN:
stats.skipped++;
break;
case UnitTestResult::PASSED:{
uint64_t passed = stats.passed.fetch_add(1) + 1;
if (passed <= 1000){
PASSED_TESTS.append(test->name() + "\n");
}else if (passed == 1001){
PASSED_TESTS.append("<more not shown...>\n");
}
break;
}
case UnitTestResult::FAILED:{
uint64_t failed = m_total_failed.fetch_add(1) + 1;
stats.failed++;
if (failed <= 1000){
FAILED_TESTS.append("FAILED: " + test->name() + "\n");
}else if (failed == 1001){
FAILED_TESTS.append("<more not shown...>\n");
}
break;
}
case UnitTestResult::SKIPPED:
stats.skipped++;
break;
case UnitTestResult::OOM:{
uint64_t failed = m_total_failed.fetch_add(1) + 1;
stats.oom++;
if (failed <= 1000){
FAILED_TESTS.append("OOM: " + test->name() + "\n");
}else if (failed == 1001){
FAILED_TESTS.append("<more not shown...>\n");
}
break;
}
}
m_env->update_stats();
}
bool CommandLineUnitTestRunner::run(bool augment_color_distortions){
if (augment_color_distortions){
// Load the distortion data before any test runs. Each `ImageUnitTest` checks the global
// manager when it runs, and only runs with distortions if it is loaded.
try{
initialize_test_image_distortions(UNIT_TEST_RESOURCE_PATH());
}catch (Exception& e){
m_logger.log("Unable to load the test image distortions: " + e.to_str(), COLOR_RED);
return true;
}
const TestImageDistortionManager& manager = *test_image_distortion_manager();
std::string labels;
for (size_t c = 0; c < manager.num_test_distortions(); c++){
labels += c == 0 ? "" : ", ";
labels += manager.test_distortion_label(c);
}
m_logger.log("Color distortion augmentation enabled. Image tests with labeled images also run with: " + labels);
}
PokemonAutomation::UnitTestRunner runner(
m_logger,
GlobalThreadPools::computation_normal()
);
runner.add_listener(*this);
for (const auto& test : UNIT_TESTS_ALL()){
runner.add_test(test.second);
}
runner.run();
m_logger.log(
"Tests Finished:"
"\n Passed: " + tostr_u_commas(m_passed_tests.load(std::memory_order_acquire)) +
"\n Failed: " + tostr_u_commas(m_failed_tests.load(std::memory_order_acquire)) +
"\n Skipped: " + tostr_u_commas(m_skipped_tests.load(std::memory_order_acquire))
);
return m_failed_tests.load(std::memory_order_acquire) > 0;
}
void CommandLineUnitTestRunner::on_test_finished(
std::shared_ptr<const UnitTest> test,
UnitTestResult result
){
switch (result.result){
case UnitTestResult::NOT_RUN:
m_logger.log("NOT RUN: " + test->name(), COLOR_ORANGE);
m_skipped_tests++;
break;
case UnitTestResult::PASSED:
m_logger.log("PASSED: " + test->name(), COLOR_BLUE);
m_passed_tests++;
break;
case UnitTestResult::FAILED:
m_logger.log("FAILED: " + test->name(), COLOR_RED);
m_failed_tests++;
break;
case UnitTestResult::SKIPPED:
m_logger.log("SKIPPED: " + test->name(), COLOR_ORANGE);
m_skipped_tests++;
break;
case UnitTestResult::OOM:
m_logger.log("OOM: " + test->name(), COLOR_RED);
m_failed_tests++;
break;
}
}
}
}