From 71ff481f6c80bccab4316c39b76f5476c6f8ea97 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Sun, 28 Aug 2022 08:10:41 +0100 Subject: [PATCH] Began working on unit tests --- makefile | 3 +++ test/makefile | 32 ++++++++++++++++++++++++++++++ test/test_memory.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 test/makefile create mode 100644 test/test_memory.c diff --git a/makefile b/makefile index 053b768..5d695c6 100644 --- a/makefile +++ b/makefile @@ -3,6 +3,9 @@ export OUTDIR = out all: $(OUTDIR) $(MAKE) -C source +test: clean $(OUTDIR) + $(MAKE) -C test + $(OUTDIR): mkdir $(OUTDIR) diff --git a/test/makefile b/test/makefile new file mode 100644 index 0000000..3d641b1 --- /dev/null +++ b/test/makefile @@ -0,0 +1,32 @@ +CC=gcc + +IDIR +=. ../source +CFLAGS +=$(addprefix -I,$(IDIR)) -g -Wall -W -pedantic -Wno-unused-parameter -Wno-unused-function -Wno-unused-variable +LIBS += + +ODIR = obj +TARGETS = $(filter-out $(wildcard ../source/*main.c),$(wildcard ../source/*.c)) +TESTS = $(wildcard *.c) +OBJ = $(addprefix $(ODIR)/,$(TARGETS:../source/%.c=%.o)) $(addprefix $(ODIR)/,$(TESTS:.c=.o)) + +all: $(OBJ) $(TESTS:%.c=../$(OUTDIR)/%.exe) + +../$(OUTDIR)/%.exe: $(ODIR)/%.o + $(CC) -o $@ $< $(TARGETS:../source/%.c=$(ODIR)/%.o) $(CFLAGS) $(LIBS) + $@ + +$(OBJ): | $(ODIR) + +$(ODIR): + mkdir $(ODIR) + +$(ODIR)/%.o: %.c + $(CC) -c -o $@ $< $(CFLAGS) + +$(ODIR)/%.o: ../source/%.c + $(CC) -c -o $@ $< $(CFLAGS) + +.PHONY: clean + +clean: + $(RM) $(ODIR) \ No newline at end of file diff --git a/test/test_memory.c b/test/test_memory.c new file mode 100644 index 0000000..c956fbd --- /dev/null +++ b/test/test_memory.c @@ -0,0 +1,49 @@ +#include "memory.h" + +#include "console_colors.h" + +#include + +int main() { + { + //test single pointer + int* integer = ALLOCATE(int, 1); + + FREE(int, integer); + + if (getAllocatedMemoryCount() != 0) { + fprintf(stderr, ERROR "ERROR: integer failed to be allocated and freed correctly\n" RESET); + return -1; + } + } + + { + //test single pointer + int* array = ALLOCATE(int, 10); + + FREE_ARRAY(int, array, 10); + + if (getAllocatedMemoryCount() != 0) { + fprintf(stderr, ERROR "ERROR: integer array failed to be allocated and freed correctly\n" RESET); + return -1; + } + } + + { + //test single pointer + int* array1 = ALLOCATE(int, 10); + int* array2 = ALLOCATE(int, 10); + + FREE_ARRAY(int, array1, 10); + FREE_ARRAY(int, array2, 10); + + if (getAllocatedMemoryCount() != 0) { + fprintf(stderr, ERROR "ERROR: integer array failed to be allocated and freed correctly\n" RESET); + return -1; + } + } + + printf(NOTICE "All good\n" RESET); + return 0; +} +