GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: tests/signal-time-dependent.cpp Lines: 79 79 100.0 %
Date: 2023-03-15 12:04:10 Branches: 236 460 51.3 %

Line Branch Exec Source
1
// Copyright 2010 Thomas Moulard.
2
//
3
#include <dynamic-graph/signal-time-dependent.h>
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
20
typedef dynamicgraph::SignalTimeDependent<double, int> sigDouble_t;
21
typedef dynamicgraph::SignalTimeDependent<std::string, int> sigString_t;
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
12
  explicit DummyClass(const std::string &n)
31
12
      : proname(n), res(), call(), timedata() {}
32
33
38
  T &fun(T &res, int t) {
34
38
    ++call;
35
38
    timedata = t;
36
37








74
    BOOST_FOREACH (sigDouble_t *ptr, inputsig) ptr->access(timedata);
38
39








122
    BOOST_FOREACH (sigString_t *ptr, inputsigV) ptr->access(timedata);
40
41
38
    res = (*this)();
42
38
    return res;
43
  }
44
45
6
  void add(sigDouble_t &sig) { inputsig.push_back(&sig); }
46
7
  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 <>
56
12
double DummyClass<double>::operator()() {
57
12
  res = call * timedata;
58
12
  return res;
59
}
60
template <>
61
7
std::string DummyClass<std::string>::operator()() {
62
14
  std::ostringstream oss;
63
7
  oss << call * timedata;
64
14
  return oss.str();
65
}
66
67
template <class T>
68
T DummyClass<T>::operator()() {
69
  return this->res;
70
}
71
72
















4
BOOST_AUTO_TEST_CASE(signaltimedependent) {
73



10
  DummyClass<double> pro1("pro1"), pro3("pro3"), pro5("pro5");
74



10
  DummyClass<std::string> pro2("pro2"), pro4("pro4"), pro6("pro6");
75
76

6
  sigDouble_t sig5("Sig5");
77

6
  sigString_t sig6("Sig6");
78
79

6
  sigString_t sig4(sig5, "Sig4");
80


6
  sigString_t sig2(sig4 << sig4 << sig4 << sig6, "Sig2");
81


6
  sigDouble_t sig3(sig2 << sig5 << sig6, "Sig3");
82
  sigDouble_t sig1(boost::bind(&DummyClass<double>::fun, &pro1, _1, _2),
83


6
                   sig2 << sig3, "Sig1");
84

6
  sigDouble_t sig7("Sig7");
85
86

2
  sig2.setFunction(boost::bind(&DummyClass<std::string>::fun, &pro2, _1, _2));
87

2
  sig3.setFunction(boost::bind(&DummyClass<double>::fun, &pro3, _1, _2));
88

2
  sig4.setFunction(boost::bind(&DummyClass<std::string>::fun, &pro4, _1, _2));
89

2
  sig5.setFunction(boost::bind(&DummyClass<double>::fun, &pro5, _1, _2));
90

2
  sig6.setFunction(boost::bind(&DummyClass<std::string>::fun, &pro6, _1, _2));
91
92
2
  pro1.add(sig2);
93
2
  pro1.add(sig3);
94
2
  pro2.add(sig4);
95
2
  pro2.add(sig4);
96
2
  pro2.add(sig4);
97
2
  pro3.add(sig2);
98
2
  pro4.add(sig5);
99
2
  pro2.add(sig6);
100
2
  pro3.add(sig5);
101
2
  pro3.add(sig6);
102
103
2
  sig5.setDependencyType(dynamicgraph::TimeDependency<int>::ALWAYS_READY);
104
2
  sig6.setDependencyType(dynamicgraph::TimeDependency<int>::BOOL_DEPENDENT);
105
106
2
  sig5.setPeriodTime(3);
107

2
  assert(sig5.getPeriodTime() == 3);
108
109
2
  sig6.setReady();
110
111
  {
112

4
    output_test_stream output;
113


2
    sig1.displayDependencies(output);
114



2
    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



2
  BOOST_CHECK(sig1.needUpdate(2));
138
2
  sig1.access(2);
139
140
  {
141

4
    output_test_stream output;
142


2
    sig1.displayDependencies(output);
143



2
    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
2
  sig2.access(4);
166
167
  {
168

4
    output_test_stream output;
169


2
    sig1.displayDependencies(output);
170



2
    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
2
  sig1.access(4);
193
194
  {
195

4
    output_test_stream output;
196


2
    sig1.displayDependencies(output);
197



2
    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
2
  sig1.needUpdate(6);
221
2
  sig1.needUpdate(6);
222

4
  output_test_stream output;
223
2
  sig1.writeGraph(output);
224



2
  BOOST_CHECK(output.is_equal(""));
225
226
2
  sig1.removeDependency(sig3);
227



2
  BOOST_CHECK(true);
228
2
  const double &avalue = sig1(6);
229
2
  output << avalue;
230



2
  BOOST_CHECK(true);
231
  /// Verify check compatibility
232
  try {
233
2
    sig1.checkCompatibility();
234
  }
235
  //  catch(double e)
236
4
  catch (...) {
237
2
    std::cout << "Message: test \n";
238
  }
239



2
  BOOST_CHECK(true);
240
2
}