2 libtap - Write tests in C
3 Copyright 2012 Jake Gelbman <gelbman@gmail.com>
4 This file is licensed under the GPLv2
15 static int expected_tests = NO_PLAN;
16 static int failed_tests;
17 static int current_test;
18 static char *todo_mesg;
21 vstrdupf (const char *fmt, va_list args) {
28 size = vsnprintf(NULL, 0, fmt, args2) + 2;
31 perror("malloc error");
34 vsprintf(str, fmt, args);
40 tap_plan (int tests, const char *fmt, ...) {
41 expected_tests = tests;
42 if (tests == SKIP_ALL) {
46 why = vstrdupf(fmt, args);
49 note("SKIP %s\n", why);
52 if (tests != NO_PLAN) {
53 printf("1..%d\n", tests);
58 vok_at_loc (const char *file, int line, int test, const char *fmt,
61 char *name = vstrdupf(fmt, args);
62 printf("%sok %d", test ? "" : "not ", ++current_test);
64 printf(" - %s", name);
68 printf(" %s", todo_mesg);
73 diag(" Failed%s test '%s'\n at %s line %d.",
74 todo_mesg ? " (TODO)" : "", name, file, line);
76 diag(" Failed%s test at %s line %d.",
77 todo_mesg ? " (TODO)" : "", file, line);
86 ok_at_loc (const char *file, int line, int test, const char *fmt, ...) {
89 vok_at_loc(file, line, test, fmt, args);
95 mystrcmp (const char *a, const char *b) {
96 return a == b ? 0 : !a ? -1 : !b ? 1 : strcmp(a, b);
99 #define eq(a, b) (!mystrcmp(a, b))
100 #define ne(a, b) (mystrcmp(a, b))
103 is_at_loc (const char *file, int line, const char *got, const char *expected,
104 const char *fmt, ...)
106 int test = eq(got, expected);
109 vok_at_loc(file, line, test, fmt, args);
112 diag(" got: '%s'", got);
113 diag(" expected: '%s'", expected);
119 isnt_at_loc (const char *file, int line, const char *got, const char *expected,
120 const char *fmt, ...)
122 int test = ne(got, expected);
125 vok_at_loc(file, line, test, fmt, args);
128 diag(" got: '%s'", got);
129 diag(" expected: anything else");
135 cmp_ok_at_loc (const char *file, int line, int a, const char *op, int b,
136 const char *fmt, ...)
138 int test = 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 : eq(op, ">>") ? a >> b
151 : eq(op, "+") ? a + b
152 : eq(op, "-") ? a - b
153 : eq(op, "*") ? a * b
154 : eq(op, "/") ? a / b
155 : eq(op, "%") ? a % b
156 : diag("unrecognized operator '%s'", op);
159 vok_at_loc(file, line, test, fmt, args);
170 vdiag_to_fh (FILE *fh, const char *fmt, va_list args) {
175 mesg = vstrdupf(fmt, args);
177 for (i = 0; *line; i++) {
179 if (!c || c == '\n') {
181 fprintf(fh, "# %s\n", line);
193 diag (const char *fmt, ...) {
196 vdiag_to_fh(stderr, fmt, args);
202 note (const char *fmt, ...) {
205 vdiag_to_fh(stdout, fmt, args);
213 if (expected_tests == NO_PLAN) {
214 printf("1..%d\n", current_test);
216 else if (current_test != expected_tests) {
217 diag("Looks like you planned %d test%s but ran %d.",
218 expected_tests, expected_tests > 1 ? "s" : "", current_test);
222 diag("Looks like you failed %d test%s of %d run.",
223 failed_tests, failed_tests > 1 ? "s" : "", current_test);
224 if (expected_tests == NO_PLAN)
225 retval = failed_tests;
227 retval = expected_tests - current_test + failed_tests;
233 bail_out (int ignore, const char *fmt, ...) {
236 printf("Bail out! ");
245 tap_skip (int n, const char *fmt, ...) {
249 why = vstrdupf(fmt, args);
252 printf("ok %d ", ++current_test);
253 note("skip %s\n", why);
259 tap_todo (int ignore, const char *fmt, ...) {
262 todo_mesg = vstrdupf(fmt, args);
273 #include <sys/mman.h>
274 #include <sys/param.h>
277 #if defined __APPLE__ || defined BSD
278 #define MAP_ANONYMOUS MAP_ANON
281 /* Create a shared memory int to keep track of whether a piece of code executed
282 dies. to be used in the dies_ok and lives_ok macros. */
284 tap_test_died (int status) {
285 static int *test_died = NULL;
288 test_died = mmap(0, sizeof (int), PROT_READ | PROT_WRITE,
289 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
298 like_at_loc (int for_match, const char *file, int line, const char *got,
299 const char *expected, const char *fmt, ...)
304 int err = regcomp(&re, expected, REG_EXTENDED);
307 regerror(err, &re, errbuf, sizeof errbuf);
308 fprintf(stderr, "Unable to compile regex '%s': %s at %s line %d\n",
309 expected, errbuf, file, line);
312 err = regexec(&re, got, 0, NULL, 0);
314 test = for_match ? !err : err;
316 vok_at_loc(file, line, test, fmt, args);
321 diag(" doesn't match: '%s'", expected);
325 diag(" matches: '%s'", expected);