From c7c8fb5fd9a8fb34da4be01cbab18f0712d10026 Mon Sep 17 00:00:00 2001 From: Stephane Del Pino <stephane.delpino44@gmail.com> Date: Fri, 20 Dec 2019 15:06:55 +0100 Subject: [PATCH] Add tests for ASTNodeDataTypeFlattener --- tests/CMakeLists.txt | 1 + tests/test_ASTNodeDataTypeFlattener.cpp | 145 ++++++++++++++++++++++++ 2 files changed, 146 insertions(+) create mode 100644 tests/test_ASTNodeDataTypeFlattener.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 60393d75f..ffa8ee7a3 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -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 diff --git a/tests/test_ASTNodeDataTypeFlattener.cpp b/tests/test_ASTNodeDataTypeFlattener.cpp new file mode 100644 index 000000000..bd942da83 --- /dev/null +++ b/tests/test_ASTNodeDataTypeFlattener.cpp @@ -0,0 +1,145 @@ +#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()); + } + } +} -- GitLab