From 1a61e37936e6fb6da8a176cdf3a6ea34e25de006 Mon Sep 17 00:00:00 2001 From: 21105419 Date: Wed, 24 Jan 2024 11:24:49 +0100 Subject: [PATCH] Making sure unit tests work --- .github/workflows/test.yaml | 32 ++++++++++++++++++++++++++++++++ .gitignore | 3 ++- Makefile | 25 +++++++++++++++++++++++++ src/main.c => main.c | 0 test/john.c | 9 +++++++++ 5 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/test.yaml create mode 100644 Makefile rename src/main.c => main.c (100%) create mode 100644 test/john.c diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 00000000..38568945 --- /dev/null +++ b/.github/workflows/test.yaml @@ -0,0 +1,32 @@ +name: Unit Tests + +on: + push: + branches: + - main + +jobs: + format: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Install valgrind + run: | + sudo apt-get update + sudo apt-get install -y valgrind + + - name: Compile Tests + run: | + make test + + - name: Run Tests + run: | + for test in test/bin/*; do + valgrind --leak-check=full --error-exitcode=1 --show-leak-kinds=all --errors-for-leak-kinds=all $test + if [ $? -ne 0 ]; then + exit 1 + fi + done diff --git a/.gitignore b/.gitignore index fb80b9bf..371a09ad 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .vscode/ -.bin/ \ No newline at end of file +bin/ +test/bin/ \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..ff208f0b --- /dev/null +++ b/Makefile @@ -0,0 +1,25 @@ +TARGET := ../edgerunner +DEBUG := ../edgerunner_dbg + +TEST := ../test +TEST_DBG := ../test_dbg + +CC := gcc + +DEBUG_FLAGS := -g -fsanitize=address -fsanitize=object-size -fno-optimize-sibling-calls -fsanitize=undefined -fsanitize=leak -fsanitize=alignment +CFLAGS := -Og -std=gnu17 -Wall -Wextra -Wpedantic -Wno-unused-parameter + +SRCS := $(wildcard src/*.c) + +all: $(TARGET) + +$(TARGET): $(SRCS) main.c + $(CC) $(CFLAGS) $^ -o $(TARGET) +$(DEBUG) : $(SRCS) main.c + $(CC) $(CFLAGS) $(DEBUG_FLAGS) $^ -o $(DEBUG) + + +test: $(TEST) +$(TEST): $(SRCS) $(wildcard test/*.c) + $(CC) $(SRCS) $(CFLAGS) $^ -o test/bin/$(basename $(notdir $^)) + diff --git a/src/main.c b/main.c similarity index 100% rename from src/main.c rename to main.c diff --git a/test/john.c b/test/john.c new file mode 100644 index 00000000..872ca2a9 --- /dev/null +++ b/test/john.c @@ -0,0 +1,9 @@ +#include +#include + +int main() { + int* thing = (int*)malloc(sizeof(int)); + *thing = 5; + printf("thing = %d\n", *thing); + return 0; +} \ No newline at end of file