use dprintf(1,..) instead of buffered stderr
[libc-test] / common / t.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <stdarg.h>
4 #include <string.h>
5 #include <errno.h>
6 #include <sys/types.h>
7 #include <sys/wait.h>
8 #include <unistd.h>
9 #include "test.h"
10
11 #define B(f)
12 #define T(f) void f();
13 #include "tests.h"
14 #undef T
15
16 static int failed;
17 static const char *name;
18
19 static int slow;
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 void start_timer() { errtimer(); }
26 void stop_timer() { errtimer(); }
27 void reset_timer() { errtimer(); }
28
29 void error__(const char *n, int l, const char *s, ...) {
30         va_list ap;
31
32         if (failed == 0 && nfailed == 0)
33                 dprintf(1, "FAIL\n", n);
34         failed = 1;
35         dprintf(1, " ERROR %s %s:%d: ", name, n, l);
36         va_start(ap, s);
37         vdprintf(1, s, ap);
38         va_end(ap);
39 }
40
41 static void run(const char *n, void (*f)()) {
42         pid_t pid;
43         int s;
44
45         count++;
46         failed = 0;
47         name = n;
48         if (verbose)
49                 dprintf(1, "running %s:\n", name);
50
51         pid = fork();
52         if (pid == 0) {
53                 /* run test in a child process */
54                 f();
55                 exit(failed);
56         }
57
58         if (pid == -1)
59                 error("fork failed: %s\n", strerror(errno));
60         else {
61                 if (waitpid(pid, &s, 0) == -1)
62                         error("waitpid failed: %s\n", strerror(errno));
63                 else if (!WIFEXITED(s))
64                         error("abnormal exit: %s\n", WIFSIGNALED(s) ? strsignal(WTERMSIG(s)) : "(unknown)");
65                 else
66                         failed = !!WEXITSTATUS(s);
67         }
68
69         if (failed) {
70                 nfailed++;
71                 dprintf(1, "FAILED %s\n", name);
72         } else if (verbose)
73                 dprintf(1, "PASSED %s\n", name);
74 }
75
76 static int summary() {
77         if (nfailed)
78                 dprintf(1, "FAIL (%d out of %d tests)\n", nfailed, count);
79         else
80                 dprintf(1, "ok (%d tests)\n", count);
81         return !!nfailed;
82 }
83
84 static void usage() {
85         dprintf(1, "usage: ./t [-vs]\n");
86         exit(1);
87 }
88
89 int main(int argc, char *argv[]) {
90         int c;
91
92         while((c = getopt(argc, argv, "vs")) != -1)
93                 switch(c) {
94                 case 'v':
95                         verbose = 1;
96                         break;
97                 case 's':
98                         slow = 1; /* TODO */
99                         break;
100                 default:
101                         usage();
102                 }
103         if (optind != argc)
104                 usage();
105
106 #define T(t) run(#t, t);
107 #include "tests.h"
108         return summary();
109 }