feat: Add testing module

This commit is contained in:
overflowerror 2024-02-25 11:54:59 +01:00
parent 755bdcd189
commit 4d75f1fccf
5 changed files with 175 additions and 0 deletions

57
testing/.gitignore vendored Normal file
View file

@ -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

32
testing/Makefile Normal file
View file

@ -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

0
testing/obj/.gitignore vendored Normal file
View file

61
testing/src/testing.c Normal file
View file

@ -0,0 +1,61 @@
#include <stdio.h>
#include <time.h>
#include <stdbool.h>
#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);
}
}

25
testing/src/testing.h Normal file
View file

@ -0,0 +1,25 @@
#ifndef TESTING_H
#define TESTING_H
#include <stdbool.h>
#include <string.h>
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