7 static int expected_tests = NO_PLAN;
8 static int failed_tests;
9 static int current_test;
10 static char *todo_mesg;
13 vstrdupf (const char *fmt, va_list args) {
20 size = vsnprintf(NULL, 0, fmt, args2) + 2;
22 vsprintf(str, fmt, args);
28 cplan (int tests, const char *fmt, ...) {
29 expected_tests = tests;
30 if (tests == SKIP_ALL) {
34 why = vstrdupf(fmt, args);
37 note("SKIP %s\n", why);
40 if (tests != NO_PLAN) {
41 printf("1..%d\n", tests);
46 vok_at_loc (const char *file, int line, int test, const char *fmt,
49 char *name = vstrdupf(fmt, args);
50 printf("%sok %d", test ? "" : "not ", ++current_test);
52 printf(" - %s", name);
56 printf(" %s", todo_mesg);
61 diag(" Failed%s test '%s'\n at %s line %d.",
62 todo_mesg ? " (TODO)" : "", name, file, line);
64 diag(" Failed%s test at %s line %d.",
65 todo_mesg ? " (TODO)" : "", file, line);
74 ok_at_loc (const char *file, int line, int test, const char *fmt, ...) {
77 vok_at_loc(file, line, test, fmt, args);
83 mystrcmp (const char *a, const char *b) {
84 return a == b ? 0 : !a ? -1 : !b ? 1 : strcmp(a, b);
87 #define eq(a, b) (!mystrcmp(a, b))
88 #define ne(a, b) (mystrcmp(a, b))
91 is_at_loc (const char *file, int line, const char *got, const char *expected,
94 int test = eq(got, expected);
97 vok_at_loc(file, line, test, fmt, args);
100 diag(" got: '%s'", got);
101 diag(" expected: '%s'", expected);
107 isnt_at_loc (const char *file, int line, const char *got, const char *expected,
108 const char *fmt, ...)
110 int test = ne(got, expected);
113 vok_at_loc(file, line, test, fmt, args);
116 diag(" got: '%s'", got);
117 diag(" expected: anything else");
123 cmp_ok_at_loc (const char *file, int line, int a, const char *op, int b,
124 const char *fmt, ...)
126 int test = eq(op, "||") ? a || b
127 : eq(op, "&&") ? a && b
128 : eq(op, "|") ? a | b
129 : eq(op, "^") ? a ^ b
130 : eq(op, "&") ? a & b
131 : eq(op, "==") ? a == b
132 : eq(op, "!=") ? a != b
133 : eq(op, "<") ? a < b
134 : eq(op, ">") ? a > b
135 : eq(op, "<=") ? a <= b
136 : eq(op, ">=") ? a >= b
137 : eq(op, "<<") ? a << b
138 : eq(op, ">>") ? a >> b
139 : eq(op, "+") ? a + b
140 : eq(op, "-") ? a - b
141 : eq(op, "*") ? a * b
142 : eq(op, "/") ? a / b
143 : eq(op, "%") ? a % b
144 : diag("unrecognized operator '%s'", op);
147 vok_at_loc(file, line, test, fmt, args);
158 vdiag_to_fh (FILE *fh, const char *fmt, va_list args) {
163 mesg = vstrdupf(fmt, args);
165 for (i = 0; *line; i++) {
167 if (!c || c == '\n') {
169 fprintf(fh, "# %s\n", line);
180 diag (const char *fmt, ...) {
183 vdiag_to_fh(stderr, fmt, args);
189 note (const char *fmt, ...) {
192 vdiag_to_fh(stdout, fmt, args);
200 if (expected_tests == NO_PLAN) {
201 printf("1..%d\n", current_test);
203 else if (current_test != expected_tests) {
204 diag("Looks like you planned %d test%s but ran %d.",
205 expected_tests, expected_tests > 1 ? "s" : "", current_test);
209 diag("Looks like you failed %d test%s of %d run.",
210 failed_tests, failed_tests > 1 ? "s" : "", current_test);
211 if (expected_tests == NO_PLAN)
212 retval = failed_tests;
214 retval = expected_tests - current_test + failed_tests;
220 bail_out (int ignore, const char *fmt, ...) {
223 printf("Bail out! ");
232 skippy (int n, const char *fmt, ...) {
236 why = vstrdupf(fmt, args);
239 printf("ok %d ", ++current_test);
240 note("skip %s\n", why);
246 ctodo (int ignore, const char *fmt, ...) {
249 todo_mesg = vstrdupf(fmt, args);
260 #include <sys/mman.h>
264 #define MAP_ANONYMOUS MAP_ANON
267 /* Create a shared memory int to keep track of whether a piece of code executed
268 dies. to be used in the dies_ok and lives_ok macros */
270 tap_test_died (int status) {
271 static int *test_died = NULL;
274 test_died = mmap(0, sizeof (int), PROT_READ | PROT_WRITE,
275 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
284 like_at_loc (int for_match, const char *file, int line, const char *got,
285 const char *expected, const char *fmt, ...)
289 int err = regcomp(&re, expected, REG_EXTENDED);
292 regerror(err, &re, errbuf, sizeof errbuf);
293 fprintf(stderr, "Unable to compile regex '%s': %s at %s line %d\n",
294 expected, errbuf, file, line);
297 err = regexec(&re, got, 0, NULL, 0);
299 test = for_match ? !err : err;
302 vok_at_loc(file, line, test, fmt, args);
307 diag(" doesn't match: '%s'", expected);
311 diag(" matches: '%s'", expected);