Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion rclcpp/src/rclcpp/node_interfaces/node_parameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ RCLCPP_LOCAL
bool
__are_doubles_equal(double x, double y, double ulp = 100.0)
{
if (!std::isfinite(x) || !std::isfinite(y)) {
return x == y;
}
return std::abs(x - y) <= std::numeric_limits<double>::epsilon() * std::abs(x + y) * ulp;
}

Expand Down Expand Up @@ -234,7 +237,7 @@ __check_double_range(
{
return result;
}
if ((value < fp_range.from_value) || (value > fp_range.to_value)) {
if (!(value >= fp_range.from_value && value <= fp_range.to_value)) {
result.successful = false;
result.reason = format_range_reason(descriptor.name, "floating point");
return result;
Expand Down
22 changes: 21 additions & 1 deletion rclcpp/test/rclcpp/test_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1732,6 +1732,26 @@ TEST_F(TestNode, set_parameter_undeclared_parameters_not_allowed) {
EXPECT_FALSE(node->set_parameter(rclcpp::Parameter(name, 9.999)).successful);
EXPECT_EQ(node->get_parameter(name).get_value<double>(), 11.0);
}
{
// setting a parameter to non-finite values with floating point range descriptor
auto name = "parameter"_unq;
rcl_interfaces::msg::ParameterDescriptor descriptor;
descriptor.floating_point_range.resize(1);
auto & floating_point_range = descriptor.floating_point_range.at(0);
floating_point_range.from_value = 0.0;
floating_point_range.to_value = 100.0;
floating_point_range.step = 0.0;
node->declare_parameter(name, 50.0, descriptor);

constexpr double inf = std::numeric_limits<double>::infinity();
constexpr double nan = std::numeric_limits<double>::quiet_NaN();
EXPECT_FALSE(node->set_parameter(rclcpp::Parameter(name, inf)).successful);
EXPECT_EQ(node->get_parameter(name).get_value<double>(), 50.0);
EXPECT_FALSE(node->set_parameter(rclcpp::Parameter(name, -inf)).successful);
EXPECT_EQ(node->get_parameter(name).get_value<double>(), 50.0);
EXPECT_FALSE(node->set_parameter(rclcpp::Parameter(name, nan)).successful);
EXPECT_EQ(node->get_parameter(name).get_value<double>(), 50.0);
}
{
// setting an array parameter with floating point range descriptor
auto name = "parameter"_unq;
Expand Down Expand Up @@ -1969,7 +1989,7 @@ TEST_F(TestNode, set_parameter_undeclared_parameters_not_allowed) {
RCPPUTILS_SCOPE_EXIT(
{node->remove_pre_set_parameters_callback(handler.get());}); // always reset
}
}
} // NOLINT(readability/fn_size)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

test function has been growing for a while.
i understand that this is not for this PR's job to refactor, but we might ask for the new block to be split into its own test fixture instead. that would also be cleaner because the test name (set_parameter_undeclared_parameters_not_allowed) doesn't really describe the new assertion...

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Hey, thanks for the feedback — agreed the test function has grown too big.

That said, my new case is testing node->set_parameter under the allow_undeclared_parameters(false) constraint, so it does fit this fixture. I'm a bit worried that pulling it out into its own TEST_F would raise the question of why it doesn't belong with set_parameter_undeclared_parameters_not_allowed in the first place.

Happy to split it if you still prefer, but wanted to flag the category concern first.


TEST_F(TestNode, set_parameter_undeclared_parameters_allowed) {
rclcpp::NodeOptions no;
Expand Down