From 4de0c57236d5bd2979fa626f6168fa9c269241b9 Mon Sep 17 00:00:00 2001 From: augustin64 Date: Wed, 3 Jan 2024 12:48:25 +0100 Subject: [PATCH] Add test/functions --- test/functions.cpp | 66 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 test/functions.cpp diff --git a/test/functions.cpp b/test/functions.cpp new file mode 100644 index 0000000..aaf29ec --- /dev/null +++ b/test/functions.cpp @@ -0,0 +1,66 @@ +#include "include/test.h" + +#include "../src/include/memory.h" +#include "../src/include/tokenize.h" +#include "../src/include/parser.h" +#include "../src/include/analysis.h" +#include "../src/include/interpreter.h" + +EvalResult execute(string s) { + Memory memory;; + vector tokens = tokenize({ s }); + Node ast = parse(tokens); + analyze(ast, memory); + EvalResult res = eval(ast, memory); + + return res; +} + +int main() { + _TEST_PRESENTATION("Fonctions"); + + _TEST_ASSERT( + _TEST_NO_EXCEPTION(holds_alternative(execute("int a();"))), + "Prototype de fonction sans variables", + true + ); + + _TEST_ASSERT( + _TEST_NO_EXCEPTION(holds_alternative(execute("void a(int b);"))), + "Prototype de fonction à une variable", + true + ); + + _TEST_ASSERT( + _TEST_NO_EXCEPTION(holds_alternative(execute("double a(int b, double c, int d);"))), + "Prototype de fonction à plusieurs variables", + true + ); + + _TEST_ASSERT( + _TEST_NO_EXCEPTION(holds_alternative(execute("double a(int b, double c, int d) { return b+c+d; };"))), + "Déclaration de fonction", + true + ); + + _TEST_ASSERT( + _TEST_IS_EXCEPTION(execute("int a(int b) { int c(int d) {return d;} return c(b); };"), ErrorType::NestedFunction), + "Fonctions imbriquées", + true + ); + + + _TEST_ASSERT( + _TEST_NO_EXCEPTION(get(execute("int sum(int a, int b) {return a+b;}; sum(20, 7);")) == 27), + "Exécution de fonction", + true + ); + + _TEST_ASSERT( + _TEST_NO_EXCEPTION(get(execute("int recurse(int a) {if (a < 27) {return recurse(a+1);} else {return a;}}; recurse(0);")) == 27), + "Fonction récursive", + true + ); + + return 0; +} \ No newline at end of file