2023-11-10 22:24:14 +01:00
|
|
|
#include "include/test.h"
|
|
|
|
|
2023-11-22 13:52:16 +01:00
|
|
|
#include "../src/include/memory.h"
|
2023-11-10 22:24:14 +01:00
|
|
|
#include "../src/include/tokenize.h"
|
|
|
|
#include "../src/include/parser.h"
|
|
|
|
#include "../src/include/interpreter.h"
|
|
|
|
|
|
|
|
int execute(string s) {
|
2023-12-08 15:29:30 +01:00
|
|
|
Memory memory;;
|
2023-11-15 14:31:11 +01:00
|
|
|
vector<Token> tokens = tokenize({ s });
|
2023-11-10 22:24:14 +01:00
|
|
|
Node ast = parse(tokens);
|
2023-11-22 13:52:16 +01:00
|
|
|
EvalResult res = eval(ast, memory);
|
2023-11-10 22:24:14 +01:00
|
|
|
|
|
|
|
return get<int>(res);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
_TEST_PRESENTATION("Expressions Arithmétiques");
|
|
|
|
|
2023-11-14 17:03:42 +01:00
|
|
|
_TEST_ASSERT(
|
|
|
|
_TEST_NO_EXCEPTION(execute("10 + 3 * (7 - 2);") == 25),
|
2023-11-15 11:59:38 +01:00
|
|
|
"Priorités",
|
|
|
|
true
|
2023-11-14 17:03:42 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
_TEST_ASSERT(
|
|
|
|
_TEST_NO_EXCEPTION(execute("12 - 4 - 20;") == -12),
|
2023-11-27 21:16:39 +01:00
|
|
|
"Parenthésage",
|
2023-11-15 11:59:38 +01:00
|
|
|
true
|
2023-11-14 17:03:42 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
_TEST_ASSERT(
|
|
|
|
_TEST_NO_EXCEPTION(execute("-7 + 13 + -6;") == 0),
|
2023-11-15 11:59:38 +01:00
|
|
|
"Opérateurs unaires",
|
|
|
|
true
|
2023-11-14 17:03:42 +01:00
|
|
|
);
|
2023-11-10 22:24:14 +01:00
|
|
|
|
2023-11-15 16:10:36 +01:00
|
|
|
_TEST_ASSERT(
|
2023-11-27 21:16:39 +01:00
|
|
|
_TEST_IS_EXCEPTION(execute("1 / 0;"), RuntimeError),
|
2023-11-15 16:10:36 +01:00
|
|
|
"Division par 0",
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
_TEST_ASSERT(
|
2023-11-27 21:16:39 +01:00
|
|
|
_TEST_IS_EXCEPTION(execute("1 % 0;"), RuntimeError),
|
2023-11-15 16:10:36 +01:00
|
|
|
"Modulo par 0",
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2023-11-22 15:31:30 +01:00
|
|
|
_TEST_ASSERT(
|
|
|
|
_TEST_NO_EXCEPTION(execute("int a = 27; ++a;") == 28),
|
|
|
|
"Incrémentation par la gauche",
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
_TEST_ASSERT(
|
|
|
|
_TEST_NO_EXCEPTION(execute("int a = 27; a--;") == 27),
|
|
|
|
"Décrémentation par la droite",
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
_TEST_ASSERT(
|
|
|
|
_TEST_NO_EXCEPTION(execute("int a = 27; -(+-++a);") == 28),
|
|
|
|
"Incrémentation et opérations unaires",
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2023-11-27 21:16:39 +01:00
|
|
|
_TEST_ASSERT(
|
|
|
|
_TEST_NO_EXCEPTION(execute("1 < 2;") == 1),
|
|
|
|
"Inférieur strict",
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
_TEST_ASSERT(
|
|
|
|
_TEST_NO_EXCEPTION(execute("2 > 3;") == 0),
|
|
|
|
"Supérieur strict",
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
_TEST_ASSERT(
|
|
|
|
_TEST_NO_EXCEPTION(execute("2 <= 1;") == 0),
|
|
|
|
"Inférieur ou égal",
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
_TEST_ASSERT(
|
|
|
|
_TEST_NO_EXCEPTION(execute("2 >= 1;") == 1),
|
|
|
|
"Supérieur ou égal",
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2023-11-10 22:24:14 +01:00
|
|
|
return 0;
|
|
|
|
}
|