]> begriffs open source - libtap/blob - tap.c
Rely on prove (or another tap consumer) to provide color results
[libtap] / tap.c
1 /*
2 libtap - Write tests in C
3 Copyright 2012 Jake Gelbman <gelbman@gmail.com>
4 This file is licensed under the LGPL
5 */
6
7 #define _DEFAULT_SOURCE 1
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <stdarg.h>
12 #include <string.h>
13 #include "tap.h"
14
15 static int expected_tests = NO_PLAN;
16 static int failed_tests;
17 static int current_test;
18 static char *todo_mesg;
19
20 static char *
21 vstrdupf (const char *fmt, va_list args) {
22     char *str;
23     int size;
24     va_list args2;
25     va_copy(args2, args);
26     if (!fmt)
27         fmt = "";
28     size = vsnprintf(NULL, 0, fmt, args2) + 2;
29     str = malloc(size);
30     if (!str) {
31         perror("malloc error");
32         exit(1);
33     }
34     vsprintf(str, fmt, args);
35     va_end(args2);
36     return str;
37 }
38
39 void
40 tap_plan (int tests, const char *fmt, ...) {
41     expected_tests = tests;
42     if (tests == SKIP_ALL) {
43         char *why;
44         va_list args;
45         va_start(args, fmt);
46         why = vstrdupf(fmt, args);
47         va_end(args);
48         printf("1..0 ");
49         note("SKIP %s\n", why);
50         exit(0);
51     }
52     if (tests != NO_PLAN) {
53         printf("1..%d\n", tests);
54     }
55 }
56
57 int
58 vok_at_loc (const char *file, int line, int test, const char *fmt,
59             va_list args)
60 {
61     char *name = vstrdupf(fmt, args);
62     if (!test) {
63         printf("not ");
64     }
65     printf("ok %d", ++current_test);
66     if (*name)
67         printf(" - %s", name);
68     if (todo_mesg) {
69         printf(" # TODO");
70         if (*todo_mesg)
71             printf(" %s", todo_mesg);
72     }
73     printf("\n");
74     if (!test) {
75         fprintf(stderr, "#   Failed ");
76         if (todo_mesg)
77             fprintf(stderr, "(TODO) ");
78         fprintf(stderr, "test ");
79         if (*name)
80             fprintf(stderr, "'%s'\n#   ", name);
81         fprintf(stderr, "at %s line %d.\n", file, line);
82         if (!todo_mesg)
83             failed_tests++;
84     }
85     free(name);
86     return test;
87 }
88
89 int
90 ok_at_loc (const char *file, int line, int test, const char *fmt, ...) {
91     va_list args;
92     va_start(args, fmt);
93     vok_at_loc(file, line, test, fmt, args);
94     va_end(args);
95     return test;
96 }
97
98 static int
99 mystrcmp (const char *a, const char *b) {
100     return a == b ? 0 : !a ? -1 : !b ? 1 : strcmp(a, b);
101 }
102
103 #define eq(a, b) (!mystrcmp(a, b))
104 #define ne(a, b) (mystrcmp(a, b))
105
106 int
107 is_at_loc (const char *file, int line, const char *got, const char *expected,
108            const char *fmt, ...)
109 {
110     int test = eq(got, expected);
111     va_list args;
112     va_start(args, fmt);
113     vok_at_loc(file, line, test, fmt, args);
114     va_end(args);
115     if (!test) {
116         diag("         got: '%s'", got);
117         diag("    expected: '%s'", expected);
118     }
119     return test;
120 }
121
122 int
123 isnt_at_loc (const char *file, int line, const char *got, const char *expected,
124              const char *fmt, ...)
125 {
126     int test = ne(got, expected);
127     va_list args;
128     va_start(args, fmt);
129     vok_at_loc(file, line, test, fmt, args);
130     va_end(args);
131     if (!test) {
132         diag("         got: '%s'", got);
133         diag("    expected: anything else");
134     }
135     return test;
136 }
137
138 int
139 cmp_ok_at_loc (const char *file, int line, int a, const char *op, int b,
140                const char *fmt, ...)
141 {
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);
161     va_list args;
162     va_start(args, fmt);
163     vok_at_loc(file, line, test, fmt, args);
164     va_end(args);
165     if (!test) {
166         diag("    %d", a);
167         diag("        %s", op);
168         diag("    %d", b);
169     }
170     return test;
171 }
172
173 static int
174 find_mem_diff (const char *a, const char *b, size_t n, size_t *offset) {
175     size_t i;
176     if (a == b)
177         return 0;
178     if (!a || !b)
179         return 2;
180     for (i = 0; i < n; i++) {
181         if (a[i] != b[i]) {
182             *offset = i;
183             return 1;
184         }
185     }
186     return 0;
187 }
188
189 int
190 cmp_mem_at_loc (const char *file, int line, const void *got,
191                 const void *expected, size_t n, const char *fmt, ...)
192 {
193     size_t offset;
194     int diff = find_mem_diff(got, expected, n, &offset);
195     va_list args;
196     va_start(args, fmt);
197     vok_at_loc(file, line, !diff, fmt, args);
198     va_end(args);
199     if (diff == 1) {
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]);
203     }
204     else if (diff == 2) {
205         diag("         got: %s", got ? "not NULL" : "NULL");
206         diag("    expected: %s", expected ? "not NULL" : "NULL");
207     }
208     return !diff;
209 }
210
211 static void
212 vdiag_to_fh (FILE *fh, const char *fmt, va_list args) {
213     char *mesg, *line;
214     int i;
215     if (!fmt)
216         return;
217     mesg = vstrdupf(fmt, args);
218     line = mesg;
219     for (i = 0; *line; i++) {
220         char c = mesg[i];
221         if (!c || c == '\n') {
222             mesg[i] = '\0';
223             fprintf(fh, "# %s\n", line);
224             if (!c)
225                 break;
226             mesg[i] = c;
227             line = mesg + i + 1;
228         }
229     }
230     free(mesg);
231     return;
232 }
233
234 int
235 diag (const char *fmt, ...) {
236     va_list args;
237     va_start(args, fmt);
238     vdiag_to_fh(stderr, fmt, args);
239     va_end(args);
240     return 0;
241 }
242
243 int
244 note (const char *fmt, ...) {
245     va_list args;
246     va_start(args, fmt);
247     vdiag_to_fh(stdout, fmt, args);
248     va_end(args);
249     return 0;
250 }
251
252 int
253 exit_status () {
254     int retval = 0;
255     if (expected_tests == NO_PLAN) {
256         printf("1..%d\n", current_test);
257     }
258     else if (current_test != expected_tests) {
259         diag("Looks like you planned %d test%s but ran %d.",
260             expected_tests, expected_tests > 1 ? "s" : "", current_test);
261         retval = 2;
262     }
263     if (failed_tests) {
264         diag("Looks like you failed %d test%s of %d run.",
265             failed_tests, failed_tests > 1 ? "s" : "", current_test);
266         retval = 1;
267     }
268     return retval;
269 }
270
271 int
272 bail_out (int ignore, const char *fmt, ...) {
273     va_list args;
274     va_start(args, fmt);
275     printf("Bail out!  ");
276     vprintf(fmt, args);
277     printf("\n");
278     va_end(args);
279     exit(255);
280     return 0;
281 }
282
283 void
284 tap_skip (int n, const char *fmt, ...) {
285     char *why;
286     va_list args;
287     va_start(args, fmt);
288     why = vstrdupf(fmt, args);
289     va_end(args);
290     while (n --> 0) {
291         printf("ok %d ", ++current_test);
292         note("skip %s\n", why);
293     }
294     free(why);
295 }
296
297 void
298 tap_todo (int ignore, const char *fmt, ...) {
299     va_list args;
300     va_start(args, fmt);
301     todo_mesg = vstrdupf(fmt, args);
302     va_end(args);
303 }
304
305 void
306 tap_end_todo () {
307     free(todo_mesg);
308     todo_mesg = NULL;
309 }
310
311 #ifndef _WIN32
312 #include <sys/mman.h>
313 #include <sys/param.h>
314 #include <regex.h>
315
316 #if defined __APPLE__ || defined BSD
317 #define MAP_ANONYMOUS MAP_ANON
318 #endif
319
320 /* Create a shared memory int to keep track of whether a piece of code executed
321 dies. to be used in the dies_ok and lives_ok macros.  */
322 int
323 tap_test_died (int status) {
324     static int *test_died = NULL;
325     int prev;
326     if (!test_died) {
327         test_died = mmap(0, sizeof (int), PROT_READ | PROT_WRITE,
328                          MAP_SHARED | MAP_ANONYMOUS, -1, 0);
329         *test_died = 0;
330     }
331     prev = *test_died;
332     *test_died = status;
333     return prev;
334 }
335
336 int
337 like_at_loc (int for_match, const char *file, int line, const char *got,
338              const char *expected, const char *fmt, ...)
339 {
340     int test;
341     regex_t re;
342     va_list args;
343     int err = regcomp(&re, expected, REG_EXTENDED);
344     if (err) {
345         char errbuf[256];
346         regerror(err, &re, errbuf, sizeof errbuf);
347         fprintf(stderr, "Unable to compile regex '%s': %s at %s line %d\n",
348                         expected, errbuf, file, line);
349         exit(255);
350     }
351     err = regexec(&re, got, 0, NULL, 0);
352     regfree(&re);
353     test = for_match ? !err : err;
354     va_start(args, fmt);
355     vok_at_loc(file, line, test, fmt, args);
356     va_end(args);
357     if (!test) {
358         if (for_match) {
359             diag("                   '%s'", got);
360             diag("    doesn't match: '%s'", expected);
361         }
362         else {
363             diag("                   '%s'", got);
364             diag("          matches: '%s'", expected);
365         }
366     }
367     return test;
368 }
369 #endif