Skip to content
Snippets Groups Projects
Commit 0f7ad039 authored by Joan Vallvé Navarro's avatar Joan Vallvé Navarro
Browse files

Merge branch '392-int-and-unsigned-int-parameters-warnings' into 'devel'

Resolve "int and unsigned int parameters warnings"

Closes #392

See merge request !408
parents 0ed28b0b c250ce1c
No related branches found
No related tags found
1 merge request!408Resolve "int and unsigned int parameters warnings"
Pipeline #6313 passed
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include <vector> #include <vector>
#include <stack> #include <stack>
#include <list> #include <list>
#include <math.h>
/** /**
@file @file
...@@ -96,13 +97,21 @@ struct converter<utils::list<A>>{ ...@@ -96,13 +97,21 @@ struct converter<utils::list<A>>{
template<> template<>
struct converter<int>{ struct converter<int>{
static int convert(std::string val){ static int convert(std::string val){
return stod(val); double res;
if (modf(stod(val),&res) > 0)
throw std::runtime_error("Invalid conversion to int: The number contains decimals: " + val);
return res;
} }
}; };
template<> template<>
struct converter<unsigned int>{ struct converter<unsigned int>{
static unsigned int convert(std::string val){ static unsigned int convert(std::string val){
return stod(val); double res;
if (modf(stod(val),&res) > 0)
throw std::runtime_error("Invalid conversion to unsigned int: The number contains decimals: " + val);
if (res < 0)
throw std::runtime_error("Invalid conversion to unsigned int: The number is negative: " + val);
return res;
} }
}; };
template<> template<>
...@@ -114,8 +123,8 @@ struct converter<double>{ ...@@ -114,8 +123,8 @@ struct converter<double>{
template<> template<>
struct converter<bool>{ struct converter<bool>{
static bool convert(std::string val){ static bool convert(std::string val){
if(val == "true") return true; if(val == "true" or val=="True" or val=="TRUE") return true;
else if (val == "false") return false; else if (val == "false" or val=="False" or val=="FALSE") return false;
else throw std::runtime_error("Invalid conversion to bool (Must be either \"true\" or \"false\"). String provided: " + val); else throw std::runtime_error("Invalid conversion to bool (Must be either \"true\" or \"false\"). String provided: " + val);
} }
}; };
......
...@@ -9,18 +9,63 @@ using namespace wolf; ...@@ -9,18 +9,63 @@ using namespace wolf;
std::string wolf_root = _WOLF_ROOT_DIR; std::string wolf_root = _WOLF_ROOT_DIR;
//TEST(ParamsServer, Default) TEST(ParamsServer, Default)
//{ {
// auto parser = parse("test/yaml/params1.yaml", wolf_root); ParserYaml parser = ParserYaml("test/yaml/params_basic.yaml", wolf_root);
// auto params = parser.getParams(); ParamsServer server = ParamsServer(parser.getParams());
// ParamsServer server = ParamsServer(params, parser.sensorsSerialization(), parser.processorsSerialization()); }
// EXPECT_EQ(server.getParam<double>("should_not_exist", "2.6"), 2.6);
// EXPECT_EQ(server.getParam<bool>("my_proc_test/voting_active", "true"), false); TEST(ParamsServer, getParamsOk)
// EXPECT_NE(server.getParam<unsigned int>("my_proc_test/time_tolerance", "23"), 23); {
// EXPECT_THROW({ server.getParam<unsigned int>("test error"); }, std::runtime_error); ParserYaml parser = ParserYaml("test/yaml/params_basic.yaml", wolf_root);
// EXPECT_NE(server.getParam<unsigned int>("my_proc_test/time_tolerance"), 23); ParamsServer server = ParamsServer(parser.getParams());
// EXPECT_EQ(server.getParam<bool>("my_proc_test/voting_active"), false);
//} EXPECT_EQ(server.getParam<int>("int_1"), -3);
EXPECT_EQ(server.getParam<int>("int_2"), 0);
EXPECT_EQ(server.getParam<int>("int_3"), 6);
EXPECT_EQ(server.getParam<unsigned int>("uint_1"), 2);
EXPECT_EQ(server.getParam<unsigned int>("uint_2"), 0);
EXPECT_EQ(server.getParam<unsigned int>("uint_3"), 6);
EXPECT_EQ(server.getParam<double>("double_1"), 3.6);
EXPECT_EQ(server.getParam<double>("double_2"), -3);
EXPECT_EQ(server.getParam<double>("double_3"), 3.6);
EXPECT_EQ(server.getParam<std::string>("string_1"), std::string("wolf"));
EXPECT_EQ(server.getParam<std::string>("string_2"), std::string("Wolf"));
EXPECT_EQ(server.getParam<bool>("bool_1"), true);
EXPECT_EQ(server.getParam<bool>("bool_2"), true);
EXPECT_EQ(server.getParam<bool>("bool_3"), true);
EXPECT_EQ(server.getParam<bool>("bool_4"), true);
EXPECT_EQ(server.getParam<bool>("bool_5"), false);
EXPECT_EQ(server.getParam<bool>("bool_6"), false);
EXPECT_EQ(server.getParam<bool>("bool_7"), false);
EXPECT_EQ(server.getParam<bool>("bool_8"), false);
}
TEST(ParamsServer, getParamsWrong)
{
ParserYaml parser = ParserYaml("test/yaml/params_basic.yaml", wolf_root);
ParamsServer server = ParamsServer(parser.getParams());
EXPECT_ANY_THROW({ server.getParam<double>("should_not_exist"); });
EXPECT_ANY_THROW({ server.getParam<int>("int_wrong_1"); });
EXPECT_ANY_THROW({ server.getParam<int>("int_wrong_2"); });
EXPECT_ANY_THROW({ server.getParam<int>("int_wrong_3"); });
EXPECT_ANY_THROW({ server.getParam<unsigned int>("uint_wrong_1"); });
EXPECT_ANY_THROW({ server.getParam<unsigned int>("uint_wrong_2"); });
EXPECT_ANY_THROW({ server.getParam<unsigned int>("uint_wrong_3"); });
EXPECT_ANY_THROW({ server.getParam<unsigned int>("uint_wrong_4"); });
EXPECT_ANY_THROW({ server.getParam<double>("double_wrong_1"); });
EXPECT_ANY_THROW({ server.getParam<double>("double_wrong_2"); });
EXPECT_ANY_THROW({ server.getParam<bool>("bool_wrong_2"); });
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
......
...@@ -39,6 +39,3 @@ config: ...@@ -39,6 +39,3 @@ config:
sensor_name: "odom" sensor_name: "odom"
plugin: "core" plugin: "core"
follow: "test/yaml/processor_odom_3d.yaml" follow: "test/yaml/processor_odom_3d.yaml"
files:
- "/home/jcasals/workspace/wip/wolf/lib/libsensor_odo.so"
- "/home/jcasals/workspace/wip/wolf/lib/librange_bearing.so"
\ No newline at end of file
...@@ -36,6 +36,3 @@ config: ...@@ -36,6 +36,3 @@ config:
name: "my_proc_test" name: "my_proc_test"
sensor_name: "odom" sensor_name: "odom"
plugin: "core" plugin: "core"
files:
- "/home/jcasals/workspace/wip/wolf/lib/libsensor_odo.so"
- "/home/jcasals/workspace/wip/wolf/lib/librange_bearing.so"
\ No newline at end of file
config:
problem:
dim: 2
int_1: -3
int_2: 0
int_3: "6"
uint_1: 2
uint_2: 0
uint_3: "6"
double_1: 3.6
double_2: -3
double_3: "3.6"
string_1: wolf
string_2: "Wolf"
bool_1: true
bool_2: True
bool_3: TRUE
bool_4: "true"
bool_5: false
bool_6: False
bool_7: FALSE
bool_8: "false"
int_wrong_1: 2.3
int_wrong_2: "wolf"
int_wrong_3: true
uint_wrong_1: -2
uint_wrong_2: 3.5
uint_wrong_3: "wolf"
uint_wrong_4: true
double_wrong_1: "wolf"
double_wrong_2: true
bool_wrong: 1
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment