diff --git a/src/language/node_processor/DoWhileProcessor.hpp b/src/language/node_processor/DoWhileProcessor.hpp index cd94fef46b65bbbbc04ff0f24c8db9e058c82c7f..3a1a9d59bd485973e52ac60eb076efc003e89f4e 100644 --- a/src/language/node_processor/DoWhileProcessor.hpp +++ b/src/language/node_processor/DoWhileProcessor.hpp @@ -30,7 +30,7 @@ class DoWhileProcessor final : public INodeProcessor if constexpr (std::is_arithmetic_v<T>) { return value; } else { - return false; + return false; // LCOV_EXCL_LINE (unreachable: only there for compilation purpose) } }, m_node.children[1]->m_value)); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 1d81a071698a09d9e664cddac818f1e48c5c175b..baa29881a0efbf691c00506ce6f34571ef8b1be6 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -30,6 +30,7 @@ add_executable (unit_tests test_BiCGStab.cpp test_ContinueProcessor.cpp test_CRSMatrix.cpp + test_DoWhileProcessor.cpp test_ExecUntilBreakOrContinue.cpp test_FakeProcessor.cpp test_ForProcessor.cpp diff --git a/tests/test_DoWhileProcessor.cpp b/tests/test_DoWhileProcessor.cpp new file mode 100644 index 0000000000000000000000000000000000000000..a04801163ba3f16c3054145049c2ba85060c2120 --- /dev/null +++ b/tests/test_DoWhileProcessor.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("DoWhileProcessor", "[language]") +{ + SECTION("simple loop") + { + std::string_view data = R"( +N i = 3; +N j = 0; +do { + j++; + i += j; +} while(i<10); +)"; + CHECK_WHILE_PROCESSOR_RESULT(data, "i", 13ul); + } + + SECTION("do-while with break") + { + std::string_view data = R"( +N i = 3; +N j = 0; +do { + j++; + if (j==2) break; + i += j; +} while(i<10); +)"; + CHECK_WHILE_PROCESSOR_RESULT(data, "i", 4ul); + } + + SECTION("do-while with continue") + { + std::string_view data = R"( +N i = 3; +N j = 0; +do { + j++; + if (j<=3) continue; + i += j; +} while(i<10); +)"; + CHECK_WHILE_PROCESSOR_RESULT(data, "i", 12ul); + } +}