Add tests

This commit is contained in:
augustin64 2023-11-10 22:24:14 +01:00
parent 691bf3a232
commit f9159fa92e
5 changed files with 103 additions and 3 deletions

3
.gitignore vendored
View File

@ -1,4 +1,3 @@
*.o *.o
build/* build/*
.vscode .vscode
test

View File

@ -1,7 +1,15 @@
BUILDDIR := ./build BUILDDIR := ./build
TEST_SRCDIR := ./test
SRCDIR := ./src SRCDIR := ./src
CXX := g++ 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 # Linker flag
LD_CXXFLAGS = LD_CXXFLAGS =
@ -14,7 +22,7 @@ CXXFLAGS = -Wall -Wextra -g -O3 -std=c++2a
# See memory leaks and Incorrect Read/Write # See memory leaks and Incorrect Read/Write
# -fsanitize=address -lasan # -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) $(CXX) $^ -o $@ $(CXXFLAGS) $(LD_CXXFLAGS)
@ -22,5 +30,27 @@ $(BUILDDIR)/%.o: $(SRCDIR)/%.cpp $(SRCDIR)/include/%.h
$(CXX) -c $< -o $@ $(CXXFLAGS) $(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: clean:
rm -rf $(BUILDDIR)/* rm -rf $(BUILDDIR)/*

View File

@ -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<Token> tokens = tokenize(s);
Node ast = parse(tokens);
EvalResult res = eval(ast);
return get<int>(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;
}

23
test/include/test.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef DEF_TEST_H
#define DEF_TEST_H
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#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

23
test/variables.cpp Normal file
View File

@ -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<Token> tokens = tokenize(s);
Node ast = parse(tokens);
EvalResult res = eval(ast);
return get<int>(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;
}