command-test.cpp
Go to the documentation of this file.
1 /* Copyright 2019, LAAS-CNRS
2  *
3  * Olivier Stasse
4  *
5  * See LICENSE file
6  *
7  */
8 #include <dynamic-graph/entity.h>
10 
11 #include <iostream>
12 #include <sstream>
13 
15 #include "dynamic-graph/factory.h"
16 #include "dynamic-graph/pool.h"
17 
18 #define ENABLE_RT_LOG
19 #include <dynamic-graph/logger.h>
21 
22 #define BOOST_TEST_MODULE debug - logger
23 
24 #if BOOST_VERSION >= 105900
25 #include <boost/test/tools/output_test_stream.hpp>
26 #else
27 #include <boost/test/output_test_stream.hpp>
28 #endif
29 #include <boost/test/unit_test.hpp>
30 
31 using boost::test_tools::output_test_stream;
32 
33 using namespace dynamicgraph::command;
34 
35 namespace dynamicgraph {
36 class CustomEntity : public Entity {
37  public:
38  static const std::string CLASS_NAME;
46 
47  virtual const std::string &getClassName() const { return CLASS_NAME; }
48  explicit CustomEntity(const std::string &n) : Entity(n) {
49  test_zero_arg_ = false;
50  test_one_arg_ = false;
51  test_two_args_ = false;
52  test_three_args_ = false;
53  test_four_args_ = false;
54  test_one_arg_ret_ = false;
55  test_two_args_ret_ = false;
56 
57  addCommand("0_arg", makeCommandVoid0(*this, &CustomEntity::zero_arg,
58  docCommandVoid0("zero arg")));
59 
60  addCommand("1_arg", makeCommandVoid1(*this, &CustomEntity::one_arg,
61  docCommandVoid1("one arg", "int")));
62 
63  addCommand("2_args",
65  docCommandVoid2("two args", "int", "int")));
66 
67  addCommand("3_args", makeCommandVoid3(*this, &CustomEntity::three_args,
68  docCommandVoid3("three args", "int",
69  "int", "int")));
70 
71  addCommand("4_args",
74  docCommandVoid4("four args", "int", "int", "int", "int")));
75 
76  addCommand("1_arg_r",
78  docCommandVoid1("one arg", "int")));
79 
80  addCommand("2_args_r", makeCommandReturnType2(
82  docCommandVoid2("two args", "int", "int")));
83 
84  addCommand(
85  "cmd_verbose",
87  docCommandVerbose("Display some information")));
88 
90  bool res = false;
91  std::string e_1_arg("1_arg");
92  try {
93  addCommand(e_1_arg, getNewStyleCommand(e_1_arg));
94  } catch (dynamicgraph::ExceptionFactory &aef) {
96  }
97  BOOST_CHECK(res);
98  }
99 
101 
102  void zero_arg() { test_zero_arg_ = true; }
103 
104  void one_arg(const int &) { test_one_arg_ = true; }
105 
106  void two_args(const int &, const int &) { test_two_args_ = true; }
107 
108  void three_args(const int &, const int &, const int &) {
109  test_three_args_ = true;
110  }
111 
112  void four_args(const int &, const int &, const int &, const int &) {
113  test_four_args_ = true;
114  }
115 
116  int one_arg_ret(const int &) {
117  test_one_arg_ret_ = true;
118  return 2;
119  }
120 
121  std::string two_args_ret(const int &, const int &) {
122  test_two_args_ret_ = true;
123  return std::string("return");
124  }
125 
126  void cmd_verbose(std::ostream &oss) {
127  std::string as("print verbose");
128  oss << as;
129  }
130 };
132 } // namespace dynamicgraph
133 
134 BOOST_AUTO_TEST_CASE(command_test) {
135  dynamicgraph::CustomEntity *ptr_entity =
136  (dynamic_cast<dynamicgraph::CustomEntity *>(
138  "my-entity")));
139  dynamicgraph::CustomEntity &entity = *ptr_entity;
140 
141  std::map<const std::string, Command *> aCommandMap =
142  entity.getNewStyleCommandMap();
143 
144  std::map<const std::string, Command *>::iterator it_map;
145 
146  it_map = aCommandMap.find("0_arg");
147  if (it_map == aCommandMap.end()) BOOST_CHECK(false);
148  it_map->second->execute();
149  BOOST_CHECK(entity.test_zero_arg_);
150 
151  int first_arg = 1;
152  Value aValue(first_arg);
153  std::vector<std::string> vec_fname(4);
154  vec_fname[0] = "1_arg";
155  vec_fname[1] = "2_args";
156  vec_fname[2] = "3_args";
157  vec_fname[3] = "4_args";
158  std::vector<Value> values;
159 
160  for (unsigned int i = 0; i < 4; i++) {
161  it_map = aCommandMap.find(vec_fname[i]);
162  if (it_map == aCommandMap.end()) BOOST_CHECK(false);
163  values.push_back(aValue);
164  it_map->second->setParameterValues(values);
165  it_map->second->execute();
166  it_map->second->owner();
167  it_map->second->getDocstring();
168  }
169 
170  BOOST_CHECK(entity.test_one_arg_);
171  BOOST_CHECK(entity.test_two_args_);
172  BOOST_CHECK(entity.test_three_args_);
173  BOOST_CHECK(entity.test_four_args_);
174 
175  // With return type.
176  vec_fname.clear();
177  vec_fname.push_back(std::string("1_arg_r"));
178  vec_fname.push_back(std::string("2_args_r"));
179  values.clear();
180 
181  for (unsigned int i = 0; i < 2; i++) {
182  it_map = aCommandMap.find(vec_fname[i]);
183  if (it_map == aCommandMap.end()) {
184  BOOST_CHECK(false);
185  exit(-1);
186  }
187  values.push_back(aValue);
188  it_map->second->setParameterValues(values);
189  Value aValue = it_map->second->execute();
190  it_map->second->owner();
191  it_map->second->getDocstring();
192  }
193 
194  BOOST_CHECK(entity.test_one_arg_ret_);
195  BOOST_CHECK(entity.test_two_args_ret_);
196 
197  std::vector<Value> values_two;
198  values_two.push_back(aValue);
200  bool res = false;
201  it_map = aCommandMap.find(std::string("2_args"));
202  try {
203  it_map->second->setParameterValues(values_two);
204  } catch (const dynamicgraph::ExceptionAbstract &aea) {
206  }
207  BOOST_CHECK(res);
208 
209  double snd_arg_db = 10.0;
210  Value aValue2(snd_arg_db);
211  values_two.push_back(aValue2);
212 
214  res = false;
215  it_map = aCommandMap.find(std::string("2_args"));
216  try {
217  it_map->second->setParameterValues(values_two);
218  } catch (const dynamicgraph::ExceptionAbstract &aea) {
220  }
221  BOOST_CHECK(res);
222 
224  res = false;
225  entity.getNewStyleCommand(vec_fname[0]);
226  BOOST_CHECK(true);
227 
229  std::string empty("");
230  try {
231  entity.getNewStyleCommand(empty);
232  } catch (dynamicgraph::ExceptionFactory &aef) {
234  }
235  BOOST_CHECK(res);
236 
238  delete ptr_entity;
239 }
dynamicgraph::command::makeCommandReturnType2
CommandReturnType2< E, ReturnType, T1, T2 > * makeCommandReturnType2(E &entity, boost::function< ReturnType(const T1 &, const T2 &)> function, const std::string &docString)
Definition: command-bind.h:897
entity.h
dynamicgraph::CustomEntity::getClassName
virtual const std::string & getClassName() const
Definition: command-test.cpp:47
dynamicgraph::CustomEntity::zero_arg
void zero_arg()
Definition: command-test.cpp:102
logger.h
dynamicgraph
dynamicgraph::command::makeCommandReturnType1
CommandReturnType1< E, ReturnType, T > * makeCommandReturnType1(E &entity, boost::function< ReturnType(const T &)> function, const std::string &docString)
Definition: command-bind.h:831
dynamicgraph::CustomEntity::~CustomEntity
~CustomEntity()
Definition: command-test.cpp:100
dynamicgraph::command
Definition: command-bind.h:31
dynamicgraph::Entity
This class represents an entity, i.e. a generic computational unit that provides input and output sig...
Definition: include/dynamic-graph/entity.h:52
dynamicgraph::ExceptionFactory::UNREFERED_FUNCTION
@ UNREFERED_FUNCTION
Definition: exception-factory.h:24
dynamicgraph::CustomEntity::test_one_arg_ret_
bool test_one_arg_ret_
Definition: command-test.cpp:44
dynamicgraph::command::makeCommandVoid3
CommandVoid3< E, T1, T2, T3 > * makeCommandVoid3(E &entity, typename CommandVoid3< E, T1, T2, T3 >::function_t function, const std::string &docString)
Definition: command-bind.h:234
dynamicgraph::CustomEntity::test_one_arg_
bool test_one_arg_
Definition: command-test.cpp:40
dynamicgraph::ExceptionFactory::OBJECT_CONFLICT
@ OBJECT_CONFLICT
Definition: exception-factory.h:28
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(command_test)
Definition: command-test.cpp:134
dynamicgraph::command::docCommandVoid0
std::string docCommandVoid0(const std::string &doc)
Definition: command-bind.h:70
dynamicgraph::CustomEntity::two_args_ret
std::string two_args_ret(const int &, const int &)
Definition: command-test.cpp:121
dynamicgraph::DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(CustomEntity, "CustomEntity")
dynamicgraph::CustomEntity
Definition: command-test.cpp:36
dynamicgraph::Entity::getNewStyleCommandMap
CommandMap_t getNewStyleCommandMap()
Provides the std::map where all the commands are registered.
Definition: src/dgraph/entity.cpp:202
dynamicgraph::CustomEntity::test_four_args_
bool test_four_args_
Definition: command-test.cpp:43
dynamicgraph::CustomEntity::CustomEntity
CustomEntity(const std::string &n)
Definition: command-test.cpp:48
CustomEntity
Definition: custom-entity.cpp:22
dynamicgraph::command::docCommandVoid1
std::string docCommandVoid1(const std::string &doc, const std::string &type)
Definition: command-bind.h:129
dynamicgraph::CustomEntity::test_two_args_
bool test_two_args_
Definition: command-test.cpp:41
dynamicgraph::CustomEntity::cmd_verbose
void cmd_verbose(std::ostream &oss)
Definition: command-test.cpp:126
dynamicgraph::command::docCommandVoid4
std::string docCommandVoid4(const std::string &doc, const std::string &type1, const std::string &type2, const std::string &type3, const std::string &type4)
Definition: command-bind.h:333
dynamicgraph::CustomEntity::test_zero_arg_
bool test_zero_arg_
Definition: command-test.cpp:39
dynamicgraph::CustomEntity::test_three_args_
bool test_three_args_
Definition: command-test.cpp:42
dynamicgraph::ExceptionAbstract::TOOLS
@ TOOLS
Definition: exception-abstract.h:72
dynamicgraph::ExceptionAbstract
Abstract root class for all dynamic-graph exceptions.
Definition: exception-abstract.h:31
dynamicgraph::CustomEntity::three_args
void three_args(const int &, const int &, const int &)
Definition: command-test.cpp:108
dynamicgraph::command::makeCommandVoid4
CommandVoid4< E, T1, T2, T3, T4 > * makeCommandVoid4(E &entity, typename CommandVoid4< E, T1, T2, T3, T4 >::function_t function, const std::string &docString)
Definition: command-bind.h:307
dynamicgraph::command::docCommandVoid3
std::string docCommandVoid3(const std::string &doc, const std::string &type1, const std::string &type2, const std::string &type3)
Definition: command-bind.h:260
dynamicgraph::ExceptionAbstract::getCode
int getCode() const
Access to the error code.
Definition: exception-abstract.cpp:24
dynamicgraph::Entity::getNewStyleCommand
command::Command * getNewStyleCommand(const std::string &cmdName)
Provides the pointer towards the Command object cmdName.
Definition: src/dgraph/entity.cpp:206
dynamicgraph::ExceptionFactory
Generic error class.
Definition: exception-factory.h:18
exception-factory.h
dynamicgraph::command::docCommandVerbose
std::string docCommandVerbose(const std::string &doc)
Definition: command-bind.h:748
dynamicgraph::command::makeCommandVoid2
CommandVoid2< E, T1, T2 > * makeCommandVoid2(E &entity, boost::function< void(const T1 &, const T2 &)> function, const std::string &docString)
Definition: command-bind.h:167
dynamicgraph::command::docCommandVoid2
std::string docCommandVoid2(const std::string &doc, const std::string &type1, const std::string &type2)
Definition: command-bind.h:194
factory.h
dynamicgraph::CustomEntity::CLASS_NAME
static const std::string CLASS_NAME
Definition: command-test.cpp:38
command-bind.h
dynamicgraph::ExceptionAbstract::ABSTRACT
@ ABSTRACT
Definition: exception-abstract.h:68
dynamicgraph::CustomEntity::test_two_args_ret_
bool test_two_args_ret_
Definition: command-test.cpp:45
real-time-logger.h
dynamicgraph::command::makeCommandVerbose
CommandVerbose< E > * makeCommandVerbose(E &entity, typename CommandVerbose< E >::function_t function, const std::string &docString)
Definition: command-bind.h:732
dynamicgraph::CustomEntity::two_args
void two_args(const int &, const int &)
Definition: command-test.cpp:106
dynamicgraph::CustomEntity::one_arg_ret
int one_arg_ret(const int &)
Definition: command-test.cpp:116
dynamicgraph::command::Value
This class implements a variant design pattern to handle basic types in Command.
Definition: value.h:51
dynamicgraph::command::makeCommandVoid0
CommandVoid0< E > * makeCommandVoid0(E &entity, boost::function< void(void)> function, const std::string &docString)
Definition: command-bind.h:51
dynamicgraph::CustomEntity::one_arg
void one_arg(const int &)
Definition: command-test.cpp:104
dynamicgraph::command::makeCommandVoid1
CommandVoid1< E, T > * makeCommandVoid1(E &entity, boost::function< void(const T &)> function, const std::string &docString)
Definition: command-bind.h:103
dynamicgraph::FactoryStorage::newEntity
Entity * newEntity(const std::string &classname, const std::string &objname) const
Instantiate (and allocate) an entity.
Definition: src/dgraph/factory.cpp:79
pool.h
dynamicgraph::FactoryStorage::getInstance
static FactoryStorage * getInstance()
Get pointer to unique object of the class.
Definition: src/dgraph/factory.cpp:15
dynamicgraph::CustomEntity::four_args
void four_args(const int &, const int &, const int &, const int &)
Definition: command-test.cpp:112


dynamic-graph
Author(s): Nicolas Mansard, Olivier Stasse
autogenerated on Fri Aug 2 2024 08:36:38