From c982485bc907582dfeb58f7f074cbd49f6d34137 Mon Sep 17 00:00:00 2001 From: Stephane Del Pino <stephane.delpino44@gmail.com> Date: Tue, 3 Sep 2019 17:34:15 +0200 Subject: [PATCH] Add tests for WhileProcessor --- .../node_processor/WhileProcessor.hpp | 2 +- tests/CMakeLists.txt | 1 + tests/test_WhileProcessor.cpp | 96 +++++++++++++++++++ 3 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 tests/test_WhileProcessor.cpp diff --git a/src/language/node_processor/WhileProcessor.hpp b/src/language/node_processor/WhileProcessor.hpp index 60e2702f9..cf8b329d4 100644 --- a/src/language/node_processor/WhileProcessor.hpp +++ b/src/language/node_processor/WhileProcessor.hpp @@ -21,7 +21,7 @@ class WhileProcessor final : public INodeProcessor if constexpr (std::is_arithmetic_v<T>) { return value; } else { - return false; + return false; // LCOV_EXCL_LINE (only there for compilation purpose unreachable) } }, m_node.children[0]->m_value)); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index ef6cd0260..456f6f987 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -46,6 +46,7 @@ add_executable (unit_tests test_TinyVector.cpp test_UnaryExpressionProcessor.cpp test_Vector.cpp + test_WhileProcessor.cpp ) add_executable (mpi_unit_tests diff --git a/tests/test_WhileProcessor.cpp b/tests/test_WhileProcessor.cpp new file mode 100644 index 000000000..0b947150c --- /dev/null +++ b/tests/test_WhileProcessor.cpp @@ -0,0 +1,96 @@ +#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_WHILE_PROCESSOR_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("WhileProcessor", "[language]") +{ + SECTION("simple loop") + { + std::string_view data = R"( +N i = 3; +N j = 0; +while(i<10) { + j++; + i += j; +} +)"; + CHECK_WHILE_PROCESSOR_RESULT(data, "i", 13ul); + } + + SECTION("simple with break") + { + std::string_view data = R"( +N i = 3; +N j = 0; +while(i<10) { + j++; + if (j==2) break; + i += j; +} +)"; + CHECK_WHILE_PROCESSOR_RESULT(data, "i", 4ul); + } + + SECTION("simple with continue") + { + std::string_view data = R"( +N i = 3; +N j = 0; +while(i<10) { + j++; + if (j<=3) continue; + i += j; +} +)"; + CHECK_WHILE_PROCESSOR_RESULT(data, "i", 12ul); + } +} -- GitLab