tests/entity.cpp
Go to the documentation of this file.
1 /* Copyright 2010-2019 LAAS, CNRS
2  * Thomas Moulard.
3  *
4  */
5 
6 #define ENABLE_RT_LOG
7 
10 #include <dynamic-graph/entity.h>
15 
16 #include <sstream>
17 
18 #include "dynamic-graph/factory.h"
19 #include "dynamic-graph/pool.h"
20 
21 #define BOOST_TEST_MODULE entity
22 
23 #if BOOST_VERSION >= 105900
24 #include <boost/test/tools/output_test_stream.hpp>
25 #else
26 #include <boost/test/output_test_stream.hpp>
27 #endif
28 #include <boost/test/unit_test.hpp>
29 
30 using boost::test_tools::output_test_stream;
31 
32 namespace dynamicgraph {
33 class CustomEntity : public Entity {
34  public:
37 
38  static const std::string CLASS_NAME;
39  virtual const std::string &getClassName() const { return CLASS_NAME; }
40  explicit CustomEntity(const std::string &n)
41  : Entity(n),
42  m_sigdSIN(NULL, "CustomEntity(" + name + ")::input(double)::in_double"),
43  m_sigdSIN2(NULL,
44  "CustomEntity(" + name + ")::input(double)::in_double"),
45  m_sigdTimeDepSOUT(
46  boost::bind(&CustomEntity::update, this, _1, _2), m_sigdSIN,
47  "CustomEntity(" + name + ")::input(double)::out_double"),
48  m_value(0.0) {}
49 
51 
52  void addSignal() {
53  signalRegistration(m_sigdSIN << m_sigdTimeDepSOUT);
55  try {
56  signalRegistration(m_sigdSIN2 << m_sigdTimeDepSOUT);
57  } catch (ExceptionFactory &aef) {
58  BOOST_CHECK_EQUAL(aef.getCode(),
60  }
61  }
62 
63  void rmValidSignal() {
64  signalDeregistration("in_double");
65  signalDeregistration("out_double");
66  }
67 
68  double &update(double &res, const int &inTime) {
69  const double &aDouble = m_sigdSIN(inTime);
70  res = aDouble;
71  return res;
72  }
73 
74  public:
75  double m_value;
76 };
78 } // namespace dynamicgraph
79 
80 BOOST_AUTO_TEST_CASE(constructor) {
81  namespace dg = dynamicgraph;
82  namespace dgc = dynamicgraph::command;
83 
84  BOOST_CHECK_EQUAL(dg::CustomEntity::CLASS_NAME, "CustomEntity");
85 
87  "CustomEntity", "my-entity");
88  BOOST_CHECK_EQUAL(entity.getName(), "my-entity");
89  BOOST_CHECK_EQUAL(entity.getClassName(), dg::CustomEntity::CLASS_NAME);
90 
91  dg::CustomEntity entity2("");
92 
93  // Test Commands
94  dgc::DirectGetter<dg::CustomEntity, double> a_direct_getter(
95  entity2, &entity2.m_value,
96  dgc::docDirectGetter("Get value m_value", "double"));
97 
98  dgc::DirectSetter<dg::CustomEntity, double> a_direct_setter(
99  entity2, &entity2.m_value,
100  dgc::docDirectSetter("Set value m_value", "double"));
101 
102  dgc::Value aValue(2.0);
103  std::vector<dgc::Value> values;
104  values.push_back(aValue);
105  a_direct_setter.setParameterValues(values);
106  a_direct_setter.execute();
107  a_direct_getter.execute();
108 
109  output_test_stream output;
110  output << entity2.m_value;
111  output << entity2;
112 
113  entity.getDocString();
114 }
115 
117  dynamicgraph::Entity &entity =
119 
120  // Non const getter.
121  try {
122  entity.getSignal("I do not exist");
123  BOOST_ERROR("Should never happen.");
124  } catch (const dynamicgraph::ExceptionFactory &exception) {
125  BOOST_CHECK_EQUAL(exception.getCode(),
127  }
128 
129  // Const getter.
130  try {
131  const dynamicgraph::Entity &entityConst = entity;
132  entityConst.getSignal("I do not exist");
133  BOOST_ERROR("Should never happen.");
134  } catch (const dynamicgraph::ExceptionFactory &exception) {
135  BOOST_CHECK_EQUAL(exception.getCode(),
137  }
138  // deregistration
139  try {
140  dynamicgraph::CustomEntity *customEntity =
141  dynamic_cast<dynamicgraph::CustomEntity *>(&entity);
142  customEntity->addSignal();
143  std::string signame("CustomEntity(my-entity)::input(double)::in_double");
144  customEntity->Entity::hasSignal(signame);
145  output_test_stream output;
146  customEntity->Entity::displaySignalList(output);
147  dynamicgraph::Entity::SignalMap asigmap = customEntity->getSignalMap();
148  output << customEntity;
149  // Removing signals is working the first time
150  customEntity->rmValidSignal();
151  // Removing signals generates an exception the second time.
152  customEntity->rmValidSignal();
153  BOOST_ERROR("Should never happen.");
154  } catch (const dynamicgraph::ExceptionFactory &exception) {
155  BOOST_CHECK_EQUAL(exception.getCode(),
157  }
158 }
159 
161  dynamicgraph::Entity &entity =
163 
164  output_test_stream output;
165 
166  entity.displaySignalList(output);
167  BOOST_CHECK(output.is_equal("--- <my-entity> signal list: \n"));
168 }
169 
171  dynamicgraph::Entity &entity =
173 
174  output_test_stream output;
175 
176  entity.display(output);
177  BOOST_CHECK(output.is_equal("CustomEntity: my-entity"));
178 }
179 
181  dynamicgraph::Entity &entity =
183 
184  BOOST_CHECK_EQUAL(entity.getCommandList(), "print\nsignals\nsignalDep");
185 }
186 
188  dynamicgraph::Entity &entity =
190 
191  output_test_stream output;
192  entity.writeGraph(output);
193 
194  BOOST_CHECK(output.is_equal(""));
195 }
196 
198  dynamicgraph::Entity &entity =
200 
201  output_test_stream output;
202  entity.writeCompletionList(output);
203 
204  BOOST_CHECK(output.is_equal("print\nsignals\nsignalDep\n"));
205 }
206 
208  std::ofstream of;
209  of.open("/tmp/dg-LOGS.txt", std::ofstream::out | std::ofstream::app);
211 
212  dynamicgraph::Entity &entity =
214 
215  output_test_stream output;
216 
217  for (unsigned int i = 0; i < 4; i++) {
218  for (unsigned int j = 0; j < 2000; j++) {
219  dynamicgraph::LoggerVerbosity aLoggerVerbosityLevel =
221  entity.setLoggerVerbosityLevel(aLoggerVerbosityLevel);
222  if (entity.getLoggerVerbosityLevel() != aLoggerVerbosityLevel)
223  output << "Mismatch output";
224 
225 #define __FILELINE__ __FILE__ BOOST_PP_STRINGIZE(__LINE__)
227  << "Auto Test Case"
228  << " DEBUG" << '\n';
230  << "Auto Test Case"
231  << " INFO" << '\n';
233  << "Auto Test Case"
234  << " WARNING" << '\n';
236  << "Auto Test Case"
237  << " ERROR" << '\n';
238 #undef __FILELINE__
239  };
240  };
241 
242  BOOST_CHECK(output.is_equal(""));
244 }
245 
246 // WTF?
248  dynamicgraph::Entity &entity =
250 
251  BOOST_CHECK_EQUAL(entity.test(),
252  static_cast<dynamicgraph::SignalBase<int> *>(0));
253 
254  entity.test2(static_cast<dynamicgraph::SignalBase<int> *>(0));
255 }
BOOST_AUTO_TEST_CASE(constructor)
std::string docDirectGetter(const std::string &name, const std::string &type)
const std::string & getCommandList() const
std::ostream & displaySignalList(std::ostream &os) const
Display the list of signals of this entity in output stream os.
virtual std::string getDocString() const
Returns the Entity documentation.
virtual void test2(SignalBase< int > *)
This class represents an entity, i.e. a generic computational unit that provides input and output sig...
#define dgADD_OSTREAM_TO_RTLOG(ostr)
RTLoggerStream stream()
Definition: logger.h:200
void sendMsg(const std::string &msg, MsgType t=MSG_TYPE_INFO, const std::string &lineId="")
Send messages msg with level t. Add string file and line to message.
void signalRegistration(const SignalArray< int > &signals)
SignalMap getSignalMap() const
Provides a map of all the signals.
#define __FILELINE__
virtual std::ostream & writeGraph(std::ostream &os) const
This method is used to write down in os the edges of the graph by calling the signals writeGraph meth...
std::map< std::string, SignalBase< int > * > SignalMap
virtual SignalBase< int > * test()
static const std::string CLASS_NAME
dynamicgraph::SignalPtr< double, int > m_sigdSIN
virtual const std::string & getClassName() const
virtual const std::string & getClassName() const
virtual std::ostream & writeCompletionList(std::ostream &os) const
This method is used write in the output stream os the signals names and the commands of the entity...
int getCode() const
Access to the error code.
Entity & getEntity(const std::string &name)
Get an entity.
virtual void display(std::ostream &os) const
Display information on the entity inside the output stream os.
static PoolStorage * getInstance()
Get unique instance of the class.
double & update(double &res, const int &inTime)
CustomEntity(const std::string &n)
void setLoggerVerbosityLevel(LoggerVerbosity lv)
Specify the verbosity level of the logger.
Entity(const std::string &name)
dynamicgraph::SignalTimeDependent< double, int > m_sigdTimeDepSOUT
LoggerVerbosity getLoggerVerbosityLevel()
Get the logger&#39;s verbosity level.
const std::string & getName() const
Entity * newEntity(const std::string &classname, const std::string &objname) const
Instantiate (and allocate) an entity.
static FactoryStorage * getInstance()
Get pointer to unique object of the class.
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(CustomEntity, "CustomEntity")
SignalBase< int > & getSignal(const std::string &signalName)
Provides a reference to the signal named signalName.
void signalDeregistration(const std::string &name)
std::string docDirectSetter(const std::string &name, const std::string &type)
dynamicgraph::SignalPtr< double, int > m_sigdSIN2


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