50#if defined(__unix__) || defined(__APPLE__) || defined(__OpenBSD__)
53#if defined(_WIN32) || defined(__CYGWIN__)
55#elif (defined(__unix__) || defined(__APPLE__)) && !defined(__OpenBSD__)
61#if defined(__cplusplus)
70#if defined(__cplusplus)
71#define TAP_ATOMIC_SIZE_T std::atomic<size_t>
72#define TAP_ATOMIC_FETCH_ADD(ptr, val) std::atomic_fetch_add(ptr, (size_t)(val))
73#define TAP_ATOMIC_INIT(val) (val)
74#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_ATOMICS__)
76#define TAP_ATOMIC_SIZE_T _Atomic size_t
77#define TAP_ATOMIC_FETCH_ADD(ptr, val) atomic_fetch_add(ptr, val)
78#define TAP_ATOMIC_INIT(val) = val
79#elif defined(__GNUC__) || defined(__clang__)
80#define TAP_ATOMIC_SIZE_T size_t
81#define TAP_ATOMIC_FETCH_ADD(ptr, val) __sync_fetch_and_add(ptr, val)
82#define TAP_ATOMIC_INIT(val) = val
85#define TAP_ATOMIC_SIZE_T size_t
86#define TAP_ATOMIC_FETCH_ADD(ptr, val) ((*ptr) += (val))
87#define TAP_ATOMIC_INIT(val) = val
89#warning "Compiler does not support C11 atomics or GCC builtins; global counters will not be thread-safe."
93#if defined(__OpenBSD__)
96#define TAP_THREAD_LOCAL
97#elif defined(__cplusplus)
98#define TAP_THREAD_LOCAL thread_local
99#elif defined(_MSC_VER)
101#define TAP_THREAD_LOCAL __declspec(thread)
102#elif defined(_WIN32) && defined(__clang__)
104#define TAP_THREAD_LOCAL __declspec(thread)
105#elif defined(__GNUC__)
107#define TAP_THREAD_LOCAL __thread
108#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_THREADS__)
109#define TAP_THREAD_LOCAL _Thread_local
111#define TAP_THREAD_LOCAL
112#if !defined(_MSC_VER)
113#warning "Compiler does not support thread-local storage; tests will not be thread-safe."
117#if defined(__GNUC__) || defined(__clang__)
118#define DBLTAP_NOINLINE __attribute__((noinline))
119#elif defined(_MSC_VER)
120#define DBLTAP_NOINLINE __declspec(noinline)
122#define DBLTAP_NOINLINE
126#if defined(__GNUC__) || defined(__clang__)
127#define DBLTAP_PRINTF_FORMAT(fmt_index, arg_index) __attribute__((format(printf, fmt_index, arg_index)))
129#define DBLTAP_PRINTF_FORMAT(fmt_index, arg_index)
134void tap_plan(
size_t count);
136void tap_bail_out(
const char * reason, ...) DBLTAP_PRINTF_FORMAT(1, 2);
137bool tap_ok(
bool condition, const
char * file,
int line, const
char * func, const
char * expr, const
char * name, ...)
138 DBLTAP_PRINTF_FORMAT(6, 7);
139bool tap_subtest_start(const
char * name);
140bool tap_subtest_end(
void);
141void tap_todo_start(const
char * reason, ...) DBLTAP_PRINTF_FORMAT(1, 2);
142void tap_todo_end(
void);
143void tap_skip(
size_t count, const
char * reason, ...) DBLTAP_PRINTF_FORMAT(2, 3);
144void tap_skip_all(const
char * reason, ...) DBLTAP_PRINTF_FORMAT(1, 2);
145void diag(const
char * fmt, ...) DBLTAP_PRINTF_FORMAT(1, 2);
146void tap_note(const
char * fmt, ...) DBLTAP_PRINTF_FORMAT(1, 2);
155#define plan(count) tap_plan(count)
157#define done() tap_done()
159#define bail_out(...) tap_bail_out(__VA_ARGS__)
162#define ok(cond, ...) tap_ok(!!(cond), __FILE__, __LINE__, __func__, #cond, __VA_ARGS__)
164#define pass(...) ok(true, __VA_ARGS__)
166#define fail(...) ok(false, __VA_ARGS__)
168#define subtest(name) \
169 for (bool _tap_subtest_once = tap_subtest_start(name); _tap_subtest_once; _tap_subtest_once = tap_subtest_end())
171#define skip(count, ...) tap_skip(count, __VA_ARGS__)
173#define skip_all(...) tap_skip_all(__VA_ARGS__)
176#define TODO(reason) \
177 for (int _tap_todo_once = (tap_todo_start(reason), 1); _tap_todo_once; _tap_todo_once = (tap_todo_end(), 0))
179#define diag(...) diag(__VA_ARGS__)
182#define note(...) tap_note(__VA_ARGS__)
186#define TEST extern "C" void test_body(void)
188#define TEST void test_body(void)
192#define plan(count) ((void)0)
194#define bail_out(...) \
196 fprintf(stderr, "Bail out! "); \
197 fprintf(stderr, __VA_ARGS__); \
198 fprintf(stderr, "\n"); \
201#define ok(cond, ...) (true)
202#define pass(...) ((void)0)
203#define fail(...) ((void)0)
204#define subtest(name) if (0)
205#define skip(count, ...) ((void)0)
206#define skip_all(...) ((void)0)
207#define TODO(reason, ...) if (0)
208#define diag(...) ((void)0)
210#define note(...) ((void)0)
213 int main(void) { return 0; }
216#if defined(DBLTAP_ENABLE) && defined(DBLTAP_IMPLEMENTATION)
234 char subtest_name[256];
235 char todo_reason[256];
236 char skip_reason[256];
240#define NO_PLAN ((size_t)-1)
243static TAP_THREAD_LOCAL tap_state_t state_stack[MAX_DEPTH];
245static TAP_THREAD_LOCAL tap_state_t * current_state = NULL;
247static TAP_ATOMIC_SIZE_T g_total_failed TAP_ATOMIC_INIT(0);
250#if defined(_WIN32) || defined(__CYGWIN__)
251static INIT_ONCE g_tap_init_once = INIT_ONCE_STATIC_INIT;
252static BOOL CALLBACK _tap_init_routine(PINIT_ONCE initOnce, PVOID param, PVOID * context) {
256 printf(
"TAP version %d\n", TAP_VERSION);
260#elif (defined(__unix__) || defined(__APPLE__)) && !defined(__OpenBSD__)
261static pthread_once_t g_tap_init_once = PTHREAD_ONCE_INIT;
262static void _tap_init_routine(
void) {
263 printf(
"TAP version %d\n", TAP_VERSION);
267static bool g_tap_initialized =
false;
277static void _tap_ensure_initialized(
void) {
278#if defined(_WIN32) || defined(__CYGWIN__)
279 InitOnceExecuteOnce(&g_tap_init_once, _tap_init_routine, NULL, NULL);
280#elif (defined(__unix__) || defined(__APPLE__)) && !defined(__OpenBSD__)
281 pthread_once(&g_tap_init_once, _tap_init_routine);
284 if (!g_tap_initialized) {
285 printf(
"TAP version %d\n", TAP_VERSION);
287 g_tap_initialized =
true;
290 if (!current_state) {
291 current_state = &state_stack[0];
292 memset(current_state, 0,
sizeof(tap_state_t));
293 current_state->plan = NO_PLAN;
299static void print_indent(FILE * stream) {
300 _tap_ensure_initialized();
301 for (
int i = 0; i < current_state->indent_level; ++i)
302 fprintf(stream,
" ");
306static void push_state(
void) {
307 if (current_state >= &state_stack[MAX_DEPTH - 1])
308 tap_bail_out(
"Exceeded maximum subtest depth of %d", MAX_DEPTH);
309 tap_state_t * parent = current_state;
311 memset(current_state, 0,
sizeof(tap_state_t));
312 current_state->plan = NO_PLAN;
313 current_state->indent_level = parent->indent_level + 1;
316 current_state->todo =
true;
317 snprintf(current_state->todo_reason,
sizeof(current_state->todo_reason),
"%s", parent->todo_reason);
322static void pop_state(
void) {
323 if (current_state <= &state_stack[0])
324 tap_bail_out(
"Internal error: Attempted to pop base test state");
329void tap_init(
void) { _tap_ensure_initialized(); }
331void tap_plan(
size_t count) {
332 _tap_ensure_initialized();
333 if (current_state->has_plan || current_state->count > 0)
334 tap_bail_out(
"Plan declared after tests have run or a plan was already set");
335 current_state->plan = count;
336 current_state->has_plan =
true;
337 print_indent(stdout);
338 printf(
"1..%llu\n", (
unsigned long long)count);
342bool tap_ok(
bool condition,
const char * file,
int line,
const char * func,
const char * expr,
const char * name, ...) {
343 _tap_ensure_initialized();
344 if (current_state->skipping) {
345 current_state->count++;
348 char name_buffer[256] = {0};
349 if (name && name[0] !=
'\0') {
351 va_start(
args, name);
352 vsnprintf(name_buffer,
sizeof(name_buffer), name,
args);
355 current_state->count++;
357 if (current_state->todo)
358 current_state->failed_todo++;
360 current_state->failed++;
361 if (current_state == &state_stack[0])
362 TAP_ATOMIC_FETCH_ADD(&g_total_failed, 1);
365 print_indent(stdout);
366 printf(
"%s %llu", condition ?
"ok" :
"not ok", (unsigned long long)current_state->count);
367 if (name_buffer[0] !=
'\0')
368 printf(
" - %s", name_buffer);
369 if (current_state->todo)
370 printf(
" # TODO %s", current_state->todo_reason);
372 if (!condition && !current_state->todo) {
374 print_indent(stdout);
375 fprintf(stdout,
"#\n");
376 print_indent(stdout);
377 fprintf(stdout,
"# message: 'Test failed'\n");
378 print_indent(stdout);
379 fprintf(stdout,
"# severity: fail\n");
380 print_indent(stdout);
381 fprintf(stdout,
"# data:\n");
382 print_indent(stdout);
383 fprintf(stdout,
"# file: %s\n", file);
384 print_indent(stdout);
385 fprintf(stdout,
"# line: %d\n", line);
386 print_indent(stdout);
387 fprintf(stdout,
"# function: %s\n", func);
388 print_indent(stdout);
389 fprintf(stdout,
"# expression: '%s'\n", expr);
390 print_indent(stdout);
391 fprintf(stdout,
"# ...\n");
397void tap_skip(
size_t count,
const char * reason, ...) {
398 _tap_ensure_initialized();
401 va_start(
args, reason);
402 vsnprintf(buffer,
sizeof(buffer), reason,
args);
404 for (
size_t i = 0; i < count; ++i) {
405 current_state->count++;
406 print_indent(stdout);
407 printf(
"ok %llu # SKIP %s\n", (
unsigned long long)current_state->count, buffer);
412void tap_skip_all(
const char * reason, ...) {
413 _tap_ensure_initialized();
414 current_state->skipping =
true;
416 va_start(
args, reason);
417 vsnprintf(current_state->skip_reason,
sizeof(current_state->skip_reason), reason,
args);
421void tap_todo_start(
const char * reason, ...) {
422 _tap_ensure_initialized();
423 current_state->todo =
true;
425 va_start(
args, reason);
426 vsnprintf(current_state->todo_reason,
sizeof(current_state->todo_reason), reason,
args);
430void tap_todo_end(
void) {
431 _tap_ensure_initialized();
432 current_state->todo =
false;
433 current_state->todo_reason[0] =
'\0';
436void diag(
const char * fmt, ...) {
437 _tap_ensure_initialized();
441 vsnprintf(buffer,
sizeof(buffer), fmt,
args);
443 print_indent(stderr);
444 fprintf(stderr,
"# %s\n", buffer);
448void tap_note(
const char * fmt, ...) {
449 _tap_ensure_initialized();
453 vsnprintf(buffer,
sizeof(buffer), fmt,
args);
455 print_indent(stdout);
456 fprintf(stdout,
"# %s\n", buffer);
460void tap_bail_out(
const char * reason, ...) {
461 fprintf(stderr,
"Bail out! ");
463 va_start(
args, reason);
464 vfprintf(stderr, reason,
args);
466 fprintf(stderr,
"\n");
471bool tap_subtest_start(
const char * name) {
472 _tap_ensure_initialized();
473 print_indent(stdout);
474 fprintf(stdout,
"# Subtest: %s\n", name);
477 snprintf(current_state->subtest_name,
sizeof(current_state->subtest_name),
"%s", name);
481bool tap_subtest_end(
void) {
482 _tap_ensure_initialized();
483 if (!current_state->has_plan) {
485 current_state->plan = current_state->count;
486 print_indent(stdout);
487 printf(
"1..%llu\n", (
unsigned long long)current_state->plan);
489 bool plan_ok = (current_state->plan == current_state->count);
490 bool subtest_ok = (current_state->failed == 0) && plan_ok;
491 char name_buffer[256];
492 snprintf(name_buffer,
sizeof(name_buffer),
"%s", current_state->subtest_name);
495 ok(subtest_ok,
"%s", name_buffer);
500 _tap_ensure_initialized();
501 if (current_state != &state_stack[0])
502 tap_bail_out(
"tap_done() called inside a subtest");
503 if (!current_state->has_plan) {
504 current_state->plan = current_state->count;
505 print_indent(stdout);
506 printf(
"1..%llu\n", (
unsigned long long)current_state->plan);
509 if (current_state->skipping) {
510 print_indent(stdout);
511 printf(
"1..%llu # SKIP %s\n", (
unsigned long long)current_state->plan, current_state->skip_reason);
515 if (current_state->plan != current_state->count)
516 fail(
"Test plan adherence (planned %llu, but ran %llu)",
517 (
unsigned long long)current_state->plan,
518 (
unsigned long long)current_state->count);
519 size_t final_failed_count = (size_t)TAP_ATOMIC_FETCH_ADD(&g_total_failed, 0);
520 if (final_failed_count > 0)
521 diag(
"Looks like you failed %llu out of %llu tests.",
522 (
unsigned long long)final_failed_count,
523 (
unsigned long long)current_state->plan);
524 return (
int)final_failed_count;
531 int result = tap_done();
532#if defined(__OpenBSD__) || (defined(_WIN32) && defined(__clang__))
void * args[]
Definition 202_in_structs.c:59
int main(void)
Definition 821_threading_bare.c:71
#define plan(count)
Definition double_tap.h:192
#define diag(...)
Definition double_tap.h:208
#define ok(cond,...)
Definition double_tap.h:201
#define fail(...)
Definition double_tap.h:203