source: finroc_plugins_data_ports/tests/test_collection.cpp @ 68:556f9c02bfb0

Last change on this file since 68:556f9c02bfb0 was 68:556f9c02bfb0, checked in by Max Reichardt <mreichardt@…>, 8 years ago

Compile fix for clang

File size: 9.5 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
47//----------------------------------------------------------------------
48// Debugging
49//----------------------------------------------------------------------
50#include <cassert>
51
52//----------------------------------------------------------------------
53// Namespace usage
54//----------------------------------------------------------------------
55
56//----------------------------------------------------------------------
57// Namespace declaration
58//----------------------------------------------------------------------
59namespace finroc
60{
61namespace data_ports
62{
63
64//----------------------------------------------------------------------
65// Forward declarations / typedefs / enums
66//----------------------------------------------------------------------
67
68//----------------------------------------------------------------------
69// Const values
70//----------------------------------------------------------------------
71
72//----------------------------------------------------------------------
73// Implementation
74//----------------------------------------------------------------------
75
76void TestPortChains()
77{
78  FINROC_LOG_PRINT(DEBUG_VERBOSE_1, "\nTesting forwarding data among port chains");
79  core::tFrameworkElement* parent = new core::tFrameworkElement(&core::tRuntimeEnvironment::GetInstance(), "TestPortChains");
80
81  // Create ports
82  tOutputPort<std::string> output_port1("Output Port 1", parent);
83  tOutputPort<std::string> output_port2("Output Port 2", parent);
84  tOutputPort<std::string> output_port3("Output Port 3", parent);
85  tProxyPort<std::string, true> proxy_port1("Proxy Port 1", parent);
86  tProxyPort<std::string, true> proxy_port2("Proxy Port 2", parent);
87  tProxyPort<std::string, true> proxy_port3("Proxy Port 3", parent);
88  tInputPort<std::string> input_port1("Input Port 1", parent);
89  tInputPort<std::string> input_port2("Input Port 2", parent);
90  tInputPort<std::string> input_port3("Input Port 3", parent);
91
92  // Connect ports
93  output_port1.ConnectTo(proxy_port1);
94  output_port2.ConnectTo(proxy_port2);
95  output_port3.ConnectTo(proxy_port3);
96  proxy_port1.ConnectTo(input_port1);
97  proxy_port2.ConnectTo(input_port2);
98  proxy_port3.ConnectTo(input_port3);
99  parent->Init();
100
101  std::string test_string = "12345";
102  for (int i = 0; i < 20; i++)
103  {
104    // Publish data
105    tPortDataPointer<std::string> unused_buffer = output_port1.GetUnusedBuffer();
106    std::string test_string2 = "Test" + std::to_string(i);
107    *unused_buffer = test_string2;
108    output_port1.Publish(unused_buffer);
109
110    // Forward data to second and third chain
111    output_port2.Publish(input_port1.GetPointer());
112    output_port3.Publish(input_port2.GetPointer());
113    RRLIB_UNIT_TESTS_ASSERT(test_string2 == *input_port3.GetPointer());
114
115    if (i > 10)
116    {
117      output_port2.Publish(test_string);
118      output_port3.Publish(input_port2.GetPointer());
119      RRLIB_UNIT_TESTS_ASSERT(test_string == *input_port3.GetPointer());
120      RRLIB_UNIT_TESTS_ASSERT(test_string == *input_port2.GetPointer());
121      RRLIB_UNIT_TESTS_ASSERT(test_string2 == *input_port1.GetPointer());
122    }
123  }
124
125  parent->ManagedDelete();
126}
127
128template <typename T>
129void TestPortQueues(const T& value1, const T& value2, const T& value3)
130{
131  FINROC_LOG_PRINT(DEBUG_VERBOSE_1, "\nTesting port queue basic operation for type ", (rrlib::rtti::tDataType<T>()).GetName());
132  core::tFrameworkElement* parent = new core::tFrameworkElement(&core::tRuntimeEnvironment::GetInstance(), "TestPortQueue");
133
134  tOutputPort<T> output_port("Output Port", parent);
135  tInputPort<T> input_port_fifo("Input Port FIFO", parent, tQueueSettings(false));
136  tInputPort<T> input_port_all("Input Port ALL", parent, tQueueSettings(true));
137  output_port.ConnectTo(input_port_fifo);
138  output_port.ConnectTo(input_port_all);
139  parent->Init();
140
141  FINROC_LOG_PRINT(DEBUG_VERBOSE_1, " Enqueueing three values");
142  output_port.Publish(value1);
143  output_port.Publish(value2);
144  output_port.Publish(value3);
145
146  FINROC_LOG_PRINT(DEBUG_VERBOSE_1, " Dequeueing five values FIFO");
147  for (size_t i = 0; i < 5; ++i)
148  {
149    tPortDataPointer<const T> result = input_port_fifo.Dequeue();
150    if (result)
151    {
152      FINROC_LOG_PRINT(DEBUG_VERBOSE_1, "  Dequeued ", *result);
153    }
154    else
155    {
156      FINROC_LOG_PRINT(DEBUG_VERBOSE_1, "  Dequeued nothing");
157    }
158    RRLIB_UNIT_TESTS_ASSERT((i == 0 && value1 == *result) || (i == 1 && value2 == *result) || (i == 2 && value3 == *result) || (i > 2 && (!result)));
159  }
160
161  FINROC_LOG_PRINT(DEBUG_VERBOSE_1, " Dequeueing all values at once");
162  tPortBuffers<tPortDataPointer<const T>> dequeued = input_port_all.DequeueAllBuffers();
163  size_t i = 0;
164  while (!dequeued.Empty())
165  {
166    T result = *dequeued.PopFront();
167    FINROC_LOG_PRINT(DEBUG_VERBOSE_1, "  Dequeued ", result);
168    RRLIB_UNIT_TESTS_ASSERT((i == 0 && value1 == result) || (i == 1 && value2 == result) || (i == 2 && value3 == result));
169    i++;
170  }
171
172  parent->ManagedDelete();
173};
174
175
176
177template <typename T>
178void TestPortListeners(const T& publish_value)
179{
180  class tListener
181  {
182  public:
183    void OnPortChange(const T& value, tChangeContext& change_context)
184    {
185      FINROC_LOG_PRINT(DEBUG_VERBOSE_1, "  Port Changed: ", value);
186      this->value1 = value;
187      this->calls++;
188    }
189    void OnPortChange(tPortDataPointer<const T>& value, tChangeContext& change_context)
190    {
191      FINROC_LOG_PRINT(DEBUG_VERBOSE_1, "  Port Changed (tPortDataPointer): ", *value);
192      this->value2 = *value;
193      this->calls++;
194    }
195    void OnPortChange(const rrlib::rtti::tGenericObject& value, tChangeContext& change_context)
196    {
197      FINROC_LOG_PRINT(DEBUG_VERBOSE_1, "  Port Changed Generic: ", value);
198      this->calls++;
199    }
200    void OnPortChange(tPortDataPointer<const rrlib::rtti::tGenericObject>& value, tChangeContext& change_context)
201    {
202      FINROC_LOG_PRINT(DEBUG_VERBOSE_1, "  Port Changed Generic (tPortDataPointer): ", *value);
203      this->calls++;
204    }
205    void OnPortChange(tChangeContext& change_context)
206    {
207      FINROC_LOG_PRINT(DEBUG_VERBOSE_1, "  Port Changed Simple");
208      this->calls++;
209    }
210
211    T value1, value2;
212    size_t calls;
213
214    tListener() : value1(), value2(), calls(0) {}
215  };
216
217  FINROC_LOG_PRINT(DEBUG_VERBOSE_1, "\nTesting port listeners for type ", (rrlib::rtti::tDataType<T>()).GetName());
218  tListener listener;
219  core::tFrameworkElement* parent = new core::tFrameworkElement(&core::tRuntimeEnvironment::GetInstance(), "TestPortListeners");
220
221  tOutputPort<T> output_port("Output Port", parent);
222  tInputPort<T> input_port("Input Port", parent);
223  output_port.ConnectTo(input_port);
224  input_port.AddPortListener(listener);
225  input_port.AddPortListenerForPointer(listener);
226  input_port.AddPortListenerSimple(listener);
227  tGenericPort generic_input_port = tGenericPort::Wrap(*input_port.GetWrapped());
228  generic_input_port.AddPortListener(listener);
229  generic_input_port.AddPortListenerForPointer(listener);
230  generic_input_port.AddPortListenerSimple(listener);
231  parent->Init();
232
233  output_port.Publish(publish_value);
234
235  RRLIB_UNIT_TESTS_ASSERT(listener.value1 == publish_value && listener.value2 == publish_value && listener.calls == 6);
236
237  parent->ManagedDelete();
238}
239
240class DataPortsTestCollection : public rrlib::util::tUnitTestSuite
241{
242  RRLIB_UNIT_TESTS_BEGIN_SUITE(DataPortsTestCollection);
243  RRLIB_UNIT_TESTS_ADD_TEST(Test);
244  RRLIB_UNIT_TESTS_END_SUITE;
245
246  void Test()
247  {
248    TestPortChains();
249    TestPortQueues<int>(1, 2, 3);
250    TestPortQueues<std::string>("1", "2", "3");
251    TestPortListeners<int>(1);
252    TestPortListeners<std::string>("test");
253
254    tThreadLocalBufferManagement local_buffers;
255    TestPortChains();
256    TestPortQueues<int>(1, 2, 3);
257    TestPortListeners<int>(1);
258  }
259};
260
261RRLIB_UNIT_TESTS_REGISTER_SUITE(DataPortsTestCollection);
262
263//----------------------------------------------------------------------
264// End of namespace declaration
265//----------------------------------------------------------------------
266}
267}
Note: See TracBrowser for help on using the repository browser.