tests/pool.cpp
Go to the documentation of this file.
1 // Copyright 2010 Thomas Moulard.
2 //
3 
4 #include <dynamic-graph/entity.h>
7 #include <dynamic-graph/pool.h>
10 
11 #include <iostream>
12 #include <sstream>
13 
14 #define BOOST_TEST_MODULE pool
15 
16 #if BOOST_VERSION >= 105900
17 #include <boost/test/tools/output_test_stream.hpp>
18 #else
19 #include <boost/test/output_test_stream.hpp>
20 #endif
21 #include <boost/test/unit_test.hpp>
22 
23 using boost::test_tools::output_test_stream;
25 
26 struct MyEntity : public dynamicgraph::Entity {
27  static const std::string CLASS_NAME;
28 
31 
32  explicit MyEntity(const std::string &name)
33  : Entity(name),
34  m_sigdSIN(NULL, "MyEntity(" + name + ")::input(double)::in_double"),
35  m_sigdTimeDepSOUT(boost::bind(&MyEntity::update, this, _1, _2),
36  m_sigdSIN,
37  "MyEntity(" + name + ")::input(double)::out_double") {
39  }
40 
41  virtual void display(std::ostream &os) const {
42  os << "Hello! My name is " << getName() << " !" << std::endl;
43  }
44 
45  virtual const std::string &getClassName() const { return CLASS_NAME; }
46 
47  double &update(double &res, const sigtime_t &inTime) {
48  const double &aDouble = m_sigdSIN(inTime);
49  res = aDouble;
50  return res;
51  }
52 };
53 
55 
56 namespace dg = dynamicgraph;
57 BOOST_AUTO_TEST_CASE(pool_display) {
59  dg::Entity *entity =
60  dg::FactoryStorage::getInstance()->newEntity("MyEntity", "MyEntityInst");
61 
63  bool res = false;
64  try {
66  "MyEntity", "MyEntityInst");
67 
68  bool res2 = (entity2 == entity);
69  BOOST_CHECK(res2);
70  } catch (const dg::ExceptionFactory &aef) {
72  }
73  BOOST_CHECK(res);
74 
76  res = false;
77  try {
78  dg::FactoryStorage::getInstance()->deregisterEntity("MyEntityInstFailure");
79  } catch (const dg::ExceptionFactory &aef) {
81  }
82  BOOST_CHECK(res);
83 
85  output_test_stream output;
86  dg::Entity &e = dg::PoolStorage::getInstance()->getEntity("MyEntityInst");
87  e.display(output);
88  BOOST_CHECK(output.is_equal("Hello! My name is MyEntityInst !\n"));
89 
91  res = false;
92  try {
93  dg::PoolStorage::getInstance()->getEntity("MyEntityInstFailure");
94  } catch (const dg::ExceptionFactory &aef) {
96  }
97  BOOST_CHECK(res);
98 
100  const dg::PoolStorage::Entities &anEntityMap =
102 
103  bool testExistence = anEntityMap.find("MyEntityInst") == anEntityMap.end();
104  BOOST_CHECK(!testExistence);
105 
107  testExistence =
108  dg::PoolStorage::getInstance()->existEntity("MyEntityInst", entity);
109 
110  BOOST_CHECK(testExistence);
111 
114  BOOST_CHECK(
115  output.is_equal("MyEntityInst.in_double\nMyEntityInst.out_double\n"
116  "print\nsignals\nsignalDep\n"));
117 
119  dg::PoolStorage::getInstance()->writeGraph("output.dot");
120  std::fstream the_debug_file;
121  the_debug_file.open("output.dot");
122  std::ostringstream oss_output_wgph;
123  oss_output_wgph << the_debug_file.rdbuf();
124  the_debug_file.close();
125 
127  std::string str_to_test =
128  "/* This graph has been automatically generated.\n"
129  " 2019 Month: 2 Day: 28 Time: 11:28 */\n"
130  "digraph \"output\" { \t graph [ label=\"output\" "
131  "bgcolor = white rankdir=LR ]\n"
132  "\t node [ fontcolor = black, color = black,fillcolor = gold1,"
133  " style=filled, shape=box ] ; \n"
134  "\tsubgraph cluster_Entities { \n"
135  "\t} \n"
136  "\"MyEntityInst\" [ label = \"MyEntityInst\" ,\n"
137  " fontcolor = black, color = black, fillcolor=cyan, style=filled,"
138  " shape=box ]\n"
139  "}\n";
140 
142  std::string s_output_wgph = oss_output_wgph.str();
143  std::string s_crmk = "*/";
144 
145  std::size_t find_s_output_wgph = s_output_wgph.find(s_crmk);
146  std::string sub_s_output_wgph =
147  s_output_wgph.substr(find_s_output_wgph, s_output_wgph.length());
148  std::size_t find_str_to_test = str_to_test.find(s_crmk);
149  std::string sub_str_to_test =
150  str_to_test.substr(find_str_to_test, str_to_test.length());
151 
152  bool two_sub_string_identical;
153  two_sub_string_identical = sub_str_to_test == sub_s_output_wgph;
154  std::cout << sub_str_to_test << std::endl;
155  std::cout << sub_s_output_wgph << std::endl;
156  std::cout << sub_str_to_test.compare(sub_s_output_wgph) << std::endl;
157  BOOST_CHECK(two_sub_string_identical);
158 
160  std::istringstream an_iss("MyEntityInst.in_double");
161 
162  dg::SignalBase<sigtime_t> &aSignal =
164 
165  std::string aSignalName = aSignal.getName();
166  testExistence =
167  aSignalName == "MyEntity(MyEntityInst)::input(double)::in_double";
168  BOOST_CHECK(testExistence);
169 
171  an_iss.str("MyEntityInst.in2double");
172 
173  try {
175  } catch (const dg::ExceptionFactory &aef) {
177  }
178  BOOST_CHECK(res);
179 
182 
184  testExistence =
185  dg::PoolStorage::getInstance()->existEntity("MyEntityInst", entity);
186 
187  BOOST_CHECK(!testExistence);
188 
190  std::string name_entity("MyEntityInst2");
192 
194 }
dynamicgraph::ExceptionFactory::UNREFERED_SIGNAL
@ UNREFERED_SIGNAL
Definition: exception-factory.h:23
entity.h
signal-time-dependent.h
dynamicgraph::PoolStorage::getSignal
SignalBase< sigtime_t > & getSignal(std::istringstream &sigpath)
Get a signal by name.
Definition: src/dgraph/pool.cpp:203
MyEntity::getClassName
virtual const std::string & getClassName() const
Definition: tests/pool.cpp:45
MyEntity::m_sigdSIN
dynamicgraph::SignalPtr< double, sigtime_t > m_sigdSIN
Definition: tests/pool.cpp:29
dynamicgraph::SignalPtr< double, sigtime_t >
dynamicgraph
dynamicgraph::PoolStorage::deregisterEntity
void deregisterEntity(const std::string &entname)
Unregister an entity.
Definition: src/dgraph/pool.cpp:75
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::OBJECT_CONFLICT
@ OBJECT_CONFLICT
Definition: exception-factory.h:28
dynamicgraph::PoolStorage::writeGraph
void writeGraph(const std::string &aFileName)
This method write a graph description on the file named FileName.
Definition: src/dgraph/pool.cpp:141
sigtime_t
dynamicgraph::sigtime_t sigtime_t
Definition: debug-tracer.cpp:75
dynamicgraph::Entity::name
std::string name
Definition: include/dynamic-graph/entity.h:174
MyEntity::update
double & update(double &res, const sigtime_t &inTime)
Definition: tests/pool.cpp:47
dynamicgraph::PoolStorage::getEntity
Entity & getEntity(const std::string &name)
Get an entity.
Definition: src/dgraph/pool.cpp:93
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(MyEntity, "MyEntity")
signal-ptr.h
dynamicgraph::Entity::getName
const std::string & getName() const
Definition: include/dynamic-graph/entity.h:60
dynamicgraph::sigtime_t
int64_t sigtime_t
Definition: fwd.hh:12
dynamicgraph::PoolStorage::clearPlugin
void clearPlugin(const std::string &name)
Disallocate an entity.
Definition: src/dgraph/pool.cpp:122
dynamicgraph::SignalBase::getName
const std::string & getName() const
Definition: signal-base.h:42
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
MyEntity::CLASS_NAME
static const std::string CLASS_NAME
Definition: tests/pool.cpp:27
dynamicgraph::ExceptionFactory
Generic error class.
Definition: exception-factory.h:18
MyEntity::display
virtual void display(std::ostream &os) const
Display information on the entity inside the output stream os.
Definition: tests/pool.cpp:41
dynamicgraph::Entity::Entity
Entity(const std::string &name)
Definition: src/dgraph/entity.cpp:33
dynamicgraph::PoolStorage::getEntityMap
const Entities & getEntityMap() const
Const access to entity map.
Definition: src/dgraph/pool.cpp:104
exception-factory.h
dynamicgraph::FactoryStorage::deregisterEntity
void deregisterEntity(const std::string &entname)
Delete an entity from the factory.
Definition: src/dgraph/factory.cpp:61
dynamicgraph::ExceptionFactory::UNREFERED_OBJECT
@ UNREFERED_OBJECT
Definition: exception-factory.h:22
factory.h
MyEntity::m_sigdTimeDepSOUT
dynamicgraph::SignalTimeDependent< double, sigtime_t > m_sigdTimeDepSOUT
Definition: tests/pool.cpp:30
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(pool_display)
Definition: tests/pool.cpp:57
dynamicgraph::PoolStorage::existEntity
bool existEntity(const std::string &name)
Test if the entity exists.
Definition: src/dgraph/pool.cpp:108
dynamicgraph::SignalTimeDependent< double, sigtime_t >
dynamicgraph::PoolStorage::writeCompletionList
void writeCompletionList(std::ostream &os)
Definition: src/dgraph/pool.cpp:179
dynamicgraph::PoolStorage::Entities
std::map< std::string, Entity * > Entities
Sorted set of entities with unique key (name).
Definition: include/dynamic-graph/pool.h:39
dynamicgraph::PoolStorage::getInstance
static PoolStorage * getInstance()
Get unique instance of the class.
Definition: src/dgraph/pool.cpp:31
dynamicgraph::PoolStorage::destroy
static void destroy()
Destroy the unique instance of the class.
Definition: src/dgraph/pool.cpp:38
MyEntity
Definition: tests/pool.cpp:26
MyEntity::MyEntity
MyEntity(const std::string &name)
Definition: tests/pool.cpp:32
dynamicgraph::SignalBase< sigtime_t >
sigtime_t
dynamicgraph::sigtime_t sigtime_t
Definition: tests/pool.cpp:24
dynamicgraph::Entity::signalRegistration
void signalRegistration(const SignalArray< sigtime_t > &signals)
Definition: src/dgraph/entity.cpp:59
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


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