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