diff --git a/.github/workflows/standalone_tests.yml b/.github/workflows/standalone_tests.yml new file mode 100644 index 0000000..445b753 --- /dev/null +++ b/.github/workflows/standalone_tests.yml @@ -0,0 +1,39 @@ +name: Standalone Tests + +#trigger when these occur +on: + push: + branches: + - v2 + pull_request: + types: + - opened + - edited + - reopened + branches: + - v2 + workflow_dispatch: + +#These tests are more stand-alone than the others +jobs: + run-test-cases: + continue-on-error: true + strategy: + matrix: + platforms: + - { os: ubuntu-latest, preinstall: sudo apt-get install gdb, gdb_enabled: true } + - { os: windows-latest, preinstall: , gdb_enabled: true } + - { os: macos-latest, preinstall: , gdb_enabled: false } + commands: + - { exec: make -C tests/standalone -k, gdb: false } + - { exec: make -C tests/standalone gdb -k, gdb: true } + + runs-on: ${{ matrix.platforms.os }} + steps: + - uses: actions/checkout@v4 + - name: Preinstall dependencies + if: (matrix.commands.gdb == true && matrix.platforms.gdb_enabled == false) != true + run: ${{ matrix.platforms.preinstall }} + - name: run the tests + if: (matrix.commands.gdb == true && matrix.platforms.gdb_enabled == false) != true + run: ${{ matrix.commands.exec }} diff --git a/repl/main.c b/repl/main.c index 5f04af6..3d75cd3 100644 --- a/repl/main.c +++ b/repl/main.c @@ -40,7 +40,7 @@ unsigned char* readFile(char* path, int* size) { return buffer; } -int getDirPath(char* dest, const char* src) { +int getFilePath(char* dest, const char* src) { //extract the directory from src, and store it in dest #if defined(_WIN32) || defined(_WIN64) char* p = strrchr(src, '\\'); @@ -58,11 +58,16 @@ int getDirPath(char* dest, const char* src) { int getFileName(char* dest, const char* src) { //extract the directory from src, and store it in dest #if defined(_WIN32) || defined(_WIN64) - char* p = strrchr(src, '\\') + 1; + char* p = strrchr(src, '\\'); #else - char* p = strrchr(src, '/') + 1; + char* p = strrchr(src, '/'); #endif + //if we're not at the end of the string, skip the slash + if (*p != '\0') { + p++; + } + int len = strlen(p); strncpy(dest, p, len); dest[len] = '\0'; @@ -160,9 +165,9 @@ CmdLine parseCmdLine(int argc, const char* argv[]) { exit(-1); } - fprintf(stdout, TOY_CC_WARN "Debug: Parsing the infile name\n" TOY_CC_RESET); + fprintf(stdout, TOY_CC_WARN "Debug: Parsing the infile name from argv[0]: \n" TOY_CC_RESET, argv[0]); - getDirPath(cmd.infile, argv[0]); + getFilePath(cmd.infile, argv[0]); fprintf(stdout, TOY_CC_WARN "\t%s\n" TOY_CC_RESET, cmd.infile); diff --git a/tests/standalone/gdb_init b/tests/standalone/gdb_init new file mode 100644 index 0000000..8475b20 --- /dev/null +++ b/tests/standalone/gdb_init @@ -0,0 +1,8 @@ +set breakpoint pending on + +break strlen + +command 1 + bt + continue +end diff --git a/tests/standalone/makefile b/tests/standalone/makefile new file mode 100644 index 0000000..9ede8a2 --- /dev/null +++ b/tests/standalone/makefile @@ -0,0 +1,60 @@ +#compiler settings +CC=gcc +CFLAGS+=-std=c17 -g -Wall -Werror -Wno-unused-parameter -Wno-unused-function -Wno-unused-variable -Wformat=2 +LIBS+=-lm +LDFLAGS+= + +ifeq ($(shell uname),Linux) +LDFLAGS=-Wl,--gc-sections +else ifeq ($(OS),Windows_NT) +LDFLAGS=-Wl,--gc-sections +else ifeq ($(shell uname),Darwin) +LDFLAGS=-Wl,-dead_strip +else + @echo "LDFLAGS set failed - what platform is this?" +endif + +#directories +TEST_SRCDIR= + +TEST_OUTDIR=out/ +TEST_OBJDIR=obj/ + +#file names +TEST_SRCFILES=$(wildcard $(TEST_SRCDIR)*.c) + +#kick off +all: $(TEST_OBJDIR) $(TEST_OUTDIR) build run + +gdb: $(TEST_OBJDIR) $(TEST_OUTDIR) build gdb-run + +#build +build: $(TEST_OBJDIR)$(TEST_SRCFILES:.c=.o) + +.PRECIOUS: $(TEST_OBJDIR)%.o +$(TEST_OBJDIR)%.o: $(TEST_SRCDIR)%.c + $(CC) -c -o $@ $< $(CFLAGS) -fdata-sections -ffunction-sections + + +.PRECIOUS: $(TEST_OUTDIR)%.exe +$(TEST_OUTDIR)%.exe: $(TEST_OBJDIR)%.o + $(CC) -o $@ $< $(CFLAGS) $(LIBS) $(LDFLAGS) + +#run +run: $(addprefix $(TEST_OUTDIR),$(TEST_SRCFILES:.c=.exe-run)) + +$(TEST_OUTDIR)%.exe-run: $(TEST_OUTDIR)%.exe + $< + +#gdb-run +gdb-run: $(addprefix $(TEST_OUTDIR),$(TEST_SRCFILES:.c=.exe-gdb-run)) + +$(TEST_OUTDIR)%.exe-gdb-run: $(TEST_OUTDIR)%.exe + gdb $< -ix gdb_init -ex=run --batch --return-child-result + +#util targets +$(TEST_OUTDIR): + mkdir $(TEST_OUTDIR) + +$(TEST_OBJDIR): + mkdir $(TEST_OBJDIR) diff --git a/tests/standalone/platform_behaviours.c b/tests/standalone/platform_behaviours.c new file mode 100644 index 0000000..0237324 --- /dev/null +++ b/tests/standalone/platform_behaviours.c @@ -0,0 +1,93 @@ +#include +#include + +int getFilePath(char* dest, const char* src) { + //extract the directory from src, and store it in dest +#if defined(_WIN32) || defined(_WIN64) + char* p = strrchr(src, '\\'); +#else + char* p = strrchr(src, '/'); +#endif + + int len = p != NULL ? p - src + 1 : 0; + strncpy(dest, src, len); + dest[len] = '\0'; + + return len; +} + +int getFileName(char* dest, const char* src) { + //extract the directory from src, and store it in dest +#if defined(_WIN32) || defined(_WIN64) + char* p = strrchr(src, '\\'); +#else + char* p = strrchr(src, '/'); +#endif + + //if we're not at the end of the string, skip the slash + if (*p != '\0') { + p++; + } + + int len = strlen(p); + strncpy(dest, p, len); + dest[len] = '\0'; + + return len; +} + +#define APPEND(dest, src) \ + strncpy((dest) + (strlen(dest)), (src), strlen((src)) + 1); + +#if defined(_WIN32) || defined(_WIN64) + #define FLIPSLASH(str) for (int i = 0; str[i]; i++) str[i] = str[i] == '/' ? '\\' : str[i]; +#else + #define FLIPSLASH(str) for (int i = 0; str[i]; i++) str[i] = str[i] == '\\' ? '/' : str[i]; +#endif + +int main() { + //check the platform + printf("Platform: "); +#if defined(__linux__) + printf("Linux"); +#elif defined(_WIN64) + printf("Win64"); +#elif defined(_WIN32) + printf("Win32"); +#elif defined(__APPLE__) + printf("macOS"); +#else + printf("Unknown"); +#endif + + printf("\n"); + + //run each test + { + char src[256] = "../folder/file.txt"; + char dest[256]; + FLIPSLASH(src); + getFilePath(dest, src); + printf("Path: %s\n", dest); + } + + { + char src[256] = "../folder/file.txt"; + char dest[256]; + FLIPSLASH(src); + getFileName(dest, src); + printf("Name: %s\n", dest); + } + + { + char src[256] = "../folder/file.txt"; + char dest[256]; + FLIPSLASH(src); + getFilePath(dest, src); + APPEND(dest, "target.txt"); + FLIPSLASH(dest); + printf("Target: %s\n", dest); + } + + return 0; +} \ No newline at end of file