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
16 #include <sys/types.h>
18 #include <sys/param.h>
23 #define MAP_ANONYMOUS MAP_ANON
25 #error "System does not support mapping anonymous pages"
30 static int expected_tests = NO_PLAN;
31 static int failed_tests;
32 static int current_test;
33 static char *todo_mesg;
36 vstrdupf (const char *fmt, va_list args) {
43 size = vsnprintf(NULL, 0, fmt, args2) + 2;
46 perror("malloc error");
49 vsprintf(str, fmt, args);
55 tap_plan (int tests, const char *fmt, ...) {
56 expected_tests = tests;
57 if (tests == SKIP_ALL) {
61 why = vstrdupf(fmt, args);
64 diag("SKIP %s\n", why);
67 if (tests != NO_PLAN) {
68 printf("1..%d\n", tests);
73 vok_at_loc (const char *file, int line, int test, const char *fmt,
76 char *name = vstrdupf(fmt, args);
80 printf("ok %d", ++current_test);
82 printf(" - %s", name);
86 printf(" %s", todo_mesg);
95 printf("'%s'\n# ", name);
96 printf("at %s line %d.\n", file, line);
105 ok_at_loc (const char *file, int line, int test, const char *fmt, ...) {
108 vok_at_loc(file, line, test, fmt, args);
114 mystrcmp (const char *a, const char *b) {
115 return a == b ? 0 : !a ? -1 : !b ? 1 : strcmp(a, b);
118 #define eq(a, b) (!mystrcmp(a, b))
119 #define ne(a, b) (mystrcmp(a, b))
122 is_at_loc (const char *file, int line, const char *got, const char *expected,
123 const char *fmt, ...)
125 int test = eq(got, expected);
128 vok_at_loc(file, line, test, fmt, args);
131 diag(" got: '%s'", got);
132 diag(" expected: '%s'", expected);
138 isnt_at_loc (const char *file, int line, const char *got, const char *expected,
139 const char *fmt, ...)
141 int test = ne(got, expected);
144 vok_at_loc(file, line, test, fmt, args);
147 diag(" got: '%s'", got);
148 diag(" expected: anything else");
154 cmp_ok_at_loc (const char *file, int line, int a, const char *op, int b,
155 const char *fmt, ...)
157 int test = eq(op, "||") ? a || b
158 : eq(op, "&&") ? a && b
159 : eq(op, "|") ? a | b
160 : eq(op, "^") ? a ^ b
161 : eq(op, "&") ? a & b
162 : eq(op, "==") ? a == b
163 : eq(op, "!=") ? a != b
164 : eq(op, "<") ? a < b
165 : eq(op, ">") ? a > b
166 : eq(op, "<=") ? a <= b
167 : eq(op, ">=") ? a >= b
168 : eq(op, "<<") ? a << b
169 : eq(op, ">>") ? a >> b
170 : eq(op, "+") ? a + b
171 : eq(op, "-") ? a - b
172 : eq(op, "*") ? a * b
173 : eq(op, "/") ? a / b
174 : eq(op, "%") ? a % b
175 : diag("unrecognized operator '%s'", op);
178 vok_at_loc(file, line, test, fmt, args);
189 find_mem_diff (const char *a, const char *b, size_t n, size_t *offset) {
195 for (i = 0; i < n; i++) {
205 cmp_mem_at_loc (const char *file, int line, const void *got,
206 const void *expected, size_t n, const char *fmt, ...)
209 int diff = find_mem_diff(got, expected, n, &offset);
212 vok_at_loc(file, line, !diff, fmt, args);
215 diag(" Difference starts at offset %d", offset);
216 diag(" got: 0x%02x", ((const unsigned char *)got)[offset]);
217 diag(" expected: 0x%02x", ((const unsigned char *)expected)[offset]);
219 else if (diff == 2) {
220 diag(" got: %s", got ? "not NULL" : "NULL");
221 diag(" expected: %s", expected ? "not NULL" : "NULL");
227 diag (const char *fmt, ...) {
236 mesg = vstrdupf(fmt, args);
238 for (i = 0; *line; i++) {
240 if (!c || c == '\n') {
242 printf("# %s\n", line);
257 if (expected_tests == NO_PLAN) {
258 printf("1..%d\n", current_test);
260 else if (current_test != expected_tests) {
261 diag("Looks like you planned %d test%s but ran %d.",
262 expected_tests, expected_tests > 1 ? "s" : "", current_test);
266 diag("Looks like you failed %d test%s of %d run.",
267 failed_tests, failed_tests > 1 ? "s" : "", current_test);
274 bail_out (int ignore, const char *fmt, ...) {
278 printf("Bail out! ");
287 tap_skip (int n, const char *fmt, ...) {
291 why = vstrdupf(fmt, args);
294 printf("ok %d ", ++current_test);
295 diag("skip %s\n", why);
301 tap_todo (int ignore, const char *fmt, ...) {
305 todo_mesg = vstrdupf(fmt, args);
316 /* Create a shared memory int to keep track of whether a piece of code executed
317 dies. to be used in the dies_ok and lives_ok macros. */
319 tap_test_died (int status) {
320 static int *test_died = NULL;
323 test_died = mmap(0, sizeof (int), PROT_READ | PROT_WRITE,
324 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
333 like_at_loc (int for_match, const char *file, int line, const char *got,
334 const char *expected, const char *fmt, ...)
339 int err = regcomp(&re, expected, REG_EXTENDED);
342 regerror(err, &re, errbuf, sizeof errbuf);
343 fprintf(stderr, "Unable to compile regex '%s': %s at %s line %d\n",
344 expected, errbuf, file, line);
347 err = regexec(&re, got, 0, NULL, 0);
349 test = for_match ? !err : err;
351 vok_at_loc(file, line, test, fmt, args);
356 diag(" doesn't match: '%s'", expected);
360 diag(" matches: '%s'", expected);