Skip to content

Commit

Permalink
Add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Acuadros95 committed Apr 13, 2022
1 parent 2c9c696 commit d2b2bfe
Showing 1 changed file with 136 additions and 0 deletions.
136 changes: 136 additions & 0 deletions rclc_parameter/test/rclc_parameter/test_parameter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,100 @@ class ParameterTestBase : public ::testing::TestWithParam<rclc_parameter_options
bool spin;
};

TEST_P(ParameterTestBase, rclc_add_parameter_constraint) {
// Test with NULL arguments
ASSERT_EQ(
rclc_add_parameter_constraint_integer(NULL, "param2", 0, 0, 0),
RCL_RET_INVALID_ARGUMENT);
ASSERT_EQ(
rclc_add_parameter_constraint_integer(
&param_server, NULL, 0, 0,
0), RCL_RET_INVALID_ARGUMENT);
ASSERT_EQ(
rclc_add_parameter_constraint_double(
NULL, "param3", 0.0, 0.0,
0.0), RCL_RET_INVALID_ARGUMENT);
ASSERT_EQ(
rclc_add_parameter_constraint_double(
&param_server, NULL, 0.0, 0.0,
0.0), RCL_RET_INVALID_ARGUMENT);

// Test with invalid parameter name
ASSERT_EQ(
rclc_add_parameter_constraint_integer(
&param_server, "invalid_param", 0, 0,
0), RCL_RET_ERROR);
ASSERT_EQ(
rclc_add_parameter_constraint_double(
&param_server, "invalid_param", 0.0, 0.0,
0.0), RCL_RET_ERROR);

// Test with invalid parameter type
ASSERT_EQ(rclc_add_parameter_constraint_integer(&param_server, "param3", 0, 0, 0), RCL_RET_ERROR);
ASSERT_EQ(
rclc_add_parameter_constraint_double(
&param_server, "param2", 0.0, 0.0,
0.0), RCL_RET_ERROR);

// Test valid values
ASSERT_EQ(rclc_add_parameter_constraint_integer(&param_server, "param2", 0, 10, 1), RCL_RET_OK);
ASSERT_EQ(
rclc_add_parameter_constraint_double(
&param_server, "param3", 0.0, 0.1,
0.01), RCL_RET_OK);
}

TEST_P(ParameterTestBase, rclc_add_parameter_description) {
if (options.low_mem_mode) {
ASSERT_EQ(
rclc_add_parameter_description(
&param_server, "param1", "parameter_description",
"additional_constraints"), RCLC_PARAMETER_UNSUPORTED_ON_LOW_MEM);
return;
}

// Test with NULL arguments
ASSERT_EQ(rclc_add_parameter_description(NULL, "", "", ""), RCL_RET_INVALID_ARGUMENT);
ASSERT_EQ(rclc_add_parameter_description(&param_server, NULL, "", ""), RCL_RET_INVALID_ARGUMENT);
ASSERT_EQ(rclc_add_parameter_description(&param_server, "", NULL, ""), RCL_RET_INVALID_ARGUMENT);
ASSERT_EQ(rclc_add_parameter_description(&param_server, "", "", NULL), RCL_RET_INVALID_ARGUMENT);

// Test with invalid parameter name
ASSERT_EQ(rclc_add_parameter_description(&param_server, "invalid_param", "", ""), RCL_RET_ERROR);

// Fail on string length
char overflow_string[RCLC_PARAMETER_MAX_STRING_LENGTH + 1];
memset(overflow_string, ' ', RCLC_PARAMETER_MAX_STRING_LENGTH + 1);
overflow_string[RCLC_PARAMETER_MAX_STRING_LENGTH] = '\0';

ASSERT_EQ(
rclc_add_parameter_description(
&param_server, "param1", overflow_string,
""), RCL_RET_ERROR);
ASSERT_EQ(
rclc_add_parameter_description(
&param_server, "param1", "",
overflow_string), RCL_RET_ERROR);

// Test valid values
ASSERT_EQ(
rclc_add_parameter_description(
&param_server, "param1", "parameter_description",
"additional_constraints"), RCL_RET_OK);
}

TEST_P(ParameterTestBase, rclc_set_parameter_read_only) {
// Test with NULL arguments
ASSERT_EQ(rclc_set_parameter_read_only(NULL, "param1", true), RCL_RET_INVALID_ARGUMENT);
ASSERT_EQ(rclc_set_parameter_read_only(&param_server, NULL, true), RCL_RET_INVALID_ARGUMENT);

// Test with invalid parameter name
ASSERT_EQ(rclc_set_parameter_read_only(&param_server, "invalid_param", true), RCL_RET_ERROR);

// Test valid values
ASSERT_EQ(rclc_set_parameter_read_only(&param_server, "param1", true), RCL_RET_OK);
}

TEST_P(ParameterTestBase, rclc_set_get_parameter) {
size_t expected_callback_calls = 1;

Expand Down Expand Up @@ -876,6 +970,48 @@ TEST_P(ParameterTestBase, rclcpp_get_parameter_types) {
}
}

TEST_P(ParameterTestBase, rclc_disabled_on_callback) {
// Test disabled methods on callback
on_parameter_changed = [&](const Parameter *, const Parameter *) -> bool {
EXPECT_EQ(
rclc_add_parameter(
&param_server, "param_new",
RCLC_PARAMETER_BOOL), RCLC_PARAMETER_DISABLED_ON_CALLBACK);
EXPECT_EQ(
rclc_delete_parameter(&param_server, "param1"),
RCLC_PARAMETER_DISABLED_ON_CALLBACK);
EXPECT_EQ(
rclc_parameter_set_bool(
&param_server, "param1",
true), RCLC_PARAMETER_DISABLED_ON_CALLBACK);
EXPECT_EQ(
rclc_parameter_set_int(
&param_server, "param2",
10), RCLC_PARAMETER_DISABLED_ON_CALLBACK);
EXPECT_EQ(
rclc_parameter_set_double(
&param_server, "param3",
0.1), RCLC_PARAMETER_DISABLED_ON_CALLBACK);
EXPECT_EQ(
rclc_set_parameter_read_only(
&param_server, "param1",
true), RCLC_PARAMETER_DISABLED_ON_CALLBACK);
EXPECT_EQ(
rclc_add_parameter_constraint_integer(
&param_server, "param2", 0, 0,
0), RCLC_PARAMETER_DISABLED_ON_CALLBACK);
EXPECT_EQ(
rclc_add_parameter_constraint_double(
&param_server, "param3", 0.0, 0.0,
0.0), RCLC_PARAMETER_DISABLED_ON_CALLBACK);
return true;
};

// Trigger callback
ASSERT_EQ(rclc_parameter_set_bool(&param_server, "param1", true), RCL_RET_OK);
ASSERT_EQ(callback_calls, 1U);
}

// Init parameter server with allow_undeclared_parameters flag
rclc_parameter_options_t options_low_mem = {
true, // notify_changed_over_dds
Expand Down

0 comments on commit d2b2bfe

Please sign in to comment.