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

Add tests for TupleToVectorProcessor

parent 23f44a92
No related branches found
No related tags found
1 merge request!37Feature/language
......@@ -28,7 +28,9 @@ class TupleToVectorProcessor final : public INodeProcessor
if constexpr (std::is_arithmetic_v<ValueT>) {
x[i] = v;
} else {
// LCOV_EXCL_START
Assert(false, "unexpected value type");
// LCOV_EXCL_STOP
}
},
v[i]);
......
......@@ -68,6 +68,7 @@ add_executable (unit_tests
test_SymbolTable.cpp
test_TinyMatrix.cpp
test_TinyVector.cpp
test_TupleToVectorProcessor.cpp
test_UnaryExpressionProcessor.cpp
test_Vector.cpp
test_WhileProcessor.cpp
......
#include <catch2/catch.hpp>
#include <ASTBuilder.hpp>
#include <ASTNodeDataTypeBuilder.hpp>
#include <ASTModulesImporter.hpp>
#include <ASTNodeDeclarationToAffectationConverter.hpp>
#include <ASTNodeTypeCleaner.hpp>
#include <ASTNodeExpressionBuilder.hpp>
#include <ASTNodeAffectationExpressionBuilder.hpp>
#include <ASTSymbolTableBuilder.hpp>
#include <ASTPrinter.hpp>
#include <Demangle.hpp>
#include <PEGGrammar.hpp>
#include <sstream>
#define CHECK_EVALUATION_RESULT(data, variable_name, expected_value) \
{ \
string_input input{data, "test.pgs"}; \
auto ast = ASTBuilder::build(input); \
\
ASTModulesImporter{*ast}; \
ASTNodeTypeCleaner<language::import_instruction>{*ast}; \
\
ASTSymbolTableBuilder{*ast}; \
ASTNodeDataTypeBuilder{*ast}; \
\
ASTNodeDeclarationToAffectationConverter{*ast}; \
ASTNodeTypeCleaner<language::declaration>{*ast}; \
ASTNodeTypeCleaner<language::let_declaration>{*ast}; \
\
ASTNodeExpressionBuilder{*ast}; \
ExecutionPolicy 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 attributes = symbol->attributes(); \
auto value = std::get<decltype(expected_value)>(attributes.value()); \
\
REQUIRE(value == expected_value); \
}
#define CHECK_EVALUATION_THROWS_WITH(data, error_message) \
{ \
auto eval = [&] { \
string_input input{data, "test.pgs"}; \
auto ast = ASTBuilder::build(input); \
\
ASTModulesImporter{*ast}; \
ASTNodeTypeCleaner<language::import_instruction>{*ast}; \
\
ASTSymbolTableBuilder{*ast}; \
ASTNodeDataTypeBuilder{*ast}; \
\
ASTNodeDeclarationToAffectationConverter{*ast}; \
ASTNodeTypeCleaner<language::declaration>{*ast}; \
ASTNodeTypeCleaner<language::let_declaration>{*ast}; \
\
ASTNodeExpressionBuilder{*ast}; \
ExecutionPolicy exec_policy; \
ast->execute(exec_policy); \
}; \
\
REQUIRE_THROWS_WITH(eval(), error_message); \
}
TEST_CASE("TupleToVectorProcessor", "[language]")
{
SECTION("Return tuple -> R^3")
{
std::string_view data = R"(
let f: R*R*R->R^3, (x,y,z) -> (x,y,z);
R^3 x = f(1,2,3);
)";
CHECK_EVALUATION_RESULT(data, "x", (TinyVector<3>{1, 2, 3}));
}
SECTION("Return tuple -> R^2")
{
std::string_view data = R"(
let f: R^3->R^2, x -> (x[2],x[1]);
R^3 x = (1,2,3);
R^2 y = f(x);
)";
CHECK_EVALUATION_RESULT(data, "y", (TinyVector<2>{3, 2}));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment