GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: tests/pool.cpp Lines: 73 80 91.2 %
Date: 2023-03-15 12:04:10 Branches: 186 382 48.7 %

Line Branch Exec Source
1
// Copyright 2010 Thomas Moulard.
2
//
3
4
#include <dynamic-graph/entity.h>
5
#include <dynamic-graph/exception-factory.h>
6
#include <dynamic-graph/factory.h>
7
#include <dynamic-graph/pool.h>
8
#include <dynamic-graph/signal-ptr.h>
9
#include <dynamic-graph/signal-time-dependent.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
28
  dynamicgraph::SignalPtr<double, int> m_sigdSIN;
29
  dynamicgraph::SignalTimeDependent<double, int> m_sigdTimeDepSOUT;
30
31
2
  explicit MyEntity(const std::string &name)
32
2
      : Entity(name),
33
2
        m_sigdSIN(NULL, "MyEntity(" + name + ")::input(double)::in_double"),
34
        m_sigdTimeDepSOUT(boost::bind(&MyEntity::update, this, _1, _2),
35
                          m_sigdSIN,
36




4
                          "MyEntity(" + name + ")::input(double)::out_double") {
37

1
    signalRegistration(m_sigdSIN << m_sigdTimeDepSOUT);
38
1
  }
39
40
1
  virtual void display(std::ostream &os) const {
41
1
    os << "Hello! My name is " << getName() << " !" << std::endl;
42
1
  }
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
53
2
DYNAMICGRAPH_FACTORY_ENTITY_PLUGIN(MyEntity, "MyEntity");
54
55
namespace dg = dynamicgraph;
56
















4
BOOST_AUTO_TEST_CASE(pool_display) {
57
  /// Create Entity
58
  dg::Entity *entity =
59


2
      dg::FactoryStorage::getInstance()->newEntity("MyEntity", "MyEntityInst");
60
61
  /// Test exception catching when registering Entity
62
2
  bool res = false;
63
  try {
64


10
    dg::Entity *entity2 = dg::FactoryStorage::getInstance()->newEntity(
65
        "MyEntity", "MyEntityInst");
66
67
    bool res2 = (entity2 == entity);
68
    BOOST_CHECK(res2);
69
2
  } catch (const dg::ExceptionFactory &aef) {
70
2
    res = (aef.getCode() == dg::ExceptionFactory::OBJECT_CONFLICT);
71
  }
72



2
  BOOST_CHECK(res);
73
74
  /// Test exception catching when deregistering Entity
75
2
  res = false;
76
  try {
77

6
    dg::FactoryStorage::getInstance()->deregisterEntity("MyEntityInstFailure");
78
2
  } catch (const dg::ExceptionFactory &aef) {
79
2
    res = (aef.getCode() == dg::ExceptionFactory::OBJECT_CONFLICT);
80
  }
81



2
  BOOST_CHECK(res);
82
83
  /// Search for an entity inside the map
84

4
  output_test_stream output;
85

2
  dg::Entity &e = dg::PoolStorage::getInstance()->getEntity("MyEntityInst");
86
2
  e.display(output);
87



2
  BOOST_CHECK(output.is_equal("Hello! My name is MyEntityInst !\n"));
88
89
  /// Search for an entity inside the map
90
2
  res = false;
91
  try {
92

6
    dg::PoolStorage::getInstance()->getEntity("MyEntityInstFailure");
93
2
  } catch (const dg::ExceptionFactory &aef) {
94
2
    res = (aef.getCode() == dg::ExceptionFactory::UNREFERED_OBJECT);
95
  }
96



2
  BOOST_CHECK(res);
97
98
  /// Testing entityMap
99
  const dg::PoolStorage::Entities &anEntityMap =
100

2
      dg::PoolStorage::getInstance()->getEntityMap();
101
102

2
  bool testExistence = anEntityMap.find("MyEntityInst") == anEntityMap.end();
103



2
  BOOST_CHECK(!testExistence);
104
105
  /// Testing the existence of an entity
106
  testExistence =
107

2
      dg::PoolStorage::getInstance()->existEntity("MyEntityInst", entity);
108
109



2
  BOOST_CHECK(testExistence);
110
111
  /// Testing the completion list of pool storage
112

2
  dg::PoolStorage::getInstance()->writeCompletionList(output);
113



2
  BOOST_CHECK(
114
      output.is_equal("MyEntityInst.in_double\nMyEntityInst.out_double\n"
115
                      "print\nsignals\nsignalDep\n"));
116
117
  /// Checking the graph generated by the pool
118

2
  dg::PoolStorage::getInstance()->writeGraph("output.dot");
119
4
  std::fstream the_debug_file;
120
2
  the_debug_file.open("output.dot");
121
4
  std::ostringstream oss_output_wgph;
122

2
  oss_output_wgph << the_debug_file.rdbuf();
123
2
  the_debug_file.close();
124
125
  /// Use a predefined output
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
4
      "}\n";
139
140
  /// Check the two substring (remove the date) -
141
4
  std::string s_output_wgph = oss_output_wgph.str();
142
4
  std::string s_crmk = "*/";
143
144
2
  std::size_t find_s_output_wgph = s_output_wgph.find(s_crmk);
145
  std::string sub_s_output_wgph =
146
4
      s_output_wgph.substr(find_s_output_wgph, s_output_wgph.length());
147
2
  std::size_t find_str_to_test = str_to_test.find(s_crmk);
148
  std::string sub_str_to_test =
149
4
      str_to_test.substr(find_str_to_test, str_to_test.length());
150
151
  bool two_sub_string_identical;
152
2
  two_sub_string_identical = sub_str_to_test == sub_s_output_wgph;
153

2
  std::cout << sub_str_to_test << std::endl;
154

2
  std::cout << sub_s_output_wgph << std::endl;
155

2
  std::cout << sub_str_to_test.compare(sub_s_output_wgph) << std::endl;
156



2
  BOOST_CHECK(two_sub_string_identical);
157
158
  /// Test name of a valid signal.
159

6
  std::istringstream an_iss("MyEntityInst.in_double");
160
161
  dg::SignalBase<int> &aSignal =
162

2
      dg::PoolStorage::getInstance()->getSignal(an_iss);
163
164
4
  std::string aSignalName = aSignal.getName();
165
  testExistence =
166
2
      aSignalName == "MyEntity(MyEntityInst)::input(double)::in_double";
167



2
  BOOST_CHECK(testExistence);
168
169
  /// Test name of an unvalid signal.
170

2
  an_iss.str("MyEntityInst.in2double");
171
172
  try {
173

2
    dg::PoolStorage::getInstance()->getSignal(an_iss);
174
2
  } catch (const dg::ExceptionFactory &aef) {
175
2
    res = (aef.getCode() == dg::ExceptionFactory::UNREFERED_SIGNAL);
176
  }
177



2
  BOOST_CHECK(res);
178
179
  /// Deregister the entity.
180

2
  dg::PoolStorage::getInstance()->deregisterEntity(entity->getName());
181
182
  /// Testing the existance of an entity
183
  testExistence =
184

2
      dg::PoolStorage::getInstance()->existEntity("MyEntityInst", entity);
185
186



2
  BOOST_CHECK(!testExistence);
187
188
  /// Create Entity
189
4
  std::string name_entity("MyEntityInst2");
190

2
  dg::PoolStorage::getInstance()->clearPlugin(name_entity);
191
192
2
  dg::PoolStorage::destroy();
193
2
}