signal-time-dependent.cpp
Go to the documentation of this file.
1 // Copyright 2010 Thomas Moulard.
2 //
4 #include <dynamic-graph/signal.h>
5 
6 #include <boost/foreach.hpp>
7 #include <iostream>
8 
9 #define BOOST_TEST_MODULE signal_time_dependent
10 
11 #if BOOST_VERSION >= 105900
12 #include <boost/test/tools/output_test_stream.hpp>
13 #else
14 #include <boost/test/output_test_stream.hpp>
15 #endif
16 #include <boost/test/unit_test.hpp>
17 
18 using boost::test_tools::output_test_stream;
19 
22 
23 template <class T>
24 class DummyClass {
25  public:
26  std::string proname;
27  std::list<sigDouble_t *> inputsig;
28  std::list<sigString_t *> inputsigV;
29 
30  explicit DummyClass(const std::string &n)
31  : proname(n), res(), call(), timedata() {}
32 
33  T &fun(T &res, int t) {
34  ++call;
35  timedata = t;
36 
37  BOOST_FOREACH (sigDouble_t *ptr, inputsig) ptr->access(timedata);
38 
39  BOOST_FOREACH (sigString_t *ptr, inputsigV) ptr->access(timedata);
40 
41  res = (*this)();
42  return res;
43  }
44 
45  void add(sigDouble_t &sig) { inputsig.push_back(&sig); }
46  void add(sigString_t &sig) { inputsigV.push_back(&sig); }
47 
48  T operator()();
49 
50  T res;
51  int call;
52  int timedata;
53 };
54 
55 template <>
57  res = call * timedata;
58  return res;
59 }
60 template <>
62  std::ostringstream oss;
63  oss << call * timedata;
64  return oss.str();
65 }
66 
67 template <class T>
69  return this->res;
70 }
71 
72 BOOST_AUTO_TEST_CASE(signaltimedependent) {
73  DummyClass<double> pro1("pro1"), pro3("pro3"), pro5("pro5");
74  DummyClass<std::string> pro2("pro2"), pro4("pro4"), pro6("pro6");
75 
76  sigDouble_t sig5("Sig5");
77  sigString_t sig6("Sig6");
78 
79  sigString_t sig4(sig5, "Sig4");
80  sigString_t sig2(sig4 << sig4 << sig4 << sig6, "Sig2");
81  sigDouble_t sig3(sig2 << sig5 << sig6, "Sig3");
82  sigDouble_t sig1(boost::bind(&DummyClass<double>::fun, &pro1, _1, _2),
83  sig2 << sig3, "Sig1");
84  sigDouble_t sig7("Sig7");
85 
86  sig2.setFunction(boost::bind(&DummyClass<std::string>::fun, &pro2, _1, _2));
87  sig3.setFunction(boost::bind(&DummyClass<double>::fun, &pro3, _1, _2));
88  sig4.setFunction(boost::bind(&DummyClass<std::string>::fun, &pro4, _1, _2));
89  sig5.setFunction(boost::bind(&DummyClass<double>::fun, &pro5, _1, _2));
90  sig6.setFunction(boost::bind(&DummyClass<std::string>::fun, &pro6, _1, _2));
91 
92  pro1.add(sig2);
93  pro1.add(sig3);
94  pro2.add(sig4);
95  pro2.add(sig4);
96  pro2.add(sig4);
97  pro3.add(sig2);
98  pro4.add(sig5);
99  pro2.add(sig6);
100  pro3.add(sig5);
101  pro3.add(sig6);
102 
105 
106  sig5.setPeriodTime(3);
107  assert(sig5.getPeriodTime() == 3);
108 
109  sig6.setReady();
110 
111  {
112  output_test_stream output;
113  sig1.displayDependencies(output);
114  BOOST_CHECK(
115  output.is_equal("-- Sig:Sig1 (Type Fun) (t=0 (/1) )\n"
116  " |-- Sig:Sig3 (Type Fun) (t=0 (/1) )\n"
117  " | |-- Sig:Sig6 (Type Fun) (ready=TRUE)\n"
118  " | |-- Sig:Sig5 (Type Fun) (A)\n"
119  " | `-- Sig:Sig2 (Type Fun) (t=0 (/1) )\n"
120  " | |-- Sig:Sig6 (Type Fun) (ready=TRUE)\n"
121  " | |-- Sig:Sig4 (Type Fun) (t=0 (/1) )\n"
122  " | | `-- Sig:Sig5 (Type Fun) (A)\n"
123  " | |-- Sig:Sig4 (Type Fun) (t=0 (/1) )\n"
124  " | | `-- Sig:Sig5 (Type Fun) (A)\n"
125  " | `-- Sig:Sig4 (Type Fun) (t=0 (/1) )\n"
126  " | `-- Sig:Sig5 (Type Fun) (A)\n"
127  " `-- Sig:Sig2 (Type Fun) (t=0 (/1) )\n"
128  " |-- Sig:Sig6 (Type Fun) (ready=TRUE)\n"
129  " |-- Sig:Sig4 (Type Fun) (t=0 (/1) )\n"
130  " | `-- Sig:Sig5 (Type Fun) (A)\n"
131  " |-- Sig:Sig4 (Type Fun) (t=0 (/1) )\n"
132  " | `-- Sig:Sig5 (Type Fun) (A)\n"
133  " `-- Sig:Sig4 (Type Fun) (t=0 (/1) )\n"
134  " `-- Sig:Sig5 (Type Fun) (A)"));
135  }
136 
137  BOOST_CHECK(sig1.needUpdate(2));
138  sig1.access(2);
139 
140  {
141  output_test_stream output;
142  sig1.displayDependencies(output);
143  BOOST_CHECK(
144  output.is_equal("-- Sig:Sig1 (Type Fun) (t=2 (/1) )\n"
145  " |-- Sig:Sig3 (Type Fun) (t=2 (/1) )\n"
146  " | |-- Sig:Sig6 (Type Fun) (ready=FALSE)\n"
147  " | |-- Sig:Sig5 (Type Fun) (A)\n"
148  " | `-- Sig:Sig2 (Type Fun) (t=2 (/1) )\n"
149  " | |-- Sig:Sig6 (Type Fun) (ready=FALSE)\n"
150  " | |-- Sig:Sig4 (Type Fun) (t=2 (/1) )\n"
151  " | | `-- Sig:Sig5 (Type Fun) (A)\n"
152  " | |-- Sig:Sig4 (Type Fun) (t=2 (/1) )\n"
153  " | | `-- Sig:Sig5 (Type Fun) (A)\n"
154  " | `-- Sig:Sig4 (Type Fun) (t=2 (/1) )\n"
155  " | `-- Sig:Sig5 (Type Fun) (A)\n"
156  " `-- Sig:Sig2 (Type Fun) (t=2 (/1) )\n"
157  " |-- Sig:Sig6 (Type Fun) (ready=FALSE)\n"
158  " |-- Sig:Sig4 (Type Fun) (t=2 (/1) )\n"
159  " | `-- Sig:Sig5 (Type Fun) (A)\n"
160  " |-- Sig:Sig4 (Type Fun) (t=2 (/1) )\n"
161  " | `-- Sig:Sig5 (Type Fun) (A)\n"
162  " `-- Sig:Sig4 (Type Fun) (t=2 (/1) )\n"
163  " `-- Sig:Sig5 (Type Fun) (A)"));
164  }
165  sig2.access(4);
166 
167  {
168  output_test_stream output;
169  sig1.displayDependencies(output);
170  BOOST_CHECK(
171  output.is_equal("-- Sig:Sig1 (Type Fun) (t=2 (/1) )\n"
172  " |-- Sig:Sig3 (Type Fun) (t=2 (/1) )\n"
173  " | |-- Sig:Sig6 (Type Fun) (ready=FALSE)\n"
174  " | |-- Sig:Sig5 (Type Fun) (A)\n"
175  " | `-- Sig:Sig2 (Type Fun) (t=4 (/1) )\n"
176  " | |-- Sig:Sig6 (Type Fun) (ready=FALSE)\n"
177  " | |-- Sig:Sig4 (Type Fun) (t=4 (/1) )\n"
178  " | | `-- Sig:Sig5 (Type Fun) (A)\n"
179  " | |-- Sig:Sig4 (Type Fun) (t=4 (/1) )\n"
180  " | | `-- Sig:Sig5 (Type Fun) (A)\n"
181  " | `-- Sig:Sig4 (Type Fun) (t=4 (/1) )\n"
182  " | `-- Sig:Sig5 (Type Fun) (A)\n"
183  " `-- Sig:Sig2 (Type Fun) (t=4 (/1) )\n"
184  " |-- Sig:Sig6 (Type Fun) (ready=FALSE)\n"
185  " |-- Sig:Sig4 (Type Fun) (t=4 (/1) )\n"
186  " | `-- Sig:Sig5 (Type Fun) (A)\n"
187  " |-- Sig:Sig4 (Type Fun) (t=4 (/1) )\n"
188  " | `-- Sig:Sig5 (Type Fun) (A)\n"
189  " `-- Sig:Sig4 (Type Fun) (t=4 (/1) )\n"
190  " `-- Sig:Sig5 (Type Fun) (A)"));
191  }
192  sig1.access(4);
193 
194  {
195  output_test_stream output;
196  sig1.displayDependencies(output);
197  BOOST_CHECK(
198  output.is_equal("-- Sig:Sig1 (Type Fun) (t=4 (/1) )\n"
199  " |-- Sig:Sig3 (Type Fun) (t=4 (/1) )\n"
200  " | |-- Sig:Sig6 (Type Fun) (ready=FALSE)\n"
201  " | |-- Sig:Sig5 (Type Fun) (A)\n"
202  " | `-- Sig:Sig2 (Type Fun) (t=4 (/1) )\n"
203  " | |-- Sig:Sig6 (Type Fun) (ready=FALSE)\n"
204  " | |-- Sig:Sig4 (Type Fun) (t=4 (/1) )\n"
205  " | | `-- Sig:Sig5 (Type Fun) (A)\n"
206  " | |-- Sig:Sig4 (Type Fun) (t=4 (/1) )\n"
207  " | | `-- Sig:Sig5 (Type Fun) (A)\n"
208  " | `-- Sig:Sig4 (Type Fun) (t=4 (/1) )\n"
209  " | `-- Sig:Sig5 (Type Fun) (A)\n"
210  " `-- Sig:Sig2 (Type Fun) (t=4 (/1) )\n"
211  " |-- Sig:Sig6 (Type Fun) (ready=FALSE)\n"
212  " |-- Sig:Sig4 (Type Fun) (t=4 (/1) )\n"
213  " | `-- Sig:Sig5 (Type Fun) (A)\n"
214  " |-- Sig:Sig4 (Type Fun) (t=4 (/1) )\n"
215  " | `-- Sig:Sig5 (Type Fun) (A)\n"
216  " `-- Sig:Sig4 (Type Fun) (t=4 (/1) )\n"
217  " `-- Sig:Sig5 (Type Fun) (A)"));
218  }
219 
220  sig1.needUpdate(6);
221  sig1.needUpdate(6);
222  output_test_stream output;
223  sig1.writeGraph(output);
224  BOOST_CHECK(output.is_equal(""));
225 
226  sig1.removeDependency(sig3);
227  BOOST_CHECK(true);
228  const double &avalue = sig1(6);
229  output << avalue;
230  BOOST_CHECK(true);
232  try {
233  sig1.checkCompatibility();
234  }
235  // catch(double e)
236  catch (...) {
237  std::cout << "Message: test \n";
238  }
239  BOOST_CHECK(true);
240 }
std::ostream & writeGraph(std::ostream &os) const
A helper class for setting and specifying dependencies between signals.
Definition: fwd.hh:58
T & fun(T &res, int t)
void setDependencyType(DependencyType dep)
dynamicgraph::SignalArray_const< double > sig
Definition: signal-all.cpp:25
std::string proname
Definition: signal-ptr.cpp:37
std::list< sigDouble_t * > inputsig
Definition: signal-ptr.cpp:38
DummyClass(const std::string &n)
std::list< sigString_t * > inputsigV
Definition: signal-ptr.cpp:39
virtual void removeDependency(const SignalBase< Time > &signal)
T operator()()
Definition: signal-ptr.cpp:79
dynamicgraph::SignalTimeDependent< double, int > sigDouble_t
void add(sigString_t &sig)
void add(sigDouble_t &sig)
dynamicgraph::SignalTimeDependent< std::string, int > sigString_t
BOOST_AUTO_TEST_CASE(signaltimedependent)
void setReady(const bool sready=true)
Definition: signal-base.h:65
virtual bool needUpdate(const Time &t) const
virtual void setFunction(boost::function2< T &, T &, Time > t, Mutex *mutexref=NULL)
Definition: signal.t.cpp:124
std::ostream & displayDependencies(std::ostream &os, const int depth=-1, std::string space="", std::string next1="", std::string next2="") const
virtual void setPeriodTime(const Time &p)


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