2 libtap - Write tests in C
3 Copyright 2012 Jake Gelbman <gelbman@gmail.com>
4 This file is licensed under the LGPL
7 #define _DEFAULT_SOURCE 1
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 diag("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);
65 printf("ok %d", ++current_test);
67 printf(" - %s", name);
71 printf(" %s", todo_mesg);
80 printf("'%s'\n# ", name);
81 printf("at %s line %d.\n", file, line);
90 ok_at_loc (const char *file, int line, int test, const char *fmt, ...) {
93 vok_at_loc(file, line, test, fmt, args);
99 mystrcmp (const char *a, const char *b) {
100 return a == b ? 0 : !a ? -1 : !b ? 1 : strcmp(a, b);
103 #define eq(a, b) (!mystrcmp(a, b))
104 #define ne(a, b) (mystrcmp(a, b))
107 is_at_loc (const char *file, int line, const char *got, const char *expected,
108 const char *fmt, ...)
110 int test = eq(got, expected);
113 vok_at_loc(file, line, test, fmt, args);
116 diag(" got: '%s'", got);
117 diag(" expected: '%s'", expected);
123 isnt_at_loc (const char *file, int line, const char *got, const char *expected,
124 const char *fmt, ...)
126 int test = ne(got, expected);
129 vok_at_loc(file, line, test, fmt, args);
132 diag(" got: '%s'", got);
133 diag(" expected: anything else");
139 cmp_ok_at_loc (const char *file, int line, int a, const char *op, int b,
140 const char *fmt, ...)
142 int test = 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 : eq(op, "-") ? a - b
157 : eq(op, "*") ? a * b
158 : eq(op, "/") ? a / b
159 : eq(op, "%") ? a % b
160 : diag("unrecognized operator '%s'", op);
163 vok_at_loc(file, line, test, fmt, args);
174 find_mem_diff (const char *a, const char *b, size_t n, size_t *offset) {
180 for (i = 0; i < n; i++) {
190 cmp_mem_at_loc (const char *file, int line, const void *got,
191 const void *expected, size_t n, const char *fmt, ...)
194 int diff = find_mem_diff(got, expected, n, &offset);
197 vok_at_loc(file, line, !diff, fmt, args);
200 diag(" Difference starts at offset %d", offset);
201 diag(" got: 0x%02x", ((unsigned char *)got)[offset]);
202 diag(" expected: 0x%02x", ((unsigned char *)expected)[offset]);
204 else if (diff == 2) {
205 diag(" got: %s", got ? "not NULL" : "NULL");
206 diag(" expected: %s", expected ? "not NULL" : "NULL");
212 diag (const char *fmt, ...) {
219 mesg = vstrdupf(fmt, args);
221 for (i = 0; *line; i++) {
223 if (!c || c == '\n') {
225 printf("# %s\n", line);
240 if (expected_tests == NO_PLAN) {
241 printf("1..%d\n", current_test);
243 else if (current_test != expected_tests) {
244 diag("Looks like you planned %d test%s but ran %d.",
245 expected_tests, expected_tests > 1 ? "s" : "", current_test);
249 diag("Looks like you failed %d test%s of %d run.",
250 failed_tests, failed_tests > 1 ? "s" : "", current_test);
257 bail_out (int ignore, const char *fmt, ...) {
260 printf("Bail out! ");
269 tap_skip (int n, const char *fmt, ...) {
273 why = vstrdupf(fmt, args);
276 printf("ok %d ", ++current_test);
277 diag("skip %s\n", why);
283 tap_todo (int ignore, const char *fmt, ...) {
286 todo_mesg = vstrdupf(fmt, args);
297 #include <sys/mman.h>
298 #include <sys/param.h>
301 #ifndef MAP_ANONYMOUS
303 #define MAP_ANONYMOUS MAP_ANON
305 #error "System does not support mapping anonymous pages"
309 /* Create a shared memory int to keep track of whether a piece of code executed
310 dies. to be used in the dies_ok and lives_ok macros. */
312 tap_test_died (int status) {
313 static int *test_died = NULL;
316 test_died = mmap(0, sizeof (int), PROT_READ | PROT_WRITE,
317 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
326 like_at_loc (int for_match, const char *file, int line, const char *got,
327 const char *expected, const char *fmt, ...)
332 int err = regcomp(&re, expected, REG_EXTENDED);
335 regerror(err, &re, errbuf, sizeof errbuf);
336 fprintf(stderr, "Unable to compile regex '%s': %s at %s line %d\n",
337 expected, errbuf, file, line);
340 err = regexec(&re, got, 0, NULL, 0);
342 test = for_match ? !err : err;
344 vok_at_loc(file, line, test, fmt, args);
349 diag(" doesn't match: '%s'", expected);
353 diag(" matches: '%s'", expected);