From 39aae056559b689d1c5b39a5413559050441e714 Mon Sep 17 00:00:00 2001 From: Stephane Del Pino <stephane.delpino44@gmail.com> Date: Mon, 2 Sep 2019 19:10:11 +0200 Subject: [PATCH] Add tests for UnaryExpressionProcessor --- tests/CMakeLists.txt | 1 + tests/test_UnaryExpressionProcessor.cpp | 74 +++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 tests/test_UnaryExpressionProcessor.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 9c9404535..f49aaa3a6 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 000000000..f20c42e6d --- /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); + } +} -- GitLab