diff --git a/testing/.gitignore b/testing/.gitignore new file mode 100644 index 0000000..990da86 --- /dev/null +++ b/testing/.gitignore @@ -0,0 +1,57 @@ +# Prerequisites +*.d + +# Object files +*.o +*.ko +*.obj +*.elf + +# Linker output +*.ilk +*.map +*.exp + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +#*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# Debug files +*.dSYM/ +*.su +*.idb +*.pdb + +# Kernel Module Compile Results +*.mod* +*.cmd +.tmp_versions/ +modules.order +Module.symvers +Mkfile.old +dkms.conf + +obj/** +testing.a + +*.swp diff --git a/testing/Makefile b/testing/Makefile new file mode 100644 index 0000000..b423bf9 --- /dev/null +++ b/testing/Makefile @@ -0,0 +1,32 @@ +CC = gcc +CFLAGS = -std=c11 -D_POSIX_C_SOURCE=200809L -Wall -Wpedantic -g +LD = gcc +LDFLAGS = +AR = ar +ARFLAGS = rcs + + +OBJS = obj/testing.o +DEPS = $(OBJS:%.o=%.d) + +-include $(DEPS) + +all: testing.a test + +obj/%.o: src/%.c obj + $(CC) $(CFLAGS) -c -o $@ $< + +testing.a: CFLAGS += -fPIC +testing.a: $(OBJS) + $(AR) $(ARFLAGS) $@ $^ + +test: testing.a + @echo "no tests for testing module" + +FORCE: ; + +clean: + @echo "Cleaning up" + @rm -f obj/*.o + @rm -f obj/*.d + @rm testing.a diff --git a/testing/obj/.gitignore b/testing/obj/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/testing/src/testing.c b/testing/src/testing.c new file mode 100644 index 0000000..0e8eec2 --- /dev/null +++ b/testing/src/testing.c @@ -0,0 +1,61 @@ +#include +#include +#include + +#include "testing.h" + +size_t tests_total; +size_t tests_succeeded; + +bool success; + +void test_init(void) { + tests_total = 0; + tests_succeeded = 0; +} + +bool test_results(void) { + fprintf(stderr, "\nTest Results:\n"); + fprintf(stderr, "%7zu total tests\n", tests_total); + fprintf(stderr, "%7zu successful tests\n", tests_succeeded); + fprintf(stderr, "%7zu failed tests\n", tests_total - tests_succeeded); + fprintf(stderr, "\n"); + + fprintf(stderr, "Overall Result: "); + if (tests_total == tests_succeeded) { + fprintf(stderr, "\033[32mSUCCESS\033[0m\n"); + return true; + } else { + fprintf(stderr, "\033[31mFAIL\033[0m\n"); + return false; + } +} + +void test_run_named(void (*test)(void), const char* test_name) { + fprintf(stderr, "Test %s:\n", test_name); + success = true; + tests_total++; + + test(); + + if (success) { + fprintf(stderr, " \033[32mSUCCESS\033[0m\n"); + tests_succeeded++; + } else { + fprintf(stderr, " \033[31mFAIL\033[0m\n"); + } +} + +void __assert(bool result, const char* message, const char* test_str, const char* file, const char* function, size_t lineno) { + if (!result) { + success = false; + + fprintf(stderr, " "); + if (message) { + fprintf(stderr, "%s (%s)", message, test_str); + } else { + fprintf(stderr, "%s", test_str); + } + fprintf(stderr, " (%s, %s, %zu)\n", file, function, lineno); + } +} diff --git a/testing/src/testing.h b/testing/src/testing.h new file mode 100644 index 0000000..adc75b6 --- /dev/null +++ b/testing/src/testing.h @@ -0,0 +1,25 @@ +#ifndef TESTING_H +#define TESTING_H + +#include +#include + +void test_init(void); +void test_run_named(void (*)(void), const char*); +bool test_results(void); + +#define test_run(f) test_run_named(f, # f) + +void __assert(bool, const char*, const char*, const char*, const char*, size_t); + +#define _assert(b, m, t) { bool r = b; __assert(r, m, t, __FILE__, __FUNCTION__, __LINE__); if (!r) return; } +#define assert_true(b, m) _assert(b, m, # b) +#define assert_false(b, m) assert_true(b, m) +#define assert_equals(e, a, m) assert_true(e == a, m) +#define assert_null(a, m) assert_true(NULL == a, m) +#define assert_not_null(a, m) assert_true(NULL != a, m) +#define assert_strlen(s, l, m) assert_true(strlen(s) == l, m) +#define assert_streq(s1, s2, m) _assert(strcmp(s1, s2) == 0, m, # s1 " == " # s2) +#define assert_array_equals(a1, a2, l, m) { for(size_t _i = 0; _i < l; _i++) { assert_equals(a1[i], a2[i], m); } } + +#endif