-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing.cpp
81 lines (61 loc) · 1.65 KB
/
testing.cpp
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
#include "testing.h"
#include "vector.h"
struct TestInfo {
TestRunFunc* func;
const char* name;
const char* file;
int line;
};
#define TEST_MAX_COUNT (64 * 1024)
static int _test_info_to_run_count = 0;
TestInfo _test_info_to_run[TEST_MAX_COUNT];
void RegisterTest(const char* name, const char* file, int line, TestRunFunc* testFunc) {
TestInfo info;
info.func = testFunc;
info.name = name;
info.file = file;
info.line = line;
_test_info_to_run[_test_info_to_run_count] = info;
_test_info_to_run_count++;
}
int RunAllTests(bool outputResults) {
FILE* testResults = NULL;
if (outputResults) {
testResults = fopen("ci_test_results.txt", "wb");
for (int i = 0; i < _test_info_to_run_count; i++) {
auto* ptr = &_test_info_to_run[i];
fprintf(testResults, "E$%s$%s$%d\n", ptr->name, ptr->file, ptr->line);
}
fflush(testResults);
}
for (int i = 0; i < _test_info_to_run_count; i++) {
auto* ptr = &_test_info_to_run[i];
// TODO: Try catch to get failures from any assertions...
int rc = (*ptr->func)();
if (outputResults) {
if (rc == 0) {
fprintf(testResults, "P$%s$%s$%d\n", ptr->name, ptr->file, ptr->line);
} else {
fprintf(testResults, "F$%s$%s$%d\n", ptr->name, ptr->file, ptr->line);
}
fflush(testResults);
}
}
if (outputResults) {
fclose(testResults);
}
printf("All %d tests passed!\n", _test_info_to_run_count);
return 0;
}
#if defined(CREATE_TEST_MAIN)
int main(int argc, char** argv) {
bool outputResults = false;
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--output-results") == 0) {
outputResults = true;
break;
}
}
return RunAllTests(outputResults);
}
#endif