Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Yangbo Long <yangbo.long.mav@gmail.com>
  • Loading branch information
YangboLong committed Sep 28, 2024
1 parent 7f55883 commit f4ffd30
Show file tree
Hide file tree
Showing 3 changed files with 263 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/feature/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
# limitations under the License.

add_subdirectory(dynamic_types)
add_subdirectory(idl_parser)
48 changes: 48 additions & 0 deletions test/feature/idl_parser/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
$<$<AND:$<NOT:$<BOOL:${WIN32}>>,$<STREQUAL:"${CMAKE_BUILD_TYPE}","Debug">>:__DEBUG>
$<$<BOOL:${INTERNAL_DEBUG}>:__INTERNALDEBUG> # Internal debug activated.
)
target_include_directories(IdlParserTests
PRIVATE
${PROJECT_SOURCE_DIR}/test/utils/
)
target_link_libraries(IdlParserTests
GTest::gtest
$<$<BOOL:${WIN32}>:iphlpapi$<SEMICOLON>Shlwapi>
$<$<BOOL:${WIN32}>: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 $<TARGET_FILE_DIR:IdlParserTests>/IDL
)
else()
message(WARNING "The folder thirdparty/dds-types-test/IDL does not exist. The git submodule might not have been initialized.")
endif()
214 changes: 214 additions & 0 deletions test/feature/idl_parser/IdlParserTests.cpp
Original file line number Diff line number Diff line change
@@ -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 <gtest/gtest.h>

#include <fastdds/dds/xtypes/dynamic_types/DynamicDataFactory.hpp>
#include <fastdds/dds/xtypes/dynamic_types/DynamicTypeBuilder.hpp>
#include <fastdds/dds/xtypes/dynamic_types/DynamicTypeBuilderFactory.hpp>
#include <ScopedLogs.hpp>

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<std::string> 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<std::string> 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();
}

0 comments on commit f4ffd30

Please sign in to comment.