diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/Makefile | 17 | ||||
| -rw-r--r-- | tests/cases_list.h | 0 | ||||
| -rw-r--r-- | tests/test.c | 56 | ||||
| -rw-r--r-- | tests/test.h | 22 |
4 files changed, 95 insertions, 0 deletions
diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 0000000..19881a2 --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,17 @@ +CASES := $(wildcard test_*.c) +CASES_OBJ := $(CASES:.c=.o) + +.PHONY: run + +run: test + +test: test.o $(CASES_OBJ) test.h + @echo Case sources: $(CASES) + @echo Objects: $(CASES_OBJ) + $(CC) -o $@ $^ + +test%.o: test%.c + $(CC) -I../src -c -o $@ $< + +clean: + rm *.o test diff --git a/tests/cases_list.h b/tests/cases_list.h new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/cases_list.h diff --git a/tests/test.c b/tests/test.c new file mode 100644 index 0000000..a919301 --- /dev/null +++ b/tests/test.c @@ -0,0 +1,56 @@ +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> + +#define COLOR_RED "\e[31m" +#define COLOR_GREEN "\e[32m" +#define COLOR_R "\e[0m" + +#define GET_RES(func) {\ + int res = func();\ + if (res > 0) {\ + /*printf(COLOR_RED "Case " #func " failed" COLOR_R "\n");*/\ + failed_cases += 1;\ + }\ + failed_assertions += res;\ +} + +enum TestCases { +#define CASE(__case__) TestCase_##__case__ , +#include "cases_list.h" +#undef CASE + TestCase_MAXCASES, +}; + +#define CASE(__case__) \ +extern unsigned __case__(); +#include "cases_list.h" +#undef CASE + +unsigned ((*test_cases[TestCase_MAXCASES])(void)) = { +#define CASE(__case__) [TestCase_##__case__] = &(__case__), +#include "cases_list.h" +#undef CASE +}; + +int total_assertions = 0; + +int main () { + int failed_assertions = 0; + int failed_cases = 0; + + for (int i = 0; i < TestCase_MAXCASES; i++) { + GET_RES(test_cases[i]); + } + + if (!failed_assertions) { + printf(COLOR_GREEN "All tests passed!\n" COLOR_R); + exit(EXIT_SUCCESS); + } else { + puts("\n=================================== TEST SUMMARY ================================\n"); + printf(COLOR_GREEN "Pass: %d \t" COLOR_RED "Failed: %d\n" COLOR_R, total_assertions - failed_assertions, failed_assertions); + exit(EXIT_FAILURE); + } + + return 0; +} diff --git a/tests/test.h b/tests/test.h new file mode 100644 index 0000000..1d60ca8 --- /dev/null +++ b/tests/test.h @@ -0,0 +1,22 @@ +#ifndef TEST_H +#define TEST_H + +#include <stdio.h> + +#define TEST_CASE(__case_name__, __case__) int __case_name__(){ \ + const char* testname = #__case_name__;\ + int fail_counter = 0;\ + __case__\ + return fail_counter;\ +} + +#define CHECK(__assertion__) {\ + total_assertions += 1; \ + if (!(__assertion__)) {\ + printf("Test failed: %s in %s:%d\n> " #__assertion__ "\n", testname, __FILE__, __LINE__);\ + fail_counter += 1;\ +}} + +extern int total_assertions; + +#endif |
