From 6e2fae213a482f363bc4e08e7f7fe6a5024aaf1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benedek=20L=C3=A1szl=C3=B3?= Date: Mon, 15 Apr 2024 18:54:04 +0200 Subject: [PATCH] testing --- inc/cex/test/assert.h | 55 +++++++++++++++++++++++++++++++++++++++++++ src/cex/test/assert.c | 8 +++++++ 2 files changed, 63 insertions(+) create mode 100644 inc/cex/test/assert.h create mode 100644 src/cex/test/assert.c 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