This commit is contained in:
Benedek László 2024-04-15 18:54:04 +02:00
parent 53830c0d73
commit 6e2fae213a
2 changed files with 63 additions and 0 deletions

55
inc/cex/test/assert.h Normal file
View File

@ -0,0 +1,55 @@
#ifndef CEX_TEST_ASSERT_H
#define CEX_TEST_ASSERT_H
#include <assert.h>
#include <cex/ansi/codes.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
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

8
src/cex/test/assert.c Normal file
View File

@ -0,0 +1,8 @@
#include <cex/test/assert.h>
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);
}