diff --git a/src/language/node_processor/ForProcessor.hpp b/src/language/node_processor/ForProcessor.hpp index 990640db1d3d84db55fb97dd752f18021bb3a734..e06db01d08fd2cdf540f59be51f8fec0042f14e4 100644 --- a/src/language/node_processor/ForProcessor.hpp +++ b/src/language/node_processor/ForProcessor.hpp @@ -22,7 +22,7 @@ class ForProcessor 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 5b8af5949b7cc1b246c059c0d23cfa7b0810e11b..1d81a071698a09d9e664cddac818f1e48c5c175b 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -32,6 +32,7 @@ add_executable (unit_tests test_CRSMatrix.cpp test_ExecUntilBreakOrContinue.cpp test_FakeProcessor.cpp + test_ForProcessor.cpp test_IfProcessor.cpp test_IncDecExpressionProcessor.cpp test_INodeProcessor.cpp diff --git a/tests/test_ForProcessor.cpp b/tests/test_ForProcessor.cpp new file mode 100644 index 0000000000000000000000000000000000000000..bd833f7f540f40fdaa1e80d8691867b91b3463ec --- /dev/null +++ b/tests/test_ForProcessor.cpp @@ -0,0 +1,116 @@ +#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_FOR_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("ForProcessor", "[language]") +{ + SECTION("simple for") + { + std::string_view data = R"( +N i = 0; +for(N l=0; l<10; ++l) { + i += l; +} +)"; + CHECK_FOR_PROCESSOR_RESULT(data, "i", 45ul); + } + + SECTION("for with break") + { + std::string_view data = R"( +N i = 0; +for(N l=0; l<10; ++l) { + i += l; + if (i > 30) break; +} +)"; + CHECK_FOR_PROCESSOR_RESULT(data, "i", 36ul); + } + + SECTION("for with continue") + { + std::string_view data = R"( +N i = 0; +for(N l=0; l<10; ++l) { + if (l<3) continue; + i += l; +} +)"; + CHECK_FOR_PROCESSOR_RESULT(data, "i", 42ul); + } + + // SECTION("simple if(true)else") + // { + // std::string_view data = R"( + // N i = 0; + // if(true) { + // i = 1; + // } else { + // i = 2; + // } + // )"; + // CHECK_FOR_PROCESSOR_RESULT(data, "i", 1ul); + // } + + // SECTION("simple if(false)") + // { + // std::string_view data = R"( + // N i = 0; + // if(false) { + // i = 1; + // } else { + // i = 2; + // } + // )"; + // CHECK_FOR_PROCESSOR_RESULT(data, "i", 2ul); + // } +}