forking bench, renames
[libc-test] / common / t.c
1 #define _POSIX_C_SOURCE 200809L
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <stdarg.h>
5 #include <string.h>
6 #include <errno.h>
7 #include <sys/types.h>
8 #include <sys/wait.h>
9 #include <unistd.h>
10 #include "test.h"
11
12 #define B(t)
13 #define T(t) void t();
14 #include "main.h"
15 #undef T
16
17 static int failed;
18 static const char *name;
19
20 static int verbose;
21 static int count;
22 static int nfailed;
23
24 static void errtimer() { error("use *_timer in benchmarks only\n"); }
25 int N = 0;
26 void start_timer() { errtimer(); }
27 void stop_timer() { errtimer(); }
28 void reset_timer() { errtimer(); }
29
30 void error__(const char *n, int l, const char *s, ...) {
31         va_list ap;
32
33         failed = 1;
34         fprintf(stderr, "- ERROR %s at %s:%d: ", name, n, l);
35         va_start(ap, s);
36         vfprintf(stderr, s, ap);
37         va_end(ap);
38 }
39
40 static void run(const char *n, void (*f)()) {
41         pid_t pid;
42         int s;
43
44         count++;
45         failed = 0;
46         name = n;
47         if (verbose)
48                 fprintf(stderr, "running %s:\n", name);
49
50         pid = fork();
51         if (pid == 0) {
52                 /* run test in a child process */
53                 f();
54                 exit(failed);
55         }
56
57         if (pid == -1)
58                 error("fork failed: %s\n", strerror(errno));
59         else {
60                 if (waitpid(pid, &s, 0) == -1)
61                         error("waitpid failed: %s\n", strerror(errno));
62                 else if (!WIFEXITED(s))
63                         error("abnormal exit: %s\n", WIFSIGNALED(s) ? strsignal(WTERMSIG(s)) : "(unknown)");
64                 else
65                         failed = !!WEXITSTATUS(s);
66         }
67
68         if (failed) {
69                 nfailed++;
70                 fprintf(stderr, "FAILED %s\n", name);
71         } else if (verbose)
72                 fprintf(stderr, "PASSED %s\n", name);
73 }
74
75 static int summary() {
76         fprintf(stderr, "PASS:%d FAIL:%d\n", count-nfailed, nfailed);
77         return !!nfailed;
78 }
79
80 int main() {
81 #define T(t) run(#t, t);
82 #include "main.h"
83         return summary();
84 }