signal-time-dependent.cpp 9.2 KB
Newer Older
1
2
3
// Copyright 2010 Thomas Moulard.
//
#include <boost/foreach.hpp>
4
#include <iostream>
5
6

#include <dynamic-graph/signal-time-dependent.h>
7
#include <dynamic-graph/signal.h>
8
9
10

#define BOOST_TEST_MODULE signal_time_dependent

Guilhem Saurel's avatar
Guilhem Saurel committed
11
12
13
#if BOOST_VERSION >= 105900
#include <boost/test/tools/output_test_stream.hpp>
#else
14
#include <boost/test/output_test_stream.hpp>
Guilhem Saurel's avatar
Guilhem Saurel committed
15
#endif
16
#include <boost/test/unit_test.hpp>
17
18
19
20
21
22

using boost::test_tools::output_test_stream;

typedef dynamicgraph::SignalTimeDependent<double, int> sigDouble_t;
typedef dynamicgraph::SignalTimeDependent<std::string, int> sigString_t;

23
24
template <class T> class DummyClass {
public:
25
  std::string proname;
26
27
  std::list<sigDouble_t *> inputsig;
  std::list<sigString_t *> inputsigV;
28

Olivier Stasse's avatar
Olivier Stasse committed
29
30
  explicit DummyClass(const std::string &n)
      : proname(n), res(), call(), timedata() {}
31

32
  T &fun(T &res, int t) {
33
    ++call;
Bergé's avatar
Bergé committed
34
    timedata = t;
35

36
    BOOST_FOREACH (sigDouble_t *ptr, inputsig)
37
38
      ptr->access(timedata);

39
    BOOST_FOREACH (sigString_t *ptr, inputsigV)
40
41
      ptr->access(timedata);

Bergé's avatar
Bergé committed
42
    res = (*this)();
43
44
45
    return res;
  }

46
47
  void add(sigDouble_t &sig) { inputsig.push_back(&sig); }
  void add(sigString_t &sig) { inputsigV.push_back(&sig); }
48

Bergé's avatar
Bergé committed
49
  T operator()();
50
51
52
53
54
55

  T res;
  int call;
  int timedata;
};

56
template <> double DummyClass<double>::operator()() {
Bergé's avatar
Bergé committed
57
  res = call * timedata;
58
59
  return res;
}
60
template <> std::string DummyClass<std::string>::operator()() {
61
62
  std::ostringstream oss;
  oss << call * timedata;
Bergé's avatar
Bergé committed
63
  return oss.str();
64
65
}

66
template <class T> T DummyClass<T>::operator()() { return this->res; }
67

Bergé's avatar
Bergé committed
68
69
70
BOOST_AUTO_TEST_CASE(signaltimedependent) {
  DummyClass<double> pro1("pro1"), pro3("pro3"), pro5("pro5");
  DummyClass<std::string> pro2("pro2"), pro4("pro4"), pro6("pro6");
71

Bergé's avatar
Bergé committed
72
73
  sigDouble_t sig5("Sig5");
  sigString_t sig6("Sig6");
74
75
76
77

  sigString_t sig4(sig5, "Sig4");
  sigString_t sig2(sig4 << sig4 << sig4 << sig6, "Sig2");
  sigDouble_t sig3(sig2 << sig5 << sig6, "Sig3");
78
  sigDouble_t sig1(boost::bind(&DummyClass<double>::fun, &pro1, _1, _2),
79
                   sig2 << sig3, "Sig1");
80
  sigDouble_t sig7("Sig7");
81

82
83
84
85
86
  sig2.setFunction(boost::bind(&DummyClass<std::string>::fun, &pro2, _1, _2));
  sig3.setFunction(boost::bind(&DummyClass<double>::fun, &pro3, _1, _2));
  sig4.setFunction(boost::bind(&DummyClass<std::string>::fun, &pro4, _1, _2));
  sig5.setFunction(boost::bind(&DummyClass<double>::fun, &pro5, _1, _2));
  sig6.setFunction(boost::bind(&DummyClass<std::string>::fun, &pro6, _1, _2));
87
88
89
90
91
92
93
94
95
96
97
98

  pro1.add(sig2);
  pro1.add(sig3);
  pro2.add(sig4);
  pro2.add(sig4);
  pro2.add(sig4);
  pro3.add(sig2);
  pro4.add(sig5);
  pro2.add(sig6);
  pro3.add(sig5);
  pro3.add(sig6);

Bergé's avatar
Bergé committed
99
100
  sig5.setDependencyType(dynamicgraph::TimeDependency<int>::ALWAYS_READY);
  sig6.setDependencyType(dynamicgraph::TimeDependency<int>::BOOL_DEPENDENT);
101

102
103
104
  sig5.setPeriodTime(3);
  assert(sig5.getPeriodTime() == 3);

105
106
107
108
  sig6.setReady();

  {
    output_test_stream output;
Bergé's avatar
Bergé committed
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
    sig1.displayDependencies(output);
    BOOST_CHECK(
        output.is_equal("-- Sig:Sig1 (Type Fun) (t=0 (/1) )\n"
                        "   |-- Sig:Sig3 (Type Fun) (t=0 (/1) )\n"
                        "   |   |-- Sig:Sig6 (Type Fun) (ready=TRUE)\n"
                        "   |   |-- Sig:Sig5 (Type Fun) (A)\n"
                        "   |   `-- Sig:Sig2 (Type Fun) (t=0 (/1) )\n"
                        "   |       |-- Sig:Sig6 (Type Fun) (ready=TRUE)\n"
                        "   |       |-- Sig:Sig4 (Type Fun) (t=0 (/1) )\n"
                        "   |       |   `-- Sig:Sig5 (Type Fun) (A)\n"
                        "   |       |-- Sig:Sig4 (Type Fun) (t=0 (/1) )\n"
                        "   |       |   `-- Sig:Sig5 (Type Fun) (A)\n"
                        "   |       `-- Sig:Sig4 (Type Fun) (t=0 (/1) )\n"
                        "   |           `-- Sig:Sig5 (Type Fun) (A)\n"
                        "   `-- Sig:Sig2 (Type Fun) (t=0 (/1) )\n"
                        "       |-- Sig:Sig6 (Type Fun) (ready=TRUE)\n"
                        "       |-- Sig:Sig4 (Type Fun) (t=0 (/1) )\n"
                        "       |   `-- Sig:Sig5 (Type Fun) (A)\n"
                        "       |-- Sig:Sig4 (Type Fun) (t=0 (/1) )\n"
                        "       |   `-- Sig:Sig5 (Type Fun) (A)\n"
                        "       `-- Sig:Sig4 (Type Fun) (t=0 (/1) )\n"
                        "           `-- Sig:Sig5 (Type Fun) (A)"));
131
132
  }

Bergé's avatar
Bergé committed
133
134
  BOOST_CHECK(sig1.needUpdate(2));
  sig1.access(2);
135
136
137

  {
    output_test_stream output;
Bergé's avatar
Bergé committed
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
    sig1.displayDependencies(output);
    BOOST_CHECK(
        output.is_equal("-- Sig:Sig1 (Type Fun) (t=2 (/1) )\n"
                        "   |-- Sig:Sig3 (Type Fun) (t=2 (/1) )\n"
                        "   |   |-- Sig:Sig6 (Type Fun) (ready=FALSE)\n"
                        "   |   |-- Sig:Sig5 (Type Fun) (A)\n"
                        "   |   `-- Sig:Sig2 (Type Fun) (t=2 (/1) )\n"
                        "   |       |-- Sig:Sig6 (Type Fun) (ready=FALSE)\n"
                        "   |       |-- Sig:Sig4 (Type Fun) (t=2 (/1) )\n"
                        "   |       |   `-- Sig:Sig5 (Type Fun) (A)\n"
                        "   |       |-- Sig:Sig4 (Type Fun) (t=2 (/1) )\n"
                        "   |       |   `-- Sig:Sig5 (Type Fun) (A)\n"
                        "   |       `-- Sig:Sig4 (Type Fun) (t=2 (/1) )\n"
                        "   |           `-- Sig:Sig5 (Type Fun) (A)\n"
                        "   `-- Sig:Sig2 (Type Fun) (t=2 (/1) )\n"
                        "       |-- Sig:Sig6 (Type Fun) (ready=FALSE)\n"
                        "       |-- Sig:Sig4 (Type Fun) (t=2 (/1) )\n"
                        "       |   `-- Sig:Sig5 (Type Fun) (A)\n"
                        "       |-- Sig:Sig4 (Type Fun) (t=2 (/1) )\n"
                        "       |   `-- Sig:Sig5 (Type Fun) (A)\n"
                        "       `-- Sig:Sig4 (Type Fun) (t=2 (/1) )\n"
                        "           `-- Sig:Sig5 (Type Fun) (A)"));
160
  }
Bergé's avatar
Bergé committed
161
  sig2.access(4);
162
163
164

  {
    output_test_stream output;
Bergé's avatar
Bergé committed
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
    sig1.displayDependencies(output);
    BOOST_CHECK(
        output.is_equal("-- Sig:Sig1 (Type Fun) (t=2 (/1) )\n"
                        "   |-- Sig:Sig3 (Type Fun) (t=2 (/1) )\n"
                        "   |   |-- Sig:Sig6 (Type Fun) (ready=FALSE)\n"
                        "   |   |-- Sig:Sig5 (Type Fun) (A)\n"
                        "   |   `-- Sig:Sig2 (Type Fun) (t=4 (/1) )\n"
                        "   |       |-- Sig:Sig6 (Type Fun) (ready=FALSE)\n"
                        "   |       |-- Sig:Sig4 (Type Fun) (t=4 (/1) )\n"
                        "   |       |   `-- Sig:Sig5 (Type Fun) (A)\n"
                        "   |       |-- Sig:Sig4 (Type Fun) (t=4 (/1) )\n"
                        "   |       |   `-- Sig:Sig5 (Type Fun) (A)\n"
                        "   |       `-- Sig:Sig4 (Type Fun) (t=4 (/1) )\n"
                        "   |           `-- Sig:Sig5 (Type Fun) (A)\n"
                        "   `-- Sig:Sig2 (Type Fun) (t=4 (/1) )\n"
                        "       |-- Sig:Sig6 (Type Fun) (ready=FALSE)\n"
                        "       |-- Sig:Sig4 (Type Fun) (t=4 (/1) )\n"
                        "       |   `-- Sig:Sig5 (Type Fun) (A)\n"
                        "       |-- Sig:Sig4 (Type Fun) (t=4 (/1) )\n"
                        "       |   `-- Sig:Sig5 (Type Fun) (A)\n"
                        "       `-- Sig:Sig4 (Type Fun) (t=4 (/1) )\n"
                        "           `-- Sig:Sig5 (Type Fun) (A)"));
187
  }
Bergé's avatar
Bergé committed
188
  sig1.access(4);
189
190
191

  {
    output_test_stream output;
Bergé's avatar
Bergé committed
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
    sig1.displayDependencies(output);
    BOOST_CHECK(
        output.is_equal("-- Sig:Sig1 (Type Fun) (t=4 (/1) )\n"
                        "   |-- Sig:Sig3 (Type Fun) (t=4 (/1) )\n"
                        "   |   |-- Sig:Sig6 (Type Fun) (ready=FALSE)\n"
                        "   |   |-- Sig:Sig5 (Type Fun) (A)\n"
                        "   |   `-- Sig:Sig2 (Type Fun) (t=4 (/1) )\n"
                        "   |       |-- Sig:Sig6 (Type Fun) (ready=FALSE)\n"
                        "   |       |-- Sig:Sig4 (Type Fun) (t=4 (/1) )\n"
                        "   |       |   `-- Sig:Sig5 (Type Fun) (A)\n"
                        "   |       |-- Sig:Sig4 (Type Fun) (t=4 (/1) )\n"
                        "   |       |   `-- Sig:Sig5 (Type Fun) (A)\n"
                        "   |       `-- Sig:Sig4 (Type Fun) (t=4 (/1) )\n"
                        "   |           `-- Sig:Sig5 (Type Fun) (A)\n"
                        "   `-- Sig:Sig2 (Type Fun) (t=4 (/1) )\n"
                        "       |-- Sig:Sig6 (Type Fun) (ready=FALSE)\n"
                        "       |-- Sig:Sig4 (Type Fun) (t=4 (/1) )\n"
                        "       |   `-- Sig:Sig5 (Type Fun) (A)\n"
                        "       |-- Sig:Sig4 (Type Fun) (t=4 (/1) )\n"
                        "       |   `-- Sig:Sig5 (Type Fun) (A)\n"
                        "       `-- Sig:Sig4 (Type Fun) (t=4 (/1) )\n"
                        "           `-- Sig:Sig5 (Type Fun) (A)"));
214
215
  }

Bergé's avatar
Bergé committed
216
217
  sig1.needUpdate(6);
  sig1.needUpdate(6);
218
  output_test_stream output;
219
220
  sig1.writeGraph(output);
  BOOST_CHECK(output.is_equal(""));
221

222
223
  sig1.removeDependency(sig3);
  BOOST_CHECK(true);
224
  const double &avalue = sig1(6);
225
226
227
228
229
230
231
  output << avalue;
  BOOST_CHECK(true);
  /// Verify check compatibility
  try {
    sig1.checkCompatibility();
  }
  //  catch(double e)
232
  catch (...) {
233
234
235
    std::cout << "Message: test \n";
  }
  BOOST_CHECK(true);
236
}