6 static int expected_tests = NO_PLAN;
\r
7 static int failed_tests;
\r
8 static int current_test;
\r
10 void plan (int tests) {
\r
11 expected_tests = tests;
\r
12 if (tests != NO_PLAN)
\r
13 printf("1..%d\n", tests);
\r
16 static char *vstrdupf (const char *fmt, va_list args) {
18 int size = vsnprintf(NULL, 0, fmt, args) + 1;
20 vsprintf(str, fmt, args);
24 int ok_at_loc (const char *file, int line, int test, const char *fmt, ...) {
30 name = vstrdupf(fmt, args);
\r
34 printf("%sok %d%s%s\n",
42 diag(" Failed test '%s'\n at %s line %d.", name, file, line);
\r
44 diag(" Failed test at %s line %d.", file, line);
\r
54 static void diag_to_fh_v (FILE *fh, const char *fmt, va_list args) {
\r
60 mesg = vstrdupf(fmt, args);
\r
63 for (i = 0; *line; i++) {
\r
65 if (!c || c == '\n') {
\r
67 fprintf(fh, "# %s\n", line);
\r
79 int diag (const char *fmt, ...) {
82 diag_to_fh_v(stderr, fmt, args);
87 int note (const char *fmt, ...) {
90 diag_to_fh_v(stdout, fmt, args);
95 int exit_status () {
\r
96 int retval = EXIT_SUCCESS;
\r
97 if (expected_tests == NO_PLAN) {
\r
98 printf("1..%d\n", current_test);
\r
100 else if (current_test != expected_tests) {
\r
101 diag("Looks like you planned %d test%s but ran %d.",
\r
102 expected_tests, expected_tests > 1 ? "s" : "", current_test);
\r
103 retval = EXIT_FAILURE;
\r
105 if (failed_tests) {
\r
106 diag("Looks like you failed %d test%s of %d run.",
\r
107 failed_tests, failed_tests > 1 ? "s" : "", current_test);
\r
108 retval = EXIT_FAILURE;
\r
114 #include <sys/mman.h>
115 /* Create a shared memory int to keep track of whether a piece of code
116 executed dies. to be used in the dies_ok and lives_ok macros */
117 int tap_test_died (int status) {
118 static int *test_died = NULL;
122 test_died = mmap(0, sizeof (int), PROT_READ | PROT_WRITE,
123 MAP_SHARED | MAP_ANONYMOUS, -1, 0);