7 static int expected_tests = NO_PLAN;
\r
8 static int failed_tests;
\r
9 static int current_test;
10 static char *todo_mesg;
\r
14 expected_tests = tests;
15 if (tests != NO_PLAN)
\r
16 printf("1..%d\n", tests);
\r
20 vstrdupf (const char *fmt, va_list args) {
26 int size = vsnprintf(NULL, 0, fmt, args2) + 2;
28 vsprintf(str, fmt, args);
34 vok_at_loc (const char *file, int line, int test, const char *fmt,
37 char *name = vstrdupf(fmt, args);
38 printf("%sok %d", test ? "" : "not ", ++current_test);
40 printf(" - %s", name);
44 printf(" %s", todo_mesg);
49 diag(" Failed%s test '%s'\n at %s line %d.",
50 todo_mesg ? " (TODO)" : "", name, file, line);
\r
52 diag(" Failed%s test at %s line %d.",
53 todo_mesg ? " (TODO)" : "", file, line);
62 ok_at_loc (const char *file, int line, int test, const char *fmt, ...) {
65 vok_at_loc(file, line, test, fmt, args);
71 mystrcmp (const char *a, const char *b) {
72 return a == b ? 0 : !a ? -1 : !b ? 1 : strcmp(a, b);
75 #define eq(a, b) (!mystrcmp(a, b))
76 #define ne(a, b) (mystrcmp(a, b))
79 is_at_loc (const char *file, int line, const char *got, const char *expected,
82 int test = eq(got, expected);
85 vok_at_loc(file, line, test, fmt, args);
88 diag(" got: '%s'", got);
89 diag(" expected: '%s'", expected);
95 isnt_at_loc (const char *file, int line, const char *got, const char *expected,
98 int test = ne(got, expected);
101 vok_at_loc(file, line, test, fmt, args);
104 diag(" got: '%s'", got);
105 diag(" expected: anything else");
111 cmp_ok_at_loc (const char *file, int line, int a, const char *op, int b,
112 const char *fmt, ...)
114 int test = eq(op, "||") ? a || b
115 : 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 : diag("unrecognized operator '%s'", op);
135 vok_at_loc(file, line, test, fmt, args);
146 vdiag_to_fh (FILE *fh, const char *fmt, va_list args) {
\r
151 mesg = vstrdupf(fmt, args);
\r
153 for (i = 0; *line; i++) {
\r
155 if (!c || c == '\n') {
\r
157 fprintf(fh, "# %s\n", line);
\r
168 diag (const char *fmt, ...) {
171 vdiag_to_fh(stderr, fmt, args);
177 note (const char *fmt, ...) {
180 vdiag_to_fh(stdout, fmt, args);
188 if (expected_tests == NO_PLAN) {
\r
189 printf("1..%d\n", current_test);
\r
191 else if (current_test != expected_tests) {
\r
192 diag("Looks like you planned %d test%s but ran %d.",
\r
193 expected_tests, expected_tests > 1 ? "s" : "", current_test);
\r
196 if (failed_tests) {
\r
197 diag("Looks like you failed %d test%s of %d run.",
\r
198 failed_tests, failed_tests > 1 ? "s" : "", current_test);
199 if (expected_tests == NO_PLAN)
\r
200 retval = failed_tests;
202 retval = expected_tests - current_test + failed_tests;
\r
208 skippy (int n, const char *fmt, ...) {
212 why = vstrdupf(fmt, args);
215 printf("ok %d ", ++current_test);
216 note("skip %s\n", why);
222 ctodo (int ignore, const char *fmt, ...) {
225 todo_mesg = vstrdupf(fmt, args);
236 #include <sys/mman.h>
240 #define MAP_ANONYMOUS MAP_ANON
243 /* Create a shared memory int to keep track of whether a piece of code executed
244 dies. to be used in the dies_ok and lives_ok macros */
246 tap_test_died (int status) {
247 static int *test_died = NULL;
250 test_died = mmap(0, sizeof (int), PROT_READ | PROT_WRITE,
251 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
260 like_at_loc (int for_match, const char *file, int line, const char *got,
261 const char *expected, const char *fmt, ...)
265 int err = regcomp(&re, expected, REG_EXTENDED);
268 regerror(err, &re, errbuf, sizeof errbuf);
269 fprintf(stderr, "Unable to compile regex '%s': %s at %s line %d\n",
270 expected, errbuf, file, line);
273 err = regexec(&re, got, 0, NULL, 0);
275 test = for_match ? !err : err;
278 vok_at_loc(file, line, test, fmt, args);
283 diag(" doesn't match: '%s'", expected);
287 diag(" matches: '%s'", expected);