pool.cpp 6.21 KB
Newer Older
1
2
3
4
5
// Copyright 2010 Thomas Moulard.
//

#include <dynamic-graph/entity.h>
#include <dynamic-graph/exception-factory.h>
6
#include <dynamic-graph/factory.h>
7
#include <dynamic-graph/pool.h>
Olivier Stasse's avatar
Olivier Stasse committed
8
9
#include <dynamic-graph/signal-ptr.h>
#include <dynamic-graph/signal-time-dependent.h>
10
11
#include <iostream>
#include <sstream>
12
13
14

#define BOOST_TEST_MODULE pool

Guilhem Saurel's avatar
Guilhem Saurel committed
15
16
17
#if BOOST_VERSION >= 105900
#include <boost/test/tools/output_test_stream.hpp>
#else
18
#include <boost/test/output_test_stream.hpp>
Guilhem Saurel's avatar
Guilhem Saurel committed
19
#endif
20
#include <boost/test/unit_test.hpp>
21
22
23

using boost::test_tools::output_test_stream;

24
struct MyEntity : public dynamicgraph::Entity {
25
26
  static const std::string CLASS_NAME;

Olivier Stasse's avatar
Olivier Stasse committed
27
28
  dynamicgraph::SignalPtr<double, int> m_sigdSIN;
  dynamicgraph::SignalTimeDependent<double, int> m_sigdTimeDepSOUT;
Guilhem Saurel's avatar
Guilhem Saurel committed
29

Olivier Stasse's avatar
Olivier Stasse committed
30
  explicit MyEntity(const std::string &name)
Bergé's avatar
Bergé committed
31
32
      : Entity(name),
        m_sigdSIN(NULL, "MyEntity(" + name + ")::input(double)::in_double"),
33
34
35
36
        m_sigdTimeDepSOUT(
            boost::bind(&MyEntity::update, this, boost::placeholders::_1,
                        boost::placeholders::_2),
            m_sigdSIN, "MyEntity(" + name + ")::input(double)::out_double") {
Olivier Stasse's avatar
Olivier Stasse committed
37
38
    signalRegistration(m_sigdSIN << m_sigdTimeDepSOUT);
  }
39

40
41
  virtual void display(std::ostream &os) const {
    os << "Hello! My name is " << getName() << " !" << std::endl;
42
  }
43

Bergé's avatar
Bergé committed
44
  virtual const std::string &getClassName() const { return CLASS_NAME; }
Olivier Stasse's avatar
Olivier Stasse committed
45

46
  double &update(double &res, const int &inTime) {
Olivier Stasse's avatar
Olivier Stasse committed
47
48
49
50
    const double &aDouble = m_sigdSIN(inTime);
    res = aDouble;
    return res;
  }
51
52
};

Bergé's avatar
Bergé committed
53
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(MyEntity, "MyEntity");
54

55
namespace dg = dynamicgraph;
56
BOOST_AUTO_TEST_CASE(pool_display) {
57
  /// Create Entity
58
59
  dg::Entity *entity =
      dg::FactoryStorage::getInstance()->newEntity("MyEntity", "MyEntityInst");
60

61
  /// Test exception catching when registering Entity
Bergé's avatar
Bergé committed
62
  bool res = false;
63
64
65
66
67
68
69
70
71
  try {
    dg::Entity *entity2 = dg::FactoryStorage::getInstance()->newEntity(
        "MyEntity", "MyEntityInst");

    bool res2 = (entity2 == entity);
    BOOST_CHECK(res2);
  } catch (const dg::ExceptionFactory &aef) {
    res = (aef.getCode() == dg::ExceptionFactory::OBJECT_CONFLICT);
  }
72
73
74
  BOOST_CHECK(res);

  /// Test exception catching when deregistering Entity
Bergé's avatar
Bergé committed
75
  res = false;
76
77
78
79
80
  try {
    dg::FactoryStorage::getInstance()->deregisterEntity("MyEntityInstFailure");
  } catch (const dg::ExceptionFactory &aef) {
    res = (aef.getCode() == dg::ExceptionFactory::OBJECT_CONFLICT);
  }
81
82
83
  BOOST_CHECK(res);

  /// Search for an entity inside the map
84
  output_test_stream output;
Bergé's avatar
Bergé committed
85
  dg::Entity &e = dg::PoolStorage::getInstance()->getEntity("MyEntityInst");
86
  e.display(output);
Bergé's avatar
Bergé committed
87
  BOOST_CHECK(output.is_equal("Hello! My name is MyEntityInst !\n"));
88

89
  /// Search for an entity inside the map
Bergé's avatar
Bergé committed
90
  res = false;
91
92
93
94
95
  try {
    dg::PoolStorage::getInstance()->getEntity("MyEntityInstFailure");
  } catch (const dg::ExceptionFactory &aef) {
    res = (aef.getCode() == dg::ExceptionFactory::UNREFERED_OBJECT);
  }
Bergé's avatar
Bergé committed
96
  BOOST_CHECK(res);
97
98

  /// Testing entityMap
99
  const dg::PoolStorage::Entities &anEntityMap =
100
      dg::PoolStorage::getInstance()->getEntityMap();
101

Bergé's avatar
Bergé committed
102
  bool testExistence = anEntityMap.find("MyEntityInst") == anEntityMap.end();
103
  BOOST_CHECK(!testExistence);
Guilhem Saurel's avatar
Guilhem Saurel committed
104

105
  /// Testing the existence of an entity
106
107
  testExistence =
      dg::PoolStorage::getInstance()->existEntity("MyEntityInst", entity);
108
109
110

  BOOST_CHECK(testExistence);

111
  /// Testing the completion list of pool storage
Bergé's avatar
Bergé committed
112
  dg::PoolStorage::getInstance()->writeCompletionList(output);
113
114
115
  BOOST_CHECK(
      output.is_equal("MyEntityInst.in_double\nMyEntityInst.out_double\n"
                      "print\nsignals\nsignalDep\n"));
Olivier Stasse's avatar
Olivier Stasse committed
116

117
  /// Checking the graph generated by the pool
Olivier Stasse's avatar
Olivier Stasse committed
118
119
120
121
122
123
124
  dg::PoolStorage::getInstance()->writeGraph("output.dot");
  std::fstream the_debug_file;
  the_debug_file.open("output.dot");
  std::ostringstream oss_output_wgph;
  oss_output_wgph << the_debug_file.rdbuf();
  the_debug_file.close();

Guilhem Saurel's avatar
Guilhem Saurel committed
125
  /// Use a predefined output
Bergé's avatar
Bergé committed
126
127
128
  std::string str_to_test =
      "/* This graph has been automatically generated.\n"
      "   2019 Month: 2 Day: 28 Time: 11:28 */\n"
129
130
131
132
      "digraph \"output\" { \t graph [ label=\"output\" "
      "bgcolor = white rankdir=LR ]\n"
      "\t node [ fontcolor = black, color = black,fillcolor = gold1,"
      " style=filled, shape=box ] ; \n"
Bergé's avatar
Bergé committed
133
134
135
      "\tsubgraph cluster_Entities { \n"
      "\t} \n"
      "\"MyEntityInst\" [ label = \"MyEntityInst\" ,\n"
136
137
      "   fontcolor = black, color = black, fillcolor=cyan, style=filled,"
      " shape=box ]\n"
Bergé's avatar
Bergé committed
138
      "}\n";
Olivier Stasse's avatar
Olivier Stasse committed
139

Guilhem Saurel's avatar
Guilhem Saurel committed
140
  /// Check the two substring (remove the date) -
Olivier Stasse's avatar
Olivier Stasse committed
141
  std::string s_output_wgph = oss_output_wgph.str();
Bergé's avatar
Bergé committed
142
  std::string s_crmk = "*/";
Olivier Stasse's avatar
Olivier Stasse committed
143
144

  std::size_t find_s_output_wgph = s_output_wgph.find(s_crmk);
145
146
  std::string sub_s_output_wgph =
      s_output_wgph.substr(find_s_output_wgph, s_output_wgph.length());
Olivier Stasse's avatar
Olivier Stasse committed
147
  std::size_t find_str_to_test = str_to_test.find(s_crmk);
148
149
  std::string sub_str_to_test =
      str_to_test.substr(find_str_to_test, str_to_test.length());
Olivier Stasse's avatar
Olivier Stasse committed
150
151

  bool two_sub_string_identical;
Bergé's avatar
Bergé committed
152
  two_sub_string_identical = sub_str_to_test == sub_s_output_wgph;
153
154
155
  std::cout << sub_str_to_test << std::endl;
  std::cout << sub_s_output_wgph << std::endl;
  std::cout << sub_str_to_test.compare(sub_s_output_wgph) << std::endl;
Olivier Stasse's avatar
Olivier Stasse committed
156
157
  BOOST_CHECK(two_sub_string_identical);

158
  /// Test name of a valid signal.
159
  std::istringstream an_iss("MyEntityInst.in_double");
Guilhem Saurel's avatar
Guilhem Saurel committed
160

161
  dg::SignalBase<int> &aSignal =
162
      dg::PoolStorage::getInstance()->getSignal(an_iss);
163

Bergé's avatar
Bergé committed
164
  std::string aSignalName = aSignal.getName();
165
166
  testExistence =
      aSignalName == "MyEntity(MyEntityInst)::input(double)::in_double";
167
  BOOST_CHECK(testExistence);
168
169
170
171

  /// Test name of an unvalid signal.
  an_iss.str("MyEntityInst.in2double");

172
173
174
175
176
  try {
    dg::PoolStorage::getInstance()->getSignal(an_iss);
  } catch (const dg::ExceptionFactory &aef) {
    res = (aef.getCode() == dg::ExceptionFactory::UNREFERED_SIGNAL);
  }
177
  BOOST_CHECK(res);
Guilhem Saurel's avatar
Guilhem Saurel committed
178

179
  /// Deregister the entity.
Bergé's avatar
Bergé committed
180
  dg::PoolStorage::getInstance()->deregisterEntity(entity->getName());
181

182
  /// Testing the existance of an entity
183
184
  testExistence =
      dg::PoolStorage::getInstance()->existEntity("MyEntityInst", entity);
Guilhem Saurel's avatar
Guilhem Saurel committed
185

186
  BOOST_CHECK(!testExistence);
187
188
189

  /// Create Entity
  std::string name_entity("MyEntityInst2");
Bergé's avatar
Bergé committed
190
  dg::PoolStorage::getInstance()->clearPlugin(name_entity);
Guilhem Saurel's avatar
Guilhem Saurel committed
191

192
  dg::PoolStorage::destroy();
193
}