Affix
view release on metacpan or search on metacpan
infix/src/common/double_tap.h view on Meta::CPAN
// One-Time Initialization for TAP Header
#if defined(_WIN32) || defined(__CYGWIN__)
static INIT_ONCE g_tap_init_once = INIT_ONCE_STATIC_INIT;
static BOOL CALLBACK _tap_init_routine(PINIT_ONCE initOnce, PVOID param, PVOID * context) {
(void)initOnce;
(void)param;
(void)context;
printf("TAP version %d\n", TAP_VERSION);
fflush(stdout);
return TRUE;
}
#elif (defined(__unix__) || defined(__APPLE__)) && !defined(__OpenBSD__)
static pthread_once_t g_tap_init_once = PTHREAD_ONCE_INIT;
static void _tap_init_routine(void) {
printf("TAP version %d\n", TAP_VERSION);
fflush(stdout);
}
#else // OpenBSD or other platforms without robust pthread_once support in this context
static bool g_tap_initialized = false;
#endif
/**
* @internal
* @brief Ensures the TAP header has been printed and thread-local state is initialized.
* Uses `pthread_once` or `InitOnceExecuteOnce` to guarantee the TAP version header
* is printed exactly once per process, even with multiple threads. It also initializes
* the thread-local state for the current thread if it's the first test call on that thread.
*/
static void _tap_ensure_initialized(void) {
#if defined(_WIN32) || defined(__CYGWIN__)
InitOnceExecuteOnce(&g_tap_init_once, _tap_init_routine, NULL, NULL);
#elif (defined(__unix__) || defined(__APPLE__)) && !defined(__OpenBSD__)
pthread_once(&g_tap_init_once, _tap_init_routine);
#else
// Fallback for OpenBSD/single-threaded builds
if (!g_tap_initialized) {
printf("TAP version %d\n", TAP_VERSION);
fflush(stdout);
g_tap_initialized = true;
}
#endif
if (!current_state) {
current_state = &state_stack[0];
memset(current_state, 0, sizeof(tap_state_t));
current_state->plan = NO_PLAN;
}
}
// Internal Helper Functions
/** @internal Prints the indentation corresponding to the current subtest depth. */
static void print_indent(FILE * stream) {
_tap_ensure_initialized();
for (int i = 0; i < current_state->indent_level; ++i)
fprintf(stream, " ");
}
/** @internal Pushes a new state onto the thread-local stack for entering a subtest. */
static void push_state(void) {
if (current_state >= &state_stack[MAX_DEPTH - 1])
tap_bail_out("Exceeded maximum subtest depth of %d", MAX_DEPTH);
tap_state_t * parent = current_state;
current_state++;
memset(current_state, 0, sizeof(tap_state_t));
current_state->plan = NO_PLAN;
current_state->indent_level = parent->indent_level + 1;
// A subtest inherits the 'todo' state from its parent.
if (parent->todo) {
current_state->todo = true;
snprintf(current_state->todo_reason, sizeof(current_state->todo_reason), "%s", parent->todo_reason);
}
}
/** @internal Pops the current state from the stack when a subtest ends. */
static void pop_state(void) {
if (current_state <= &state_stack[0])
tap_bail_out("Internal error: Attempted to pop base test state");
current_state--;
}
// Public API Implementation
void tap_init(void) { _tap_ensure_initialized(); }
void tap_plan(size_t count) {
_tap_ensure_initialized();
if (current_state->has_plan || current_state->count > 0)
tap_bail_out("Plan declared after tests have run or a plan was already set");
current_state->plan = count;
current_state->has_plan = true;
print_indent(stdout);
printf("1..%llu\n", (unsigned long long)count);
fflush(stdout);
}
bool tap_ok(bool condition, const char * file, int line, const char * func, const char * expr, const char * name, ...) {
_tap_ensure_initialized();
if (current_state->skipping) {
current_state->count++;
return true;
}
char name_buffer[256] = {0};
if (name && name[0] != '\0') {
va_list args;
va_start(args, name);
vsnprintf(name_buffer, sizeof(name_buffer), name, args);
va_end(args);
}
current_state->count++;
if (!condition) {
if (current_state->todo)
current_state->failed_todo++;
else {
current_state->failed++;
if (current_state == &state_stack[0]) // Only increment global fail count for top-level tests
TAP_ATOMIC_FETCH_ADD(&g_total_failed, 1);
}
}
print_indent(stdout);
printf("%s %llu", condition ? "ok" : "not ok", (unsigned long long)current_state->count);
if (name_buffer[0] != '\0')
printf(" - %s", name_buffer);
if (current_state->todo)
printf(" # TODO %s", current_state->todo_reason);
printf("\n");
if (!condition && !current_state->todo) {
// Print detailed diagnostics in YAML block format on failure.
print_indent(stdout);
fprintf(stdout, "#\n");
print_indent(stdout);
fprintf(stdout, "# message: 'Test failed'\n");
infix/src/common/double_tap.h view on Meta::CPAN
current_state->todo_reason[0] = '\0';
}
void diag(const char * fmt, ...) {
_tap_ensure_initialized();
char buffer[1024];
va_list args;
va_start(args, fmt);
vsnprintf(buffer, sizeof(buffer), fmt, args);
va_end(args);
print_indent(stderr);
fprintf(stderr, "# %s\n", buffer);
fflush(stderr);
}
void tap_note(const char * fmt, ...) {
_tap_ensure_initialized();
char buffer[1024];
va_list args;
va_start(args, fmt);
vsnprintf(buffer, sizeof(buffer), fmt, args);
va_end(args);
print_indent(stdout);
fprintf(stdout, "# %s\n", buffer);
fflush(stdout);
}
void tap_bail_out(const char * reason, ...) {
fprintf(stderr, "Bail out! ");
va_list args;
va_start(args, reason);
vfprintf(stderr, reason, args);
va_end(args);
fprintf(stderr, "\n");
fflush(stderr);
exit(1);
}
bool tap_subtest_start(const char * name) {
_tap_ensure_initialized();
print_indent(stdout);
fprintf(stdout, "# Subtest: %s\n", name);
fflush(stdout);
push_state();
snprintf(current_state->subtest_name, sizeof(current_state->subtest_name), "%s", name);
return true; // Enters the `for` loop body.
}
bool tap_subtest_end(void) {
_tap_ensure_initialized();
if (!current_state->has_plan) {
// If no plan was declared, implicitly plan for the number of tests that ran.
current_state->plan = current_state->count;
print_indent(stdout);
printf("1..%llu\n", (unsigned long long)current_state->plan);
}
bool plan_ok = (current_state->plan == current_state->count);
bool subtest_ok = (current_state->failed == 0) && plan_ok;
char name_buffer[256];
snprintf(name_buffer, sizeof(name_buffer), "%s", current_state->subtest_name);
pop_state(); // Return to the parent's state.
// Report the subtest's success or failure as a single test point in the parent scope.
ok(subtest_ok, "%s", name_buffer);
return false; // Exits the `for` loop.
}
int tap_done(void) {
_tap_ensure_initialized();
if (current_state != &state_stack[0])
tap_bail_out("tap_done() called inside a subtest");
if (!current_state->has_plan) {
current_state->plan = current_state->count;
print_indent(stdout);
printf("1..%llu\n", (unsigned long long)current_state->plan);
fflush(stdout);
}
if (current_state->skipping) {
print_indent(stdout);
printf("1..%llu # SKIP %s\n", (unsigned long long)current_state->plan, current_state->skip_reason);
fflush(stdout);
return 0;
}
if (current_state->plan != current_state->count)
fail("Test plan adherence (planned %llu, but ran %llu)",
(unsigned long long)current_state->plan,
(unsigned long long)current_state->count);
size_t final_failed_count = (size_t)TAP_ATOMIC_FETCH_ADD(&g_total_failed, 0);
if (final_failed_count > 0)
diag("Looks like you failed %llu out of %llu tests.",
(unsigned long long)final_failed_count,
(unsigned long long)current_state->plan);
return (int)final_failed_count;
}
// The main test runner that gets compiled into the test executable.
int main(void) {
tap_init();
test_body();
int result = tap_done();
#if defined(_WIN32) && !defined(__clang__)
return result;
#else
// Use _exit() to bypass standard atexit() cleanup which can segfault on some
// platforms when TLS or coverage profiling is involved.
_exit(result);
#endif
}
#endif // DBLTAP_ENABLE && DBLTAP_IMPLEMENTATION
/** @endinternal */
( run in 0.696 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )