forked from znort987/blockparser
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.cpp
230 lines (195 loc) · 4.94 KB
/
test.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
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
#include <regex>
#include <string>
#include <test.h>
#include <vector>
#include <stdio.h>
#include <timer.h>
#include <assert.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <algorithm>
static std::vector<Test*> *tests;
static std::vector<std::string> msgs;
struct TestOrdering {
bool operator()(
const Test *const &a,
const Test *const &b
) {
auto aName = a->getName().c_str();
auto bName = b->getName().c_str();
return (strcasecmp(aName, bName)<0);
}
};
Test::Test() {
if(0==tests) {
tests = new std::vector<Test*>;
}
tests->push_back(this);
}
void Test::runFilter(
const char *_filter,
int &failCount,
int &testCount
) {
if(0==tests) {
return;
}
auto negate = false;
if(_filter && '!'==_filter[0]) {
negate = true;
++_filter;
}
std::string filter = ".*";
filter += (_filter ? _filter : "");
filter += ".*";
TestOrdering ordering;
std::stable_sort(
tests->begin(),
tests->end(),
ordering
);
size_t maxLen = 0;
for(auto test:*tests) {
auto name = test->getName();
auto f = filter.c_str();
auto n = name.c_str();
std::regex regexp(f, std::regex_constants::icase);
auto matches = std::regex_match(n, regexp);
if(negate) {
matches = !matches;
}
if(matches) {
maxLen = std::max(maxLen, name.size());
}
}
auto fakeIt = (0!=getenv("FAKE"));
for(auto test:*tests) {
auto name = test->getName();
auto f = filter.c_str();
auto n = name.c_str();
std::regex regexp(f, std::regex_constants::icase);
auto matches = std::regex_match(n, regexp);
if(negate) {
matches = !matches;
}
if(false==matches) {
continue;
}
printf(" %s ", name.c_str());
auto nameSize = name.size();
auto nbDots = (5 + maxLen) - nameSize;
for(size_t j=0; j<nbDots; ++j) {
putchar('.');
}
fflush(stdout);
msgs.clear();
if(false==test->isActive()) {
printf(" %s ", "SKIP <----");
} else {
auto start = Timer::nanos();
auto status = fakeIt ? 0 : test->run();
if(0!=status && 2!=status) {
++failCount;
}
++testCount;
auto now = Timer::nanos();
auto elapsed = (now - start)*1e-9;
printf(
" %s ",
status==0 ? "pass" :
status==1 ? "FAIL <----" :
status==2 ? "SKIP <----" :
"unknown"
);
printf("%7.3f secs ", elapsed);
}
for(size_t i=0; i<msgs.size(); ++i) {
printf("%s ", msgs[i].c_str());
}
msgs.clear();
printf("\n");
}
}
void Test::runAll(
char *filters[]
) {
if(0==tests) {
return;
}
auto start = Timer::nanos();
auto nbTests = tests->size();
printf("\nRegression tests: %d tests registered\n\n", (int)nbTests);
auto failCount = 0;
auto testCount = 0;
if(filters==0 || filters[0]==0) {
Test::runFilter(0, failCount, testCount);
}
while(*filters) {
Test::runFilter(*(filters++), failCount, testCount);
}
printf("\nRegression tests:");
if(0==failCount) {
printf("all %d matched tests have passed.\n", testCount);
} else {
printf("%d/%d tests have failed.\n", failCount, testCount);
}
auto now = Timer::nanos();
auto elapsed = (now-start)*1e-9;
printf("Tests ran in %.3f secs.\n", elapsed);
printf("\n");
}
void Test::pushMsg(
const char *msg,
...
) {
va_list list;
va_start(list, msg);
char *result = 0;
auto r = vasprintf(&result, msg, list);
if(r<0) {
abort();
}
if(result) {
msgs.push_back(result);
free(result);
}
va_end(list);
}
void Test::check(
bool &ok,
bool cond,
const char *func,
const char *file,
int line,
const char *cStr,
const char *msg,
...
) {
ok = ok && cond;
if(false==cond) {
fprintf(
stderr,
"\n"
"\n"
"In file %s\n"
"At line %d\n"
"In function %s\n"
"Condition failed: %s\n"
"\n",
file,
line,
func,
cStr
);
if(msg) {
va_list list;
va_start(list, msg);
vfprintf(stderr, msg, list);
va_end(list);
}
fflush(stderr);
fflush(stdout);
assert(0);
}
}