Debian Codegraph @

src/kj/test.c++

raw
1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
2 // Licensed under the MIT License:
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21
22 #ifndef _GNU_SOURCE
23 #define _GNU_SOURCE
24 #endif
25
26 #include "test.h"
27 #include "main.h"
28 #include "io.h"
29 #include "miniposix.h"
30 #include <stdlib.h>
31 #include <signal.h>
32 #include <string.h>
33 #include "time.h"
34 #ifndef _WIN32
35 #include <sys/mman.h>
36 #endif
37
38 namespace kj {
39
40 namespace {
41
42 TestCase* testCasesHead = nullptr;
43 TestCase** testCasesTail = &testCasesHead;
44
45 size_t benchmarkIterCount = 1;
46
47 } // namespace
48
49 TestCase::TestCase(const char* file, uint line, const char* description)
50 : file(file), line(line), description(description), next(nullptr), prev(testCasesTail),
51 matchedFilter(false) {
52 *prev = this;
53 testCasesTail = &next;
54 }
55
56 TestCase::~TestCase() {
57 *prev = next;
58 if (next == nullptr) {
59 testCasesTail = prev;
60 } else {
61 next->prev = prev;
62 }
63 }
64
65 size_t TestCase::iterCount() {
66 return benchmarkIterCount;
67 }
68
69 // =======================================================================================
70
71 namespace _ { // private
72
73 GlobFilter::GlobFilter(const char* pattern): pattern(heapString(pattern)) {}
74 GlobFilter::GlobFilter(ArrayPtr<const char> pattern): pattern(heapString(pattern)) {}
75
76 bool GlobFilter::matches(StringPtr name) {
77 // Get out your computer science books. We're implementing a non-deterministic finite automaton.
78 //
79 // Our NDFA has one "state" corresponding to each character in the pattern.
80 //
81 // As you may recall, an NDFA can be transformed into a DFA where every state in the DFA
82 // represents some combination of states in the NDFA. Therefore, we actually have to store a
83 // list of states here. (Actually, what we really want is a set of states, but because our
84 // patterns are mostly non-cyclic a list of states should work fine and be a bit more efficient.)
85
86 // Our state list starts out pointing only at the start of the pattern.
87 states.resize(0);
88 states.add(0);
89
90 Vector<uint> scratch;
91
92 // Iterate through each character in the name.
93 for (char c: name) {
94 // Pull the current set of states off to the side, so that we can populate `states` with the
95 // new set of states.
96 Vector<uint> oldStates = kj::mv(states);
97 states = kj::mv(scratch);
98 states.resize(0);
99
100 // The pattern can omit a leading path. So if we're at a '/' then enter the state machine at
101 // the beginning on the next char.
102 if (c == '/' || c == '\\') {
103 states.add(0);
104 }
105
106 // Process each state.
107 for (uint state: oldStates) {
108 applyState(c, state);
109 }
110
111 // Store the previous state vector for reuse.
112 scratch = kj::mv(oldStates);
113 }
114
115 // If any one state is at the end of the pattern (or at a wildcard just before the end of the
116 // pattern), we have a match.
117 for (uint state: states) {
118 while (state < pattern.size() && pattern[state] == '*') {
119 ++state;
120 }
121 if (state == pattern.size()) {
122 return true;
123 }
124 }
125 return false;
126 }
127
128 void GlobFilter::applyState(char c, int state) {
129 if (state < pattern.size()) {
130 switch (pattern[state]) {
131 case '*':
132 // At a '*', we both re-add the current state and attempt to match the *next* state.
133 if (c != '/' && c != '\\') { // '*' doesn't match '/'.
134 states.add(state);
135 }
136 applyState(c, state + 1);
137 break;
138
139 case '?':
140 // A '?' matches one character (never a '/').
141 if (c != '/' && c != '\\') {
142 states.add(state + 1);
143 }
144 break;
145
146 default:
147 // Any other character matches only itself.
148 if (c == pattern[state]) {
149 states.add(state + 1);
150 }
151 break;
152 }
153 }
154 }
155
156 } // namespace _ (private)
157
158 // =======================================================================================
159
160 namespace {
161
162 class TestExceptionCallback: public ExceptionCallback {
163 public:
164 TestExceptionCallback(ProcessContext& context): context(context) {}
165
166 bool failed() { return sawError; }
167
168 void logMessage(LogSeverity severity, const char* file, int line, int contextDepth,
169 String&& text) override {
170 void* traceSpace[32];
171 auto trace = getStackTrace(traceSpace, 2);
172
173 if (text.size() == 0) {
174 text = kj::heapString("expectation failed");
175 }
176
177 text = kj::str(kj::repeat('_', contextDepth), file, ':', line, ": ", kj::mv(text));
178
179 if (severity == LogSeverity::ERROR || severity == LogSeverity::FATAL) {
180 sawError = true;
181 context.error(kj::str(text, "\nstack: ", stringifyStackTraceAddresses(trace), stringifyStackTrace(trace)));
182 } else {
183 context.warning(text);
184 }
185 }
186
187 private:
188 ProcessContext& context;
189 bool sawError = false;
190 };
191
192 TimePoint readClock() {
193 return systemPreciseMonotonicClock().now();
194 }
195
196 } // namespace
197
198 class TestRunner {
199 public:
200 explicit TestRunner(ProcessContext& context)
201 : context(context), useColor(isatty(STDOUT_FILENO)) {}
202
203 MainFunc getMain() {
204 return MainBuilder(context, "KJ Test Runner (version not applicable)",
205 "Run all tests that have been linked into the binary with this test runner.")
206 .addOptionWithArg({'f', "filter"}, KJ_BIND_METHOD(*this, setFilter), "<file>[:<line>]",
207 "Run only the specified test case(s). You may use a '*' wildcard in <file>. You may "
208 "also omit any prefix of <file>'s path; test from all matching files will run. "
209 "You may specify multiple filters; any test matching at least one filter will run. "
210 "<line> may be a range, e.g. \"100-500\".")
211 .addOption({'l', "list"}, KJ_BIND_METHOD(*this, setList),
212 "List all test cases that would run, but don't run them. If --filter is specified "
213 "then only the match tests will be listed.")
214 .addOptionWithArg({'b', "benchmark"}, KJ_BIND_METHOD(*this, setBenchmarkIters), "<iters>",
215 "Specifies that any benchmarks in the tests should run for <iters> iterations. "
216 "If not specified, then count is 1, which simply tests that the benchmarks function.")
217 .callAfterParsing(KJ_BIND_METHOD(*this, run))
218 .build();
219 }
220
221 MainBuilder::Validity setFilter(StringPtr pattern) {
222 hasFilter = true;
223 ArrayPtr<const char> filePattern = pattern;
224 uint minLine = kj::minValue;
225 uint maxLine = kj::maxValue;
226
227 KJ_IF_MAYBE(colonPos, pattern.findLast(':')) {
228 char* end;
229 StringPtr lineStr = pattern.slice(*colonPos + 1);
230
231 bool parsedRange = false;
232 minLine = strtoul(lineStr.cStr(), &end, 0);
233 if (end != lineStr.begin()) {
234 if (*end == '-') {
235 // A range.
236 const char* part2 = end + 1;
237 maxLine = strtoul(part2, &end, 0);
238 if (end > part2 && *end == '\0') {
239 parsedRange = true;
240 }
241 } else if (*end == '\0') {
242 parsedRange = true;
243 maxLine = minLine;
244 }
245 }
246
247 if (parsedRange) {
248 // We have an exact line number.
249 filePattern = pattern.slice(0, *colonPos);
250 } else {
251 // Can't parse as a number. Maybe the colon is part of a Windows path name or something.
252 // Let's just keep it as part of the file pattern.
253 minLine = kj::minValue;
254 maxLine = kj::maxValue;
255 }
256 }
257
258 _::GlobFilter filter(filePattern);
259
260 for (TestCase* testCase = testCasesHead; testCase != nullptr; testCase = testCase->next) {
261 if (!testCase->matchedFilter && filter.matches(testCase->file) &&
262 testCase->line >= minLine && testCase->line <= maxLine) {
263 testCase->matchedFilter = true;
264 }
265 }
266
267 return true;
268 }
269
270 MainBuilder::Validity setList() {
271 listOnly = true;
272 return true;
273 }
274
275 MainBuilder::Validity setBenchmarkIters(StringPtr param) {
276 KJ_IF_MAYBE(i, param.tryParseAs<size_t>()) {
277 benchmarkIterCount = *i;
278 return true;
279 } else {
280 return "expected an integer";
281 }
282 }
283
284 MainBuilder::Validity run() {
285 if (testCasesHead == nullptr) {
286 return "no tests were declared";
287 }
288
289 // Find the common path prefix of all filenames, so we can strip it off.
290 ArrayPtr<const char> commonPrefix = StringPtr(testCasesHead->file);
291 for (TestCase* testCase = testCasesHead; testCase != nullptr; testCase = testCase->next) {
292 for (size_t i: kj::indices(commonPrefix)) {
293 if (testCase->file[i] != commonPrefix[i]) {
294 commonPrefix = commonPrefix.slice(0, i);
295 break;
296 }
297 }
298 }
299
300 // Back off the prefix to the last '/'.
301 while (commonPrefix.size() > 0 && commonPrefix.back() != '/' && commonPrefix.back() != '\\') {
302 commonPrefix = commonPrefix.slice(0, commonPrefix.size() - 1);
303 }
304
305 // Run the testts.
306 uint passCount = 0;
307 uint failCount = 0;
308 for (TestCase* testCase = testCasesHead; testCase != nullptr; testCase = testCase->next) {
309 if (!hasFilter || testCase->matchedFilter) {
310 auto name = kj::str(testCase->file + commonPrefix.size(), ':', testCase->line,
311 ": ", testCase->description);
312
313 write(BLUE, "[ TEST ]", name);
314
315 if (!listOnly) {
316 bool currentFailed = true;
317 auto start = readClock();
318 KJ_IF_MAYBE(exception, runCatchingExceptions([&]() {
319 TestExceptionCallback exceptionCallback(context);
320 testCase->run();
321 currentFailed = exceptionCallback.failed();
322 })) {
323 context.error(kj::str(*exception));
324 }
325 auto end = readClock();
326
327 auto message = kj::str(name, " (", (end - start) / kj::MICROSECONDS, " μs)");
328
329 if (currentFailed) {
330 write(RED, "[ FAIL ]", message);
331 ++failCount;
332 } else {
333 write(GREEN, "[ PASS ]", message);
334 ++passCount;
335 }
336 }
337 }
338 }
339
340 if (passCount > 0) write(GREEN, kj::str(passCount, " test(s) passed"), "");
341 if (failCount > 0) write(RED, kj::str(failCount, " test(s) failed"), "");
342 context.exit();
343
344 KJ_UNREACHABLE;
345 }
346
347 private:
348 ProcessContext& context;
349 bool useColor;
350 bool hasFilter = false;
351 bool listOnly = false;
352
353 enum Color {
354 RED,
355 GREEN,
356 BLUE
357 };
358
359 void write(StringPtr text) {
360 FdOutputStream(STDOUT_FILENO).write(text.begin(), text.size());
361 }
362
363 void write(Color color, StringPtr prefix, StringPtr message) {
364 StringPtr startColor, endColor;
365 if (useColor) {
366 switch (color) {
367 case RED: startColor = "\033[0;1;31m"; break;
368 case GREEN: startColor = "\033[0;1;32m"; break;
369 case BLUE: startColor = "\033[0;1;34m"; break;
370 }
371 endColor = "\033[0m";
372 }
373
374 String text = kj::str(startColor, prefix, endColor, ' ', message, '\n');
375 write(text);
376 }
377 };
378
379 } // namespace kj
380
381 KJ_MAIN(kj::TestRunner);