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);
187 diag (const char *fmt, ...) {
190 vdiag_to_fh(stderr, fmt, args);
196 note (const char *fmt, ...) {
199 vdiag_to_fh(stdout, fmt, args);
207 if (expected_tests == NO_PLAN) {
208 printf("1..%d\n", current_test);
210 else if (current_test != expected_tests) {
211 diag("Looks like you planned %d test%s but ran %d.",
212 expected_tests, expected_tests > 1 ? "s" : "", current_test);
216 diag("Looks like you failed %d test%s of %d run.",
217 failed_tests, failed_tests > 1 ? "s" : "", current_test);
218 if (expected_tests == NO_PLAN)
219 retval = failed_tests;
221 retval = expected_tests - current_test + failed_tests;
227 bail_out (int ignore, const char *fmt, ...) {
230 printf("Bail out! ");
239 skippy (int n, const char *fmt, ...) {
243 why = vstrdupf(fmt, args);
246 printf("ok %d ", ++current_test);
247 note("skip %s\n", why);
253 ctodo (int ignore, const char *fmt, ...) {
256 todo_mesg = vstrdupf(fmt, args);
267 #include <sys/mman.h>
271 #define MAP_ANONYMOUS MAP_ANON
274 /* Create a shared memory int to keep track of whether a piece of code executed
275 dies. to be used in the dies_ok and lives_ok macros */
277 tap_test_died (int status) {
278 static int *test_died = NULL;
281 test_died = mmap(0, sizeof (int), PROT_READ | PROT_WRITE,
282 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
291 like_at_loc (int for_match, const char *file, int line, const char *got,
292 const char *expected, const char *fmt, ...)
296 int err = regcomp(&re, expected, REG_EXTENDED);
299 regerror(err, &re, errbuf, sizeof errbuf);
300 fprintf(stderr, "Unable to compile regex '%s': %s at %s line %d\n",
301 expected, errbuf, file, line);
304 err = regexec(&re, got, 0, NULL, 0);
306 test = for_match ? !err : err;
309 vok_at_loc(file, line, test, fmt, args);
314 diag(" doesn't match: '%s'", expected);
318 diag(" matches: '%s'", expected);