Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test coverage for rmw_init_options API. #385

Merged
merged 3 commits into from
May 18, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions rmw_fastrtps_shared_cpp/test/test_rmw_init_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,38 @@ TEST(RMWInitOptionsTest, copy) {
EXPECT_STREQ("/root", options.security_options.security_root_path);
}

static void * failing_allocate(size_t size, void * state)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You definitely don't need to change this here, but one thing I learned while writing failing_allocators like this, is that you can just define the function without parameter names to avoid unused variable errors.

static void * failing_allocate(size_t , void* ) { return NULL; }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've learned not to do that because of compiler warnings. My memory may be failing me here though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh funny! I sometimes see compiler warnings when cast to void.

{
(void)size;
(void)state;
return NULL;
}

TEST(RMWInitOptionsTest, bad_alloc_on_copy) {
rcutils_allocator_t failing_allocator = rcutils_get_default_allocator();
failing_allocator.allocate = failing_allocate;

rmw_init_options_t preset_options = rmw_get_zero_initialized_init_options();
ASSERT_EQ(
RMW_RET_OK,
rmw_init_options_init("some_identifier", &preset_options, failing_allocator)) <<
rcutils_get_error_string().str;
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT(
{
rcutils_reset_error();
EXPECT_EQ(RMW_RET_OK, rmw_init_options_fini("some_identifier", &preset_options)) <<
rcutils_get_error_string().str;
rcutils_reset_error();
});
preset_options.enclave = rcutils_strdup("/test", rcutils_get_default_allocator());
ASSERT_TRUE(preset_options.enclave != nullptr);

rmw_init_options_t options = rmw_get_zero_initialized_init_options();
EXPECT_EQ(
RMW_RET_BAD_ALLOC,
rmw_init_options_copy("some_identifier", &preset_options, &options));
}

TEST(RMWInitOptionsTest, fini_w_invalid_args_fails) {
// Cannot finalize a null options instance.
EXPECT_EQ(RMW_RET_INVALID_ARGUMENT, rmw_init_options_fini("some_identifier", nullptr));
Expand Down