Debian Codegraph @

unittests/integration/test_Signal.cpp

raw
1 /*
2 * Copyright (c) 2011-2022, The DART development contributors
3 * All rights reserved.
4 *
5 * The list of contributors can be found at:
6 * https://github.com/dartsim/dart/blob/master/LICENSE
7 *
8 * This file is provided under the following "BSD-style" License:
9 * Redistribution and use in source and binary forms, with or
10 * without modification, are permitted provided that the following
11 * conditions are met:
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <numeric>
34
35 #include <gtest/gtest.h>
36
37 #include "dart/dart.hpp"
38
39 using namespace std;
40 using namespace Eigen;
41 using namespace dart;
42 using namespace common;
43 using namespace dynamics;
44
45 static int callCount0 = 0;
46 static int callCount1 = 0;
47 static int callCount2 = 0;
48
49 //==============================================================================
50 void foo0()
51 {
52 callCount0++;
53 }
54 void foo1(int /*_val*/)
55 {
56 callCount1++;
57 }
58 void foo2(int /*_val1*/, float /*_val2*/)
59 {
60 callCount2++;
61 }
62 double foo3()
63 {
64 return 10.0;
65 }
66
67 //==============================================================================
68 class Viewer
69 {
70 public:
71 static void onSignal1Static(int /*_val*/)
72 {
73 callCount1++;
74 }
75 static void onSignal2Static(int /*_val1*/, float /*_val2*/)
76 {
77 callCount2++;
78 }
79 void onSignal1(int /*_val*/)
80 {
81 callCount1++;
82 }
83 void onSignal2(int /*_val1*/, float /*_val2*/)
84 {
85 callCount2++;
86 }
87 };
88
89 //==============================================================================
90 TEST(Signal, Basic)
91 {
92 Signal<void()> signal0;
93 Signal<void(int)> signal1;
94 Signal<void(int, float)> signal2;
95 Signal<double()> signal3;
96
97 Connection connection0 = signal0.connect(&foo0);
98 Connection connection1 = signal1.connect(&foo1);
99 Connection connection2 = signal2.connect(&foo2);
100 Connection connection3 = signal3.connect(&foo3);
101
102 EXPECT_EQ(static_cast<int>(signal0.getNumConnections()), 1);
103 EXPECT_EQ(static_cast<int>(signal1.getNumConnections()), 1);
104 EXPECT_EQ(static_cast<int>(signal2.getNumConnections()), 1);
105 EXPECT_EQ(static_cast<int>(signal3.getNumConnections()), 1);
106
107 EXPECT_TRUE(connection0.isConnected());
108 EXPECT_TRUE(connection1.isConnected());
109 EXPECT_TRUE(connection2.isConnected());
110 EXPECT_TRUE(connection3.isConnected());
111
112 connection0.disconnect();
113 connection1.disconnect();
114 connection2.disconnect();
115 connection3.disconnect();
116
117 EXPECT_EQ(static_cast<int>(signal0.getNumConnections()), 0);
118 EXPECT_EQ(static_cast<int>(signal1.getNumConnections()), 0);
119 EXPECT_EQ(static_cast<int>(signal2.getNumConnections()), 0);
120 EXPECT_EQ(static_cast<int>(signal3.getNumConnections()), 0);
121
122 signal0();
123
124 EXPECT_FALSE(connection0.isConnected());
125 EXPECT_FALSE(connection1.isConnected());
126 EXPECT_FALSE(connection2.isConnected());
127 EXPECT_FALSE(connection3.isConnected());
128 }
129
130 //==============================================================================
131 TEST(Signal, NonStaticMemberFunction)
132 {
133 Signal<void(int)> signal1;
134 Signal<void(int, float)> signal2;
135 Viewer viewer;
136
137 // Connect static member function
138 signal1.connect(&Viewer::onSignal1Static);
139 signal2.connect(&Viewer::onSignal2Static);
140
141 // Connect non-static member function
142 using std::placeholders::_1;
143 using std::placeholders::_2;
144 signal1.connect(bind(&Viewer::onSignal1, &viewer, _1));
145 signal2.connect(bind(&Viewer::onSignal2, &viewer, _1, _2));
146
147 // The signal should have the maximum number of listeners
148 EXPECT_EQ(static_cast<int>(signal1.getNumConnections()), 2);
149 EXPECT_EQ(static_cast<int>(signal2.getNumConnections()), 2);
150
151 // Check the number of calls
152 callCount1 = 0;
153 callCount2 = 0;
154 signal1.raise(0);
155 signal2.raise(0, 0);
156 EXPECT_EQ(callCount1, 2);
157 EXPECT_EQ(callCount2, 2);
158 }
159
160 //==============================================================================
161 TEST(Signal, ScopedConnection)
162 {
163 Signal<void(int)> signal;
164 Connection c = signal.connect(foo1);
165 EXPECT_EQ(static_cast<int>(signal.getNumConnections()), 1);
166
167 {
168 ScopedConnection sc(signal.connect(foo1));
169 EXPECT_EQ(static_cast<int>(signal.getNumConnections()), 2);
170 }
171
172 EXPECT_EQ(static_cast<int>(signal.getNumConnections()), 1);
173 }
174
175 //==============================================================================
176 TEST(Signal, ConnectionLifeTime)
177 {
178 Signal<void(int)>* signal = new Signal<void(int)>();
179
180 Connection connection1 = signal->connect(foo1);
181 EXPECT_TRUE(connection1.isConnected());
182
183 Connection connection2 = signal->connect(foo1);
184 EXPECT_TRUE(connection2.isConnected());
185
186 {
187 ScopedConnection scopedConnection(connection2);
188 EXPECT_TRUE(scopedConnection.isConnected());
189 EXPECT_TRUE(connection2.isConnected());
190 // scopedConnection disconnected connection2 when it was destroyed.
191 }
192 EXPECT_FALSE(connection2.isConnected());
193 EXPECT_EQ(signal->getNumConnections(), 1);
194
195 delete signal;
196
197 // Although the signal is destroyed, its connection still works. For those
198 // connections, isConnected() returns false.
199 EXPECT_FALSE(connection1.isConnected());
200 }
201
202 //==============================================================================
203 float product(float x, float y)
204 {
205 return x * y;
206 }
207 float quotient(float x, float y)
208 {
209 return x / y;
210 }
211 float sum(float x, float y)
212 {
213 return x + y;
214 }
215 float difference(float x, float y)
216 {
217 return x - y;
218 }
219
220 // combiner which returns the maximum value returned by all slots
221 template <typename T>
222 struct signal_maximum
223 {
224 typedef T result_type;
225
226 template <typename InputIterator>
227 static T process(InputIterator first, InputIterator last)
228 {
229 // If there are no slots to call, just return the
230 // default-constructed value
231 if (first == last)
232 return T();
233
234 T max_value = *first++;
235
236 while (first != last)
237 {
238 if (max_value < *first)
239 max_value = *first;
240 ++first;
241 }
242
243 return max_value;
244 }
245 };
246
247 // combiner which returns the maximum value returned by all slots
248 template <typename T>
249 struct signal_sum
250 {
251 typedef T result_type;
252
253 template <typename InputIterator>
254 static T process(InputIterator first, InputIterator last)
255 {
256 // If there are no slots to call, just return the
257 // default-constructed value
258 if (first == last)
259 return T();
260
261 T sum = *first;
262 first++;
263
264 while (first != last)
265 {
266 sum += *first;
267 ++first;
268 }
269
270 return sum;
271 }
272 };
273
274 //==============================================================================
275 TEST(Signal, ReturnValues)
276 {
277 const float tol = 1.5e-6;
278
279 const float a = 5.0f;
280 const float b = 3.0f;
281
282 std::vector<float> res(4);
283 res[0] = product(a, b);
284 res[1] = quotient(a, b);
285 res[2] = sum(a, b);
286 res[3] = difference(a, b);
287
288 // signal_maximum
289 Signal<float(float, float), signal_maximum> signal1;
290 signal1.connect(&product);
291 signal1.connect(&quotient);
292 signal1.connect(&sum);
293 signal1.connect(&difference);
294 EXPECT_EQ(signal1(5, 3), *std::max_element(res.begin(), res.end()));
295
296 // signal_sum
297 Signal<float(float, float), signal_sum> signal2;
298 signal2.connect(&product);
299 signal2.connect(&quotient);
300 signal2.connect(&sum);
301 signal2.connect(&difference);
302 EXPECT_NEAR(signal2(5, 3), std::accumulate(res.begin(), res.end(), 0.0), tol);
303 }
304
305 //==============================================================================
306 void frameChangeCallback(
307 const Entity* _entity,
308 const Frame* _oldParentFrame,
309 const Frame* _newParentFrame)
310 {
311 assert(_entity);
312
313 std::string oldFrameName
314 = _oldParentFrame == nullptr ? "(empty)" : _oldParentFrame->getName();
315 std::string newFrameName
316 = _newParentFrame == nullptr ? "(empty)" : _newParentFrame->getName();
317
318 if (_newParentFrame)
319 std::cout << "[" << _entity->getName() << "]: " << oldFrameName << " --> "
320 << newFrameName << std::endl;
321 else
322 std::cout << "Entity (" << _entity << ") has been destroyed" << std::endl;
323 }
324
325 //==============================================================================
326 void nameChangedCallback(
327 const Entity* entity,
328 const std::string& oldName,
329 const std::string& newName)
330 {
331 assert(entity);
332
333 std::cout << "[" << entity->getName() << "]: Name changed: '" << oldName
334 << "' --> '" << newName << "'.\n";
335 }
336
337 //==============================================================================
338 TEST(Signal, FrameSignals)
339 {
340 Isometry3d tf1(Isometry3d::Identity());
341 tf1.translate(Vector3d(0.1, -0.1, 0));
342
343 Isometry3d tf2(Isometry3d::Identity());
344 tf2.translate(Vector3d(0, 0.1, 0));
345 tf2.rotate(AngleAxisd(dart::math::toRadian(45.0), Vector3d(1, 0, 0)));
346
347 Isometry3d tf3(Isometry3d::Identity());
348 tf3.translate(Vector3d(0, 0, 0.1));
349 tf3.rotate(AngleAxisd(dart::math::toRadian(60.0), Vector3d(0, 1, 0)));
350
351 SimpleFrame F1(Frame::World(), "F1", tf1);
352 SimpleFrame F2(&F1, "F2", tf2);
353 SimpleFrame F3(&F2, "F3", tf3);
354
355 Connection cf1 = F1.onFrameChanged.connect(frameChangeCallback);
356 Connection cf2 = F2.onFrameChanged.connect(frameChangeCallback);
357 ScopedConnection cf3(F3.onFrameChanged.connect(frameChangeCallback));
358
359 Connection cv1 = F1.onNameChanged.connect(nameChangedCallback);
360 Connection cv2 = F2.onNameChanged.connect(nameChangedCallback);
361 ScopedConnection cv3(F3.onNameChanged.connect(nameChangedCallback));
362
363 F1.setName("new " + F1.getName());
364 F2.setName("new " + F2.getName());
365 F3.setName("new " + F3.getName());
366
367 F3.setParentFrame(&F1);
368 }