GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: tests/signal-ptr.cpp Lines: 162 181 89.5 %
Date: 2023-03-15 12:04:10 Branches: 392 856 45.8 %

Line Branch Exec Source
1
// Copyright 2010 Thomas Moulard.
2
//
3
4
#include <dynamic-graph/debug.h>
5
#include <dynamic-graph/entity.h>
6
#include <dynamic-graph/factory.h>
7
#include <dynamic-graph/pool.h>
8
#include <dynamic-graph/signal-base.h>
9
#include <dynamic-graph/signal-ptr.h>
10
#include <dynamic-graph/signal-time-dependent.h>
11
#include <dynamic-graph/signal.h>
12
13
#include <boost/foreach.hpp>
14
#include <iostream>
15
#include <string>
16
17
#if BOOST_VERSION >= 105900
18
#include <boost/test/tools/output_test_stream.hpp>
19
#else
20
#include <boost/test/output_test_stream.hpp>
21
#endif
22
#include <boost/test/unit_test.hpp>
23
#include <boost/test/unit_test_suite.hpp>
24
#include <string>
25
26
using boost::test_tools::output_test_stream;
27
28
typedef dynamicgraph::SignalTimeDependent<double, int> sigDouble_t;
29
typedef dynamicgraph::SignalTimeDependent<std::string, int> sigString_t;
30
31
using namespace dynamicgraph;
32
using std::cout;
33
34
template <class T>
35
class DummyClass {
36
 public:
37
  std::string proname;
38
  std::list<sigDouble_t *> inputsig;
39
  std::list<sigString_t *> inputsigV;
40
41
1
  explicit DummyClass(const std::string &n)
42
1
      : proname(n), res(), call(), timedata() {}
43
44
1
  T &fun(T &res, int t) {
45
1
    ++call;
46
1
    timedata = t;
47
48








1
    BOOST_FOREACH (sigDouble_t *ptr, inputsig) ptr->access(timedata);
49
50








1
    BOOST_FOREACH (sigString_t *ptr, inputsigV) ptr->access(timedata);
51
52
1
    res = (*this)();
53
1
    return res;
54
  }
55
56
  void add(sigDouble_t &sig) { inputsig.push_back(&sig); }
57
  void add(sigString_t &sig) { inputsigV.push_back(&sig); }
58
59
  T operator()();
60
61
  T res;
62
  int call;
63
  int timedata;
64
};
65
66
template <>
67
1
double DummyClass<double>::operator()() {
68
1
  res = call * timedata;
69
1
  return res;
70
}
71
template <>
72
std::string DummyClass<std::string>::operator()() {
73
  std::ostringstream oss;
74
  oss << call * timedata;
75
  return oss.str();
76
}
77
78
template <class T>
79
T DummyClass<T>::operator()() {
80
  return this->res;
81
}
82
83
















4
BOOST_AUTO_TEST_CASE(normal_cst_test) {
84

6
  SignalPtr<double, int> sigNotPlug(NULL, "sigNotPlug");
85

6
  const SignalPtr<double, int> cstSigNotPlug(NULL, "sigNotPlug");
86
87
  try {
88
2
    sigNotPlug.getPtr();
89
4
  } catch (ExceptionSignal &e) {
90

2
    cout << "Error catch" << std::endl;
91
  }
92
93
  // Test getPtr without plug
94
  /// This create a ExceptionSignal::NOT_INITIALIZED
95
2
  bool res = false;
96
  try {
97
    //    Signal<double, int> * r =
98
2
    sigNotPlug.getPtr();
99
2
  } catch (const ExceptionSignal &aea) {
100
2
    res = (aea.getCode() == ExceptionSignal::NOT_INITIALIZED);
101
  }
102



2
  BOOST_CHECK(res);
103
104
  /// Testing const getPtr() interface: no plug case
105
  try {
106
2
    cstSigNotPlug.getPtr();
107
2
  } catch (const ExceptionSignal &aea) {
108
2
    res = (aea.getCode() == ExceptionSignal::NOT_INITIALIZED);
109
  }
110



2
  BOOST_CHECK(res);
111
112
  /// Test needUpdate without plug
113
2
  res = (sigNotPlug.needUpdate(5) == false);
114



2
  BOOST_CHECK(res);
115
2
  sigNotPlug.getTime();
116

4
  output_test_stream output;
117
2
  sigNotPlug.display(output);
118
2
  cstSigNotPlug.display(output);
119
120
  /// Testing getAbsatractPtr() interface: no plug
121
2
  res = false;
122
  try {
123
2
    sigNotPlug.getAbstractPtr();
124
2
  } catch (const ExceptionSignal &aea) {
125
2
    res = (aea.getCode() == ExceptionSignal::NOT_INITIALIZED);
126
  }
127



2
  BOOST_CHECK(res);
128
129
  /// Testing const getAbstractPtr() interface: no plug case
130
  try {
131
2
    cstSigNotPlug.getAbstractPtr();
132
2
  } catch (const ExceptionSignal &aea) {
133
2
    res = (aea.getCode() == ExceptionSignal::NOT_INITIALIZED);
134
  }
135



2
  BOOST_CHECK(res);
136
137
  try {
138
2
    sigNotPlug.checkCompatibility();
139
2
  } catch (...) {
140
  }
141



2
  BOOST_CHECK(res);
142
2
}
143
144
















4
BOOST_AUTO_TEST_CASE(normal_test) {
145

6
  Signal<double, int> sig("sig");
146

6
  Signal<int, int> sigint("sig");
147

6
  Signal<std::string, int> sigstr("sig_str");
148


8
  SignalPtr<double, int> sigPtrA(NULL, "sigPtrA"), sigPtrB(NULL, "sigPtrB");
149

6
  SignalPtr<double, int> sigPtrAbstract(NULL, "sigPtrAbstract");
150

6
  DummyClass<double> pro3("pro3");
151
152
2
  sig.setConstant(1.56);
153
2
  sig.recompute(2);
154
4
  std::string name = "sig";
155
2
  sig.getClassName(name);
156
4
  std::string test = "test";
157
  try {
158
2
    sig.getClassName(test);
159
  } catch (ExceptionSignal &e) {
160
    e.getExceptionName();
161
  }
162



2
  BOOST_CHECK(true);
163
164

2
  sigPtrA.setFunction(boost::bind(&DummyClass<double>::fun, &pro3, _1, _2));
165
2
  sigPtrA.recompute(3);
166
167
  /// Plugging signal.
168

6
  SignalBase<int> &sigRef = sig, sigBase("sigBase");
169
2
  SignalBase<int> &sigPtrARef = sigPtrA, &sigPtrBRef = sigPtrB,
170
2
                  &sigPtrAbstractRef = sigPtrAbstract;
171
2
  sigPtrARef.plug(0);
172
2
  sigPtrARef.plug(&sigRef);
173
2
  sigPtrBRef.plug(&sigPtrARef);
174
  /// Try to plug an incompatible signal.
175
  /// leave
176
2
  bool res = false;
177
  try {
178
2
    sigPtrARef.plug(&sigstr);
179
2
  } catch (const ExceptionSignal &aes) {
180
2
    res = (aes.getCode() == ExceptionSignal::PLUG_IMPOSSIBLE);
181
  }
182



2
  BOOST_CHECK(res);
183
184
  /// Plug the signal.
185
2
  sigPtrAbstractRef.plug(&sigRef);
186
2
  sigPtrA.getPtr();
187



2
  BOOST_CHECK(true);
188
  try {
189
2
    sigPtrARef.checkCompatibility();
190
  } catch (const ExceptionSignal &aes) {
191
    /// Should be NOT_INITIALIZED becase the last plug
192
    /// on sigstr failed.
193
    res = (aes.getCode() == ExceptionSignal::NOT_INITIALIZED);
194
  } catch (const std::exception &e) {
195
    std::cout << "Standard Exception:" << e.what() << std::endl;
196
4
  } catch (...) {
197

2
    std::cout << "Anything else: " << std::endl;
198
  }
199
2
  sigPtrA.needUpdate(5);
200



2
  BOOST_CHECK(true);
201
202
2
  int ltime = sigPtrA.getTime();
203
2
  sigPtrA.getPluged();
204
2
  sigPtrA(ltime);
205



2
  BOOST_CHECK(true);
206
207
2
  sigPtrB.getPtr();
208
  /// Test sigPtrAbstract with a normal plug.
209
2
  res = false;
210
  try {
211
2
    sigPtrAbstract.getAbstractPtr();
212
  } catch (ExceptionSignal &aes) {
213
    /// Should be NOT_INITIALIZED becase the last plug
214
    /// on sigstr failed.
215
    std::cout << "Code: " << aes.getCode() << std::endl;
216
    res = (aes.getCode() == ExceptionSignal::NOT_INITIALIZED);
217
  } catch (...) {
218
    std::cout << "Anything else with sigPtrAbstract.getAbstractPtr()"
219
              << std::endl;
220
  }
221



2
  BOOST_CHECK(true);
222
223
  /// Test the case where the plug ref is zero.
224
2
  sigPtrAbstractRef.plug(0);
225



2
  BOOST_CHECK(true);
226
227

2
  assert(sigRef.isPlugged() != true);
228
2
  SignalBase<int> *t = sigRef.getPluged();
229
  // assert(sigPtrA.get()=false);
230
231
  // TODO Can't check if the constant change
232
2
  sigPtrA.setConstantDefault(1.2);
233
  // getconstant
234
2
  sigPtrA.setConstantDefault();
235
  // getconstant
236
2
  sigPtrA.setConstant(3.4);
237
  // getconstant
238
  double tab_D[2];
239
2
  tab_D[0] = 1.2;
240
2
  tab_D[1] = 3.4;
241
2
  sigPtrA.setReference(tab_D, NULL);
242
2
  sigPtrA.access(5);
243

4
  output_test_stream output;
244
2
  sigPtrA.display(output);
245
2
  sigPtrA.setReferenceNonConstant(tab_D, NULL);
246
2
  sigPtrA.access(5);
247
2
  sigPtrA.display(output);
248
249
  // getreference
250
2
  sigPtrA.operator=(1.2);
251
  // getconstant
252


2
  sigPtrA.displayDependencies(output);
253
254

2
  cout << t << std::endl;
255
2
  cout << "Sig = ";
256
2
  sigRef.get(cout);
257
2
  cout << std::endl;
258
2
  cout << "SigPtrA = ";
259
2
  sigPtrARef.get(cout);
260
2
  cout << std::endl;
261
2
  cout << "SigPtrB = ";
262
2
  sigPtrBRef.get(cout);
263
2
  cout << std::endl;
264
265
2
  sigPtrA.unplug();
266
2
}
267
268
















4
BOOST_AUTO_TEST_CASE(plug_signal_string) {
269

6
  Signal<std::string, int> outSig("output");
270

6
  SignalPtr<std::string, int> inSig(NULL, "input");
271
272

6
  Signal<dynamicgraph::Vector, int> outSigVec("outputVec");
273

6
  SignalPtr<dynamicgraph::Vector, int> inSigVec(NULL, "inputVec");
274
275
4
  std::string str("two words");
276
2
  outSig.setConstant(str);
277
2
  inSig.plug(&outSig);
278
2
  inSig.recompute(1);
279
4
  std::ostringstream os1;
280
2
  inSig.get(os1);
281
4
  std::string res(os1.str());
282



2
  BOOST_CHECK(res == str);
283
284
4
  dynamicgraph::Vector aVec;
285
2
  aVec.resize(5);
286
2
  aVec(0) = 1.0;
287
2
  aVec(1) = 2.0;
288
2
  aVec(2) = 3.0;
289
2
  aVec(3) = 4.0;
290
2
  aVec(4) = 5.0;
291
2
  outSigVec.setConstant(aVec);
292
2
  inSigVec.plug(&outSigVec);
293
2
  inSigVec.recompute(1);
294

4
  output_test_stream output;
295
2
  inSigVec.get(output);
296



2
  BOOST_CHECK(output.is_equal("1 2 3 4 5"));
297
298

6
  Signal<std::string, int> s("signal");
299
4
  std::ostringstream os2;
300
2
  s.setConstant(str);
301
2
  os2.clear();
302
2
  s.get(os2);
303
2
  res = os2.str();
304

2
  std::cout << "res=" << res << std::endl;
305



2
  BOOST_CHECK(res == str);
306
2
}
307
308
















4
BOOST_AUTO_TEST_CASE(set_signal_string) {
309

6
  Signal<std::string, int> s("signal");
310
4
  std::string str("");
311
4
  std::ostringstream os;
312
2
  os << str;
313

4
  std::istringstream value(os.str());
314
  try {
315
2
    s.set(value);
316
  } catch (const std::exception &exc) {
317
    std::cout << exc.what() << std::endl;
318
    BOOST_CHECK(!(bool)("Tentative to set signal to empty string"));
319
  }
320
2
}