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:
@@ -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/
|
||||
@@ -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
@@ -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;
|
||||
}
|
||||
@@ -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"); }
|
||||
Reference in New Issue
Block a user