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;
24 
25 struct MyEntity : public dynamicgraph::Entity {
26  static const std::string CLASS_NAME;
27 
30 
31  explicit MyEntity(const std::string &name)
32  : Entity(name),
33  m_sigdSIN(NULL, "MyEntity(" + name + ")::input(double)::in_double"),
34  m_sigdTimeDepSOUT(boost::bind(&MyEntity::update, this, _1, _2),
35  m_sigdSIN,
36  "MyEntity(" + name + ")::input(double)::out_double") {
37  signalRegistration(m_sigdSIN << m_sigdTimeDepSOUT);
38  }
39 
40  virtual void display(std::ostream &os) const {
41  os << "Hello! My name is " << getName() << " !" << std::endl;
42  }
43 
44  virtual const std::string &getClassName() const { return CLASS_NAME; }
45 
46  double &update(double &res, const int &inTime) {
47  const double &aDouble = m_sigdSIN(inTime);
48  res = aDouble;
49  return res;
50  }
51 };
52 
54 
55 namespace dg = dynamicgraph;
56 BOOST_AUTO_TEST_CASE(pool_display) {
58  dg::Entity *entity =
59  dg::FactoryStorage::getInstance()->newEntity("MyEntity", "MyEntityInst");
60 
62  bool res = false;
63  try {
65  "MyEntity", "MyEntityInst");
66 
67  bool res2 = (entity2 == entity);
68  BOOST_CHECK(res2);
69  } catch (const dg::ExceptionFactory &aef) {
71  }
72  BOOST_CHECK(res);
73 
75  res = false;
76  try {
77  dg::FactoryStorage::getInstance()->deregisterEntity("MyEntityInstFailure");
78  } catch (const dg::ExceptionFactory &aef) {
80  }
81  BOOST_CHECK(res);
82 
84  output_test_stream output;
85  dg::Entity &e = dg::PoolStorage::getInstance()->getEntity("MyEntityInst");
86  e.display(output);
87  BOOST_CHECK(output.is_equal("Hello! My name is MyEntityInst !\n"));
88 
90  res = false;
91  try {
92  dg::PoolStorage::getInstance()->getEntity("MyEntityInstFailure");
93  } catch (const dg::ExceptionFactory &aef) {
95  }
96  BOOST_CHECK(res);
97 
99  const dg::PoolStorage::Entities &anEntityMap =
101 
102  bool testExistence = anEntityMap.find("MyEntityInst") == anEntityMap.end();
103  BOOST_CHECK(!testExistence);
104 
106  testExistence =
107  dg::PoolStorage::getInstance()->existEntity("MyEntityInst", entity);
108 
109  BOOST_CHECK(testExistence);
110 
113  BOOST_CHECK(
114  output.is_equal("MyEntityInst.in_double\nMyEntityInst.out_double\n"
115  "print\nsignals\nsignalDep\n"));
116 
118  dg::PoolStorage::getInstance()->writeGraph("output.dot");
119  std::fstream the_debug_file;
120  the_debug_file.open("output.dot");
121  std::ostringstream oss_output_wgph;
122  oss_output_wgph << the_debug_file.rdbuf();
123  the_debug_file.close();
124 
126  std::string str_to_test =
127  "/* This graph has been automatically generated.\n"
128  " 2019 Month: 2 Day: 28 Time: 11:28 */\n"
129  "digraph \"output\" { \t graph [ label=\"output\" "
130  "bgcolor = white rankdir=LR ]\n"
131  "\t node [ fontcolor = black, color = black,fillcolor = gold1,"
132  " style=filled, shape=box ] ; \n"
133  "\tsubgraph cluster_Entities { \n"
134  "\t} \n"
135  "\"MyEntityInst\" [ label = \"MyEntityInst\" ,\n"
136  " fontcolor = black, color = black, fillcolor=cyan, style=filled,"
137  " shape=box ]\n"
138  "}\n";
139 
141  std::string s_output_wgph = oss_output_wgph.str();
142  std::string s_crmk = "*/";
143 
144  std::size_t find_s_output_wgph = s_output_wgph.find(s_crmk);
145  std::string sub_s_output_wgph =
146  s_output_wgph.substr(find_s_output_wgph, s_output_wgph.length());
147  std::size_t find_str_to_test = str_to_test.find(s_crmk);
148  std::string sub_str_to_test =
149  str_to_test.substr(find_str_to_test, str_to_test.length());
150 
151  bool two_sub_string_identical;
152  two_sub_string_identical = sub_str_to_test == sub_s_output_wgph;
153  std::cout << sub_str_to_test << std::endl;
154  std::cout << sub_s_output_wgph << std::endl;
155  std::cout << sub_str_to_test.compare(sub_s_output_wgph) << std::endl;
156  BOOST_CHECK(two_sub_string_identical);
157 
159  std::istringstream an_iss("MyEntityInst.in_double");
160 
161  dg::SignalBase<int> &aSignal =
163 
164  std::string aSignalName = aSignal.getName();
165  testExistence =
166  aSignalName == "MyEntity(MyEntityInst)::input(double)::in_double";
167  BOOST_CHECK(testExistence);
168 
170  an_iss.str("MyEntityInst.in2double");
171 
172  try {
174  } catch (const dg::ExceptionFactory &aef) {
176  }
177  BOOST_CHECK(res);
178 
181 
183  testExistence =
184  dg::PoolStorage::getInstance()->existEntity("MyEntityInst", entity);
185 
186  BOOST_CHECK(!testExistence);
187 
189  std::string name_entity("MyEntityInst2");
191 
193 }
void deregisterEntity(const std::string &entname)
Unregister an entity.
virtual void display(std::ostream &os) const
Display information on the entity inside the output stream os.
Definition: tests/pool.cpp:40
void writeGraph(const std::string &aFileName)
This method write a graph description on the file named FileName.
This class represents an entity, i.e. a generic computational unit that provides input and output sig...
dynamicgraph::SignalTimeDependent< double, int > m_sigdTimeDepSOUT
Definition: tests/pool.cpp:29
void signalRegistration(const SignalArray< int > &signals)
double & update(double &res, const int &inTime)
Definition: tests/pool.cpp:46
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(MyEntity, "MyEntity")
void deregisterEntity(const std::string &entname)
Delete an entity from the factory.
MyEntity(const std::string &name)
Definition: tests/pool.cpp:31
dynamicgraph::SignalPtr< double, int > m_sigdSIN
Definition: tests/pool.cpp:28
void writeCompletionList(std::ostream &os)
const std::string & getName() const
Definition: signal-base.h:42
bool existEntity(const std::string &name)
Test if the entity exists.
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.
static void destroy()
Destroy the unique instance of the class.
Entity(const std::string &name)
virtual const std::string & getClassName() const
Definition: tests/pool.cpp:44
const std::string & getName() const
std::map< std::string, Entity * > Entities
Sorted set of entities with unique key (name).
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.
const Entities & getEntityMap() const
Const access to entity map.
static const std::string CLASS_NAME
Definition: tests/pool.cpp:26
BOOST_AUTO_TEST_CASE(pool_display)
Definition: tests/pool.cpp:56
SignalBase< int > & getSignal(std::istringstream &sigpath)
Get a signal by name.
void clearPlugin(const std::string &name)
Disallocate an entity.


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