Skip to content

Commit 10bfb67

Browse files
committed
Refs #23753. Add unit tests
Signed-off-by: Ricardo González Moreno <ricardo@richiware.dev>
1 parent d179b4b commit 10bfb67

File tree

3 files changed

+111
-2
lines changed

3 files changed

+111
-2
lines changed

fastdds_python/src/swig/fastdds/rtps/common/InstanceHandle.i

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,11 @@ long hash(const eprosima::fastdds::rtps::InstanceHandle_t& handle)
173173
void from_sequence(PyObject* seq) {
174174
// Reutiliza el constructor para validar y copiar
175175
eprosima::fastdds::rtps::InstanceHandleValue_t* tmp = new_eprosima_fastdds_rtps_InstanceHandleValue_t(seq);
176-
for (int i = 0; i < 16; ++i) $self->value[i] = (*tmp)[i];
177-
delete tmp; // evitar fuga
176+
if (nullptr != tmp)
177+
{
178+
for (int i = 0; i < 16; ++i) $self->value[i] = (*tmp)[i];
179+
delete tmp; // evitar fuga
180+
}
178181
}
179182

180183
// Getter: return a tuple of 16 ints (0..255)

fastdds_python/test/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@
1515
# Compile types
1616
add_subdirectory(types)
1717

18+
# Unit tests
19+
add_test(NAME unit_tests
20+
COMMAND
21+
${Python3_EXECUTABLE}
22+
-m pytest
23+
-vrP
24+
WORKING_DIRECTORY
25+
${CMAKE_CURRENT_SOURCE_DIR}/unittest
26+
)
27+
28+
# DDS Api tests
1829
add_test(NAME api_tests
1930
COMMAND
2031
${Python3_EXECUTABLE}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import fastdds
2+
import pytest
3+
import sys
4+
5+
6+
def test_create_instance_handle_from_bytes():
7+
ih = fastdds.InstanceHandle_t()
8+
ih.value = b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10"
9+
assert (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16) == ih.value
10+
11+
12+
def test_create_instance_handle_from_bytearray():
13+
ih = fastdds.InstanceHandle_t()
14+
ih.value = bytearray([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16])
15+
assert (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16) == ih.value
16+
17+
18+
def test_create_instance_handle_from_tuple():
19+
ih = fastdds.InstanceHandle_t()
20+
ih.value = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)
21+
assert (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16) == ih.value
22+
23+
24+
def test_create_instance_handle_from_list():
25+
ih = fastdds.InstanceHandle_t()
26+
ih.value = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
27+
assert (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16) == ih.value
28+
29+
30+
def test_create_instance_handle_from_bytes_with_less_elements():
31+
ih = fastdds.InstanceHandle_t()
32+
with pytest.raises(SystemError) as exception:
33+
ih.value = b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e"
34+
repr = exception.getrepr()
35+
assert str(repr).split("\n")[0] == "ValueError: Expected 16 bytes"
36+
37+
38+
def test_create_instance_handle_from_bytes_with_more_elements():
39+
ih = fastdds.InstanceHandle_t()
40+
with pytest.raises(SystemError) as exception:
41+
ih.value = (
42+
b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12"
43+
)
44+
repr = exception.getrepr()
45+
assert str(repr).split("\n")[0] == "ValueError: Expected 16 bytes"
46+
47+
48+
def test_create_instance_handle_from_bytearray_with_less_elements():
49+
ih = fastdds.InstanceHandle_t()
50+
with pytest.raises(SystemError) as exception:
51+
ih.value = bytearray([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
52+
repr = exception.getrepr()
53+
assert str(repr).split("\n")[0] == "ValueError: Expected 16 bytes"
54+
55+
56+
def test_create_instance_handle_from_bytearray_with_more_elements():
57+
ih = fastdds.InstanceHandle_t()
58+
with pytest.raises(SystemError) as exception:
59+
ih.value = bytearray(
60+
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
61+
)
62+
repr = exception.getrepr()
63+
assert str(repr).split("\n")[0] == "ValueError: Expected 16 bytes"
64+
65+
66+
def test_create_instance_handle_from_tuple_with_less_elements():
67+
ih = fastdds.InstanceHandle_t()
68+
with pytest.raises(SystemError) as exception:
69+
ih.value = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14)
70+
repr = exception.getrepr()
71+
assert str(repr).split("\n")[0] == "ValueError: Expected 16 elements"
72+
73+
74+
def test_create_instance_handle_from_tuple_with_more_elements():
75+
ih = fastdds.InstanceHandle_t()
76+
with pytest.raises(SystemError) as exception:
77+
ih.value = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18)
78+
repr = exception.getrepr()
79+
assert str(repr).split("\n")[0] == "ValueError: Expected 16 elements"
80+
81+
82+
def test_create_instance_handle_from_list_with_less_elements():
83+
ih = fastdds.InstanceHandle_t()
84+
with pytest.raises(SystemError) as exception:
85+
ih.value = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
86+
repr = exception.getrepr()
87+
assert str(repr).split("\n")[0] == "ValueError: Expected 16 elements"
88+
89+
90+
def test_create_instance_handle_from_list_with_more_elements():
91+
ih = fastdds.InstanceHandle_t()
92+
with pytest.raises(SystemError) as exception:
93+
ih.value = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]
94+
repr = exception.getrepr()
95+
assert str(repr).split("\n")[0] == "ValueError: Expected 16 elements"

0 commit comments

Comments
 (0)