Single-header testing framework with pretty-printed output.
More...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
Go to the source code of this file.
|
| #define | TR_RESET (_tr_use_colors() ? "\x1b[0m" : "") |
| #define | TR_BOLD (_tr_use_colors() ? "\x1b[1m" : "") |
| #define | TR_DIM (_tr_use_colors() ? "\x1b[2m" : "") |
| #define | TR_GREEN (_tr_use_colors() ? "\x1b[32m" : "") |
| #define | TR_RED (_tr_use_colors() ? "\x1b[31m" : "") |
| #define | TR_YELLOW (_tr_use_colors() ? "\x1b[33m" : "") |
| #define | TR_WHITE (_tr_use_colors() ? "\x1b[97m" : "") |
| #define | TR_MAX_FAILURES_PER_TEST 16 |
| #define | TR_FAILURE_MSG_LEN 256 |
| #define | EXPECT(cond) |
| | Evaluates a condition. Records and counts the failure if false.
|
| #define | EXPECT_FATAL(cond) |
| | Like EXPECT but aborts immediately on failure.
|
| #define | EXPECT_STR8_EQ(a, b) |
| | Asserts two String8 values are exactly equal.
|
| #define | TEST_CASE(name) |
| | Declares a test case function.
|
| #define | BEGIN_SUITE(name) |
| | Open a named suite block. Must be paired with END_SUITE.
|
| #define | END_SUITE |
| | Close a suite block opened with BEGIN_SUITE.
|
| #define | RUN_TEST(name) |
| | Execute a test case and print the result.
|
|
| static int | _tr_use_colors (void) |
| static void | _tr_record_failure (const char *file, int line, const char *expr) |
| static void | _tr_run_test (_TrTestFn fn, const char *name) |
| | Executes a single test function, times it, and prints the result.
|
| static void | _tr_suite_begin (const char *name) |
| | Prints the suite header and resets per-suite counters.
|
| static void | _tr_suite_end (void) |
| | Called after all tests in a suite have run.
|
| static void | tr_begin (const char *title) |
| | Print the top-level banner. Call once at the start of main().
|
| static int | tr_end (void) |
| | Print the results summary. Return value is the process exit code.
|
Single-header testing framework with pretty-printed output.
Everything in one place: test case definitions, assertions, suite grouping, timing, ANSI colors, and the results summary.
- Layout
cbase test suite
Base
PASS macros_math (0ms)
PASS macros_linked_list (0ms)
FAIL strings_operations (0ms)
test_base.c:42
EXPECT(hw.size == 11)
Memory
PASS arena_allocation (1ms)
Results
----------------------------------------
15 tests 14 passed 1 failed
#define EXPECT(cond)
Evaluates a condition. Records and counts the failure if false.
- Usage in test files
{
}
Sized UTF-8 string slices (String8) and manipulation utilities.
#define STR8(s)
Wraps a C string literal into a String8 at compile time.
Single-header testing framework with pretty-printed output.
#define EXPECT_STR8_EQ(a, b)
Asserts two String8 values are exactly equal.
#define TEST_CASE(name)
Declares a test case function.
#define EXPECT_FATAL(cond)
Like EXPECT but aborts immediately on failure.
- Usage in main.c
#include "test_base.c"
#include "test_memory.c"
int main(void)
{
}
#define BEGIN_SUITE(name)
Open a named suite block. Must be paired with END_SUITE.
static int tr_end(void)
Print the results summary. Return value is the process exit code.
#define END_SUITE
Close a suite block opened with BEGIN_SUITE.
#define RUN_TEST(name)
Execute a test case and print the result.
static void tr_begin(const char *title)
Print the top-level banner. Call once at the start of main().
- Copyright
- 2026 Alfred Jijo (MIT License)
Definition in file test.h.
◆ BEGIN_SUITE
| #define BEGIN_SUITE |
( |
| name | ) |
|
Value:do { \
_tr_suite_begin(name);
Open a named suite block. Must be paired with END_SUITE.
Definition at line 330 of file test.h.
◆ END_SUITE
Value:
} \
while (0)
static void _tr_suite_end(void)
Called after all tests in a suite have run.
Close a suite block opened with BEGIN_SUITE.
Definition at line 335 of file test.h.
◆ EXPECT
Value:do { \
if (!(cond)) { \
_tr_record_failure(__FILE__, __LINE__, #cond); \
_tr_failed++; \
} \
} while (0)
Evaluates a condition. Records and counts the failure if false.
Definition at line 161 of file test.h.
◆ EXPECT_FATAL
| #define EXPECT_FATAL |
( |
| cond | ) |
|
Value:do { \
if (!(cond)) { \
_tr_record_failure(__FILE__, __LINE__, #cond); \
_tr_failed++; \
printf(" %s[FATAL]%s %s:%d: %s\n", \
TR_RED, \
TR_RESET, \
__FILE__, \
__LINE__, \
#cond); \
abort(); \
} \
} while (0)
Like EXPECT but aborts immediately on failure.
Use before any pointer dereference or operation where continuing would cause undefined behaviour.
Definition at line 175 of file test.h.
◆ EXPECT_STR8_EQ
| #define EXPECT_STR8_EQ |
( |
| a, |
|
|
| b ) |
Value:
static b32 str8_match(String8 a, String8 b)
Returns 1 if a and b contain identical bytes (case-sensitive).
Asserts two String8 values are exactly equal.
Definition at line 193 of file test.h.
◆ RUN_TEST
Value:
static void _tr_run_test(_TrTestFn fn, const char *name)
Executes a single test function, times it, and prints the result.
Execute a test case and print the result.
The test function must have been defined with TEST_CASE(name) in a unity-included .c file above this call site.
Definition at line 346 of file test.h.
◆ TEST_CASE
| #define TEST_CASE |
( |
| name | ) |
|
Value:static void test_##name(void)
Declares a test case function.
Expands to: static void test_name(void)
Definition at line 205 of file test.h.
◆ TR_BOLD
| #define TR_BOLD (_tr_use_colors() ? "\x1b[1m" : "") |
◆ TR_DIM
| #define TR_DIM (_tr_use_colors() ? "\x1b[2m" : "") |
◆ TR_FAILURE_MSG_LEN
| #define TR_FAILURE_MSG_LEN 256 |
◆ TR_GREEN
| #define TR_GREEN (_tr_use_colors() ? "\x1b[32m" : "") |
◆ TR_MAX_FAILURES_PER_TEST
| #define TR_MAX_FAILURES_PER_TEST 16 |
◆ TR_RED
| #define TR_RED (_tr_use_colors() ? "\x1b[31m" : "") |
◆ TR_RESET
| #define TR_RESET (_tr_use_colors() ? "\x1b[0m" : "") |
◆ TR_WHITE
| #define TR_WHITE (_tr_use_colors() ? "\x1b[97m" : "") |
◆ TR_YELLOW
| #define TR_YELLOW (_tr_use_colors() ? "\x1b[33m" : "") |
◆ _TrTestFn
| typedef void(* _TrTestFn) (void) |
◆ _tr_record_failure()
| void _tr_record_failure |
( |
const char * | file, |
|
|
int | line, |
|
|
const char * | expr ) |
|
static |
◆ _tr_run_test()
| void _tr_run_test |
( |
_TrTestFn | fn, |
|
|
const char * | name ) |
|
static |
Executes a single test function, times it, and prints the result.
Resets the failure capture buffer before calling fn, then compares the global failure counter before and after to detect new failures. Prints a green PASS or red FAIL line with the test name and elapsed milliseconds. On failure, prints each buffered failure message indented beneath the test name.
Do not call this directly - use the RUN_TEST macro.
- Parameters
-
| fn | The test function to run. |
| name | The display name of the test (stringified by RUN_TEST). |
Definition at line 224 of file test.h.
◆ _tr_suite_begin()
| void _tr_suite_begin |
( |
const char * | name | ) |
|
|
static |
Prints the suite header and resets per-suite counters.
Called by BEGIN_SUITE. Resets _tr_suite_total and _tr_suite_failed to zero so per-suite statistics are tracked independently.
- Parameters
-
| name | The suite display name. |
Definition at line 270 of file test.h.
◆ _tr_suite_end()
| void _tr_suite_end |
( |
void | | ) |
|
|
static |
Called after all tests in a suite have run.
Currently a no-op reserved for future suite-level summary output (e.g. a per-suite pass/fail count line). The _tr_suite_total and _tr_suite_failed counters are available here if you want to uncomment a summary.
Definition at line 285 of file test.h.
◆ _tr_use_colors()
| int _tr_use_colors |
( |
void | | ) |
|
|
static |
◆ tr_begin()
| void tr_begin |
( |
const char * | title | ) |
|
|
static |
Print the top-level banner. Call once at the start of main().
- Parameters
-
| title | The suite title string. |
Definition at line 294 of file test.h.
◆ tr_end()
Print the results summary. Return value is the process exit code.
- Returns
- 0 if all tests passed, 1 if any failed.
Definition at line 304 of file test.h.
◆ _tr_fail_buf
◆ _tr_fail_count
◆ _tr_failed
◆ _tr_passed
◆ _tr_suite_failed
◆ _tr_suite_total
◆ _tr_total