diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 9c9404535ffd08d971fa59ae32cc07607cc52b8d..f49aaa3a6ef146954b7cb593243685f39d107979 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -39,6 +39,7 @@ add_executable (unit_tests test_SymbolTable.cpp test_TinyMatrix.cpp test_TinyVector.cpp + test_UnaryExpressionProcessor.cpp test_Vector.cpp ) diff --git a/tests/test_UnaryExpressionProcessor.cpp b/tests/test_UnaryExpressionProcessor.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f20c42e6d81391b79ce92ff5e2903da4dd0b2576 --- /dev/null +++ b/tests/test_UnaryExpressionProcessor.cpp @@ -0,0 +1,74 @@ +#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); + } +}