feat(test): add comprehensive test infrastructure

- Add test target to both bmake.mk and gmake.mk Makefiles
- Add test-related artifacts to .gitignore (excluding DLLs/shared libs)
- Create test directory with cross-platform Makefiles (bmake.mk, gmake.mk)
- Implement cross-platform test program with signal handling (test.c)
- Add test module source for compilation (test_mod.c)
- Set up automated test environment with mods/ directory
- Support both POSIX and Windows platforms

The test infrastructure allows running integration tests via 'make test'
and demonstrates stk library functionality with dynamic module loading.
Generated DLLs/shared libraries are excluded from version control.
This commit is contained in:
2026-01-31 22:46:07 +01:00
parent 2f4b91c729
commit d2bf8fb67a
7 changed files with 188 additions and 2 deletions
+6
View File
@@ -58,3 +58,9 @@ dkms.conf
obj/
bin/
# Test directories
test/mods/
test/test_program*
test/test_mod.so
test/test_mod.dylib
test/test_mod.dll
+5 -1
View File
@@ -12,7 +12,7 @@ LDFLAGS_PLAT = -ldl
CFLAGS_PLAT = -fPIC
CFLAGS_BASE = -Wall -Wpedantic -I${.CURDIR}/${INC_DIR} -std=c89 ${CFLAGS_PLAT}
.PHONY: all debug release clean
.PHONY: all debug release clean test
all: debug
@@ -47,3 +47,7 @@ obj/release/${_obj_base}: ${_src}
clean:
rm -rf ${.CURDIR}/${OBJ_DIR} ${.CURDIR}/${BIN_DIR}
test: debug
@echo "=== Building and running stk tests ==="
cd test && ${MAKE} -f bmake.mk
+5 -1
View File
@@ -18,7 +18,7 @@ endif
RELEASE_LDFLAGS := -s
CFLAGS_BASE := -Wall -Wpedantic -I$(INC_DIR) -std=c89 $(CFLAGS_PLAT)
.PHONY: all debug release clean
.PHONY: all debug release clean test
all: debug
@@ -49,3 +49,7 @@ obj/release/%.o: src/%.c
clean:
@$(call RMDIR,$(OBJ_DIR))
@$(call RMDIR,$(BIN_DIR))
test: debug
@echo "=== Building and running stk tests ==="
@$(MAKE) -C test -f gmake.mk
+44
View File
@@ -0,0 +1,44 @@
CC ?= cc
CFLAGS = -Wall -Wpedantic -I../include -std=c89
LDFLAGS = -L../bin/debug -lstk
UNAME_S != uname -s
.if ${UNAME_S} == "Darwin"
LDFLAGS += -Wl,-rpath,../bin/debug
MODULE_EXT = .dylib
.else
LDFLAGS += -Wl,-rpath,../bin/debug
MODULE_EXT = .so
.endif
.PHONY: all test clean
all: test
test_program: test.c
$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)
test_mod$(MODULE_EXT): test_mod.c
$(CC) $(CFLAGS) -fPIC -shared -o $@ $<
setup:
@mkdir -p mods
@cp -f test_mod$(MODULE_EXT) mods/ 2>/dev/null || true
@echo "Test environment ready: mods/ directory with test_mod$(MODULE_EXT)"
run: test_program test_mod$(MODULE_EXT) setup
@echo "Running integration test (CTRL+C to exit)..."
@./test_program
test: test_program test_mod$(MODULE_EXT) setup
@echo "=== stk Integration Test ==="
@echo "1. Starting test program"
@echo "2. Will load test_mod$(MODULE_EXT) from mods/"
@echo "3. Press CTRL+C to exit"
@echo "============================="
@./test_program || echo "Test completed."
clean:
rm -f test_program test_mod$(MODULE_EXT)
rm -rf mods/
+43
View File
@@ -0,0 +1,43 @@
CC := cc
CFLAGS = -Wall -Wpedantic -I../include -std=c89
LDFLAGS = -L../bin/debug -lstk
UNAME_S := $(shell uname -s)
ifeq ($(OS),Windows_NT)
MODULE_EXT = .dll
else
LDFLAGS += -Wl,-rpath,../bin/debug
MODULE_EXT = .so
endif
.PHONY: all test clean
all: test
test_program: test.c
$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)
test_mod$(MODULE_EXT): test_mod.c
$(CC) $(CFLAGS) -fPIC -shared -o $@ $<
setup:
@mkdir -p mods
@cp -f test_mod$(MODULE_EXT) mods/ 2>/dev/null || true
@echo " Test environment ready: mods/ directory with test_mod$(MODULE_EXT)"
run: test_program test_mod$(MODULE_EXT) setup
@echo "Running integration test (CTRL+C to exit)..."
@./test_program
test: test_program test_mod$(MODULE_EXT) setup
@echo "=== stk Integration Test ==="
@echo "1. Starting test program"
@echo "2. Will load test_mod$(MODULE_EXT) from mods/"
@echo "3. Press CTRL+C to exit"
@echo "============================="
@./test_program || echo "Test completed."
clean:
rm -f test_program test_mod$(MODULE_EXT)
rm -rf mods/
+76
View File
@@ -0,0 +1,76 @@
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <stk.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif
volatile sig_atomic_t stop;
#ifdef _WIN32
BOOL WINAPI console_handler(DWORD signal)
{
if (signal == CTRL_C_EVENT) {
stop = 1;
printf("\nCaught Ctrl+C, shutting down...\n");
return TRUE;
}
return FALSE;
}
#else
void inthand(int signum)
{
stop = 1;
printf("\nCaught SIGINT, shutting down...\n");
}
#endif
int main(int argc, char **argv)
{
uint8_t init_result;
uint64_t iterations = 0;
printf("stk test - CTRL+C to exit\n");
#ifdef _WIN32
if (!SetConsoleCtrlHandler(console_handler, TRUE)) {
fprintf(stderr,
"ERROR: Could not set console control handler\n");
return EXIT_FAILURE;
}
#else
signal(SIGINT, inthand);
#endif
init_result = stk_init();
if (init_result != STK_INIT_SUCCESS) {
fprintf(stderr, "FAIL: stk_init() returned %d\n", init_result);
return EXIT_FAILURE;
}
while (!stop) {
size_t events = stk_poll();
if (events > 0)
printf("Poll: %lu module event(s) detected\n", events);
iterations++;
if (iterations % 5 == 0) {
printf("Still running... (iteration %lu)\n",
iterations);
}
#ifdef _WIN32
Sleep(1000);
#else
sleep(1);
#endif
}
printf("Shutting down stk...\n");
stk_shutdown();
return EXIT_SUCCESS;
}
+9
View File
@@ -0,0 +1,9 @@
#include <stdio.h>
int stk_mod_init(void)
{
printf("test mod initialized!\n");
return 0;
}
void stk_mod_shutdown(void) { printf("test mod shut down.\n"); }