From 96ecf29e8be977f51f351308ef2c7c492bb829aa Mon Sep 17 00:00:00 2001 From: Ethan Werner Date: Tue, 9 Jun 2026 01:17:41 +0400 Subject: refactor --- include/test.h | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 include/test.h (limited to 'include/test.h') 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 +#include +#include + +#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 */ -- cgit v1.3.1