]> begriffs open source - libtap/blob - tap.c
Adding pkgconfig file
[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     printf("ok %d", ++current_test);
65     if (*name)
66         printf(" - %s", name);
67     if (todo_mesg) {
68         printf(" # TODO");
69         if (*todo_mesg)
70             printf(" %s", todo_mesg);
71     }
72     printf("\n");
73     if (!test) {
74         fprintf(stderr, "#   Failed ");
75         if (todo_mesg)
76             fprintf(stderr, "(TODO) ");
77         fprintf(stderr, "test ");
78         if (*name)
79             fprintf(stderr, "'%s'\n#   ", name);
80         fprintf(stderr, "at %s line %d.\n", file, line);
81         if (!todo_mesg)
82             failed_tests++;
83     }
84     free(name);
85     return test;
86 }
87
88 int
89 ok_at_loc (const char *file, int line, int test, const char *fmt, ...) {
90     va_list args;
91     va_start(args, fmt);
92     vok_at_loc(file, line, test, fmt, args);
93     va_end(args);
94     return test;
95 }
96
97 static int
98 mystrcmp (const char *a, const char *b) {
99     return a == b ? 0 : !a ? -1 : !b ? 1 : strcmp(a, b);
100 }
101
102 #define eq(a, b) (!mystrcmp(a, b))
103 #define ne(a, b) (mystrcmp(a, b))
104
105 int
106 is_at_loc (const char *file, int line, const char *got, const char *expected,
107            const char *fmt, ...)
108 {
109     int test = eq(got, expected);
110     va_list args;
111     va_start(args, fmt);
112     vok_at_loc(file, line, test, fmt, args);
113     va_end(args);
114     if (!test) {
115         diag("         got: '%s'", got);
116         diag("    expected: '%s'", expected);
117     }
118     return test;
119 }
120
121 int
122 isnt_at_loc (const char *file, int line, const char *got, const char *expected,
123              const char *fmt, ...)
124 {
125     int test = ne(got, expected);
126     va_list args;
127     va_start(args, fmt);
128     vok_at_loc(file, line, test, fmt, args);
129     va_end(args);
130     if (!test) {
131         diag("         got: '%s'", got);
132         diag("    expected: anything else");
133     }
134     return test;
135 }
136
137 int
138 cmp_ok_at_loc (const char *file, int line, int a, const char *op, int b,
139                const char *fmt, ...)
140 {
141     int test = 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              : eq(op, "*")  ? a *  b
157              : eq(op, "/")  ? a /  b
158              : eq(op, "%")  ? a %  b
159              : diag("unrecognized operator '%s'", op);
160     va_list args;
161     va_start(args, fmt);
162     vok_at_loc(file, line, test, fmt, args);
163     va_end(args);
164     if (!test) {
165         diag("    %d", a);
166         diag("        %s", op);
167         diag("    %d", b);
168     }
169     return test;
170 }
171
172 static int
173 find_mem_diff (const char *a, const char *b, size_t n, size_t *offset) {
174     size_t i;
175     if (a == b)
176         return 0;
177     if (!a || !b)
178         return 2;
179     for (i = 0; i < n; i++) {
180         if (a[i] != b[i]) {
181             *offset = i;
182             return 1;
183         }
184     }
185     return 0;
186 }
187
188 int
189 cmp_mem_at_loc (const char *file, int line, const void *got,
190                 const void *expected, size_t n, const char *fmt, ...)
191 {
192     size_t offset;
193     int diff = find_mem_diff(got, expected, n, &offset);
194     va_list args;
195     va_start(args, fmt);
196     vok_at_loc(file, line, !diff, fmt, args);
197     va_end(args);
198     if (diff == 1) {
199         diag("    Difference starts at offset %d", offset);
200         diag("         got: 0x%02x", ((unsigned char *)got)[offset]);
201         diag("    expected: 0x%02x", ((unsigned char *)expected)[offset]);
202     }
203     else if (diff == 2) {
204         diag("         got: %s", got ? "not NULL" : "NULL");
205         diag("    expected: %s", expected ? "not NULL" : "NULL");
206     }
207     return !diff;
208 }
209
210 static void
211 vdiag_to_fh (FILE *fh, const char *fmt, va_list args) {
212     char *mesg, *line;
213     int i;
214     if (!fmt)
215         return;
216     mesg = vstrdupf(fmt, args);
217     line = mesg;
218     for (i = 0; *line; i++) {
219         char c = mesg[i];
220         if (!c || c == '\n') {
221             mesg[i] = '\0';
222             fprintf(fh, "# %s\n", line);
223             if (!c)
224                 break;
225             mesg[i] = c;
226             line = mesg + i + 1;
227         }
228     }
229     free(mesg);
230     return;
231 }
232
233 int
234 diag (const char *fmt, ...) {
235     va_list args;
236     va_start(args, fmt);
237     vdiag_to_fh(stderr, fmt, args);
238     va_end(args);
239     return 0;
240 }
241
242 int
243 note (const char *fmt, ...) {
244     va_list args;
245     va_start(args, fmt);
246     vdiag_to_fh(stdout, fmt, args);
247     va_end(args);
248     return 0;
249 }
250
251 int
252 exit_status () {
253     int retval = 0;
254     if (expected_tests == NO_PLAN) {
255         printf("1..%d\n", current_test);
256     }
257     else if (current_test != expected_tests) {
258         diag("Looks like you planned %d test%s but ran %d.",
259             expected_tests, expected_tests > 1 ? "s" : "", current_test);
260         retval = 2;
261     }
262     if (failed_tests) {
263         diag("Looks like you failed %d test%s of %d run.",
264             failed_tests, failed_tests > 1 ? "s" : "", current_test);
265         retval = 1;
266     }
267     return retval;
268 }
269
270 int
271 bail_out (int ignore, const char *fmt, ...) {
272     va_list args;
273     va_start(args, fmt);
274     printf("Bail out!  ");
275     vprintf(fmt, args);
276     printf("\n");
277     va_end(args);
278     exit(255);
279     return 0;
280 }
281
282 void
283 tap_skip (int n, const char *fmt, ...) {
284     char *why;
285     va_list args;
286     va_start(args, fmt);
287     why = vstrdupf(fmt, args);
288     va_end(args);
289     while (n --> 0) {
290         printf("ok %d ", ++current_test);
291         note("skip %s\n", why);
292     }
293     free(why);
294 }
295
296 void
297 tap_todo (int ignore, const char *fmt, ...) {
298     va_list args;
299     va_start(args, fmt);
300     todo_mesg = vstrdupf(fmt, args);
301     va_end(args);
302 }
303
304 void
305 tap_end_todo () {
306     free(todo_mesg);
307     todo_mesg = NULL;
308 }
309
310 #ifndef _WIN32
311 #include <sys/mman.h>
312 #include <sys/param.h>
313 #include <regex.h>
314
315 #if defined __APPLE__ || defined BSD
316 #define MAP_ANONYMOUS MAP_ANON
317 #endif
318
319 /* Create a shared memory int to keep track of whether a piece of code executed
320 dies. to be used in the dies_ok and lives_ok macros.  */
321 int
322 tap_test_died (int status) {
323     static int *test_died = NULL;
324     int prev;
325     if (!test_died) {
326         test_died = mmap(0, sizeof (int), PROT_READ | PROT_WRITE,
327                          MAP_SHARED | MAP_ANONYMOUS, -1, 0);
328         *test_died = 0;
329     }
330     prev = *test_died;
331     *test_died = status;
332     return prev;
333 }
334
335 int
336 like_at_loc (int for_match, const char *file, int line, const char *got,
337              const char *expected, const char *fmt, ...)
338 {
339     int test;
340     regex_t re;
341     va_list args;
342     int err = regcomp(&re, expected, REG_EXTENDED);
343     if (err) {
344         char errbuf[256];
345         regerror(err, &re, errbuf, sizeof errbuf);
346         fprintf(stderr, "Unable to compile regex '%s': %s at %s line %d\n",
347                         expected, errbuf, file, line);
348         exit(255);
349     }
350     err = regexec(&re, got, 0, NULL, 0);
351     regfree(&re);
352     test = for_match ? !err : err;
353     va_start(args, fmt);
354     vok_at_loc(file, line, test, fmt, args);
355     va_end(args);
356     if (!test) {
357         if (for_match) {
358             diag("                   '%s'", got);
359             diag("    doesn't match: '%s'", expected);
360         }
361         else {
362             diag("                   '%s'", got);
363             diag("          matches: '%s'", expected);
364         }
365     }
366     return test;
367 }
368 #endif
369