7 static int expected_tests = NO_PLAN;
8 static int failed_tests;
9 static int current_test;
10 static char *todo_mesg;
14 expected_tests = tests;
16 printf("1..%d\n", tests);
20 vstrdupf (const char *fmt, va_list args) {
27 size = vsnprintf(NULL, 0, fmt, args2) + 2;
29 vsprintf(str, fmt, args);
35 vok_at_loc (const char *file, int line, int test, const char *fmt,
38 char *name = vstrdupf(fmt, args);
39 printf("%sok %d", test ? "" : "not ", ++current_test);
41 printf(" - %s", name);
45 printf(" %s", todo_mesg);
50 diag(" Failed%s test '%s'\n at %s line %d.",
51 todo_mesg ? " (TODO)" : "", name, file, line);
53 diag(" Failed%s test at %s line %d.",
54 todo_mesg ? " (TODO)" : "", file, line);
63 ok_at_loc (const char *file, int line, int test, const char *fmt, ...) {
66 vok_at_loc(file, line, test, fmt, args);
72 mystrcmp (const char *a, const char *b) {
73 return a == b ? 0 : !a ? -1 : !b ? 1 : strcmp(a, b);
76 #define eq(a, b) (!mystrcmp(a, b))
77 #define ne(a, b) (mystrcmp(a, b))
80 is_at_loc (const char *file, int line, const char *got, const char *expected,
83 int test = eq(got, expected);
86 vok_at_loc(file, line, test, fmt, args);
89 diag(" got: '%s'", got);
90 diag(" expected: '%s'", expected);
96 isnt_at_loc (const char *file, int line, const char *got, const char *expected,
99 int test = ne(got, expected);
102 vok_at_loc(file, line, test, fmt, args);
105 diag(" got: '%s'", got);
106 diag(" expected: anything else");
112 cmp_ok_at_loc (const char *file, int line, int a, const char *op, int b,
113 const char *fmt, ...)
115 int test = eq(op, "||") ? a || b
116 : eq(op, "&&") ? a && b
117 : eq(op, "|") ? a | b
118 : eq(op, "^") ? a ^ b
119 : eq(op, "&") ? a & b
120 : eq(op, "==") ? a == b
121 : eq(op, "!=") ? a != b
122 : eq(op, "<") ? a < b
123 : eq(op, ">") ? a > b
124 : eq(op, "<=") ? a <= b
125 : eq(op, ">=") ? a >= b
126 : 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 : diag("unrecognized operator '%s'", op);
136 vok_at_loc(file, line, test, fmt, args);
147 vdiag_to_fh (FILE *fh, const char *fmt, va_list args) {
152 mesg = vstrdupf(fmt, args);
154 for (i = 0; *line; i++) {
156 if (!c || c == '\n') {
158 fprintf(fh, "# %s\n", line);
169 diag (const char *fmt, ...) {
172 vdiag_to_fh(stderr, fmt, args);
178 note (const char *fmt, ...) {
181 vdiag_to_fh(stdout, fmt, args);
189 if (expected_tests == NO_PLAN) {
190 printf("1..%d\n", current_test);
192 else if (current_test != expected_tests) {
193 diag("Looks like you planned %d test%s but ran %d.",
194 expected_tests, expected_tests > 1 ? "s" : "", current_test);
198 diag("Looks like you failed %d test%s of %d run.",
199 failed_tests, failed_tests > 1 ? "s" : "", current_test);
200 if (expected_tests == NO_PLAN)
201 retval = failed_tests;
203 retval = expected_tests - current_test + failed_tests;
209 skippy (int n, const char *fmt, ...) {
213 why = vstrdupf(fmt, args);
216 printf("ok %d ", ++current_test);
217 note("skip %s\n", why);
223 ctodo (int ignore, const char *fmt, ...) {
226 todo_mesg = vstrdupf(fmt, args);
237 #include <sys/mman.h>
241 #define MAP_ANONYMOUS MAP_ANON
244 /* Create a shared memory int to keep track of whether a piece of code executed
245 dies. to be used in the dies_ok and lives_ok macros */
247 tap_test_died (int status) {
248 static int *test_died = NULL;
251 test_died = mmap(0, sizeof (int), PROT_READ | PROT_WRITE,
252 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
261 like_at_loc (int for_match, const char *file, int line, const char *got,
262 const char *expected, const char *fmt, ...)
266 int err = regcomp(&re, expected, REG_EXTENDED);
269 regerror(err, &re, errbuf, sizeof errbuf);
270 fprintf(stderr, "Unable to compile regex '%s': %s at %s line %d\n",
271 expected, errbuf, file, line);
274 err = regexec(&re, got, 0, NULL, 0);
276 test = for_match ? !err : err;
279 vok_at_loc(file, line, test, fmt, args);
284 diag(" doesn't match: '%s'", expected);
288 diag(" matches: '%s'", expected);