Skip to content

Commit

Permalink
Add a helper function that make rcl_init_options_copy not to call rmw…
Browse files Browse the repository at this point in the history
…_init_options_fini

remove unnecessary test

Signed-off-by: Chen Lihui <Lihui.Chen@sony.com>
  • Loading branch information
Chen Lihui authored and Chen Lihui committed Nov 5, 2020
1 parent 9f7f8b2 commit 4fcec36
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 25 deletions.
31 changes: 23 additions & 8 deletions rcl/src/rcl/init_options.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@ rcl_get_zero_initialized_init_options(void)
}; // NOLINT(readability/braces): false positive
}

/// Initialize given init_options with the default values and zero-initialize implementation.
RCL_LOCAL
rcl_ret_t
_rcl_init_options_init(rcl_init_options_t * init_options, rcl_allocator_t allocator)
{
init_options->impl = allocator.allocate(sizeof(rcl_init_options_impl_t), allocator.state);
RCL_CHECK_FOR_NULL_WITH_MSG(
init_options->impl,
"failed to allocate memory for init options impl",
return RCL_RET_BAD_ALLOC);
init_options->impl->allocator = allocator;
init_options->impl->rmw_init_options = rmw_get_zero_initialized_init_options();

return RCL_RET_OK;
}

rcl_ret_t
rcl_init_options_init(rcl_init_options_t * init_options, rcl_allocator_t allocator)
{
Expand All @@ -48,13 +64,11 @@ rcl_init_options_init(rcl_init_options_t * init_options, rcl_allocator_t allocat
return RCL_RET_ALREADY_INIT;
}
RCL_CHECK_ALLOCATOR(&allocator, return RCL_RET_INVALID_ARGUMENT);
init_options->impl = allocator.allocate(sizeof(rcl_init_options_impl_t), allocator.state);
RCL_CHECK_FOR_NULL_WITH_MSG(
init_options->impl,
"failed to allocate memory for init options impl",
return RCL_RET_BAD_ALLOC);
init_options->impl->allocator = allocator;
init_options->impl->rmw_init_options = rmw_get_zero_initialized_init_options();

rcl_ret_t ret = _rcl_init_options_init(init_options, allocator);
if (RCL_RET_OK != ret) {
return ret;
}
rmw_ret_t rmw_ret = rmw_init_options_init(&(init_options->impl->rmw_init_options), allocator);
if (RMW_RET_OK != rmw_ret) {
allocator.deallocate(init_options->impl, allocator.state);
Expand All @@ -74,14 +88,15 @@ rcl_init_options_copy(const rcl_init_options_t * src, rcl_init_options_t * dst)

RCL_CHECK_ARGUMENT_FOR_NULL(src, RCL_RET_INVALID_ARGUMENT);
RCL_CHECK_ARGUMENT_FOR_NULL(src->impl, RCL_RET_INVALID_ARGUMENT);
RCL_CHECK_ALLOCATOR(&src->impl->allocator, return RCL_RET_INVALID_ARGUMENT);
RCL_CHECK_ARGUMENT_FOR_NULL(dst, RCL_RET_INVALID_ARGUMENT);
if (NULL != dst->impl) {
RCL_SET_ERROR_MSG("given dst (rcl_init_options_t) is already initialized");
return RCL_RET_ALREADY_INIT;
}

// initialize dst (since we know it's in a zero initialized state)
rcl_ret_t ret = rcl_init_options_init(dst, src->impl->allocator);
rcl_ret_t ret = _rcl_init_options_init(dst, src->impl->allocator);
if (RCL_RET_OK != ret) {
return ret; // error already set
}
Expand Down
17 changes: 0 additions & 17 deletions rcl/test/rcl/test_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -548,23 +548,6 @@ TEST_F(CLASSNAME(TestRCLFixture, RMW_IMPLEMENTATION), test_mocked_rcl_init_optio
EXPECT_EQ(RCL_RET_OK, rcl_init_options_fini(&init_options));
}

// Mock rcl_init_options_copy to fail
TEST_F(CLASSNAME(TestRCLFixture, RMW_IMPLEMENTATION), test_rcl_init_copy_mocked_fail_fini) {
rcl_init_options_t init_options = rcl_get_zero_initialized_init_options();
rcl_ret_t ret = rcl_init_options_init(&init_options, rcl_get_default_allocator());
ASSERT_EQ(RCL_RET_OK, ret) << rcl_get_error_string().str;
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT(
{
EXPECT_EQ(RCL_RET_OK, rcl_init_options_fini(&init_options)) << rcl_get_error_string().str;
});
rcl_init_options_t init_options_dst = rcl_get_zero_initialized_init_options();
auto mock = mocking_utils::inject_on_return("lib:rcl", rmw_init_options_fini, RMW_RET_ERROR);
EXPECT_EQ(RCL_RET_ERROR, rcl_init_options_copy(&init_options, &init_options_dst));
rcl_reset_error();
auto mock_ok = mocking_utils::inject_on_return("lib:rcl", rmw_init_options_fini, RMW_RET_OK);
EXPECT_EQ(RCL_RET_OK, rcl_init_options_fini(&init_options_dst));
}

// Mock rcl_init_options_copy to fail
TEST_F(CLASSNAME(TestRCLFixture, RMW_IMPLEMENTATION), test_rcl_init_options_copy_fail_rmw_copy) {
rcl_init_options_t init_options = rcl_get_zero_initialized_init_options();
Expand Down

0 comments on commit 4fcec36

Please sign in to comment.