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

Add tests for ASTNodeDataTypeFlattener

parent 8ea90776
No related branches found
No related tags found
1 merge request!37Feature/language
......@@ -17,6 +17,7 @@ add_executable (unit_tests
test_ASTNodeDataType.cpp
test_ASTNodeDataTypeBuilder.cpp
test_ASTNodeDataTypeChecker.cpp
test_ASTNodeDataTypeFlattener.cpp
test_ASTNodeDeclarationToAffectationConverter.cpp
test_ASTNodeEmptyBlockCleaner.cpp
test_ASTNodeExpressionBuilder.cpp
......
#include <catch2/catch.hpp>
#include <ASTBuilder.hpp>
#include <ASTNodeDataTypeBuilder.hpp>
#include <ASTNodeDataTypeFlattener.hpp>
#include <ASTSymbolTableBuilder.hpp>
#include <ASTNodeDeclarationToAffectationConverter.hpp>
#include <ASTNodeTypeCleaner.hpp>
#include <ASTPrinter.hpp>
#include <PEGGrammar.hpp>
TEST_CASE("ASTNodeDataTypeFlattener", "[language]")
{
SECTION("simple types")
{
SECTION("B")
{
std::string_view data = R"(
B b = true;
b;
)";
string_input input{data, "test.pgs"};
auto root_node = ASTBuilder::build(input);
ASTSymbolTableBuilder{*root_node};
ASTNodeDataTypeBuilder{*root_node};
std::stringstream ast_output;
ast_output << '\n' << ASTPrinter{*root_node, ASTPrinter::Format::raw, {ASTPrinter::Info::none}};
REQUIRE(root_node->children[1]->is_type<language::name>());
ASTNodeDataTypeFlattener::FlattenedDataTypeList flattened_datatype_list;
ASTNodeDataTypeFlattener{*root_node->children[1], flattened_datatype_list};
REQUIRE(flattened_datatype_list.size() == 1);
REQUIRE(flattened_datatype_list[0].m_data_type == ASTNodeDataType::bool_t);
REQUIRE(&flattened_datatype_list[0].m_parent_node == root_node->children[1].get());
}
SECTION("N")
{
std::string_view data = R"(
N n;
n;
)";
string_input input{data, "test.pgs"};
auto root_node = ASTBuilder::build(input);
ASTSymbolTableBuilder{*root_node};
ASTNodeDataTypeBuilder{*root_node};
std::stringstream ast_output;
ast_output << '\n' << ASTPrinter{*root_node, ASTPrinter::Format::raw, {ASTPrinter::Info::none}};
REQUIRE(root_node->children[1]->is_type<language::name>());
ASTNodeDataTypeFlattener::FlattenedDataTypeList flattened_datatype_list;
ASTNodeDataTypeFlattener{*root_node->children[1], flattened_datatype_list};
REQUIRE(flattened_datatype_list.size() == 1);
REQUIRE(flattened_datatype_list[0].m_data_type == ASTNodeDataType::unsigned_int_t);
REQUIRE(&flattened_datatype_list[0].m_parent_node == root_node->children[1].get());
}
}
SECTION("Compound types")
{
SECTION("function evaluation -> N")
{
std::string_view data = R"(
let f: N -> N, n -> n;
f(2);
)";
string_input input{data, "test.pgs"};
auto root_node = ASTBuilder::build(input);
ASTSymbolTableBuilder{*root_node};
ASTNodeDataTypeBuilder{*root_node};
// optimizations
ASTNodeDeclarationToAffectationConverter{*root_node};
ASTNodeTypeCleaner<language::declaration>{*root_node};
ASTNodeTypeCleaner<language::let_declaration>{*root_node};
std::stringstream ast_output;
ast_output << '\n' << ASTPrinter{*root_node, ASTPrinter::Format::raw, {ASTPrinter::Info::none}};
REQUIRE(root_node->children[0]->is_type<language::function_evaluation>());
ASTNodeDataTypeFlattener::FlattenedDataTypeList flattened_datatype_list;
ASTNodeDataTypeFlattener{*root_node->children[0], flattened_datatype_list};
REQUIRE(flattened_datatype_list.size() == 1);
REQUIRE(flattened_datatype_list[0].m_data_type == ASTNodeDataType::unsigned_int_t);
REQUIRE(&flattened_datatype_list[0].m_parent_node == root_node->children[0].get());
}
SECTION("function evaluation -> N*R*B*string*Z")
{
std::string_view data = R"(
let f: N -> N*R*B*string*Z, n -> (n, 0.5*n, n>2, n, 3-n);
f(2);
)";
string_input input{data, "test.pgs"};
auto root_node = ASTBuilder::build(input);
ASTSymbolTableBuilder{*root_node};
ASTNodeDataTypeBuilder{*root_node};
// optimizations
ASTNodeDeclarationToAffectationConverter{*root_node};
ASTNodeTypeCleaner<language::declaration>{*root_node};
ASTNodeTypeCleaner<language::let_declaration>{*root_node};
std::stringstream ast_output;
ast_output << '\n' << ASTPrinter{*root_node, ASTPrinter::Format::raw, {ASTPrinter::Info::none}};
REQUIRE(root_node->children[0]->is_type<language::function_evaluation>());
ASTNodeDataTypeFlattener::FlattenedDataTypeList flattened_datatype_list;
ASTNodeDataTypeFlattener{*root_node->children[0], flattened_datatype_list};
REQUIRE(flattened_datatype_list.size() == 5);
REQUIRE(flattened_datatype_list[0].m_data_type == ASTNodeDataType::unsigned_int_t);
REQUIRE(flattened_datatype_list[1].m_data_type == ASTNodeDataType::double_t);
REQUIRE(flattened_datatype_list[2].m_data_type == ASTNodeDataType::bool_t);
REQUIRE(flattened_datatype_list[3].m_data_type == ASTNodeDataType::string_t);
REQUIRE(flattened_datatype_list[4].m_data_type == ASTNodeDataType::int_t);
// REQUIRE(&flattened_datatype_list[0].m_parent_node == root_node->children[0].get());
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment