diff --git a/.gitignore b/.gitignore index 702e4e4..dcf0306 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/bmake.mk b/bmake.mk index 542981e..837642a 100644 --- a/bmake.mk +++ b/bmake.mk @@ -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 diff --git a/gmake.mk b/gmake.mk index e7151d9..4eb7472 100644 --- a/gmake.mk +++ b/gmake.mk @@ -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 diff --git a/test/bmake.mk b/test/bmake.mk new file mode 100644 index 0000000..1b46dc0 --- /dev/null +++ b/test/bmake.mk @@ -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/ diff --git a/test/gmake.mk b/test/gmake.mk new file mode 100644 index 0000000..6411b27 --- /dev/null +++ b/test/gmake.mk @@ -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/ diff --git a/test/test.c b/test/test.c new file mode 100644 index 0000000..f923664 --- /dev/null +++ b/test/test.c @@ -0,0 +1,76 @@ +#include +#include +#include +#include + +#ifdef _WIN32 +#include +#else +#include +#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; +} diff --git a/test/test_mod.c b/test/test_mod.c new file mode 100644 index 0000000..9025f6f --- /dev/null +++ b/test/test_mod.c @@ -0,0 +1,9 @@ +#include + +int stk_mod_init(void) +{ + printf("test mod initialized!\n"); + return 0; +} + +void stk_mod_shutdown(void) { printf("test mod shut down.\n"); }