Skip to content
Snippets Groups Projects
Commit 39aae056 authored by Stéphane Del Pino's avatar Stéphane Del Pino
Browse files

Add tests for UnaryExpressionProcessor

parent ff2fb440
No related branches found
No related tags found
1 merge request!37Feature/language
......@@ -39,6 +39,7 @@ add_executable (unit_tests
test_SymbolTable.cpp
test_TinyMatrix.cpp
test_TinyVector.cpp
test_UnaryExpressionProcessor.cpp
test_Vector.cpp
)
......
#include <catch2/catch.hpp>
#include <ASTNodeValueBuilder.hpp>
#include <ASTBuilder.hpp>
#include <ASTNodeDataTypeBuilder.hpp>
#include <ASTNodeDeclarationCleaner.hpp>
#include <ASTNodeDeclarationToAffectationConverter.hpp>
#include <ASTNodeExpressionBuilder.hpp>
#include <ASTNodeAffectationExpressionBuilder.hpp>
#include <ASTSymbolTableBuilder.hpp>
#include <ASTPrinter.hpp>
#include <Demangle.hpp>
#include <PEGGrammar.hpp>
#include <sstream>
#define CHECK_UNARY_EXPRESSION_RESULT(data, variable_name, expected_value) \
{ \
string_input input{data, "test.pgs"}; \
auto ast = ASTBuilder::build(input); \
\
ASTSymbolTableBuilder{*ast}; \
ASTNodeDataTypeBuilder{*ast}; \
ASTNodeValueBuilder{*ast}; \
\
ASTNodeDeclarationToAffectationConverter{*ast}; \
ASTNodeDeclarationCleaner{*ast}; \
\
ASTNodeExpressionBuilder{*ast}; \
ExecUntilBreakOrContinue exec_policy; \
ast->execute(exec_policy); \
\
auto symbol_table = ast->m_symbol_table; \
\
using namespace TAO_PEGTL_NAMESPACE; \
position use_position{internal::iterator{"fixture"}, "fixture"}; \
use_position.byte = 10000; \
auto [symbol, found] = symbol_table->find(variable_name, use_position); \
\
auto attribute = symbol->second; \
auto value = std::get<decltype(expected_value)>(attribute.value()); \
\
REQUIRE(value == expected_value); \
}
TEST_CASE("UnaryExpressionProcessor", "[language]")
{
SECTION("unary minus")
{
CHECK_UNARY_EXPRESSION_RESULT(R"(N n = 2; Z z = -n;)", "z", -2l);
CHECK_UNARY_EXPRESSION_RESULT(R"(Z p = 2; Z q = -p;)", "q", -2l);
CHECK_UNARY_EXPRESSION_RESULT(R"(R r = 2; r = -r;)", "r", -2.);
}
SECTION("unary not")
{
CHECK_UNARY_EXPRESSION_RESULT(R"(B b = false; b = not b;)", "b", true);
CHECK_UNARY_EXPRESSION_RESULT(R"(B b = true; b = not b;)", "b", false);
CHECK_UNARY_EXPRESSION_RESULT(R"(N n = 0; B b = not n;)", "b", true);
CHECK_UNARY_EXPRESSION_RESULT(R"(N n = 2; B b = not n;)", "b", false);
CHECK_UNARY_EXPRESSION_RESULT(R"(Z z = 0; B b = not z;)", "b", true);
CHECK_UNARY_EXPRESSION_RESULT(R"(Z z = -2; B b = not z;)", "b", false);
CHECK_UNARY_EXPRESSION_RESULT(R"(R r = 0; B b = not r;)", "b", true);
CHECK_UNARY_EXPRESSION_RESULT(R"(R r = 2.1; B b = not r;)", "b", false);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment