ptk/test/test.h

55 lines
1.7 KiB
C

// Copyright (jacekpoz 2024). Licensed under the EUPL-1.2 or later.
#ifndef TEST_TEST_H_
#define TEST_TEST_H_
#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
#define FG(r, g, b) "\033[38;2;" #r ";" #g ";" #b "m"
#define BG(r, g, b) "\033[48;2;" #r ";" #g ";" #b "m"
#define RESET "\033[0m"
#define TEST_START() \
int main(void) {\
uint64_t tests_failed = 0;\
uint64_t tests_total = 0;\
printf("----- STARTING TEST FILE " __FILE__ " -----\n");
#define TEST_FINISH() \
printf("----- FINISHED TEST FILE " __FILE__ " -----\n");\
printf("PASSED TESTS: %s" FG(0, 0, 0) "%ld/%ld" RESET "\n",\
(tests_failed != 0 ? BG(255, 0, 0) : BG(0, 255, 0)),\
(tests_total - tests_failed), tests_total);\
printf("\n");\
return tests_failed;\
}
#define TEST_ASSERT(condition, message) \
do {\
asserts_total += 1;\
if (!(condition)) {\
asserts_failed += 1;\
printf(" " BG(255, 0, 0) FG(0, 0, 0) #condition ": " message " (" __FILE__ ":%d)" RESET "\n", __LINE__);\
}\
} while (0)
#define TEST(name, ...) \
do {\
uint64_t asserts_failed = 0;\
uint64_t asserts_total = 0;\
tests_total += 1;\
printf(" RUNNING TEST `" name "`\n");\
do {\
__VA_ARGS__;\
} while(0);\
printf(" PASSED ASSERTS: %s" FG(0, 0, 0) "%ld/%ld" RESET "\n",\
(asserts_failed != 0 ? BG(255, 0, 0) : BG(0, 255, 0)),\
(asserts_total - asserts_failed), asserts_total);\
if (asserts_failed > 0) {\
tests_failed += 1;\
}\
} while (0)
#endif // TEST_TEST_H_