source: finroc_plugins_data_ports/tests/test_collection.cpp @ 112:4756cdcbb205

14.08
Last change on this file since 112:4756cdcbb205 was 112:4756cdcbb205, checked in by Tobias Föhst <foehst@…>, 6 years ago

Adds test for port packs

File size: 10.4 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
241class DataPortsTestCollection : public rrlib::util::tUnitTestSuite
242{
243  RRLIB_UNIT_TESTS_BEGIN_SUITE(DataPortsTestCollection);
244  RRLIB_UNIT_TESTS_ADD_TEST(Test);
245  RRLIB_UNIT_TESTS_ADD_TEST(PortPack);
246  RRLIB_UNIT_TESTS_END_SUITE;
247
248  void Test()
249  {
250    TestPortChains();
251    TestPortQueues<int>(1, 2, 3);
252    TestPortQueues<std::string>("1", "2", "3");
253    TestPortListeners<int>(1);
254    TestPortListeners<std::string>("test");
255
256    tThreadLocalBufferManagement local_buffers;
257    TestPortChains();
258    TestPortQueues<int>(1, 2, 3);
259    TestPortListeners<int>(1);
260  }
261
262  void PortPack()
263  {
264    auto parent = new core::tFrameworkElement(&core::tRuntimeEnvironment::GetInstance(), "TestPortPack");
265
266    using tTypeList = rrlib::util::tTypeList<int, double, std::string, bool>;
267    data_ports::tPortPack<tInputPort, tTypeList> ports(parent, "X");
268
269    RRLIB_UNIT_TESTS_EQUALITY(tTypeList::cSIZE, ports.NumberOfPorts());
270
271    for (size_t i = 0; i < tTypeList::cSIZE; ++i)
272    {
273      RRLIB_UNIT_TESTS_EQUALITY("X" + std::to_string(i + 1), ports.GetPort(i).GetName());
274    }
275
276    std::array<std::string, tTypeList::cSIZE> names {"foo", "bar", "baz", "fnord"};
277    data_ports::tPortPack<tInputPort, tTypeList> named_ports(parent, names.begin(), names.end());
278
279    for (size_t i = 0; i < tTypeList::cSIZE; ++i)
280    {
281      RRLIB_UNIT_TESTS_EQUALITY(names[i], named_ports.GetPort(i).GetName());
282    }
283  }
284};
285
286RRLIB_UNIT_TESTS_REGISTER_SUITE(DataPortsTestCollection);
287
288//----------------------------------------------------------------------
289// End of namespace declaration
290//----------------------------------------------------------------------
291}
292}
Note: See TracBrowser for help on using the repository browser.