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) {
22 int size = vsnprintf(NULL, 0, fmt, args) + 1;
24 vsprintf(str, fmt, args);
29 vok_at_loc (const char *file, int line, int test, const char *fmt,
32 char *name = vstrdupf(fmt, args);
33 printf("%sok %d", test ? "" : "not ", ++current_test);
35 printf(" - %s", name);
39 printf(" %s", todo_mesg);
44 diag(" Failed%s test '%s'\n at %s line %d.",
45 todo_mesg ? " (TODO)" : "", name, file, line);
\r
47 diag(" Failed%s test at %s line %d.",
48 todo_mesg ? " (TODO)" : "", file, line);
57 ok_at_loc (const char *file, int line, int test, const char *fmt, ...) {
60 vok_at_loc(file, line, test, fmt, args);
66 mystrcmp (const char *a, const char *b) {
67 return a == b ? 0 : !a ? -1 : !b ? 1 : strcmp(a, b);
70 #define eq(a, b) (!mystrcmp(a, b))
71 #define ne(a, b) (mystrcmp(a, b))
74 is_at_loc (const char *file, int line, const char *got, const char *expected,
77 int test = eq(got, expected);
80 vok_at_loc(file, line, test, fmt, args);
83 diag(" got: '%s'", got);
84 diag(" expected: '%s'", expected);
90 isnt_at_loc (const char *file, int line, const char *got, const char *expected,
93 int test = ne(got, expected);
96 vok_at_loc(file, line, test, fmt, args);
99 diag(" got: '%s'", got);
100 diag(" expected: anything else");
106 cmp_ok_at_loc (const char *file, int line, int a, const char *op, int b,
107 const char *fmt, ...)
109 int test = eq(op, "||") ? a || b
110 : eq(op, "&&") ? a && b
111 : eq(op, "|") ? a | b
112 : eq(op, "^") ? a ^ b
113 : eq(op, "&") ? a & b
114 : 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 : diag("unrecognized operator '%s'", op);
130 vok_at_loc(file, line, test, fmt, args);
141 vdiag_to_fh (FILE *fh, const char *fmt, va_list args) {
\r
146 mesg = vstrdupf(fmt, args);
\r
148 for (i = 0; *line; i++) {
\r
150 if (!c || c == '\n') {
\r
152 fprintf(fh, "# %s\n", line);
\r
163 diag (const char *fmt, ...) {
166 vdiag_to_fh(stderr, fmt, args);
172 note (const char *fmt, ...) {
175 vdiag_to_fh(stdout, fmt, args);
183 if (expected_tests == NO_PLAN) {
\r
184 printf("1..%d\n", current_test);
\r
186 else if (current_test != expected_tests) {
\r
187 diag("Looks like you planned %d test%s but ran %d.",
\r
188 expected_tests, expected_tests > 1 ? "s" : "", current_test);
\r
191 if (failed_tests) {
\r
192 diag("Looks like you failed %d test%s of %d run.",
\r
193 failed_tests, failed_tests > 1 ? "s" : "", current_test);
194 if (expected_tests == NO_PLAN)
\r
195 retval = failed_tests;
197 retval = expected_tests - current_test + failed_tests;
\r
203 skippy (int n, const char *fmt, ...) {
207 why = vstrdupf(fmt, args);
210 printf("ok %d ", ++current_test);
211 note("skip %s\n", why);
217 ctodo (int ignore, const char *fmt, ...) {
220 todo_mesg = vstrdupf(fmt, args);
231 #include <sys/mman.h>
234 /* Create a shared memory int to keep track of whether a piece of code executed
235 dies. to be used in the dies_ok and lives_ok macros */
237 tap_test_died (int status) {
238 static int *test_died = NULL;
241 test_died = mmap(0, sizeof (int), PROT_READ | PROT_WRITE,
242 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
251 like_at_loc (int for_match, const char *file, int line, const char *got,
252 const char *expected, const char *fmt, ...)
256 int err = regcomp(&re, expected, REG_EXTENDED);
259 regerror(err, &re, errbuf, sizeof errbuf);
260 fprintf(stderr, "Unable to compile regex '%s': %s at %s line %d\n",
261 expected, errbuf, file, line);
264 err = regexec(&re, got, 0, NULL, 0);
266 test = for_match ? !err : err;
269 vok_at_loc(file, line, test, fmt, args);
274 diag(" doesn't match: '%s'", expected);
278 diag(" matches: '%s'", expected);