// single-header unit test framework. // if you want to keep your sanity, do not use this. #include #include #include // globals. int tests_passed = 0; char *current_label; int current_num; /* Basic idea: TEST("...", { ASSERT(...) ASSERT(...) ... }) TEST("...", { ASSERT(...) ASSERT(...) ... }) DONE */ #define TEST(LABEL, BLOCK) \ current_label = LABEL; \ current_num = __COUNTER__ + 1; \ BLOCK; \ fprintf(stderr, "[%3d] \x1b[32m%s ✓\x1b[0m\n", current_num, current_label); \ tests_passed++; #define ASSERT(EXPR) \ if (!(EXPR)) { \ fprintf(stderr, "[%3d] \x1b[31m%s ❌\x1b[0m\n", current_num, current_label); \ fprintf(stderr, " Assertion failed in %s:%d:\n", __FILE__, __LINE__); \ fprintf(stderr, " " #EXPR "\n"); \ goto after_tests; \ } #define DONE \ after_tests: { \ int tests_total = __COUNTER__; \ fprintf(stderr, "[\x1b[%dm***\x1b[0m] %d/%d tests passed.\n", tests_passed == tests_total ? 32 : 31, tests_passed, tests_total); \ if (tests_passed != tests_total) exit(1); \ }