source: finroc_plugins_data_ports/tests/test_collection.cpp @ 124:ca459cc011c8

Last change on this file since 124:ca459cc011c8 was 124:ca459cc011c8, checked in by Tobias Föhst <foehst@…>, 2 years ago

Merge with 14.08

File size: 17.1 KB
Line 
1//
2// You received this file as part of Finroc
3// A framework for intelligent robot control
4//
5// Copyright (C) Finroc GbR (finroc.org)
6//
7// This program is free software; you can redistribute it and/or modify
8// it under the terms of the GNU General Public License as published by
9// the Free Software Foundation; either version 2 of the License, or
10// (at your option) any later version.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License along
18// with this program; if not, write to the Free Software Foundation, Inc.,
19// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20//
21//----------------------------------------------------------------------
22/*!\file    plugins/data_ports/tests/test_collection.cpp
23 *
24 * \author  Max Reichardt
25 *
26 * \date    2013-01-03
27 *
28 * Collections of tests.
29 * This is the place to add simple tests for data_ports.
30 */
31//----------------------------------------------------------------------
32
33//----------------------------------------------------------------------
34// External includes (system with <>, local with "")
35//----------------------------------------------------------------------
36#include "core/tRuntimeEnvironment.h"
37#include "rrlib/util/tUnitTestSuite.h"
38
39//----------------------------------------------------------------------
40// Internal includes with ""
41//----------------------------------------------------------------------
42#include "plugins/data_ports/tInputPort.h"
43#include "plugins/data_ports/tOutputPort.h"
44#include "plugins/data_ports/tProxyPort.h"
45#include "plugins/data_ports/tThreadLocalBufferManagement.h"
46#include "plugins/data_ports/tPortPack.h"
47
48//----------------------------------------------------------------------
49// Debugging
50//----------------------------------------------------------------------
51#include <cassert>
52
53//----------------------------------------------------------------------
54// Namespace usage
55//----------------------------------------------------------------------
56
57//----------------------------------------------------------------------
58// Namespace declaration
59//----------------------------------------------------------------------
60namespace finroc
61{
62namespace data_ports
63{
64
65//----------------------------------------------------------------------
66// Forward declarations / typedefs / enums
67//----------------------------------------------------------------------
68
69//----------------------------------------------------------------------
70// Const values
71//----------------------------------------------------------------------
72
73//----------------------------------------------------------------------
74// Implementation
75//----------------------------------------------------------------------
76
77void TestPortChains()
78{
79  FINROC_LOG_PRINT(DEBUG_VERBOSE_1, "\nTesting forwarding data among port chains");
80  core::tFrameworkElement* parent = new core::tFrameworkElement(&core::tRuntimeEnvironment::GetInstance(), "TestPortChains");
81
82  // Create ports
83  tOutputPort<std::string> output_port1("Output Port 1", parent);
84  tOutputPort<std::string> output_port2("Output Port 2", parent);
85  tOutputPort<std::string> output_port3("Output Port 3", parent);
86  tProxyPort<std::string, true> proxy_port1("Proxy Port 1", parent);
87  tProxyPort<std::string, true> proxy_port2("Proxy Port 2", parent);
88  tProxyPort<std::string, true> proxy_port3("Proxy Port 3", parent);
89  tInputPort<std::string> input_port1("Input Port 1", parent);
90  tInputPort<std::string> input_port2("Input Port 2", parent);
91  tInputPort<std::string> input_port3("Input Port 3", parent);
92
93  // Connect ports
94  output_port1.ConnectTo(proxy_port1);
95  output_port2.ConnectTo(proxy_port2);
96  output_port3.ConnectTo(proxy_port3);
97  proxy_port1.ConnectTo(input_port1);
98  proxy_port2.ConnectTo(input_port2);
99  proxy_port3.ConnectTo(input_port3);
100  parent->Init();
101
102  std::string test_string = "12345";
103  for (int i = 0; i < 20; i++)
104  {
105    // Publish data
106    tPortDataPointer<std::string> unused_buffer = output_port1.GetUnusedBuffer();
107    std::string test_string2 = "Test" + std::to_string(i);
108    *unused_buffer = test_string2;
109    output_port1.Publish(unused_buffer);
110
111    // Forward data to second and third chain
112    output_port2.Publish(input_port1.GetPointer());
113    output_port3.Publish(input_port2.GetPointer());
114    RRLIB_UNIT_TESTS_ASSERT(test_string2 == *input_port3.GetPointer());
115
116    if (i > 10)
117    {
118      output_port2.Publish(test_string);
119      output_port3.Publish(input_port2.GetPointer());
120      RRLIB_UNIT_TESTS_ASSERT(test_string == *input_port3.GetPointer());
121      RRLIB_UNIT_TESTS_ASSERT(test_string == *input_port2.GetPointer());
122      RRLIB_UNIT_TESTS_ASSERT(test_string2 == *input_port1.GetPointer());
123    }
124  }
125
126  parent->ManagedDelete();
127}
128
129template <typename T>
130void TestPortQueues(const T& value1, const T& value2, const T& value3)
131{
132  FINROC_LOG_PRINT(DEBUG_VERBOSE_1, "\nTesting port queue basic operation for type ", (rrlib::rtti::tDataType<T>()).GetName());
133  core::tFrameworkElement* parent = new core::tFrameworkElement(&core::tRuntimeEnvironment::GetInstance(), "TestPortQueue");
134
135  tOutputPort<T> output_port("Output Port", parent);
136  tInputPort<T> input_port_fifo("Input Port FIFO", parent, tQueueSettings(false));
137  tInputPort<T> input_port_all("Input Port ALL", parent, tQueueSettings(true));
138  output_port.ConnectTo(input_port_fifo);
139  output_port.ConnectTo(input_port_all);
140  parent->Init();
141
142  FINROC_LOG_PRINT(DEBUG_VERBOSE_1, " Enqueueing three values");
143  output_port.Publish(value1);
144  output_port.Publish(value2);
145  output_port.Publish(value3);
146
147  FINROC_LOG_PRINT(DEBUG_VERBOSE_1, " Dequeueing five values FIFO");
148  for (size_t i = 0; i < 5; ++i)
149  {
150    tPortDataPointer<const T> result = input_port_fifo.Dequeue();
151    if (result)
152    {
153      FINROC_LOG_PRINT(DEBUG_VERBOSE_1, "  Dequeued ", *result);
154    }
155    else
156    {
157      FINROC_LOG_PRINT(DEBUG_VERBOSE_1, "  Dequeued nothing");
158    }
159    RRLIB_UNIT_TESTS_ASSERT((i == 0 && value1 == *result) || (i == 1 && value2 == *result) || (i == 2 && value3 == *result) || (i > 2 && (!result)));
160  }
161
162  FINROC_LOG_PRINT(DEBUG_VERBOSE_1, " Dequeueing all values at once");
163  tPortBuffers<tPortDataPointer<const T>> dequeued = input_port_all.DequeueAllBuffers();
164  size_t i = 0;
165  while (!dequeued.Empty())
166  {
167    T result = *dequeued.PopFront();
168    FINROC_LOG_PRINT(DEBUG_VERBOSE_1, "  Dequeued ", result);
169    RRLIB_UNIT_TESTS_ASSERT((i == 0 && value1 == result) || (i == 1 && value2 == result) || (i == 2 && value3 == result));
170    i++;
171  }
172
173  parent->ManagedDelete();
174};
175
176
177
178template <typename T>
179void TestPortListeners(const T& publish_value)
180{
181  class tListener
182  {
183  public:
184    void OnPortChange(const T& value, tChangeContext& change_context)
185    {
186      FINROC_LOG_PRINT(DEBUG_VERBOSE_1, "  Port Changed: ", value);
187      this->value1 = value;
188      this->calls++;
189    }
190    void OnPortChange(tPortDataPointer<const T>& value, tChangeContext& change_context)
191    {
192      FINROC_LOG_PRINT(DEBUG_VERBOSE_1, "  Port Changed (tPortDataPointer): ", *value);
193      this->value2 = *value;
194      this->calls++;
195    }
196    void OnPortChange(const rrlib::rtti::tGenericObject& value, tChangeContext& change_context)
197    {
198      FINROC_LOG_PRINT(DEBUG_VERBOSE_1, "  Port Changed Generic: ", value);
199      this->calls++;
200    }
201    void OnPortChange(tPortDataPointer<const rrlib::rtti::tGenericObject>& value, tChangeContext& change_context)
202    {
203      FINROC_LOG_PRINT(DEBUG_VERBOSE_1, "  Port Changed Generic (tPortDataPointer): ", *value);
204      this->calls++;
205    }
206    void OnPortChange(tChangeContext& change_context)
207    {
208      FINROC_LOG_PRINT(DEBUG_VERBOSE_1, "  Port Changed Simple");
209      this->calls++;
210    }
211
212    T value1, value2;
213    size_t calls;
214
215    tListener() : value1(), value2(), calls(0) {}
216  };
217
218  FINROC_LOG_PRINT(DEBUG_VERBOSE_1, "\nTesting port listeners for type ", (rrlib::rtti::tDataType<T>()).GetName());
219  tListener listener;
220  core::tFrameworkElement* parent = new core::tFrameworkElement(&core::tRuntimeEnvironment::GetInstance(), "TestPortListeners");
221
222  tOutputPort<T> output_port("Output Port", parent);
223  tInputPort<T> input_port("Input Port", parent);
224  output_port.ConnectTo(input_port);
225  input_port.AddPortListener(listener);
226  input_port.AddPortListenerForPointer(listener);
227  input_port.AddPortListenerSimple(listener);
228  tGenericPort generic_input_port = tGenericPort::Wrap(*input_port.GetWrapped());
229  generic_input_port.AddPortListener(listener);
230  generic_input_port.AddPortListenerForPointer(listener);
231  generic_input_port.AddPortListenerSimple(listener);
232  parent->Init();
233
234  output_port.Publish(publish_value);
235
236  RRLIB_UNIT_TESTS_ASSERT(listener.value1 == publish_value && listener.value2 == publish_value && listener.calls == 6);
237
238  parent->ManagedDelete();
239}
240
241template <typename T>
242void TestNetworkConnectionLoss(const T& default_value, const T& publish_value)
243{
244  core::tFrameworkElement* parent = new core::tFrameworkElement(&core::tRuntimeEnvironment::GetInstance(), "TestNetworkConnectionLoss");
245
246  tOutputPort<T> output_port("Output Port", parent);
247  tInputPort<T> input_port_no_explicit_default("Input Port No Explicit Default", parent, core::tFrameworkElement::tFlag::DEFAULT_ON_DISCONNECT);
248  tInputPort<T> input_port_explicit_default("Input Port Explicit Default", parent, core::tFrameworkElement::tFlag::DEFAULT_ON_DISCONNECT, default_value);
249  tInputPort<T> input_port_deferred_default("Input Port Deferred Default", parent, core::tFrameworkElement::tFlag::DEFAULT_ON_DISCONNECT);
250  input_port_deferred_default.SetDefault(default_value);
251  output_port.ConnectTo(input_port_no_explicit_default);
252  output_port.ConnectTo(input_port_explicit_default);
253  output_port.ConnectTo(input_port_deferred_default);
254  parent->Init();
255
256  output_port.Publish(publish_value);
257  RRLIB_UNIT_TESTS_EQUALITY(publish_value, *input_port_no_explicit_default.GetPointer());
258  RRLIB_UNIT_TESTS_EQUALITY(publish_value, *input_port_explicit_default.GetPointer());
259  RRLIB_UNIT_TESTS_EQUALITY(publish_value, *input_port_deferred_default.GetPointer());
260  input_port_no_explicit_default.GetWrapped()->NotifyOfNetworkConnectionLoss();
261  input_port_explicit_default.GetWrapped()->NotifyOfNetworkConnectionLoss();
262  input_port_deferred_default.GetWrapped()->NotifyOfNetworkConnectionLoss();
263  RRLIB_UNIT_TESTS_EQUALITY(T(), *input_port_no_explicit_default.GetPointer());
264  RRLIB_UNIT_TESTS_EQUALITY(default_value, *input_port_explicit_default.GetPointer());
265  RRLIB_UNIT_TESTS_EQUALITY(default_value, *input_port_deferred_default.GetPointer());
266  output_port.Publish(publish_value);
267  RRLIB_UNIT_TESTS_EQUALITY(publish_value, *input_port_no_explicit_default.GetPointer());
268  RRLIB_UNIT_TESTS_EQUALITY(publish_value, *input_port_explicit_default.GetPointer());
269  RRLIB_UNIT_TESTS_EQUALITY(publish_value, *input_port_deferred_default.GetPointer());
270  output_port.DisconnectAll();
271  RRLIB_UNIT_TESTS_EQUALITY(T(), *input_port_no_explicit_default.GetPointer());
272  RRLIB_UNIT_TESTS_EQUALITY(default_value, *input_port_explicit_default.GetPointer());
273  RRLIB_UNIT_TESTS_EQUALITY(default_value, *input_port_deferred_default.GetPointer());
274
275  parent->ManagedDelete();
276}
277
278void TestOutOfBoundsPublish()
279{
280  core::tFrameworkElement* parent = new core::tFrameworkElement(&core::tRuntimeEnvironment::GetInstance(), "TestOutOfBoundsPublish");
281
282  tOutputPort<int> output_port("Output Port", parent, tBounds<int>(0, 2, tOutOfBoundsAction::DISCARD));
283  tInputPort<int> input_port("Input Port", parent, tBounds<int>(0, 1));
284  output_port.ConnectTo(input_port);
285  parent->Init();
286
287  output_port.Publish(3);
288  RRLIB_UNIT_TESTS_EQUALITY(0, input_port.Get());
289  output_port.Publish(2);
290  RRLIB_UNIT_TESTS_EQUALITY(1, input_port.Get());
291
292  parent->ManagedDelete();
293}
294
295template <typename T>
296void TestHijackedPublishing(const T& value_to_publish)
297{
298  core::tFrameworkElement* parent = new core::tFrameworkElement(&core::tRuntimeEnvironment::GetInstance(), "TestHijackedPublishing");
299  T default_value = T();
300
301  tOutputPort<T> output_port("Output Port", parent);
302  tProxyPort<T, true> proxy_port("Proxy Port", parent, core::tFrameworkElementFlag::PUSH_STRATEGY);
303  tInputPort<T> input_port("Input Port", parent);
304  output_port.ConnectTo(proxy_port);
305  proxy_port.ConnectTo(input_port);
306  parent->Init();
307
308  output_port.Publish(value_to_publish);
309  RRLIB_UNIT_TESTS_EQUALITY(value_to_publish, *proxy_port.GetPointer());
310  RRLIB_UNIT_TESTS_EQUALITY(value_to_publish, *input_port.GetPointer());
311  input_port.GetWrapped()->SetHijacked(true);
312  RRLIB_UNIT_TESTS_EQUALITY(value_to_publish, *proxy_port.GetPointer());
313  RRLIB_UNIT_TESTS_EQUALITY(value_to_publish, *input_port.GetPointer());
314  output_port.Publish(default_value);
315  RRLIB_UNIT_TESTS_EQUALITY(default_value, *proxy_port.GetPointer());
316  RRLIB_UNIT_TESTS_EQUALITY(value_to_publish, *input_port.GetPointer());
317  proxy_port.GetWrapped()->SetHijacked(true);
318  input_port.GetWrapped()->SetHijacked(false);
319  output_port.Publish(value_to_publish);
320  RRLIB_UNIT_TESTS_EQUALITY(default_value, *proxy_port.GetPointer());
321  RRLIB_UNIT_TESTS_EQUALITY(value_to_publish, *input_port.GetPointer());
322  output_port.GetWrapped()->SetHijacked(true);
323  output_port.Publish(value_to_publish);
324
325  parent->ManagedDelete();
326}
327
328template <typename T>
329void TestGenericPorts(T value_to_publish, T another_value)
330{
331  core::tFrameworkElement* parent = new core::tFrameworkElement(&core::tRuntimeEnvironment::GetInstance(), "TestGenericPorts");
332  T default_value = T();
333  rrlib::rtti::tGenericObjectWrapper<T> default_buffer(default_value);
334  rrlib::rtti::tGenericObjectWrapper<T> value_buffer(value_to_publish);
335  rrlib::rtti::tGenericObjectWrapper<T> another_value_buffer(another_value);
336
337  tGenericPort output_port("Output Port", rrlib::rtti::tDataType<T>(), parent, core::tFrameworkElement::tFlag::EMITS_DATA | core::tFrameworkElement::tFlag::OUTPUT_PORT);
338  tGenericPort proxy_port("Proxy Port", rrlib::rtti::tDataType<T>(), parent, core::tFrameworkElement::tFlag::ACCEPTS_DATA | core::tFrameworkElement::tFlag::PUSH_STRATEGY | core::tFrameworkElement::tFlag::EMITS_DATA);
339  tGenericPort input_port("Input Port", rrlib::rtti::tDataType<T>(), parent, core::tFrameworkElement::tFlag::ACCEPTS_DATA | core::tFrameworkElement::tFlag::PUSH_STRATEGY);
340  output_port.ConnectTo(proxy_port);
341  proxy_port.ConnectTo(input_port);
342  parent->Init();
343
344  // Publish by value
345  T get_value = T();
346  rrlib::rtti::tGenericObjectWrapper<T> get_buffer(get_value);
347  input_port.Get(get_buffer);
348  RRLIB_UNIT_TESTS_EQUALITY(default_value, get_value);
349  output_port.Publish(value_buffer);
350  proxy_port.Get(get_buffer);
351  RRLIB_UNIT_TESTS_EQUALITY(value_to_publish, get_value);
352  input_port.Get(get_buffer);
353  RRLIB_UNIT_TESTS_EQUALITY(value_to_publish, get_value);
354
355  // Publish via buffer
356  auto unused_buffer = output_port.GetUnusedBuffer();
357  unused_buffer->GetData<T>() = another_value;
358  output_port.Publish(unused_buffer);
359  proxy_port.Get(get_buffer);
360  RRLIB_UNIT_TESTS_EQUALITY(another_value, get_value);
361  input_port.Get(get_buffer);
362  RRLIB_UNIT_TESTS_EQUALITY(another_value, get_value);
363
364  parent->ManagedDelete();
365}
366
367class DataPortsTestCollection : public rrlib::util::tUnitTestSuite
368{
369  RRLIB_UNIT_TESTS_BEGIN_SUITE(DataPortsTestCollection);
370  RRLIB_UNIT_TESTS_ADD_TEST(Test);
371  RRLIB_UNIT_TESTS_ADD_TEST(PortPack);
372  RRLIB_UNIT_TESTS_END_SUITE;
373
374  void Test()
375  {
376    TestPortChains();
377    TestPortQueues<int>(1, 2, 3);
378    TestPortQueues<std::string>("1", "2", "3");
379    TestPortListeners<int>(1);
380    TestPortListeners<std::string>("test");
381    TestNetworkConnectionLoss<int>(4, 7);
382    TestNetworkConnectionLoss<std::string>("default_value", "published_value");
383    TestOutOfBoundsPublish();
384    TestHijackedPublishing<int>(42);
385    TestHijackedPublishing<std::string>("test");
386    TestGenericPorts<bool>(true, false);
387    TestGenericPorts<std::string>("123", "45");
388
389    tThreadLocalBufferManagement local_buffers;
390    TestPortChains();
391    TestPortQueues<int>(1, 2, 3);
392    TestPortListeners<int>(1);
393    TestNetworkConnectionLoss<int>(4, 7);
394    TestOutOfBoundsPublish();
395    TestHijackedPublishing<int>(42);
396    TestGenericPorts<bool>(true, false);
397  }
398
399  void PortPack()
400  {
401    auto parent = new core::tFrameworkElement(&core::tRuntimeEnvironment::GetInstance(), "TestPortPack");
402
403    using tTypeList = rrlib::util::tTypeList<int, double, std::string, bool>;
404    data_ports::tPortPack<tInputPort, tTypeList> ports(parent, "X");
405
406    RRLIB_UNIT_TESTS_EQUALITY(tTypeList::cSIZE, ports.NumberOfPorts());
407
408    for (size_t i = 0; i < tTypeList::cSIZE; ++i)
409    {
410      RRLIB_UNIT_TESTS_EQUALITY("X" + std::to_string(i + 1), ports.GetPort(i).GetName());
411    }
412
413    std::array<std::string, tTypeList::cSIZE> names {"foo", "bar", "baz", "fnord"};
414    data_ports::tPortPack<tInputPort, tTypeList> named_ports(parent, names.begin(), names.end());
415
416    for (size_t i = 0; i < tTypeList::cSIZE; ++i)
417    {
418      RRLIB_UNIT_TESTS_EQUALITY(names[i], named_ports.GetPort(i).GetName());
419    }
420  }
421};
422
423RRLIB_UNIT_TESTS_REGISTER_SUITE(DataPortsTestCollection);
424
425//----------------------------------------------------------------------
426// End of namespace declaration
427//----------------------------------------------------------------------
428}
429}
Note: See TracBrowser for help on using the repository browser.