Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 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
18 changes: 17 additions & 1 deletion rcl_yaml_param_parser/src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <stdlib.h>
#include <string.h>
#include <yaml.h>
#include <ctype.h>

#include "rcl_yaml_param_parser/parser.h"
#include "rcl_yaml_param_parser/types.h"
Expand Down Expand Up @@ -1105,7 +1106,22 @@ static void * get_value(
{
errno = 0;
endptr = NULL;
dval = strtod(value, &endptr);
const char * vernier_ptr = NULL;
if ((0 == strcasecmp(value, ".nan")) ||
(0 == strcasecmp(value, ".inf")) ||
(0 == strcasecmp(value, "+.inf")) ||
(0 == strcasecmp(value, "-.inf")) ||
(0 == strcasecmp(value, ".infinity")) ||
(0 == strcasecmp(value, "+.infinity")) ||
(0 == strcasecmp(value, "-.infinity")))
{
for (vernier_ptr = value; !isalpha(*vernier_ptr); ) {
vernier_ptr += 1;
}
dval = strtod(vernier_ptr, &endptr);
} else {
dval = strtod(value, &endptr);
}
if ((0 == errno) && (NULL != endptr)) {
if ((NULL != endptr) && (endptr != value)) {
if (('\0' != *value) && ('\0' == *endptr)) {
Expand Down
4 changes: 4 additions & 0 deletions rcl_yaml_param_parser/test/special_float.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
test_node:
ros__parameters:
isstring: [string, .nananan, .infinf]
nan_inf: [1.1, 2.2, .nan, .NAN, .inf, +.inf, -.INF, +.infinity, -.INFINITY]
46 changes: 46 additions & 0 deletions rcl_yaml_param_parser/test/test_parse_yaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

#include <stdio.h>
#include <math.h>
#include <gtest/gtest.h>

#include "osrf_testing_tools_cpp/scope_exit.hpp"
Expand Down Expand Up @@ -497,6 +498,51 @@ TEST(test_file_parser, maximum_number_parameters) {
// No cleanup, rcl_parse_yaml_file takes care of that if it fails.
}

// Test special float point(https://github.com/ros2/rcl/issues/555).
TEST(test_file_parser, special_float_point) {
rcutils_reset_error();
EXPECT_TRUE(rcutils_get_cwd(cur_dir, 1024)) << rcutils_get_error_string().str;
rcutils_allocator_t allocator = rcutils_get_default_allocator();
char * test_path = rcutils_join_path(cur_dir, "test", allocator);
ASSERT_TRUE(NULL != test_path) << rcutils_get_error_string().str;
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT(
{
allocator.deallocate(test_path, allocator.state);
});
char * path = rcutils_join_path(test_path, "special_float.yaml", allocator);
ASSERT_TRUE(NULL != path) << rcutils_get_error_string().str;
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT(
{
allocator.deallocate(path, allocator.state);
});
ASSERT_TRUE(rcutils_exists(path)) << "No test YAML file found at " << path;
rcl_params_t * params_hdl = rcl_yaml_node_struct_init(allocator);
ASSERT_TRUE(NULL != params_hdl) << rcutils_get_error_string().str;
OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT(
{
rcl_yaml_node_struct_fini(params_hdl);
});

bool res = rcl_parse_yaml_file(path, params_hdl);
EXPECT_TRUE(res) << rcutils_get_error_string().str;
rcl_variant_t * param_value = rcl_yaml_node_struct_get("test_node", "isstring", params_hdl);
ASSERT_TRUE(NULL != param_value) << rcutils_get_error_string().str;
ASSERT_TRUE(NULL != param_value->string_array_value);
EXPECT_STREQ(".nananan", param_value->string_array_value->data[1]);
EXPECT_STREQ(".infinf", param_value->string_array_value->data[2]);
param_value = rcl_yaml_node_struct_get(
"test_node", "nan_inf", params_hdl);
ASSERT_TRUE(NULL != param_value) << rcutils_get_error_string().str;
ASSERT_TRUE(NULL != param_value->double_array_value);
ASSERT_EQ(9U, param_value->double_array_value->size);
EXPECT_TRUE(isnan(param_value->double_array_value->values[2]));
EXPECT_TRUE(isnan(param_value->double_array_value->values[3]));
EXPECT_TRUE(isinf(param_value->double_array_value->values[4]));
EXPECT_TRUE(isinf(param_value->double_array_value->values[5]));

rcl_yaml_node_struct_print(params_hdl);
}

int32_t main(int32_t argc, char ** argv)
{
::testing::InitGoogleTest(&argc, argv);
Expand Down