6 static int expected_tests = NO_PLAN;
\r
7 static int failed_tests;
\r
8 static int current_test;
9 static char *todo_mesg = NULL;
\r
11 void plan (int tests) {
\r
12 expected_tests = tests;
\r
13 if (tests != NO_PLAN)
\r
14 printf("1..%d\n", tests);
\r
17 static char *vstrdupf (const char *fmt, va_list args) {
19 int size = vsnprintf(NULL, 0, fmt, args) + 1;
21 vsprintf(str, fmt, args);
25 int ok_at_loc (const char *file, int line, int test, const char *fmt, ...) {
30 name = vstrdupf(fmt, args);
\r
33 printf("%sok %d", test ? "" : "not ", ++current_test);
35 printf(" - %s", name);
39 printf(" %s", todo_mesg);
44 diag(" Failed%s test '%s'\n at %s line %d.",
45 todo_mesg ? " (TODO)" : "", name, file, line);
\r
47 diag(" Failed%s test at %s line %d.",
48 todo_mesg ? " (TODO)" : "", file, line);
57 static void vdiag_to_fh (FILE *fh, const char *fmt, va_list args) {
\r
62 mesg = vstrdupf(fmt, args);
\r
64 for (i = 0; *line; i++) {
\r
66 if (!c || c == '\n') {
\r
68 fprintf(fh, "# %s\n", line);
\r
78 int diag (const char *fmt, ...) {
81 vdiag_to_fh(stderr, fmt, args);
86 int note (const char *fmt, ...) {
89 vdiag_to_fh(stdout, fmt, args);
94 int exit_status () {
\r
95 int retval = EXIT_SUCCESS;
\r
96 if (expected_tests == NO_PLAN) {
\r
97 printf("1..%d\n", current_test);
\r
99 else if (current_test != expected_tests) {
\r
100 diag("Looks like you planned %d test%s but ran %d.",
\r
101 expected_tests, expected_tests > 1 ? "s" : "", current_test);
\r
102 retval = EXIT_FAILURE;
\r
104 if (failed_tests) {
\r
105 diag("Looks like you failed %d test%s of %d run.",
\r
106 failed_tests, failed_tests > 1 ? "s" : "", current_test);
\r
107 retval = EXIT_FAILURE;
\r
112 void skippy (int n, const char *fmt, ...) {
116 why = vstrdupf(fmt, args);
119 printf("ok %d ", ++current_test);
120 note("skip %s\n", why);
125 void ctodo (int ignore, const char *fmt, ...) {
128 todo_mesg = vstrdupf(fmt, args);
138 # include <sys/mman.h>
139 /* Create a shared memory int to keep track of whether a piece of code
140 executed dies. to be used in the dies_ok and lives_ok macros */
141 int tap_test_died (int status) {
142 static int *test_died = NULL;
145 test_died = mmap(0, sizeof (int), PROT_READ | PROT_WRITE,
146 MAP_SHARED | MAP_ANONYMOUS, -1, 0);