7#define _DEFAULT_SOURCE 1
15static int expected_tests = NO_PLAN;
16static int failed_tests;
17static int current_test;
18static char *todo_mesg;
21vstrdupf (
const char *fmt, va_list args) {
28 size = vsnprintf(NULL, 0, fmt, args2) + 2;
31 perror(
"malloc error");
34 vsprintf(str, fmt, args);
40tap_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);
58vok_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);
90ok_at_loc (
const char *file,
int line,
int test,
const char *fmt, ...) {
93 vok_at_loc(file, line, test, fmt, args);
99mystrcmp (
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))
107is_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);
123isnt_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");
139cmp_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);
174find_mem_diff (
const char *a,
const char *b,
size_t n,
size_t *offset) {
180 for (i = 0; i < n; i++) {
190cmp_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");
212diag (
const char *fmt, ...) {
221 mesg = vstrdupf(fmt, args);
223 for (i = 0; *line; i++) {
225 if (!c || c ==
'\n') {
227 printf(
"# %s\n", line);
242 if (expected_tests == NO_PLAN) {
243 printf(
"1..%d\n", current_test);
245 else if (current_test != expected_tests) {
246 diag(
"Looks like you planned %d test%s but ran %d.",
247 expected_tests, expected_tests > 1 ?
"s" :
"", current_test);
251 diag(
"Looks like you failed %d test%s of %d run.",
252 failed_tests, failed_tests > 1 ?
"s" :
"", current_test);
259bail_out (
int ignore,
const char *fmt, ...) {
263 printf(
"Bail out! ");
272tap_skip (
int n,
const char *fmt, ...) {
276 why = vstrdupf(fmt, args);
279 printf(
"ok %d ", ++current_test);
280 diag(
"skip %s\n", why);
286tap_todo (
int ignore,
const char *fmt, ...) {
290 todo_mesg = vstrdupf(fmt, args);
300#if !defined(_WIN32) && !defined(_EE)
302#include <sys/param.h>
307#define MAP_ANONYMOUS MAP_ANON
309#error "System does not support mapping anonymous pages"
316tap_test_died (
int status) {
317 static int *test_died = NULL;
320 test_died = mmap(0,
sizeof (
int), PROT_READ | PROT_WRITE,
321 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
330like_at_loc (
int for_match,
const char *file,
int line,
const char *got,
331 const char *expected,
const char *fmt, ...)
336 int err = regcomp(&re, expected, REG_EXTENDED);
339 regerror(err, &re, errbuf,
sizeof errbuf);
340 fprintf(stderr,
"Unable to compile regex '%s': %s at %s line %d\n",
341 expected, errbuf, file, line);
344 err = regexec(&re, got, 0, NULL, 0);
346 test = for_match ? !err : err;
348 vok_at_loc(file, line, test, fmt, args);
353 diag(
" doesn't match: '%s'", expected);
357 diag(
" matches: '%s'", expected);