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 }
CommandVoid0< E > * makeCommandVoid0(E &entity, boost::function< void(void)> function, const std::string &docString)
Definition: command-bind.h:51
This class represents an entity, i.e. a generic computational unit that provides input and output sig...
This class implements a variant design pattern to handle basic types in Command.
Definition: value.h:50
command::Command * getNewStyleCommand(const std::string &cmdName)
Provides the pointer towards the Command object cmdName.
CommandVoid1< E, T > * makeCommandVoid1(E &entity, boost::function< void(const T &)> function, const std::string &docString)
Definition: command-bind.h:103
std::string docCommandVoid1(const std::string &doc, const std::string &type)
Definition: command-bind.h:129
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
static const std::string CLASS_NAME
std::string docCommandVoid0(const std::string &doc)
Definition: command-bind.h:70
virtual const std::string & getClassName() const
std::string docCommandVoid3(const std::string &doc, const std::string &type1, const std::string &type2, const std::string &type3)
Definition: command-bind.h:260
int getCode() const
Access to the error code.
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
void three_args(const int &, const int &, const int &)
void cmd_verbose(std::ostream &oss)
void one_arg(const int &)
CustomEntity(const std::string &n)
std::string two_args_ret(const int &, const int &)
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
int one_arg_ret(const int &)
CommandMap_t getNewStyleCommandMap()
Provides the std::map where all the commands are registered.
Abstract root class for all dynamic-graph exceptions.
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
void four_args(const int &, const int &, const int &, const int &)
Entity * newEntity(const std::string &classname, const std::string &objname) const
Instantiate (and allocate) an entity.
void two_args(const int &, const int &)
CommandVerbose< E > * makeCommandVerbose(E &entity, typename CommandVerbose< E >::function_t function, const std::string &docString)
Definition: command-bind.h:732
static FactoryStorage * getInstance()
Get pointer to unique object of the class.
std::string docCommandVoid2(const std::string &doc, const std::string &type1, const std::string &type2)
Definition: command-bind.h:194
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(CustomEntity, "CustomEntity")
CommandReturnType1< E, ReturnType, T > * makeCommandReturnType1(E &entity, boost::function< ReturnType(const T &)> function, const std::string &docString)
Definition: command-bind.h:831
BOOST_AUTO_TEST_CASE(command_test)
std::string docCommandVerbose(const std::string &doc)
Definition: command-bind.h:748
CommandVoid2< E, T1, T2 > * makeCommandVoid2(E &entity, boost::function< void(const T1 &, const T2 &)> function, const std::string &docString)
Definition: command-bind.h:167


dynamic-graph
Author(s): Nicolas Mansard, Olivier Stasse
autogenerated on Sun Jun 25 2023 02:06:03