diff options
| author | Ethan Werner <mail@ethanwerner.com> | 2026-06-09 01:17:41 +0400 |
|---|---|---|
| committer | Ethan Werner <mail@ethanwerner.com> | 2026-06-09 01:17:41 +0400 |
| commit | 96ecf29e8be977f51f351308ef2c7c492bb829aa (patch) | |
| tree | c664aa61262e6f7627a732f0e4fbb84281b99952 /include/test.h | |
| parent | f6a144987cc403de8ad6bddd055f756120f8d405 (diff) | |
refactor
Diffstat (limited to 'include/test.h')
| -rw-r--r-- | include/test.h | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/include/test.h b/include/test.h new file mode 100644 index 0000000..9f40fd6 --- /dev/null +++ b/include/test.h @@ -0,0 +1,57 @@ +// test.h - Minimal test assertion macros +// +// Not a library for distribution. Intended for the test suite only. +// Provides ASSERT, ASSERT_FLOAT_EQ, and ASSERT_STR_EQ macros with +// failure tracking via a global test_failures counter. + +#ifndef TEST_H +#define TEST_H + +#include <stdio.h> +#include <string.h> +#include <math.h> + +#include "type.h" + +#ifndef TEST_EPS +#define TEST_EPS 1e-9 +#endif + +#ifndef TEST_FAILURES_VAR +#define TEST_FAILURES_VAR int test_failures = 0 +#endif + +#define ASSERT(cond, msg) \ + do { \ + if (!(cond)) { \ + fprintf(stderr, "FAIL: %s (%s:%d)\n", msg, __FILE__, __LINE__); \ + test_failures++; \ + } \ + } while (0) + +#define ASSERT_FLOAT_EQ(a, b, msg) \ + do { \ + f64 _a = (a); \ + f64 _b = (b); \ + if (fabs(_a - _b) > TEST_EPS) { \ + fprintf(stderr, "FAIL: %s (got %f, expected %f) (%s:%d)\n", \ + msg, (double)_a, (double)_b, __FILE__, __LINE__); \ + test_failures++; \ + } \ + } while (0) + +#define ASSERT_STR_EQ(a, b, msg) \ + do { \ + char const *_a = (a); \ + char const *_b = (b); \ + if (_a == NULL || _b == NULL || strcmp(_a, _b) != 0) { \ + fprintf(stderr, "FAIL: %s (got '%s', expected '%s') (%s:%d)\n", \ + msg, _a ? _a : "NULL", _b ? _b : "NULL", __FILE__, __LINE__); \ + test_failures++; \ + } \ + } while (0) + +#define TEST_PRINT_RESULTS(name) \ + printf("%s: %d failures\n", name, test_failures) + +#endif /* TEST_H */ |