Skip to content
Snippets Groups Projects
Commit 547eb068 authored by Stéphane Del Pino's avatar Stéphane Del Pino
Browse files

Add tests for ForProcessor

parent b29173e4
No related branches found
No related tags found
1 merge request!37Feature/language
...@@ -22,7 +22,7 @@ class ForProcessor final : public INodeProcessor ...@@ -22,7 +22,7 @@ class ForProcessor final : public INodeProcessor
if constexpr (std::is_arithmetic_v<T>) { if constexpr (std::is_arithmetic_v<T>) {
return value; return value;
} else { } else {
return false; return false; // LCOV_EXCL_LINE (unreachable: only there for compilation purpose)
} }
}, },
m_node.children[1]->m_value)); m_node.children[1]->m_value));
......
...@@ -32,6 +32,7 @@ add_executable (unit_tests ...@@ -32,6 +32,7 @@ add_executable (unit_tests
test_CRSMatrix.cpp test_CRSMatrix.cpp
test_ExecUntilBreakOrContinue.cpp test_ExecUntilBreakOrContinue.cpp
test_FakeProcessor.cpp test_FakeProcessor.cpp
test_ForProcessor.cpp
test_IfProcessor.cpp test_IfProcessor.cpp
test_IncDecExpressionProcessor.cpp test_IncDecExpressionProcessor.cpp
test_INodeProcessor.cpp test_INodeProcessor.cpp
......
#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);
// }
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment