Add tests
This commit is contained in:
parent
691bf3a232
commit
f9159fa92e
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,4 +1,3 @@
|
||||
*.o
|
||||
build/*
|
||||
.vscode
|
||||
test
|
||||
.vscode
|
32
Makefile
32
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)/*
|
||||
|
25
test/expr_arithmetiques.cpp
Normal file
25
test/expr_arithmetiques.cpp
Normal 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
23
test/include/test.h
Normal 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
23
test/variables.cpp
Normal 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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user