2 libtap - Write tests in C
3 Copyright (C) 2011 Jake Gelbman <gelbman@gmail.com>
4 This file is licensed under the GPL v3
13 static int expected_tests = NO_PLAN;
14 static int failed_tests;
15 static int current_test;
16 static char *todo_mesg;
19 vstrdupf (const char *fmt, va_list args) {
26 size = vsnprintf(NULL, 0, fmt, args2) + 2;
28 vsprintf(str, fmt, args);
34 cplan (int tests, const char *fmt, ...) {
35 expected_tests = tests;
36 if (tests == SKIP_ALL) {
40 why = vstrdupf(fmt, args);
43 note("SKIP %s\n", why);
46 if (tests != NO_PLAN) {
47 printf("1..%d\n", tests);
52 vok_at_loc (const char *file, int line, int test, const char *fmt,
55 char *name = vstrdupf(fmt, args);
56 printf("%sok %d", test ? "" : "not ", ++current_test);
58 printf(" - %s", name);
62 printf(" %s", todo_mesg);
67 diag(" Failed%s test '%s'\n at %s line %d.",
68 todo_mesg ? " (TODO)" : "", name, file, line);
70 diag(" Failed%s test at %s line %d.",
71 todo_mesg ? " (TODO)" : "", file, line);
80 ok_at_loc (const char *file, int line, int test, const char *fmt, ...) {
83 vok_at_loc(file, line, test, fmt, args);
89 mystrcmp (const char *a, const char *b) {
90 return a == b ? 0 : !a ? -1 : !b ? 1 : strcmp(a, b);
93 #define eq(a, b) (!mystrcmp(a, b))
94 #define ne(a, b) (mystrcmp(a, b))
97 is_at_loc (const char *file, int line, const char *got, const char *expected,
100 int test = eq(got, expected);
103 vok_at_loc(file, line, test, fmt, args);
106 diag(" got: '%s'", got);
107 diag(" expected: '%s'", expected);
113 isnt_at_loc (const char *file, int line, const char *got, const char *expected,
114 const char *fmt, ...)
116 int test = ne(got, expected);
119 vok_at_loc(file, line, test, fmt, args);
122 diag(" got: '%s'", got);
123 diag(" expected: anything else");
129 cmp_ok_at_loc (const char *file, int line, int a, const char *op, int b,
130 const char *fmt, ...)
132 int test = 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 : eq(op, ">>") ? a >> b
145 : eq(op, "+") ? a + b
146 : eq(op, "-") ? a - b
147 : eq(op, "*") ? a * b
148 : eq(op, "/") ? a / b
149 : eq(op, "%") ? a % b
150 : diag("unrecognized operator '%s'", op);
153 vok_at_loc(file, line, test, fmt, args);
164 vdiag_to_fh (FILE *fh, const char *fmt, va_list args) {
169 mesg = vstrdupf(fmt, args);
171 for (i = 0; *line; i++) {
173 if (!c || c == '\n') {
175 fprintf(fh, "# %s\n", line);
186 diag (const char *fmt, ...) {
189 vdiag_to_fh(stderr, fmt, args);
195 note (const char *fmt, ...) {
198 vdiag_to_fh(stdout, fmt, args);
206 if (expected_tests == NO_PLAN) {
207 printf("1..%d\n", current_test);
209 else if (current_test != expected_tests) {
210 diag("Looks like you planned %d test%s but ran %d.",
211 expected_tests, expected_tests > 1 ? "s" : "", current_test);
215 diag("Looks like you failed %d test%s of %d run.",
216 failed_tests, failed_tests > 1 ? "s" : "", current_test);
217 if (expected_tests == NO_PLAN)
218 retval = failed_tests;
220 retval = expected_tests - current_test + failed_tests;
226 bail_out (int ignore, const char *fmt, ...) {
229 printf("Bail out! ");
238 skippy (int n, const char *fmt, ...) {
242 why = vstrdupf(fmt, args);
245 printf("ok %d ", ++current_test);
246 note("skip %s\n", why);
252 ctodo (int ignore, const char *fmt, ...) {
255 todo_mesg = vstrdupf(fmt, args);
266 #include <sys/mman.h>
270 #define MAP_ANONYMOUS MAP_ANON
273 /* Create a shared memory int to keep track of whether a piece of code executed
274 dies. to be used in the dies_ok and lives_ok macros */
276 tap_test_died (int status) {
277 static int *test_died = NULL;
280 test_died = mmap(0, sizeof (int), PROT_READ | PROT_WRITE,
281 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
290 like_at_loc (int for_match, const char *file, int line, const char *got,
291 const char *expected, const char *fmt, ...)
295 int err = regcomp(&re, expected, REG_EXTENDED);
298 regerror(err, &re, errbuf, sizeof errbuf);
299 fprintf(stderr, "Unable to compile regex '%s': %s at %s line %d\n",
300 expected, errbuf, file, line);
303 err = regexec(&re, got, 0, NULL, 0);
305 test = for_match ? !err : err;
308 vok_at_loc(file, line, test, fmt, args);
313 diag(" doesn't match: '%s'", expected);
317 diag(" matches: '%s'", expected);