Debian Codegraph @

test/+test.ha

raw
1 // SPDX-License-Identifier: MPL-2.0
2 // (c) Hare authors <https://harelang.org>
3
4 use ascii;
5 use bufio;
6 use debug;
7 use debug::image;
8 use encoding::hex;
9 use encoding::utf8;
10 use fmt;
11 use fnmatch;
12 use io;
13 use math;
14 use memio;
15 use os;
16 use rt;
17 use sort;
18 use strings;
19 use time;
20 use unix::signal;
21 use unix::tty;
22
23 type test = struct {
24 name: str,
25 func: *fn() void,
26 };
27
28 // RETURN and ABORT must be 0 and 1 respectively
29 type status = enum {
30 RETURN = 0,
31 ABORT = 1,
32 SKIP,
33 SEGV,
34 };
35
36 type abort_reason = struct {
37 path: nullable *str,
38 line: u64,
39 col: u64,
40 msg: str,
41 };
42
43 type failure = struct {
44 test: str,
45 reason: abort_reason,
46 trace: u64,
47 };
48
49 type skipped = struct {
50 test: str,
51 reason: str,
52 };
53
54 type output = struct {
55 test: str,
56 stdout: str,
57 stderr: str,
58 };
59
60 fn finish_output(output: *output) void = {
61 free(output.stdout);
62 free(output.stderr);
63 };
64
65 type context = struct {
66 stdout: memio::stream,
67 stderr: memio::stream,
68 failures: []failure,
69 skipped: []skipped,
70 output: []output,
71 maxname: size,
72 total_time: time::duration,
73 default_round: math::fround,
74 cwd: str,
75 };
76
77 fn finish_context(ctx: *context) void = {
78 io::close(&ctx.stdout)!;
79 io::close(&ctx.stderr)!;
80 free(ctx.failures);
81 free(ctx.skipped);
82 for (let out &.. ctx.output) {
83 finish_output(out);
84 };
85 free(ctx.output);
86 free(ctx.cwd);
87 };
88
89 fn colored() bool = {
90 return len(os::tryenv("NO_COLOR", "")) == 0
91 && tty::isatty(os::stdout_file);
92 };
93
94 @symbol("__test_array_start") const test_start: [*]test;
95 @symbol("__test_array_end") const test_end: [*]test;
96
97 export @symbol("__test_main") fn main() size = {
98 const ntest = (&test_end: uintptr - &test_start: uintptr): size / size(test);
99 const tests = test_start[..ntest];
100 sort::sort(tests, size(test), &test_cmp)!;
101
102 let enabled_tests: []test = [];
103 defer free(enabled_tests);
104 if (len(os::args) == 1) {
105 append(enabled_tests, tests...)!;
106 } else for (let i = 0z; i < ntest; i += 1) {
107 for (let arg .. os::args) {
108 if (fnmatch::fnmatch(arg, tests[i].name)) {
109 append(enabled_tests, tests[i])!;
110 break;
111 };
112 };
113 };
114 if (len(enabled_tests) == 0) {
115 fmt::println("No tests run")!;
116 return 0;
117 };
118
119 let maxname = 0z;
120 for (let test .. enabled_tests) {
121 if (len(test.name) > maxname) {
122 maxname = len(test.name);
123 };
124 };
125
126 let ctx = context {
127 stdout = memio::dynamic(),
128 stderr = memio::dynamic(),
129 maxname = maxname,
130 default_round = math::getround(),
131 cwd = strings::dup(os::getcwd())!,
132 ...
133 };
134 defer finish_context(&ctx);
135
136 fmt::printfln("Running {}/{} tests:\n", len(enabled_tests), ntest)!;
137 reset(&ctx);
138 for (let test &.. enabled_tests) {
139 do_test(&ctx, test);
140 };
141 fmt::println()!;
142
143 if (len(ctx.skipped) > 0 && colored()) {
144 fmt::print("\x1b[37m")!;
145 };
146 for (let skipped .. ctx.skipped) {
147 fmt::printfln("Skipped {}: {}", skipped.test, skipped.reason)!;
148 };
149 if (len(ctx.skipped) > 0) {
150 fmt::println(if (colored()) "\x1b[m" else "")!;
151 };
152
153 if (len(ctx.failures) > 0) {
154 const image = match (image::self()) {
155 case let img: image::image =>
156 yield img;
157 case => yield;
158 };
159 defer match (&image) {
160 case let img: *image::image =>
161 image::close(img);
162 case void => void;
163 };
164
165 fmt::println("Failures:")!;
166 for (let failure .. ctx.failures) {
167 match (failure.reason.path) {
168 case null =>
169 fmt::printfln("{}: {}",
170 failure.test,
171 failure.reason.msg)!;
172 case let path: *str =>
173 fmt::printfln("{}: {}:{}:{}: {}",
174 failure.test,
175 *path,
176 failure.reason.line,
177 failure.reason.col,
178 failure.reason.msg)!;
179 };
180
181 if (failure.trace == 0) continue;
182
183 const image = match (&image) {
184 case let img: *image::image =>
185 yield img;
186 case void => continue;
187 };
188
189 const trace = match (debug::trace_by_id(failure.trace)) {
190 case let frame: debug::stackframe =>
191 yield frame;
192 case void => continue;
193 };
194
195 debug::backtrace(image, trace);
196 };
197 fmt::println()!;
198 };
199
200 for (let i = 0z; i < len(ctx.output); i += 1) {
201 if (ctx.output[i].stdout != "") {
202 fmt::println(ctx.output[i].test, "stdout:")!;
203 fmt::println(ctx.output[i].stdout)!;
204 };
205 if (ctx.output[i].stderr != "") {
206 fmt::println(ctx.output[i].test, "stderr:")!;
207 fmt::println(ctx.output[i].stderr)!;
208 };
209 if (i == len(ctx.output) - 1) {
210 fmt::println()!;
211 };
212 };
213
214 // XXX: revisit once time::format_duration is implemented
215 const total_cnt = len(enabled_tests);
216 const failed_cnt = len(ctx.failures);
217 const skipped_cnt = len(ctx.skipped);
218 const passed_cnt = total_cnt - failed_cnt - skipped_cnt;
219 const elapsed_whole = ctx.total_time / time::SECOND;
220 const elapsed_fraction = ctx.total_time % time::SECOND;
221 styled_print(if (passed_cnt > 0) 92 else 37, passed_cnt);
222 fmt::print(" passed; ")!;
223 styled_print(if (len(ctx.failures) > 0) 91 else 37, failed_cnt);
224 fmt::print(" failed; ")!;
225 if (len(ctx.skipped) > 0) {
226 fmt::print(len(ctx.skipped), "skipped; ")!;
227 };
228 fmt::printfln("{} completed in {}.{:.9}s", total_cnt,
229 elapsed_whole, elapsed_fraction)!;
230
231 easter_egg(ctx.failures, enabled_tests);
232
233 return len(ctx.failures);
234 };
235
236 fn test_cmp(a: const *opaque, b: const *opaque) int = {
237 const a = a: const *test;
238 const b = b: const *test;
239 return strings::compare(a.name, b.name);
240 };
241
242 fn reset(ctx: *context) void = {
243 math::setround(ctx.default_round);
244 math::clearexcept(math::fexcept::ALL);
245 signal::resetall();
246 os::chdir(ctx.cwd)!;
247 want_abort = false;
248 };
249
250 fn do_test(ctx: *context, test: *test) void = {
251 signal::handle(signal::sig::SEGV, &handle_segv,
252 signal::flag::NODEFER | signal::flag::ONSTACK);
253 memio::reset(&ctx.stdout);
254 memio::reset(&ctx.stderr);
255
256 const start_time = time::now(time::clock::MONOTONIC);
257 const status = run_test(ctx, test);
258 const end_time = time::now(time::clock::MONOTONIC);
259
260 const failed = interpret_status(ctx, test.name, status);
261 const time_diff = time::diff(start_time, end_time);
262 assert(time_diff >= 0);
263 ctx.total_time += time_diff;
264 fmt::printfln(" in {}.{:.9}s",
265 time_diff / 1000000000,
266 time_diff % 1000000000)!;
267
268 const stdout = printable(memio::buffer(&ctx.stdout));
269 const stderr = printable(memio::buffer(&ctx.stderr));
270 if (failed && (stdout != "" || stderr != "")) {
271 append(ctx.output, output {
272 test = test.name,
273 stdout = stdout,
274 stderr = stderr,
275 })!;
276 } else {
277 free(stdout);
278 free(stderr);
279 };
280
281 reset(ctx);
282 };
283
284 fn run_test(ctx: *context, test: *test) status = {
285 fmt::print(test.name)!;
286 dots(ctx.maxname - len(test.name) + 3);
287 bufio::flush(os::stdout)!; // write test name before test runs
288
289 let orig_stdout = os::stdout;
290 let orig_stderr = os::stderr;
291 os::stdout = &ctx.stdout;
292 os::stderr = &ctx.stderr;
293
294 trace = 0u64;
295
296 default_abort = rt::onabort(&onabort);
297 defer rt::onabort(default_abort);
298
299 defer jmp = null;
300 const n = rt::setjmp(&jmp_buf): status;
301 if (n == status::RETURN) {
302 jmp = &jmp_buf;
303 _current = test;
304 test.func();
305 };
306
307 os::stdout = orig_stdout;
308 os::stderr = orig_stderr;
309 _current = null;
310 return n;
311 };
312
313 fn printable(buf: []u8) str = {
314 match (strings::fromutf8(buf)) {
315 case let s: str =>
316 let it = strings::iter(s);
317 for (true) match (strings::next(&it)) {
318 case done =>
319 return strings::dup(s)!;
320 case let r: rune =>
321 if (ascii::valid(r) && !ascii::isprint(r)
322 && r != '\t' && r != '\n') {
323 break;
324 };
325 };
326 case utf8::invalid => void;
327 };
328
329 let s = memio::dynamic();
330 hex::dump(&s, buf)!;
331 return memio::string(&s)!;
332 };
333
334 fn dots(n: size) void = {
335 for (let i = 0z; i < n; i += 1) {
336 fmt::print(".")!;
337 };
338 };
339
340 // returns true if test failed, false if it passed or was skipped
341 fn interpret_status(ctx: *context, test: str, status: status) bool = {
342 switch (status) {
343 case status::RETURN =>
344 if (want_abort) {
345 styled_print(91, "FAIL");
346 append(ctx.failures, failure {
347 test = test,
348 reason = abort_reason {
349 msg = "Expected test to abort",
350 ...
351 },
352 trace = 0,
353 })!;
354 return true;
355 } else {
356 styled_print(92, "PASS");
357 return false;
358 };
359 case status::ABORT =>
360 if (want_abort) {
361 styled_print(92, "PASS");
362 return false;
363 } else {
364 styled_print(91, "FAIL");
365 append(ctx.failures, failure {
366 test = test,
367 reason = reason,
368 trace = trace,
369 })!;
370 return true;
371 };
372 case status::SKIP =>
373 styled_print(37, "SKIP");
374 append(ctx.skipped, skipped {
375 test = test,
376 reason = reason.msg,
377 })!;
378 return false;
379 case status::SEGV =>
380 styled_print(91, "FAIL");
381 append(ctx.failures, failure {
382 test = test,
383 reason = abort_reason {
384 msg = "Segmentation fault",
385 ...
386 },
387 trace = trace,
388 })!;
389 return true;
390 };
391 };
392
393 fn styled_print(color: int, result: fmt::formattable) void = {
394 if (colored()) {
395 fmt::printf("\x1b[{}m" "{}" "\x1b[m", color, result)!;
396 } else {
397 fmt::print(result)!;
398 };
399 };
400
401 let jmp_buf = rt::jmp_buf { ... };
402 let jmp: nullable *rt::jmp_buf = null;
403 let reason: abort_reason = abort_reason { ... };
404 let trace = 0u64;
405 let default_abort: *rt::abort_handler = @undefined;
406
407 fn onabort(
408 path: *str,
409 line: u64,
410 col: u64,
411 msg: str,
412 ) never = {
413 match (jmp) {
414 case let j: *rt::jmp_buf =>
415 let frame = debug::walk();
416 // Skip rt:: and test:: frames
417 frame = debug::next(frame) as debug::stackframe;
418 frame = debug::next(frame) as debug::stackframe;
419
420 trace = debug::trace_store(frame)!;
421 reason = abort_reason {
422 path = path,
423 line = line,
424 col = col,
425 msg = msg,
426 };
427 rt::longjmp(j, status::ABORT);
428 case null =>
429 default_abort(path, line, col, msg);
430 };
431 };
432
433 fn handle_segv(
434 sig: signal::sig,
435 info: *signal::siginfo,
436 uctx: *opaque,
437 ) void = {
438 rt::longjmp(&jmp_buf, status::SEGV);
439 };
440
441 fn easter_egg(fails: []failure, tests: []test) void = {
442 // norwegian deadbeef
443 let blob: ([0]u32, [96]u8) = ([], [
444 0xe1, 0x41, 0xf2, 0x21, 0x3f, 0x9e, 0x2d, 0xfe, 0x3f, 0x9e,
445 0x22, 0xfc, 0x43, 0xc2, 0x2f, 0x82, 0x15, 0xd1, 0x62, 0xae,
446 0x6c, 0x9e, 0x71, 0xfe, 0x33, 0xc2, 0x71, 0xfe, 0x63, 0xb4,
447 0x2d, 0xfe, 0x3f, 0xe1, 0x52, 0xf2, 0x43, 0xc6, 0x2d, 0xf9,
448 0x3d, 0x90, 0x07, 0xfe, 0x33, 0x9c, 0x2d, 0xfe, 0x3f, 0x96,
449 0x2d, 0x8f, 0x3f, 0x9e, 0x64, 0xd4, 0x33, 0x9c, 0x21, 0xfe,
450 0x3f, 0x9e, 0x2d, 0x82, 0x40, 0x9e, 0x54, 0xf9, 0x15, 0x99,
451 0x30, 0xfe, 0x3f, 0x92, 0x2d, 0xfe, 0x31, 0x9e, 0x2d, 0xfe,
452 0x38, 0xb4, 0x2d, 0xf9, 0x22, 0x83, 0x52, 0xf9, 0x40, 0xe1,
453 0x30, 0xe3, 0x38, 0x9e, 0x2d, 0xd4,
454 ]);
455 let words = &blob: *[24]u32;
456
457 // doesn't currently work on big-endian, would need to re-find the
458 // constants and use a different blob there
459 if (words[0]: u8 != 0xe1) return;
460
461 words[0] ^= len(tests): u32;
462
463 let hash = 2166136261u32;
464 for (let i = 0z; i < size(u32); i += 1) {
465 hash = (hash ^ blob.1[i]) * 16777619;
466 };
467
468 for (let i = 0z; i < len(words); i += 1) {
469 words[i] ^= hash;
470 };
471
472 if (-len(fails): u32 == words[0]) {
473 io::write(os::stdout, blob.1[size(u32)..])!;
474 };
475 };