Skip to content

Commit c71014c

Browse files
author
tempate
committed
Tests - Dynamic Types to IDL
Signed-off-by: tempate <danieldiaz@eprosima.com>
1 parent bf303ae commit c71014c

92 files changed

Lines changed: 15572 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

test/unittest/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ add_subdirectory(dds/status)
2323
add_subdirectory(dds/subscriber)
2424
add_subdirectory(dds/topic)
2525
add_subdirectory(dds/topic/DDSSQLFilter)
26+
add_subdirectory(dds/xtypes/type_conversion)
2627
add_subdirectory(dds/xtypes/type_representation)
2728
add_subdirectory(logging)
2829
add_subdirectory(rtps/attributes)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright 2024 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
set(RESOURCEDYNTYPETOIDLTESTS_SOURCE
16+
DynTypeToIdlTests.cpp
17+
${PROJECT_SOURCE_DIR}/src/cpp/fastdds/xtypes/utils.cpp)
18+
19+
file(GLOB DATATYPE_SOURCES_CPP "types/type_objects/**/*.cpp")
20+
file(GLOB DATATYPE_SOURCES_CXX "types/type_objects/**/*.cxx")
21+
file(GLOB DATATYPE_SOURCES_IDL "types/idls/*.idl")
22+
23+
set(
24+
DATATYPE_SOURCES
25+
${DATATYPE_SOURCES_CPP}
26+
${DATATYPE_SOURCES_CXX}
27+
${DATATYPE_SOURCES_IDL}
28+
)
29+
30+
if(WIN32)
31+
add_definitions(-D_WIN32_WINNT=0x0601)
32+
endif()
33+
34+
add_executable(DynTypeToIdlTests ${RESOURCEDYNTYPETOIDLTESTS_SOURCE} ${DATATYPE_SOURCES})
35+
target_compile_definitions(DynTypeToIdlTests PRIVATE
36+
$<$<AND:$<NOT:$<BOOL:${WIN32}>>,$<STREQUAL:"${CMAKE_BUILD_TYPE}","Debug">>:__DEBUG>
37+
$<$<BOOL:${INTERNAL_DEBUG}>:__INTERNALDEBUG> # Internal debug activated.
38+
)
39+
target_include_directories(DynTypeToIdlTests PRIVATE
40+
${PROJECT_SOURCE_DIR}/include ${PROJECT_BINARY_DIR}/include
41+
${PROJECT_SOURCE_DIR}/src/cpp
42+
)
43+
target_link_libraries(DynTypeToIdlTests
44+
fastcdr
45+
fastdds
46+
GTest::gtest
47+
${CMAKE_DL_LIBS})
48+
gtest_discover_tests(DynTypeToIdlTests SOURCES ${TYPEOBJECTUTILSTESTS_SOURCE})
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright 2024 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <algorithm>
16+
#include <fstream>
17+
#include <string>
18+
19+
#include <gtest/gtest.h>
20+
21+
#include <fastdds/dds/core/ReturnCode.hpp>
22+
#include <fastdds/dds/xtypes/dynamic_types/DynamicType.hpp>
23+
#include <fastdds/dds/xtypes/utils.hpp>
24+
25+
#include "types/all_types.hpp"
26+
27+
using namespace eprosima;
28+
29+
class DynTypeToIdlTests : public ::testing::TestWithParam<test::SupportedType>
30+
{
31+
};
32+
33+
/**
34+
* Verify that the IDL serialization of a DynamicType matches the IDL file that defined the type.
35+
*
36+
* CASES:
37+
* - Verify that the IDL file was opened successfully.
38+
* - Verify that the IDL serialization finished successfully.
39+
* - Verify that the two IDLs match.
40+
*/
41+
TEST_P(DynTypeToIdlTests, msg_schema_generation)
42+
{
43+
const test::SupportedType type = GetParam();
44+
45+
// Read the IDL file as a string
46+
const std::string full_path{"/home/daniel/Documents/eprosima/dynamic_type_idl/src/fastdds/test/unittest/dds/xtypes/type_conversion/types/idls/"};
47+
const auto file_name = full_path + to_string(type) + ".idl";
48+
49+
std::ifstream file(file_name);
50+
51+
ASSERT_TRUE(file.is_open());
52+
53+
const std::string idl_file{std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>()};
54+
55+
// Get Dynamic type
56+
const fastdds::dds::DynamicType::_ref_type dyn_type = test::get_dynamic_type(type);
57+
58+
// Serialize DynamicType to IDL
59+
std::string idl_serialization;
60+
ASSERT_EQ(fastdds::dds::idl_serialize(dyn_type, idl_serialization), fastdds::dds::RETCODE_OK);
61+
62+
// Compare IDLs
63+
ASSERT_EQ(idl_file, idl_serialization);
64+
}
65+
66+
INSTANTIATE_TEST_SUITE_P(DynTypeToIdlTests, DynTypeToIdlTests, ::testing::Values(
67+
test::SupportedType::hello_world,
68+
test::SupportedType::numeric_array,
69+
test::SupportedType::char_sequence,
70+
test::SupportedType::basic_struct,
71+
test::SupportedType::basic_array_struct,
72+
test::SupportedType::float_bounded_sequence,
73+
test::SupportedType::arrays_and_sequences,
74+
test::SupportedType::complex_nested_arrays,
75+
test::SupportedType::enum_struct,
76+
test::SupportedType::union_struct,
77+
test::SupportedType::map_struct
78+
));
79+
80+
int main(
81+
int argc,
82+
char** argv)
83+
{
84+
::testing::InitGoogleTest(&argc, argv);
85+
return RUN_ALL_TESTS();
86+
}
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
// Copyright 2024 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/**
16+
* @file all_types.hpp
17+
*/
18+
19+
/*
20+
* USEFUL COMMAND
21+
*
22+
* for TYPE in hello_world numeric_array char_sequence basic_struct basic_array_struct float_bounded_sequence arrays_and_sequences complex_nested_arrays; do ${FASTDDSGEN_WS}/scripts/fastddsgen -replace -d ${WS}/src/recorder/ddsrecorder/test/unittest/dynamic_types/types/type_objects/ -typeobject -cs ${WS}/src/recorder/ddsrecorder/test/unittest/dynamic_types/types/idls/${TYPE}.idl; done
23+
*/
24+
25+
#pragma once
26+
27+
#include <fastdds/dds/core/ReturnCode.hpp>
28+
#include <fastdds/dds/domain/DomainParticipantFactory.hpp>
29+
#include <fastdds/dds/topic/TypeSupport.hpp>
30+
#include <fastdds/dds/xtypes/dynamic_types/DynamicType.hpp>
31+
#include <fastdds/dds/xtypes/dynamic_types/DynamicTypeBuilder.hpp>
32+
#include <fastdds/dds/xtypes/dynamic_types/DynamicTypeBuilderFactory.hpp>
33+
#include <fastdds/dds/xtypes/type_representation/TypeObject.hpp>
34+
35+
#include "type_objects/arrays_and_sequences/arrays_and_sequences.hpp"
36+
#include "type_objects/arrays_and_sequences/arrays_and_sequencesPubSubTypes.h"
37+
38+
#include "type_objects/basic_array_struct/basic_array_struct.hpp"
39+
#include "type_objects/basic_array_struct/basic_array_structPubSubTypes.h"
40+
41+
#include "type_objects/basic_struct/basic_struct.hpp"
42+
#include "type_objects/basic_struct/basic_structPubSubTypes.h"
43+
44+
#include "type_objects/char_sequence/char_sequence.hpp"
45+
#include "type_objects/char_sequence/char_sequencePubSubTypes.h"
46+
47+
#include "type_objects/complex_nested_arrays/complex_nested_arrays.hpp"
48+
#include "type_objects/complex_nested_arrays/complex_nested_arraysPubSubTypes.h"
49+
50+
#include "type_objects/enum_struct/enum_struct.hpp"
51+
#include "type_objects/enum_struct/enum_structPubSubTypes.h"
52+
53+
#include "type_objects/float_bounded_sequence/float_bounded_sequence.hpp"
54+
#include "type_objects/float_bounded_sequence/float_bounded_sequencePubSubTypes.h"
55+
56+
#include "type_objects/hello_world/hello_world.hpp"
57+
#include "type_objects/hello_world/hello_worldPubSubTypes.h"
58+
59+
#include "type_objects/map_struct/map_struct.hpp"
60+
#include "type_objects/map_struct/map_structPubSubTypes.h"
61+
62+
#include "type_objects/numeric_array/numeric_array.hpp"
63+
#include "type_objects/numeric_array/numeric_arrayPubSubTypes.h"
64+
65+
#include "type_objects/union_struct/union_struct.hpp"
66+
#include "type_objects/union_struct/union_structPubSubTypes.h"
67+
68+
69+
namespace test {
70+
71+
enum SupportedType
72+
{
73+
hello_world,
74+
numeric_array,
75+
char_sequence,
76+
basic_struct,
77+
basic_array_struct,
78+
float_bounded_sequence,
79+
arrays_and_sequences,
80+
complex_nested_arrays,
81+
enum_struct,
82+
union_struct, // NOTE: default case currently not supported in dynamic types
83+
map_struct
84+
};
85+
86+
std::string to_string(const SupportedType& type)
87+
{
88+
switch (type)
89+
{
90+
case SupportedType::hello_world:
91+
return "hello_world";
92+
case SupportedType::numeric_array:
93+
return "numeric_array";
94+
case SupportedType::char_sequence:
95+
return "char_sequence";
96+
case SupportedType::basic_struct:
97+
return "basic_struct";
98+
case SupportedType::basic_array_struct:
99+
return "basic_array_struct";
100+
case SupportedType::float_bounded_sequence:
101+
return "float_bounded_sequence";
102+
case SupportedType::arrays_and_sequences:
103+
return "arrays_and_sequences";
104+
case SupportedType::complex_nested_arrays:
105+
return "complex_nested_arrays";
106+
case SupportedType::enum_struct:
107+
return "enum_struct";
108+
case SupportedType::union_struct:
109+
return "union_struct";
110+
case SupportedType::map_struct:
111+
return "map_struct";
112+
default:
113+
return "invalid";
114+
}
115+
}
116+
117+
118+
eprosima::fastdds::dds::DynamicType::_ref_type get_dynamic_type( // traits<eprosima::fastdds::dds::DynamicType>::ref_type
119+
SupportedType dyn_type)
120+
{
121+
// Register the type
122+
eprosima::fastdds::dds::TypeSupport type_arrays_and_sequences(new arrays_and_sequencesPubSubType());
123+
type_arrays_and_sequences->register_type_object_representation();
124+
125+
eprosima::fastdds::dds::TypeSupport type_basic_array_struct(new basic_array_structPubSubType());
126+
type_basic_array_struct->register_type_object_representation();
127+
128+
eprosima::fastdds::dds::TypeSupport type_basic_struct(new basic_structPubSubType());
129+
type_basic_struct->register_type_object_representation();
130+
131+
eprosima::fastdds::dds::TypeSupport type_complex_nested_arrays(new complex_nested_arraysPubSubType());
132+
type_complex_nested_arrays->register_type_object_representation();
133+
134+
eprosima::fastdds::dds::TypeSupport type_char_sequence(new char_sequencePubSubType());
135+
type_char_sequence->register_type_object_representation();
136+
137+
eprosima::fastdds::dds::TypeSupport type_enum_struct(new enum_structPubSubType());
138+
type_enum_struct->register_type_object_representation();
139+
140+
eprosima::fastdds::dds::TypeSupport type_float_bounded_sequence(new float_bounded_sequencePubSubType());
141+
type_float_bounded_sequence->register_type_object_representation();
142+
143+
eprosima::fastdds::dds::TypeSupport type_hello_world(new hello_worldPubSubType());
144+
type_hello_world->register_type_object_representation();
145+
146+
eprosima::fastdds::dds::TypeSupport type_map_struct(new map_structPubSubType());
147+
type_map_struct->register_type_object_representation();
148+
149+
eprosima::fastdds::dds::TypeSupport type_numeric_array(new numeric_arrayPubSubType());
150+
type_numeric_array->register_type_object_representation();
151+
152+
153+
eprosima::fastdds::dds::TypeSupport type_union_struct(new union_structPubSubType());
154+
type_union_struct->register_type_object_representation();
155+
156+
auto type_name = to_string(dyn_type);
157+
158+
eprosima::fastdds::dds::xtypes::TypeObjectPair type_objs;
159+
if (eprosima::fastdds::dds::RETCODE_OK == eprosima::fastdds::dds::DomainParticipantFactory::get_instance()->type_object_registry().get_type_objects(
160+
type_name,
161+
type_objs))
162+
{
163+
return eprosima::fastdds::dds::DynamicTypeBuilderFactory::get_instance()->create_type_w_type_object(type_objs.complete_type_object)->build();
164+
}
165+
166+
else
167+
{
168+
// throw eprosima::utils::InconsistencyException("No Type Object");
169+
}
170+
171+
return nullptr;
172+
}
173+
174+
} /* namespace test */
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
struct AnInternalObject
2+
{
3+
float x;
4+
boolean positive;
5+
};
6+
7+
struct arrays_and_sequences
8+
{
9+
sequence<AnInternalObject> unlimited_vector;
10+
sequence<AnInternalObject, 10> limited_vector;
11+
AnInternalObject limited_array[10];
12+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
struct TheOtherObjectInArray
2+
{
3+
long some_num;
4+
boolean positive;
5+
};
6+
7+
struct basic_array_struct
8+
{
9+
unsigned long index;
10+
TheOtherObjectInArray sub_structs[5];
11+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
struct TheOtherObject
2+
{
3+
long some_num;
4+
};
5+
6+
struct basic_struct
7+
{
8+
TheOtherObject sub_struct;
9+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
struct char_sequence
2+
{
3+
sequence<char> chars;
4+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
struct ThirdLevelElement
2+
{
3+
double x;
4+
double y;
5+
};
6+
7+
struct SecondLevelElement
8+
{
9+
ThirdLevelElement an_element_alone;
10+
sequence<ThirdLevelElement, 1> a_limited_other_value;
11+
};
12+
13+
struct FirstLevelElement
14+
{
15+
string useless_name;
16+
sequence<SecondLevelElement> sub;
17+
ThirdLevelElement an_element_alone;
18+
};
19+
20+
struct complex_nested_arrays
21+
{
22+
FirstLevelElement array_of_elements[3];
23+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
enum ColorEnum
2+
{
3+
RED,
4+
GREEN,
5+
BLUE
6+
};
7+
8+
struct enum_struct
9+
{
10+
unsigned long index;
11+
ColorEnum enum_value;
12+
};

0 commit comments

Comments
 (0)