c-repl/test/analysis.cpp

71 lines
1.5 KiB
C++
Raw Normal View History

2024-01-12 15:40:03 +01:00
#include "include/test.hpp"
#include "../src/include/execute.hpp"
#include "../src/include/utils.hpp"
int execute(string s) {
Memory memory;
return get<int>(execute(split_string(s, '\n'), memory));
}
int main() {
_TEST_PRESENTATION("Analyse statique");
_TEST_ASSERT(
_TEST_IS_EXCEPTION(execute("unknown x;"), ErrorType::UnknownType),
"Type inconnu",
true
);
_TEST_ASSERT(
_TEST_IS_EXCEPTION(execute(R"(
void f() {}
int x = f();
)"), ErrorType::TypeNotCastable),
"Cast depuis void",
true
);
_TEST_ASSERT(
_TEST_IS_EXCEPTION(execute(R"(
void f() {}
5.0 / f();
)"), ErrorType::ExpectedArithmeticType),
"Division par void",
true
);
_TEST_ASSERT(
_TEST_IS_EXCEPTION(execute("5 % (5.0 / 2.0);"), ErrorType::ExpectedIntegralType),
"Modulo flottant",
true
);
_TEST_ASSERT(
_TEST_IS_EXCEPTION(execute("void x;"), ErrorType::IncompleteType),
"Variable de type void",
true
);
_TEST_ASSERT(
_TEST_IS_EXCEPTION(execute(R"(
int x;
int x;
)"), ErrorType::AlreadyDeclaredIdentifier),
"Redéclaration",
true
);
_TEST_ASSERT(
_TEST_IS_EXCEPTION(execute(R"(
int x;
void x() {}
)"), ErrorType::IncompatibleRedefinition),
"Redéfinition",
true
);
return 0;
}