diff --git a/inc/cex/test/assert.h b/inc/cex/test/assert.h new file mode 100644 index 0000000..e2e12d1 --- /dev/null +++ b/inc/cex/test/assert.h @@ -0,0 +1,55 @@ +#ifndef CEX_TEST_ASSERT_H +#define CEX_TEST_ASSERT_H + +#include +#include +#include +#include +#include + +void _print_assert(bool success, const char* label, const char* file, size_t line); + +typedef struct { + size_t test_count; + size_t fail_count; +} test_result_t; + +#define TEST(label) \ + test_result_t test_##label() { \ + test_result_t result = {.test_count = 0, .fail_count = 0}; \ + fprintf(stderr, ANSI_BOLD " " ANSI_UNDERLINE "test: %s\n" ANSI_RESET, #label); + +#define END_TEST \ + return result; \ + } + +#define PROJECT(label) \ + int main() { \ + test_result_t result = {.test_count = 0, .fail_count = 0}; \ + test_result_t final_result = {.test_count = 0, .fail_count = 0}; \ + fprintf(stderr, ANSI_BOLD ANSI_UNDERLINE "project: %s\n" ANSI_RESET, label); + +#define END_PROJECT \ + fprintf(stderr, "%ld / %ld " ANSI_FG_GREEN " succesful" ANSI_RESET ", %ld " ANSI_FG_RED "failed\n" ANSI_RESET, \ + final_result.test_count - final_result.fail_count, final_result.test_count, final_result.fail_count); \ + return final_result.fail_count != 0; \ + } + +#define LABEL(label) fprintf(stderr, ANSI_ITALIC " %s\n" ANSI_RESET, label); + +#define ASSERT_EQ(label, a, b) \ + _print_assert((a) == (b), label, __FILE_NAME__, __LINE__); \ + result.fail_count += (a) == (b) ? 0 : 1; \ + result.test_count += 1; + +#define ASSERT_NEQ(label, a, b) \ + _print_assert((a) != (b), label, __FILE_NAME__, __LINE__); \ + result.fail_count += (a) != (b) ? 0 : 1; \ + result.test_count += 1; + +#define RUN_TEST(func) \ + result = test_##func(); \ + final_result.fail_count += result.fail_count; \ + final_result.test_count += result.test_count; + +#endif \ No newline at end of file diff --git a/src/cex/test/assert.c b/src/cex/test/assert.c new file mode 100644 index 0000000..10be72b --- /dev/null +++ b/src/cex/test/assert.c @@ -0,0 +1,8 @@ +#include + +void _print_assert(bool success, const char* label, const char* file, size_t line) { + fprintf(stderr, "%20s: \t%s" ANSI_RESET, label, success ? ANSI_FG_GREEN "OK" : ANSI_FG_RED "FAILED"); + if (!success) + fprintf(stderr, " in %s on line %ld", file, line); + putc('\n', stderr); +} \ No newline at end of file