From f9159fa92e4ca3d598eaaa1fa3e0aacdc02f67fc Mon Sep 17 00:00:00 2001 From: augustin64 Date: Fri, 10 Nov 2023 22:24:14 +0100 Subject: [PATCH] Add tests --- .gitignore | 3 +-- Makefile | 32 +++++++++++++++++++++++++++++++- test/expr_arithmetiques.cpp | 25 +++++++++++++++++++++++++ test/include/test.h | 23 +++++++++++++++++++++++ test/variables.cpp | 23 +++++++++++++++++++++++ 5 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 test/expr_arithmetiques.cpp create mode 100644 test/include/test.h create mode 100644 test/variables.cpp diff --git a/.gitignore b/.gitignore index 9567a1a..710fc64 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ *.o build/* -.vscode -test \ No newline at end of file +.vscode \ No newline at end of file diff --git a/Makefile b/Makefile index 668694e..a93c65d 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,15 @@ BUILDDIR := ./build +TEST_SRCDIR := ./test SRCDIR := ./src CXX := g++ +SRC := $(wildcard $(SRCDIR)/*.cpp) +OBJ = $(filter-out $(BUILDDIR)/main.o, $(SRC:$(SRCDIR)/%.cpp=$(BUILDDIR)/%.o)) + +TESTS_SRC = $(wildcard $(TEST_SRCDIR)/*.cpp) +TESTS_OBJ = $(TESTS_SRC:$(TEST_SRCDIR)/%.cpp=$(BUILDDIR)/test-%) + + # Linker flag LD_CXXFLAGS = @@ -14,7 +22,7 @@ CXXFLAGS = -Wall -Wextra -g -O3 -std=c++2a # See memory leaks and Incorrect Read/Write # -fsanitize=address -lasan -$(BUILDDIR)/main: $(SRCDIR)/main.cpp $(BUILDDIR)/input.o $(BUILDDIR)/interpreter.o $(BUILDDIR)/parser.o $(BUILDDIR)/tokenize.o +$(BUILDDIR)/main: $(SRCDIR)/main.cpp $(OBJ) $(CXX) $^ -o $@ $(CXXFLAGS) $(LD_CXXFLAGS) @@ -22,5 +30,27 @@ $(BUILDDIR)/%.o: $(SRCDIR)/%.cpp $(SRCDIR)/include/%.h $(CXX) -c $< -o $@ $(CXXFLAGS) +# +# Tests +# + +# 1. Compile every test/*.cpp file +# 2. Run every test/*.sh file +# 3. Run every file compiled in (1.) +run-tests: build-tests + $(foreach file, $(wildcard $(TEST_SRCDIR)/*.sh), $(file);) + @echo "$$(for file in build/test-*; do $$file || exit 1; done)" + +# Preparing tests is basically deleting every compiled file because maybe we "just changed a header" +build-tests: prepare-tests $(TESTS_OBJ) + +prepare-tests: + @rm -f $(BUILDDIR)/test-* + +$(BUILDDIR)/test-%: $(TEST_SRCDIR)/%.cpp $(OBJ) + $(CXX) $^ -o $@ $(CXXFLAGS) $(LD_CXXFLAGS) + + + clean: rm -rf $(BUILDDIR)/* diff --git a/test/expr_arithmetiques.cpp b/test/expr_arithmetiques.cpp new file mode 100644 index 0000000..9730112 --- /dev/null +++ b/test/expr_arithmetiques.cpp @@ -0,0 +1,25 @@ +#include "include/test.h" + +#include "../src/include/tokenize.h" +#include "../src/include/parser.h" +#include "../src/include/interpreter.h" + +int execute(string s) { + vector tokens = tokenize(s); + Node ast = parse(tokens); + EvalResult res = eval(ast); + + return get(res); +} + +int main() { + _TEST_PRESENTATION("Expressions Arithmétiques"); + + _TEST_ASSERT(execute("10 + 3 * (7 - 2);") == 25, "Priorités"); + + _TEST_ASSERT(execute("12 - 4 - 20;") == -12, "Ordre de soustraction"); + + _TEST_ASSERT(execute("-7 + 13 + -6;") == 0, "Opérateurs unaires"); + + return 0; +} \ No newline at end of file diff --git a/test/include/test.h b/test/include/test.h new file mode 100644 index 0000000..c079f4d --- /dev/null +++ b/test/include/test.h @@ -0,0 +1,23 @@ +#ifndef DEF_TEST_H +#define DEF_TEST_H + +#include +#include +#include + +#include "../../src/include/colors.h" + +#define _TEST_PRESENTATION(description) { printf("\n" BLUE "#### %s:" BOLD "%s" RESET BLUE " #####" RESET "\n", __FILE__, description); } + +#define _TEST_ASSERT(condition, description) { \ + if (condition) { \ + printf("[" GREEN "OK" RESET "] %s:%d: %s\n", __FILE__, __LINE__, description); \ + } else { \ + printf("[" RED "ERREUR" RESET "] %s:%d: %s\n", __FILE__, __LINE__, description); \ + exit(1); \ + } \ +} + + + +#endif \ No newline at end of file diff --git a/test/variables.cpp b/test/variables.cpp new file mode 100644 index 0000000..f4e2ba4 --- /dev/null +++ b/test/variables.cpp @@ -0,0 +1,23 @@ +#include "include/test.h" + +#include "../src/include/tokenize.h" +#include "../src/include/parser.h" +#include "../src/include/interpreter.h" + +int execute(string s) { + vector tokens = tokenize(s); + Node ast = parse(tokens); + EvalResult res = eval(ast); + + return get(res); +} + +int main() { + _TEST_PRESENTATION("Variables"); + + _TEST_ASSERT(execute("int uneVariableFarfelue__ = 12;") == 12, "Déclaration avec assignement"); + + _TEST_ASSERT(execute("int x = 5; x + 3;") == 8, "Déclaration puis assignement"); + + return 0; +} \ No newline at end of file