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 }
120 
121 BOOST_AUTO_TEST_CASE(normal_test) {
122  Signal<double, int> sig("sig");
123  Signal<int, int> sigint("sig");
124  Signal<std::string, int> sigstr("sig_str");
125  SignalPtr<double, int> sigPtrA(NULL, "sigPtrA"), sigPtrB(NULL, "sigPtrB");
126  SignalPtr<double, int> sigPtrAbstract(NULL, "sigPtrAbstract");
127  DummyClass<double> pro3("pro3");
128 
129  sig.setConstant(1.56);
130  sig.recompute(2);
131  std::string name = "sig";
132  sig.getClassName(name);
133  std::string test = "test";
134  try {
135  sig.getClassName(test);
136  } catch (ExceptionSignal &e) {
137  e.getExceptionName();
138  }
139  BOOST_CHECK(true);
140 
141  sigPtrA.setFunction(boost::bind(&DummyClass<double>::fun, &pro3, _1, _2));
142  sigPtrA.recompute(3);
143 
145  SignalBase<int> &sigRef = sig, sigBase("sigBase");
146  SignalBase<int> &sigPtrARef = sigPtrA, &sigPtrBRef = sigPtrB,
147  &sigPtrAbstractRef = sigPtrAbstract;
148  sigPtrARef.plug(0);
149  sigPtrARef.plug(&sigRef);
150  sigPtrBRef.plug(&sigPtrARef);
152  sigPtrAbstractRef.plug(&sigRef);
153  sigPtrA.getPtr();
154  BOOST_CHECK(true);
155 
156  int ltime = sigPtrA.getTime();
157  sigPtrA.getPluged();
158  sigPtrA(ltime);
159  BOOST_CHECK(true);
160 
161  sigPtrB.getPtr();
162  // TODO Can't check if the constant change
163  sigPtrA.setConstantDefault(1.2);
164  // getconstant
165  sigPtrA.setConstantDefault();
166  // getconstant
167  sigPtrA.setConstant(3.4);
168  // getconstant
169  double tab_D[2];
170  tab_D[0] = 1.2;
171  tab_D[1] = 3.4;
172  sigPtrA.setReference(tab_D, NULL);
173  sigPtrA.access(5);
174  output_test_stream output;
175  sigPtrA.display(output);
176  sigPtrA.setReferenceNonConstant(tab_D, NULL);
177  sigPtrA.access(5);
178  sigPtrA.display(output);
179 
180  // getreference
181  sigPtrA.operator=(1.2);
182  // getconstant
183  sigPtrA.displayDependencies(output);
184 
185  cout << "Sig = ";
186  sigRef.get(cout);
187  cout << std::endl;
188  cout << "SigPtrA = ";
189  sigPtrARef.get(cout);
190  cout << std::endl;
191  cout << "SigPtrB = ";
192  sigPtrBRef.get(cout);
193  cout << std::endl;
194 
195  sigPtrA.unplug();
196 }
197 
198 BOOST_AUTO_TEST_CASE(plug_signal_string) {
199  Signal<std::string, int> outSig("output");
200  SignalPtr<std::string, int> inSig(NULL, "input");
201 
202  Signal<dynamicgraph::Vector, int> outSigVec("outputVec");
203  SignalPtr<dynamicgraph::Vector, int> inSigVec(NULL, "inputVec");
204 
205  std::string str("two words");
206  outSig.setConstant(str);
207  inSig.plug(&outSig);
208  inSig.recompute(1);
209  std::ostringstream os1;
210  inSig.get(os1);
211  std::string res(os1.str());
212  BOOST_CHECK(res == str);
213 
215  aVec.resize(5);
216  aVec(0) = 1.0;
217  aVec(1) = 2.0;
218  aVec(2) = 3.0;
219  aVec(3) = 4.0;
220  aVec(4) = 5.0;
221  outSigVec.setConstant(aVec);
222  inSigVec.plug(&outSigVec);
223  inSigVec.recompute(1);
224  output_test_stream output;
225  inSigVec.get(output);
226  BOOST_CHECK(output.is_equal("1 2 3 4 5"));
227 
228  Signal<std::string, int> s("signal");
229  std::ostringstream os2;
230  s.setConstant(str);
231  os2.clear();
232  s.get(os2);
233  res = os2.str();
234  std::cout << "res=" << res << std::endl;
235  BOOST_CHECK(res == str);
236 }
237 
238 BOOST_AUTO_TEST_CASE(set_signal_string) {
239  Signal<std::string, int> s("signal");
240  std::string str("");
241  std::ostringstream os;
242  os << str;
243  std::istringstream value(os.str());
244  try {
245  s.set(value);
246  } catch (const std::exception &exc) {
247  std::cout << exc.what() << std::endl;
248  BOOST_CHECK(!(bool)("Tentative to set signal to empty string"));
249  }
250 }
DummyClass
Definition: signal-ptr.cpp:35
entity.h
dynamicgraph::Signal
Signals link I/O ports of entities. They can be constant-valued signals, or copy the value of a heap ...
Definition: fwd.hh:44
signal-time-dependent.h
dynamicgraph::SignalPtr::getPtr
Signal< T, Time > * getPtr()
Definition: signal-ptr.t.cpp:16
DummyClass::proname
std::string proname
Definition: signal-ptr.cpp:37
DummyClass::DummyClass
DummyClass(const std::string &n)
Definition: signal-ptr.cpp:41
DummyClass::res
T res
Definition: signal-ptr.cpp:61
dynamicgraph::SignalTimeDependent::access
const T & access(const Time &t1)
Definition: signal-time-dependent.h:119
dynamicgraph::Signal::get
virtual void get(std::ostream &value) const
Definition: signal.t.cpp:37
dynamicgraph::SignalPtr
This is the only type of signal that can be plugged to, using the plug () command.
Definition: fwd.hh:56
dynamicgraph
sigString_t
dynamicgraph::SignalTimeDependent< std::string, int > sigString_t
Definition: signal-ptr.cpp:29
dynamicgraph::SignalPtr::plug
virtual void plug(SignalBase< Time > *ref)
Definition: signal-ptr.t.cpp:41
DummyClass::add
void add(sigDouble_t &sig)
Definition: signal-ptr.cpp:56
DummyClass::call
int call
Definition: signal-ptr.cpp:62
DummyClass::add
void add(sigString_t &sig)
Definition: signal-ptr.cpp:57
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(normal_cst_test)
Definition: signal-ptr.cpp:83
signal-ptr.h
dynamicgraph::ExceptionSignal::NOT_INITIALIZED
@ NOT_INITIALIZED
Definition: exception-signal.h:25
dynamicgraph::SignalPtr::needUpdate
virtual bool needUpdate(const Time &t) const
Definition: signal-ptr.t.cpp:61
dynamicgraph::SignalPtr::getTime
virtual const Time & getTime() const
Definition: signal-ptr.t.cpp:69
signal-base.h
dynamicgraph::SignalBase::get
virtual void get(std::ostream &) const
Definition: signal-base.h:128
dynamicgraph::SignalPtr::display
virtual std::ostream & display(std::ostream &os) const
Definition: signal-ptr.t.cpp:122
DummyClass::timedata
int timedata
Definition: signal-ptr.cpp:63
dynamicgraph::ExceptionSignal::getExceptionName
virtual const std::string & getExceptionName() const
Definition: exception-signal.h:39
sig
dynamicgraph::SignalArray_const< double > sig
Definition: signal-all.cpp:25
dynamicgraph::SignalBase::plug
virtual void plug(SignalBase< Time > *sigarg)
Definition: signal-base.h:87
dynamicgraph::ExceptionAbstract::getCode
int getCode() const
Access to the error code.
Definition: exception-abstract.cpp:24
dynamicgraph::Vector
Eigen::VectorXd Vector
Definition: linear-algebra.h:14
DummyClass::fun
T & fun(T &res, int t)
Definition: signal-ptr.cpp:44
dynamicgraph::Signal::setConstant
virtual void setConstant(const T &t)
Definition: signal.t.cpp:98
factory.h
dynamicgraph::Signal::set
virtual void set(std::istringstream &value)
Definition: signal.t.cpp:32
dynamicgraph::ExceptionSignal
Exceptions raised when an error related to signals happen.
Definition: exception-signal.h:19
dynamicgraph::SignalTimeDependent
A type of signal that enforces a time dependency between other signals, making sure its inputs are up...
Definition: fwd.hh:58
DummyClass::inputsig
std::list< sigDouble_t * > inputsig
Definition: signal-ptr.cpp:38
dynamicgraph::SignalBase
The base class for signals: not to be used as such.
Definition: fwd.hh:53
sigDouble_t
dynamicgraph::SignalTimeDependent< double, int > sigDouble_t
Definition: signal-ptr.cpp:28
DummyClass::operator()
T operator()()
Definition: signal-ptr.cpp:79
signal.h
debug.h
DummyClass::inputsigV
std::list< sigString_t * > inputsigV
Definition: signal-ptr.cpp:39
compile.name
name
Definition: compile.py:23
pool.h


dynamic-graph
Author(s): Nicolas Mansard, Olivier Stasse
autogenerated on Fri Aug 2 2024 08:36:38