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"),
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() {
55  try {
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 sigtime_t &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 
160 BOOST_AUTO_TEST_CASE(displaySignalList) {
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 
180 BOOST_AUTO_TEST_CASE(getCommandList) {
181  dynamicgraph::Entity &entity =
183 
184  BOOST_CHECK_EQUAL(entity.getCommandList(), "print\nsignals\nsignalDep");
185 }
186 
187 BOOST_AUTO_TEST_CASE(writeGraph) {
188  dynamicgraph::Entity &entity =
190 
191  output_test_stream output;
192  entity.writeGraph(output);
193 
194  BOOST_CHECK(output.is_equal(""));
195 }
196 
197 BOOST_AUTO_TEST_CASE(writeCompletionList) {
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?
249  dynamicgraph::Entity &entity =
251 
252  BOOST_CHECK_EQUAL(entity.test(),
253  static_cast<dynamicgraph::SignalBase<sigtime_t> *>(0));
254 
255  entity.test2(static_cast<dynamicgraph::SignalBase<sigtime_t> *>(0));
256 }
dynamicgraph::ExceptionFactory::UNREFERED_SIGNAL
@ UNREFERED_SIGNAL
Definition: exception-factory.h:23
entity.h
dynamicgraph::CustomEntity::m_sigdSIN2
dynamicgraph::SignalPtr< double, sigtime_t > m_sigdSIN2
Definition: tests/entity.cpp:35
signal-time-dependent.h
dynamicgraph::Entity::logger
Logger & logger()
Definition: include/dynamic-graph/entity.h:135
dynamicgraph::CustomEntity::getClassName
virtual const std::string & getClassName() const
Definition: tests/entity.cpp:39
dynamicgraph::CustomEntity::m_sigdTimeDepSOUT
dynamicgraph::SignalTimeDependent< double, sigtime_t > m_sigdTimeDepSOUT
Definition: tests/entity.cpp:36
dynamicgraph::ExceptionFactory::SIGNAL_CONFLICT
@ SIGNAL_CONFLICT
Definition: exception-factory.h:26
__FILELINE__
#define __FILELINE__
dynamicgraph::LoggerVerbosity
LoggerVerbosity
Definition: logger.h:143
dynamicgraph::SignalPtr< double, sigtime_t >
dynamicgraph
dynamicgraph::CustomEntity::~CustomEntity
~CustomEntity()
Definition: tests/entity.cpp:50
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
sigtime_t
dynamicgraph::sigtime_t sigtime_t
Definition: tests/entity.cpp:247
dynamicgraph::Entity::writeGraph
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...
Definition: src/dgraph/entity.cpp:150
dynamicgraph::DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(CustomEntity, "CustomEntity")
dynamicgraph::Entity::name
std::string name
Definition: include/dynamic-graph/entity.h:174
dynamicgraph::CustomEntity
Definition: command-test.cpp:36
dynamicgraph::Entity::getSignalMap
SignalMap getSignalMap() const
Provides a map of all the signals.
Definition: src/dgraph/entity.cpp:179
dynamicgraph::Entity::displaySignalList
std::ostream & displaySignalList(std::ostream &os) const
Display the list of signals of this entity in output stream os.
Definition: src/dgraph/entity.cpp:135
dynamicgraph::MSG_TYPE_WARNING
@ MSG_TYPE_WARNING
Definition: logger.h:35
dynamicgraph::PoolStorage::getEntity
Entity & getEntity(const std::string &name)
Get an entity.
Definition: src/dgraph/pool.cpp:93
dynamicgraph::Entity::getDocString
virtual std::string getDocString() const
Returns the Entity documentation.
Definition: src/dgraph/entity.cpp:104
signal-ptr.h
dynamicgraph::MSG_TYPE_INFO
@ MSG_TYPE_INFO
Definition: logger.h:34
dynamicgraph::CustomEntity::CustomEntity
CustomEntity(const std::string &n)
Definition: tests/entity.cpp:40
dynamicgraph::Entity::SignalMap
std::map< std::string, SignalBase< sigtime_t > * > SignalMap
Definition: include/dynamic-graph/entity.h:54
dynamicgraph::MSG_TYPE_ERROR
@ MSG_TYPE_ERROR
Definition: logger.h:36
CustomEntity
Definition: custom-entity.cpp:22
dynamicgraph::Entity::getName
const std::string & getName() const
Definition: include/dynamic-graph/entity.h:60
dynamicgraph::CustomEntity::m_sigdSIN
dynamicgraph::SignalPtr< double, sigtime_t > m_sigdSIN
Definition: tests/entity.cpp:35
dynamicgraph::sigtime_t
int64_t sigtime_t
Definition: fwd.hh:12
dynamicgraph::CustomEntity::rmValidSignal
void rmValidSignal()
Definition: tests/entity.cpp:63
dynamicgraph::Entity::getClassName
virtual const std::string & getClassName() const
Definition: include/dynamic-graph/entity.h:61
dynamicgraph::ExceptionAbstract::getCode
int getCode() const
Access to the error code.
Definition: exception-abstract.cpp:24
dynamicgraph::Entity::display
virtual void display(std::ostream &os) const
Display information on the entity inside the output stream os.
Definition: src/dgraph/entity.cpp:170
dynamicgraph::Entity::test
virtual SignalBase< sigtime_t > * test()
Definition: include/dynamic-graph/entity.h:111
dynamicgraph::ExceptionFactory
Generic error class.
Definition: exception-factory.h:18
dynamicgraph::command::docDirectSetter
std::string docDirectSetter(const std::string &name, const std::string &type)
Definition: command-direct-setter.h:52
command-direct-getter.h
dynamicgraph::Entity::Entity
Entity(const std::string &name)
Definition: src/dgraph/entity.cpp:33
command-direct-setter.h
dynamicgraph::CustomEntity::update
double & update(double &res, const sigtime_t &inTime)
Definition: tests/entity.cpp:68
exception-factory.h
factory.h
dynamicgraph::MSG_TYPE_DEBUG
@ MSG_TYPE_DEBUG
Definition: logger.h:33
dynamicgraph::Entity::getCommandList
const std::string & getCommandList() const
Definition: src/dgraph/entity.cpp:186
dynamicgraph::CustomEntity::CLASS_NAME
static const std::string CLASS_NAME
Definition: command-test.cpp:38
dynamicgraph::Entity::entityDeregistration
void entityDeregistration()
Definition: src/dgraph/entity.cpp:29
dynamicgraph::command::docDirectGetter
std::string docDirectGetter(const std::string &name, const std::string &type)
Definition: command-direct-getter.h:49
dynamicgraph::RealTimeLogger::destroy
static void destroy()
Definition: src/debug/real-time-logger.cpp:129
dynamicgraph::Entity::writeCompletionList
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.
Definition: src/dgraph/entity.cpp:159
dynamicgraph::SignalTimeDependent< double, sigtime_t >
dynamicgraph::Logger::stream
RTLoggerStream stream()
Definition: logger.h:200
dynamicgraph::PoolStorage::getInstance
static PoolStorage * getInstance()
Get unique instance of the class.
Definition: src/dgraph/pool.cpp:31
dynamicgraph::Entity::getSignal
SignalBase< sigtime_t > & getSignal(const std::string &signalName)
Provides a reference to the signal named signalName.
real-time-logger.h
dgADD_OSTREAM_TO_RTLOG
#define dgADD_OSTREAM_TO_RTLOG(ostr)
Definition: real-time-logger.h:16
dynamicgraph::CustomEntity::addSignal
void addSignal()
Definition: tests/entity.cpp:52
dynamicgraph::Entity::test2
virtual void test2(SignalBase< sigtime_t > *)
Definition: include/dynamic-graph/entity.h:113
dynamicgraph::SignalBase< sigtime_t >
dynamicgraph::Entity::signalDeregistration
void signalDeregistration(const std::string &name)
Definition: src/dgraph/entity.cpp:89
dynamicgraph::CustomEntity::m_value
double m_value
Definition: tests/entity.cpp:75
dynamicgraph::Entity::signalRegistration
void signalRegistration(const SignalArray< sigtime_t > &signals)
Definition: src/dgraph/entity.cpp:59
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(constructor)
Definition: tests/entity.cpp:80
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::Entity::setLoggerVerbosityLevel
void setLoggerVerbosityLevel(LoggerVerbosity lv)
Specify the verbosity level of the logger.
Definition: include/dynamic-graph/entity.h:144
dynamicgraph::FactoryStorage::getInstance
static FactoryStorage * getInstance()
Get pointer to unique object of the class.
Definition: src/dgraph/factory.cpp:15
dynamicgraph::Entity::getLoggerVerbosityLevel
LoggerVerbosity getLoggerVerbosityLevel()
Get the logger's verbosity level.
Definition: include/dynamic-graph/entity.h:147


dynamic-graph
Author(s): Nicolas Mansard, Olivier Stasse
autogenerated on Thu Jun 13 2024 02:26:22