93 lines
2.0 KiB
C++
93 lines
2.0 KiB
C++
#include "include/test.h"
|
|
|
|
#include "../src/include/memory.h"
|
|
#include "../src/include/tokenize.h"
|
|
#include "../src/include/parser.h"
|
|
#include "../src/include/interpreter.h"
|
|
|
|
int execute(string s) {
|
|
Memory memory = Memory();
|
|
vector<Token> tokens = tokenize({ s });
|
|
Node ast = parse(tokens);
|
|
EvalResult res = eval(ast, memory);
|
|
|
|
return get<int>(res);
|
|
}
|
|
|
|
int main() {
|
|
_TEST_PRESENTATION("Expressions Arithmétiques");
|
|
|
|
_TEST_ASSERT(
|
|
_TEST_NO_EXCEPTION(execute("10 + 3 * (7 - 2);") == 25),
|
|
"Priorités",
|
|
true
|
|
);
|
|
|
|
_TEST_ASSERT(
|
|
_TEST_NO_EXCEPTION(execute("12 - 4 - 20;") == -12),
|
|
"Parenthésage",
|
|
true
|
|
);
|
|
|
|
_TEST_ASSERT(
|
|
_TEST_NO_EXCEPTION(execute("-7 + 13 + -6;") == 0),
|
|
"Opérateurs unaires",
|
|
true
|
|
);
|
|
|
|
_TEST_ASSERT(
|
|
_TEST_IS_EXCEPTION(execute("1 / 0;"), RuntimeError),
|
|
"Division par 0",
|
|
true
|
|
);
|
|
|
|
_TEST_ASSERT(
|
|
_TEST_IS_EXCEPTION(execute("1 % 0;"), RuntimeError),
|
|
"Modulo par 0",
|
|
true
|
|
);
|
|
|
|
_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
|
|
);
|
|
|
|
_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
|
|
);
|
|
|
|
return 0;
|
|
} |