// 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 */