From 31fd100622d9169f8b384eae29239a6e402feb00 Mon Sep 17 00:00:00 2001 From: Yangbo Long Date: Thu, 5 Sep 2024 23:05:03 -0400 Subject: [PATCH] Add unit tests Signed-off-by: Yangbo Long --- test/feature/CMakeLists.txt | 1 + test/feature/idl_parser/CMakeLists.txt | 48 +++++ test/feature/idl_parser/IdlParserTests.cpp | 214 +++++++++++++++++++++ 3 files changed, 263 insertions(+) create mode 100644 test/feature/idl_parser/CMakeLists.txt create mode 100644 test/feature/idl_parser/IdlParserTests.cpp diff --git a/test/feature/CMakeLists.txt b/test/feature/CMakeLists.txt index 2492999c12b..a4fc1572880 100644 --- a/test/feature/CMakeLists.txt +++ b/test/feature/CMakeLists.txt @@ -13,3 +13,4 @@ # limitations under the License. add_subdirectory(dynamic_types) +add_subdirectory(idl_parser) diff --git a/test/feature/idl_parser/CMakeLists.txt b/test/feature/idl_parser/CMakeLists.txt new file mode 100644 index 00000000000..c8786bf9a55 --- /dev/null +++ b/test/feature/idl_parser/CMakeLists.txt @@ -0,0 +1,48 @@ +# Copyright 2024 Proyectos y Sistemas de Mantenimiento SL (eProsima). +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +if(WIN32) + add_definitions( + -D_WIN32_WINNT=0x0601 + -D_CRT_SECURE_NO_WARNINGS + ) +endif() + +add_executable(IdlParserTests IdlParserTests.cpp) +target_compile_definitions(IdlParserTests PRIVATE + $<$>,$>:__DEBUG> + $<$:__INTERNALDEBUG> # Internal debug activated. +) +target_include_directories(IdlParserTests + PRIVATE + ${PROJECT_SOURCE_DIR}/test/utils/ +) +target_link_libraries(IdlParserTests + GTest::gtest + $<$:iphlpapi$Shlwapi> + $<$:ws2_32> + fastcdr + fastdds +) +gtest_discover_tests(IdlParserTests) + +if(EXISTS "${PROJECT_SOURCE_DIR}/thirdparty/dds-types-test/IDL") + message(STATUS "Copying IDL directory from ${PROJECT_SOURCE_DIR}/thirdparty/dds-types-test/IDL for idl_parser testing") + add_custom_command( + TARGET IdlParserTests POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_directory ${PROJECT_SOURCE_DIR}/thirdparty/dds-types-test/IDL $/IDL + ) +else() + message(WARNING "The folder thirdparty/dds-types-test/IDL does not exist. The git submodule might not have been initialized.") +endif() diff --git a/test/feature/idl_parser/IdlParserTests.cpp b/test/feature/idl_parser/IdlParserTests.cpp new file mode 100644 index 00000000000..fe8a9fec07c --- /dev/null +++ b/test/feature/idl_parser/IdlParserTests.cpp @@ -0,0 +1,214 @@ +// Copyright 2024 Proyectos y Sistemas de Mantenimiento SL (eProsima). +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include +#include +#include +#include + +using namespace eprosima::fastdds::rtps; +using namespace eprosima::fastdds::dds; + + +class IdlParserTests : public ::testing::Test +{ +public: + + IdlParserTests() = default; + + ~IdlParserTests() + { + Log::Flush(); + } + + virtual void TearDown() + { + DynamicDataFactory::delete_instance(); + DynamicTypeBuilderFactory::delete_instance(); + } + +}; + +TEST_F(IdlParserTests, primitives) +{ + DynamicTypeBuilderFactory::_ref_type factory {DynamicTypeBuilderFactory::get_instance()}; + std::vector empty_include_paths; + + DynamicTypeBuilder::_ref_type builder1 = factory->create_type_w_document("IDL/primitives.idl", "ShortStruct", empty_include_paths); + EXPECT_TRUE(builder1); + DynamicType::_ref_type type1 = builder1->build(); + ASSERT_TRUE(type1); + DynamicData::_ref_type data1 {DynamicDataFactory::get_instance()->create_data(type1)}; + ASSERT_TRUE(data1); + int16_t test1 {0}; + EXPECT_EQ(data1->set_int16_value(0, 100), RETCODE_OK); + EXPECT_EQ(data1->get_int16_value(test1, 0), RETCODE_OK); + EXPECT_EQ(test1, 100); + + DynamicTypeBuilder::_ref_type builder2 = factory->create_type_w_document("IDL/primitives.idl", "UShortStruct", empty_include_paths); + EXPECT_TRUE(builder2); + DynamicType::_ref_type type2 = builder2->build(); + ASSERT_TRUE(type2); + DynamicData::_ref_type data2 {DynamicDataFactory::get_instance()->create_data(type2)}; + ASSERT_TRUE(data2); + + DynamicTypeBuilder::_ref_type builder3 = factory->create_type_w_document("IDL/primitives.idl", "LongStruct", empty_include_paths); + EXPECT_TRUE(builder3); + DynamicType::_ref_type type3 = builder3->build(); + ASSERT_TRUE(type3); + + DynamicTypeBuilder::_ref_type builder4 = factory->create_type_w_document("IDL/primitives.idl", "ULongStruct", empty_include_paths); + EXPECT_TRUE(builder4); + DynamicType::_ref_type type4 = builder4->build(); + ASSERT_TRUE(type4); + + DynamicTypeBuilder::_ref_type builder5 = factory->create_type_w_document("IDL/primitives.idl", "LongLongStruct", empty_include_paths); + EXPECT_TRUE(builder5); + DynamicType::_ref_type type5 = builder5->build(); + ASSERT_TRUE(type5); + + DynamicTypeBuilder::_ref_type builder6 = factory->create_type_w_document("IDL/primitives.idl", "ULongLongStruct", empty_include_paths); + EXPECT_TRUE(builder6); + DynamicType::_ref_type type6 = builder6->build(); + ASSERT_TRUE(type6); + + DynamicTypeBuilder::_ref_type builder7 = factory->create_type_w_document("IDL/primitives.idl", "FloatStruct", empty_include_paths); + EXPECT_TRUE(builder7); + DynamicType::_ref_type type7 = builder7->build(); + ASSERT_TRUE(type7); + + DynamicTypeBuilder::_ref_type builder8 = factory->create_type_w_document("IDL/primitives.idl", "DoubleStruct", empty_include_paths); + EXPECT_TRUE(builder8); + DynamicType::_ref_type type8 = builder8->build(); + ASSERT_TRUE(type8); + + DynamicTypeBuilder::_ref_type builder9 = factory->create_type_w_document("IDL/primitives.idl", "LongDoubleStruct", empty_include_paths); + EXPECT_TRUE(builder9); + DynamicType::_ref_type type9 = builder9->build(); + ASSERT_TRUE(type9); + + DynamicTypeBuilder::_ref_type builder10 = factory->create_type_w_document("IDL/primitives.idl", "BooleanStruct", empty_include_paths); + EXPECT_TRUE(builder10); + DynamicType::_ref_type type10 = builder10->build(); + ASSERT_TRUE(type10); + + DynamicTypeBuilder::_ref_type builder11 = factory->create_type_w_document("IDL/primitives.idl", "OctetStruct", empty_include_paths); + EXPECT_TRUE(builder11); + DynamicType::_ref_type type11 = builder11->build(); + ASSERT_TRUE(type11); + + DynamicTypeBuilder::_ref_type builder12 = factory->create_type_w_document("IDL/primitives.idl", "CharStruct", empty_include_paths); + EXPECT_TRUE(builder12); + DynamicType::_ref_type type12 = builder12->build(); + ASSERT_TRUE(type12); + + DynamicTypeBuilder::_ref_type builder13 = factory->create_type_w_document("IDL/primitives.idl", "WCharStruct", empty_include_paths); + EXPECT_TRUE(builder13); + DynamicType::_ref_type type13 = builder13->build(); + ASSERT_TRUE(type13); + + DynamicTypeBuilder::_ref_type builder14 = factory->create_type_w_document("IDL/primitives.idl", "Int8Struct", empty_include_paths); + EXPECT_TRUE(builder14); + DynamicType::_ref_type type14 = builder14->build(); + ASSERT_TRUE(type14); + + DynamicTypeBuilder::_ref_type builder15 = factory->create_type_w_document("IDL/primitives.idl", "Uint8Struct", empty_include_paths); + EXPECT_TRUE(builder15); + DynamicType::_ref_type type15 = builder15->build(); + ASSERT_TRUE(type15); + + DynamicTypeBuilder::_ref_type builder16 = factory->create_type_w_document("IDL/primitives.idl", "Int16Struct", empty_include_paths); + EXPECT_TRUE(builder16); + DynamicType::_ref_type type16 = builder16->build(); + ASSERT_TRUE(type16); + + DynamicTypeBuilder::_ref_type builder17 = factory->create_type_w_document("IDL/primitives.idl", "Uint16Struct", empty_include_paths); + EXPECT_TRUE(builder17); + DynamicType::_ref_type type17 = builder17->build(); + ASSERT_TRUE(type17); + + DynamicTypeBuilder::_ref_type builder18 = factory->create_type_w_document("IDL/primitives.idl", "Int32Struct", empty_include_paths); + EXPECT_TRUE(builder18); + DynamicType::_ref_type type18 = builder18->build(); + ASSERT_TRUE(type18); + + DynamicTypeBuilder::_ref_type builder19 = factory->create_type_w_document("IDL/primitives.idl", "Uint32Struct", empty_include_paths); + EXPECT_TRUE(builder19); + DynamicType::_ref_type type19 = builder19->build(); + ASSERT_TRUE(type19); + + DynamicTypeBuilder::_ref_type builder20 = factory->create_type_w_document("IDL/primitives.idl", "Int64Struct", empty_include_paths); + EXPECT_TRUE(builder20); + DynamicType::_ref_type type20 = builder20->build(); + ASSERT_TRUE(type20); + + DynamicTypeBuilder::_ref_type builder21 = factory->create_type_w_document("IDL/primitives.idl", "Uint64Struct", empty_include_paths); + EXPECT_TRUE(builder21); + DynamicType::_ref_type type21 = builder21->build(); + ASSERT_TRUE(type21); +} + +TEST_F(IdlParserTests, strings) +{ + DynamicTypeBuilderFactory::_ref_type factory {DynamicTypeBuilderFactory::get_instance()}; + std::vector empty_include_paths; + + DynamicTypeBuilder::_ref_type builder1 = factory->create_type_w_document("IDL/strings.idl", "StringStruct", empty_include_paths); + EXPECT_TRUE(builder1); + DynamicType::_ref_type type1 = builder1->build(); + ASSERT_TRUE(type1); + DynamicData::_ref_type data1 {DynamicDataFactory::get_instance()->create_data(type1)}; + ASSERT_TRUE(data1); + std::string test1; + EXPECT_EQ(data1->set_string_value(0, "hello"), RETCODE_OK); + EXPECT_EQ(data1->get_string_value(test1, 0), RETCODE_OK); + EXPECT_EQ(test1, "hello"); + + DynamicTypeBuilder::_ref_type builder2 = factory->create_type_w_document("IDL/strings.idl", "WStringStruct", empty_include_paths); + EXPECT_TRUE(builder2); + DynamicType::_ref_type type2 = builder2->build(); + ASSERT_TRUE(type2); + + DynamicTypeBuilder::_ref_type builder3 = factory->create_type_w_document("IDL/strings.idl", "SmallStringStruct", empty_include_paths); + EXPECT_TRUE(builder3); + DynamicType::_ref_type type3 = builder3->build(); + ASSERT_TRUE(type3); + + DynamicTypeBuilder::_ref_type builder4 = factory->create_type_w_document("IDL/strings.idl", "SmallWStringStruct", empty_include_paths); + EXPECT_TRUE(builder4); + DynamicType::_ref_type type4 = builder4->build(); + ASSERT_TRUE(type4); + + DynamicTypeBuilder::_ref_type builder5 = factory->create_type_w_document("IDL/strings.idl", "LargeStringStruct", empty_include_paths); + EXPECT_TRUE(builder5); + DynamicType::_ref_type type5 = builder5->build(); + ASSERT_TRUE(type5); + + DynamicTypeBuilder::_ref_type builder6 = factory->create_type_w_document("IDL/strings.idl", "LargeWStringStruct", empty_include_paths); + EXPECT_TRUE(builder6); + DynamicType::_ref_type type6 = builder6->build(); + ASSERT_TRUE(type6); +} + +int main( + int argc, + char** argv) +{ + Log::SetVerbosity(Log::Info); + + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +}