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);
71 is_at_loc (const char *file, int line, const char *got, const char *expected,
74 int test = !mystrcmp(got, expected);
77 vok_at_loc(file, line, test, fmt, args);
80 diag(" got: '%s'", got);
81 diag(" expected: '%s'", expected);
87 isnt_at_loc (const char *file, int line, const char *got, const char *expected,
90 int test = mystrcmp(got, expected);
93 vok_at_loc(file, line, test, fmt, args);
96 diag(" got: '%s'", got);
97 diag(" expected: anything else");
103 vdiag_to_fh (FILE *fh, const char *fmt, va_list args) {
\r
108 mesg = vstrdupf(fmt, args);
\r
110 for (i = 0; *line; i++) {
\r
112 if (!c || c == '\n') {
\r
114 fprintf(fh, "# %s\n", line);
\r
125 diag (const char *fmt, ...) {
128 vdiag_to_fh(stderr, fmt, args);
134 note (const char *fmt, ...) {
137 vdiag_to_fh(stdout, fmt, args);
145 if (expected_tests == NO_PLAN) {
\r
146 printf("1..%d\n", current_test);
\r
148 else if (current_test != expected_tests) {
\r
149 diag("Looks like you planned %d test%s but ran %d.",
\r
150 expected_tests, expected_tests > 1 ? "s" : "", current_test);
\r
153 if (failed_tests) {
\r
154 diag("Looks like you failed %d test%s of %d run.",
\r
155 failed_tests, failed_tests > 1 ? "s" : "", current_test);
156 if (expected_tests == NO_PLAN)
\r
157 retval = failed_tests;
159 retval = expected_tests - current_test + failed_tests;
\r
165 skippy (int n, const char *fmt, ...) {
169 why = vstrdupf(fmt, args);
172 printf("ok %d ", ++current_test);
173 note("skip %s\n", why);
179 ctodo (int ignore, const char *fmt, ...) {
182 todo_mesg = vstrdupf(fmt, args);
193 #include <sys/mman.h>
196 /* Create a shared memory int to keep track of whether a piece of code executed
197 dies. to be used in the dies_ok and lives_ok macros */
199 tap_test_died (int status) {
200 static int *test_died = NULL;
203 test_died = mmap(0, sizeof (int), PROT_READ | PROT_WRITE,
204 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
213 like_at_loc (int for_match, const char *file, int line, const char *got,
214 const char *expected, const char *fmt, ...)
218 int err = regcomp(&re, expected, REG_EXTENDED);
221 regerror(err, &re, errbuf, sizeof errbuf);
222 fprintf(stderr, "Unable to compile regex '%s': %s at %s line %d\n",
223 expected, errbuf, file, line);
226 err = regexec(&re, got, 0, NULL, 0);
228 test = for_match ? !err : err;
231 vok_at_loc(file, line, test, fmt, args);
236 diag(" doesn't match: '%s'", expected);
240 diag(" matches: '%s'", expected);