Skip to content

Commit

Permalink
variant_copy
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Brawner <[email protected]>
  • Loading branch information
brawner committed Sep 2, 2020
1 parent c4352a1 commit 8ca6f42
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
7 changes: 1 addition & 6 deletions rcl_yaml_param_parser/test/test_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,6 @@ TEST(RclYamlParamParser, test_parse_file_with_bad_allocator) {
});
ASSERT_TRUE(rcutils_exists(path)) << "No test YAML file found at " << path;

// Check sporadic failing malloc calls
RCUTILS_FAULT_INJECTION_TEST(
{
rcutils_allocator_t allocator = rcutils_get_default_allocator();
Expand All @@ -355,12 +354,8 @@ TEST(RclYamlParamParser, test_parse_file_with_bad_allocator) {
(void)res;

// If `rcutils_string_array_fini` fails, there will be a small memory leak here.
// Pausing fault injection so this test runs clean
int64_t count = rcutils_fault_injection_get_count();
rcutils_fault_injection_set_count(RCUTILS_FAULT_INJECTION_NEVER_FAIL);
// However, it's necessary for coverage
rcl_yaml_node_struct_fini(params_hdl);
rcutils_fault_injection_set_count(count);

params_hdl = NULL;
});
}
Expand Down
32 changes: 32 additions & 0 deletions rcl_yaml_param_parser/test/test_yaml_variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,35 @@ TEST(TestYamlVariant, copy_string_array_values) {
src_variant.string_array_value->data[i], dest_variant.string_array_value->data[i]);
}
}

TEST(TestYamlVariant, copy_string_array_maybe_fail) {
rcl_variant_t src_variant{};
rcutils_allocator_t allocator = rcutils_get_default_allocator();
constexpr size_t size = 3u;
src_variant.string_array_value =
static_cast<rcutils_string_array_t *>(
allocator.allocate(sizeof(rcutils_string_array_t), allocator.state));
ASSERT_NE(nullptr, src_variant.string_array_value);
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT(
{
rcl_yaml_variant_fini(&src_variant, allocator);
});
*src_variant.string_array_value = rcutils_get_zero_initialized_string_array();
ASSERT_EQ(
RCUTILS_RET_OK, rcutils_string_array_init(src_variant.string_array_value, size, &allocator));
src_variant.string_array_value->size = size;
src_variant.string_array_value->data[0] = rcutils_strdup("string1", allocator);
src_variant.string_array_value->data[1] = rcutils_strdup("string2", allocator);
src_variant.string_array_value->data[2] = rcutils_strdup("string3", allocator);
for (size_t i = 0; i < size; ++i) {
ASSERT_NE(nullptr, src_variant.string_array_value->data[i]);
}

RCUTILS_FAULT_INJECTION_TEST(
{
rcl_variant_t dest_variant{};
rcutils_ret_t ret = rcl_yaml_variant_copy(&dest_variant, &src_variant, allocator);
(void)ret;
rcl_yaml_variant_fini(&dest_variant, allocator);
});
}

0 comments on commit 8ca6f42

Please sign in to comment.