c-repl/test/variables.cpp

57 lines
1.2 KiB
C++
Raw Normal View History

2023-11-10 22:24:14 +01:00
#include "include/test.h"
#include "../src/include/tokenize.h"
#include "../src/include/parser.h"
#include "../src/include/interpreter.h"
int execute(string s) {
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);
EvalResult res = eval(ast);
2023-11-15 16:10:36 +01:00
_debug_flush_memory();
2023-11-10 22:24:14 +01:00
return get<int>(res);
}
int main() {
_TEST_PRESENTATION("Variables");
2023-11-14 17:03:42 +01:00
_TEST_ASSERT(
_TEST_NO_EXCEPTION(execute("int uneVariableFarfelue__ = 12;") == 12),
2023-11-15 11:59:38 +01:00
"Déclaration avec assignement",
true
2023-11-14 17:03:42 +01:00
);
2023-11-10 22:24:14 +01:00
2023-11-14 17:03:42 +01:00
_TEST_ASSERT(
_TEST_NO_EXCEPTION(execute("int x = 5; x = x + 3;") == 8),
2023-11-15 11:59:38 +01:00
"Déclaration puis assignement",
true
2023-11-14 17:03:42 +01:00
);
2023-11-10 22:24:14 +01:00
2023-11-14 20:00:14 +01:00
_TEST_ASSERT(
_TEST_NO_EXCEPTION(execute("int a = 27; ++a;") == 28),
2023-11-15 11:59:38 +01:00
"Incrémentation par la gauche",
true
2023-11-14 20:00:14 +01:00
);
_TEST_ASSERT(
_TEST_NO_EXCEPTION(execute("int a = 27; a--;") == 27),
2023-11-15 11:59:38 +01:00
"Décrémentation par la droite",
true
2023-11-14 20:00:14 +01:00
);
_TEST_ASSERT(
_TEST_NO_EXCEPTION(execute("int a = 27; -(+-++a);") == 28),
2023-11-15 11:59:38 +01:00
"Incrémentation et opérations unaires",
true
2023-11-14 20:00:14 +01:00
);
2023-11-15 16:10:36 +01:00
_TEST_ASSERT(
_TEST_IS_EXCEPTION(execute("1 + x;"), RuntimeError),
"Identifieur indéfini",
true
);
2023-11-10 22:24:14 +01:00
return 0;
}