Skip to content

Commit

Permalink
Modify as suggested
Browse files Browse the repository at this point in the history
Signed-off-by: Ada-King <Bingtao.Du@sony.com>
  • Loading branch information
Ada-King committed Aug 18, 2020
1 parent d12c0f1 commit d326731
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 4 deletions.
21 changes: 17 additions & 4 deletions 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,11 +1106,23 @@ static void * get_value(
{
errno = 0;
endptr = NULL;
if (strcasecmp(value, ".nan") == 0) {
char * tmp_value = (char *)value;
snprintf(tmp_value, strlen(tmp_value), "%s", "nan");
char * tmp_value = 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")))
{
tmp_value = rcutils_strdup(value, allocator);
for ( ; !isalpha(*tmp_value); ) {
tmp_value += 1;
}
dval = strtod(tmp_value, &endptr);
} else {
dval = strtod(value, &endptr);
}
dval = strtod(value, &endptr);
if ((0 == errno) && (NULL != endptr)) {
if ((NULL != endptr) && (endptr != value)) {
if (('\0' != *value) && ('\0' == *endptr)) {
Expand Down
8 changes: 8 additions & 0 deletions rcl_yaml_param_parser/test/special_float1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# config/test_yaml
---

lidar_ns:
lidar_1:
ros__parameters:
test_nan: [1.1, 2.2, .nan, .NAN]
test_inf: [4.4, 5.5, .inf, +.inf, -.INF, +.infinity, -.INFINITY]
8 changes: 8 additions & 0 deletions rcl_yaml_param_parser/test/special_float2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# config/test_yaml
---

lidar_ns:
lidar_1:
ros__parameters:
test_nan: [1.1, 2.2, nananana]
test_nan2: [3.3, nanananan]
48 changes: 48 additions & 0 deletions rcl_yaml_param_parser/test/test_parse_yaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,54 @@ TEST(test_file_parser, maximum_number_parameters) {
// No cleanup, rcl_parse_yaml_file takes care of that if it fails.
}

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);
});

// Test special float point with correct value.
{
char * path = rcutils_join_path(test_path, "special_float1.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;
}

// Test special float point with improperly value.
{
char * path = rcutils_join_path(test_path, "special_float2.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;
bool res = rcl_parse_yaml_file(path, params_hdl);
fprintf(stderr, "%s\n", rcutils_get_error_string().str);
EXPECT_FALSE(res);
// No cleanup, rcl_parse_yaml_file takes care of that if it fails.
}
}

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

0 comments on commit d326731

Please sign in to comment.