@@ -51,6 +51,53 @@ TEST(TestAllocatorCommon, retyped_allocate) {
5151 EXPECT_NO_THROW (code2 ());
5252}
5353
54+ TEST (TestAllocatorCommon, retyped_zero_allocate_basic) {
55+ std::allocator<int > allocator;
56+ void * untyped_allocator = &allocator;
57+ void * allocated_mem =
58+ rclcpp::allocator::retyped_zero_allocate<std::allocator<char >>(20u , 1u , untyped_allocator);
59+ ASSERT_TRUE (nullptr != allocated_mem);
60+
61+ auto code = [&untyped_allocator, allocated_mem]() {
62+ rclcpp::allocator::retyped_deallocate<char , std::allocator<char >>(
63+ allocated_mem, untyped_allocator);
64+ };
65+ EXPECT_NO_THROW (code ());
66+ }
67+
68+ TEST (TestAllocatorCommon, retyped_zero_allocate) {
69+ std::allocator<int > allocator;
70+ void * untyped_allocator = &allocator;
71+ void * allocated_mem =
72+ rclcpp::allocator::retyped_zero_allocate<std::allocator<char >>(20u , 1u , untyped_allocator);
73+ // The more natural check here is ASSERT_NE(nullptr, ptr), but clang static
74+ // analysis throws a false-positive memory leak warning. Use ASSERT_TRUE instead.
75+ ASSERT_TRUE (nullptr != allocated_mem);
76+
77+ auto code = [&untyped_allocator, allocated_mem]() {
78+ rclcpp::allocator::retyped_deallocate<int , std::allocator<int >>(
79+ allocated_mem, untyped_allocator);
80+ };
81+ EXPECT_NO_THROW (code ());
82+
83+ allocated_mem = allocator.allocate (1 );
84+ // The more natural check here is ASSERT_NE(nullptr, ptr), but clang static
85+ // analysis throws a false-positive memory leak warning. Use ASSERT_TRUE instead.
86+ ASSERT_TRUE (nullptr != allocated_mem);
87+ void * reallocated_mem =
88+ rclcpp::allocator::retyped_reallocate<int , std::allocator<int >>(
89+ allocated_mem, 2u , untyped_allocator);
90+ // The more natural check here is ASSERT_NE(nullptr, ptr), but clang static
91+ // analysis throws a false-positive memory leak warning. Use ASSERT_TRUE instead.
92+ ASSERT_TRUE (nullptr != reallocated_mem);
93+
94+ auto code2 = [&untyped_allocator, reallocated_mem]() {
95+ rclcpp::allocator::retyped_deallocate<int , std::allocator<int >>(
96+ reallocated_mem, untyped_allocator);
97+ };
98+ EXPECT_NO_THROW (code2 ());
99+ }
100+
54101TEST (TestAllocatorCommon, get_rcl_allocator) {
55102 std::allocator<int > allocator;
56103 auto rcl_allocator = rclcpp::allocator::get_rcl_allocator<int >(allocator);
0 commit comments