signal-ptr.cpp
Go to the documentation of this file.
1 // Copyright 2010 Thomas Moulard.
2 //
3 
4 #include <dynamic-graph/debug.h>
5 #include <dynamic-graph/entity.h>
7 #include <dynamic-graph/pool.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 
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  explicit DummyClass(const std::string &n)
42  : proname(n), res(), call(), timedata() {}
43 
44  T &fun(T &res, int t) {
45  ++call;
46  timedata = t;
47 
48  BOOST_FOREACH (sigDouble_t *ptr, inputsig) ptr->access(timedata);
49 
50  BOOST_FOREACH (sigString_t *ptr, inputsigV) ptr->access(timedata);
51 
52  res = (*this)();
53  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 <>
68  res = call * timedata;
69  return res;
70 }
71 template <>
73  std::ostringstream oss;
74  oss << call * timedata;
75  return oss.str();
76 }
77 
78 template <class T>
80  return this->res;
81 }
82 
83 BOOST_AUTO_TEST_CASE(normal_cst_test) {
84  SignalPtr<double, int> sigNotPlug(NULL, "sigNotPlug");
85  const SignalPtr<double, int> cstSigNotPlug(NULL, "sigNotPlug");
86 
87  try {
88  sigNotPlug.getPtr();
89  } catch (ExceptionSignal &e) {
90  cout << "Error catch" << std::endl;
91  }
92 
93  // Test getPtr without plug
95  bool res = false;
96  try {
97  // Signal<double, int> * r =
98  sigNotPlug.getPtr();
99  } catch (const ExceptionSignal &aea) {
100  res = (aea.getCode() == ExceptionSignal::NOT_INITIALIZED);
101  }
102  BOOST_CHECK(res);
103 
105  try {
106  cstSigNotPlug.getPtr();
107  } catch (const ExceptionSignal &aea) {
108  res = (aea.getCode() == ExceptionSignal::NOT_INITIALIZED);
109  }
110  BOOST_CHECK(res);
111 
113  res = (sigNotPlug.needUpdate(5) == false);
114  BOOST_CHECK(res);
115  sigNotPlug.getTime();
116  output_test_stream output;
117  sigNotPlug.display(output);
118  cstSigNotPlug.display(output);
119 
121  res = false;
122  try {
123  sigNotPlug.getAbstractPtr();
124  } catch (const ExceptionSignal &aea) {
125  res = (aea.getCode() == ExceptionSignal::NOT_INITIALIZED);
126  }
127  BOOST_CHECK(res);
128 
130  try {
131  cstSigNotPlug.getAbstractPtr();
132  } catch (const ExceptionSignal &aea) {
133  res = (aea.getCode() == ExceptionSignal::NOT_INITIALIZED);
134  }
135  BOOST_CHECK(res);
136 
137  try {
138  sigNotPlug.checkCompatibility();
139  } catch (...) {
140  }
141  BOOST_CHECK(res);
142 }
143 
144 BOOST_AUTO_TEST_CASE(normal_test) {
145  Signal<double, int> sig("sig");
146  Signal<int, int> sigint("sig");
147  Signal<std::string, int> sigstr("sig_str");
148  SignalPtr<double, int> sigPtrA(NULL, "sigPtrA"), sigPtrB(NULL, "sigPtrB");
149  SignalPtr<double, int> sigPtrAbstract(NULL, "sigPtrAbstract");
150  DummyClass<double> pro3("pro3");
151 
152  sig.setConstant(1.56);
153  sig.recompute(2);
154  std::string name = "sig";
155  sig.getClassName(name);
156  std::string test = "test";
157  try {
158  sig.getClassName(test);
159  } catch (ExceptionSignal &e) {
160  e.getExceptionName();
161  }
162  BOOST_CHECK(true);
163 
164  sigPtrA.setFunction(boost::bind(&DummyClass<double>::fun, &pro3, _1, _2));
165  sigPtrA.recompute(3);
166 
168  SignalBase<int> &sigRef = sig, sigBase("sigBase");
169  SignalBase<int> &sigPtrARef = sigPtrA, &sigPtrBRef = sigPtrB,
170  &sigPtrAbstractRef = sigPtrAbstract;
171  sigPtrARef.plug(0);
172  sigPtrARef.plug(&sigRef);
173  sigPtrBRef.plug(&sigPtrARef);
176  bool res = false;
177  try {
178  sigPtrARef.plug(&sigstr);
179  } catch (const ExceptionSignal &aes) {
180  res = (aes.getCode() == ExceptionSignal::PLUG_IMPOSSIBLE);
181  }
182  BOOST_CHECK(res);
183 
185  sigPtrAbstractRef.plug(&sigRef);
186  sigPtrA.getPtr();
187  BOOST_CHECK(true);
188  try {
189  sigPtrARef.checkCompatibility();
190  } catch (const ExceptionSignal &aes) {
193  res = (aes.getCode() == ExceptionSignal::NOT_INITIALIZED);
194  } catch (const std::exception &e) {
195  std::cout << "Standard Exception:" << e.what() << std::endl;
196  } catch (...) {
197  std::cout << "Anything else: " << std::endl;
198  }
199  sigPtrA.needUpdate(5);
200  BOOST_CHECK(true);
201 
202  int ltime = sigPtrA.getTime();
203  sigPtrA.getPluged();
204  sigPtrA(ltime);
205  BOOST_CHECK(true);
206 
207  sigPtrB.getPtr();
209  res = false;
210  try {
211  sigPtrAbstract.getAbstractPtr();
212  } catch (ExceptionSignal &aes) {
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  BOOST_CHECK(true);
222 
224  sigPtrAbstractRef.plug(0);
225  BOOST_CHECK(true);
226 
227  assert(sigRef.isPlugged() != true);
228  SignalBase<int> *t = sigRef.getPluged();
229  // assert(sigPtrA.get()=false);
230 
231  // TODO Can't check if the constant change
232  sigPtrA.setConstantDefault(1.2);
233  // getconstant
234  sigPtrA.setConstantDefault();
235  // getconstant
236  sigPtrA.setConstant(3.4);
237  // getconstant
238  double tab_D[2];
239  tab_D[0] = 1.2;
240  tab_D[1] = 3.4;
241  sigPtrA.setReference(tab_D, NULL);
242  sigPtrA.access(5);
243  output_test_stream output;
244  sigPtrA.display(output);
245  sigPtrA.setReferenceNonConstant(tab_D, NULL);
246  sigPtrA.access(5);
247  sigPtrA.display(output);
248 
249  // getreference
250  sigPtrA.operator=(1.2);
251  // getconstant
252  sigPtrA.displayDependencies(output);
253 
254  cout << t << std::endl;
255  cout << "Sig = ";
256  sigRef.get(cout);
257  cout << std::endl;
258  cout << "SigPtrA = ";
259  sigPtrARef.get(cout);
260  cout << std::endl;
261  cout << "SigPtrB = ";
262  sigPtrBRef.get(cout);
263  cout << std::endl;
264 
265  sigPtrA.unplug();
266 }
267 
268 BOOST_AUTO_TEST_CASE(plug_signal_string) {
269  Signal<std::string, int> outSig("output");
270  SignalPtr<std::string, int> inSig(NULL, "input");
271 
272  Signal<dynamicgraph::Vector, int> outSigVec("outputVec");
273  SignalPtr<dynamicgraph::Vector, int> inSigVec(NULL, "inputVec");
274 
275  std::string str("two words");
276  outSig.setConstant(str);
277  inSig.plug(&outSig);
278  inSig.recompute(1);
279  std::ostringstream os1;
280  inSig.get(os1);
281  std::string res(os1.str());
282  BOOST_CHECK(res == str);
283 
285  aVec.resize(5);
286  aVec(0) = 1.0;
287  aVec(1) = 2.0;
288  aVec(2) = 3.0;
289  aVec(3) = 4.0;
290  aVec(4) = 5.0;
291  outSigVec.setConstant(aVec);
292  inSigVec.plug(&outSigVec);
293  inSigVec.recompute(1);
294  output_test_stream output;
295  inSigVec.get(output);
296  BOOST_CHECK(output.is_equal("1 2 3 4 5"));
297 
298  Signal<std::string, int> s("signal");
299  std::ostringstream os2;
300  s.setConstant(str);
301  os2.clear();
302  s.get(os2);
303  res = os2.str();
304  std::cout << "res=" << res << std::endl;
305  BOOST_CHECK(res == str);
306 }
307 
308 BOOST_AUTO_TEST_CASE(set_signal_string) {
309  Signal<std::string, int> s("signal");
310  std::string str("");
311  std::ostringstream os;
312  os << str;
313  std::istringstream value(os.str());
314  try {
315  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 }
virtual void getClassName(std::string &aClassName) const
Eigen::VectorXd Vector
dynamicgraph::SignalTimeDependent< double, int > sigDouble_t
Definition: signal-ptr.cpp:28
virtual void setConstantDefault()
Definition: signal-base.h:106
dynamicgraph::SignalTimeDependent< std::string, int > sigString_t
Definition: signal-ptr.cpp:29
virtual void setConstant(const T &t)
Definition: signal.t.cpp:98
T & fun(T &res, int t)
Definition: signal-ptr.cpp:44
dynamicgraph::SignalArray_const< double > sig
Definition: signal-all.cpp:25
virtual void set(std::istringstream &value)
Definition: signal.t.cpp:32
Exceptions raised when an error related to signals happen.
std::string proname
Definition: signal-ptr.cpp:37
virtual void get(std::ostream &) const
Definition: signal-base.h:128
BOOST_AUTO_TEST_CASE(normal_cst_test)
Definition: signal-ptr.cpp:83
std::list< sigDouble_t * > inputsig
Definition: signal-ptr.cpp:38
virtual void plug(SignalBase< Time > *ref)
int getCode() const
Access to the error code.
DummyClass(const std::string &n)
Definition: signal-ptr.cpp:41
std::list< sigString_t * > inputsigV
Definition: signal-ptr.cpp:39
virtual void checkCompatibility()
Definition: signal-base.h:191
virtual void checkCompatibility()
virtual const Time & getTime() const
virtual SignalBase< Time > * getPluged() const
Definition: signal-base.h:104
T operator()()
Definition: signal-ptr.cpp:79
SignalBase< Time > * getAbstractPtr()
virtual bool isPlugged() const
Definition: signal-base.h:102
virtual void recompute(const Time &t)
virtual void plug(SignalBase< Time > *sigarg)
Definition: signal-base.h:87
void add(sigString_t &sig)
Definition: signal-ptr.cpp:57
void add(sigDouble_t &sig)
Definition: signal-ptr.cpp:56
virtual std::ostream & display(std::ostream &os) const
virtual void get(std::ostream &value) const
Definition: signal.t.cpp:37
virtual bool needUpdate(const Time &t) const
virtual const std::string & getExceptionName() const
Signal< T, Time > * getPtr()


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