Skip to content

Commit

Permalink
Added standalone tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ratstail91 committed Oct 17, 2024
1 parent 90c783f commit 403fc10
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 5 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/standalone_tests.yml
Original file line number Diff line number Diff line change
@@ -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 }}
15 changes: 10 additions & 5 deletions repl/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, '\\');
Expand All @@ -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';
Expand Down Expand Up @@ -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);

Expand Down
8 changes: 8 additions & 0 deletions tests/standalone/gdb_init
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
set breakpoint pending on

break strlen

command 1
bt
continue
end
60 changes: 60 additions & 0 deletions tests/standalone/makefile
Original file line number Diff line number Diff line change
@@ -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)
93 changes: 93 additions & 0 deletions tests/standalone/platform_behaviours.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include <stdio.h>
#include <string.h>

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;
}

0 comments on commit 403fc10

Please sign in to comment.