channelelements_test.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * *
3  * This program is free software; you can redistribute it and/or modify *
4  * it under the terms of the GNU General Public License as published by *
5  * the Free Software Foundation; either version 2 of the License, or *
6  * (at your option) any later version. *
7  * *
8  ***************************************************************************/
9 
10 #include "unit.hpp"
11 #include <signal.h>
12 
13 #include <base/ChannelElement.hpp>
14 
15 using namespace std;
16 using namespace RTT;
17 using namespace RTT::base;
18 
19 // Registers the test suite into the 'registry'
20 BOOST_AUTO_TEST_SUITE( ChannelElementsTestSuite )
21 
22 BOOST_AUTO_TEST_CASE( testChannelElementConnection )
23 {
26 
27  BOOST_CHECK( !out->connected() );
28  BOOST_CHECK( !in->connected() );
29 
30  // connect from out to in
31  out->connectTo(in);
32  BOOST_CHECK_EQUAL( out->getOutput(), in );
33  BOOST_CHECK_EQUAL( in->getInput(), out );
34  BOOST_CHECK( out->connected() );
35  BOOST_CHECK( in->connected() );
36 
37  // disconnect out
38  out->disconnect(true);
39  BOOST_CHECK( !out->getOutput() );
40  BOOST_CHECK( !in->getInput() );
41  BOOST_CHECK( !out->connected() );
42  BOOST_CHECK( !in->connected() );
43 
44  // connect from in to out
45  in->connectFrom(out);
46  BOOST_CHECK_EQUAL( out->getOutput(), in );
47  BOOST_CHECK_EQUAL( in->getInput(), out );
48  BOOST_CHECK( out->connected() );
49  BOOST_CHECK( in->connected() );
50 
51  // disconnect in
52  in->disconnect(false);
53  BOOST_CHECK( !out->getOutput() );
54  BOOST_CHECK( !in->getInput() );
55  BOOST_CHECK( !out->connected() );
56  BOOST_CHECK( !in->connected() );
57 }
58 
59 BOOST_AUTO_TEST_CASE( testMultipleOutputsChannelElementConnection )
60 {
64 
65  BOOST_CHECK( !out->connected() );
66  BOOST_CHECK( !in1->connected() );
67  BOOST_CHECK( !in2->connected() );
68 
69  // connect from output to inputs
70  BOOST_CHECK( out->connectTo(in1) );
71  BOOST_CHECK( out->connectTo(in2) );
72  BOOST_CHECK( out->connected() );
73  BOOST_CHECK( in1->connected() );
74  BOOST_CHECK( in2->connected() );
75  BOOST_CHECK( !out->getOutput() );
76  BOOST_CHECK_EQUAL( in1->getInput(), out );
77  BOOST_CHECK_EQUAL( in2->getInput(), out );
78 
79  // disconnect output
80  out->disconnect(true);
81  BOOST_CHECK( !out->connected() );
82  BOOST_CHECK( !in1->connected() );
83  BOOST_CHECK( !in2->connected() );
84  BOOST_CHECK( !in1->getInput() );
85  BOOST_CHECK( !in2->getInput() );
86 
87  // connect from inputs to output
88  in1->connectFrom(out);
89  in2->connectFrom(out);
90  BOOST_CHECK( out->connected() );
91  BOOST_CHECK( in1->connected() );
92  BOOST_CHECK( in2->connected() );
93  BOOST_CHECK( !out->getOutput() );
94  BOOST_CHECK_EQUAL( in1->getInput(), out );
95  BOOST_CHECK_EQUAL( in2->getInput(), out );
96 
97  // add another output that writes into out
99  before_out->connectTo(out);
100 
101  // disconnect from inputs
102  in1->disconnect(out, false);
103  BOOST_CHECK( before_out->connected() );
104  BOOST_CHECK( out->connected() );
105  BOOST_CHECK( !in1->connected() );
106  BOOST_CHECK( in2->connected() );
107  in2->disconnect(out, false);
108  BOOST_CHECK( !out->connected() );
109  BOOST_CHECK( !in1->connected() );
110  BOOST_CHECK( !in2->connected() );
111 
112  // check that the disconnect call was forwarded to out's input after the last disconnect
113  BOOST_CHECK( !before_out->connected() );
114 }
115 
116 BOOST_AUTO_TEST_CASE( testMultipleInputsChannelElementConnection )
117 {
121 
122  BOOST_CHECK( !out1->connected() );
123  BOOST_CHECK( !out2->connected() );
124  BOOST_CHECK( !in->connected() );
125 
126  // connect from input to outputs
127  BOOST_CHECK( in->connectFrom(out1) );
128  BOOST_CHECK( in->connectFrom(out2) );
129  BOOST_CHECK( in->connected() );
130  BOOST_CHECK( out1->connected() );
131  BOOST_CHECK( out2->connected() );
132  BOOST_CHECK( !in->getInput() );
133  BOOST_CHECK_EQUAL( out1->getOutput(), in );
134  BOOST_CHECK_EQUAL( out2->getOutput(), in );
135 
136  // disconnect input
137  in->disconnect(false);
138  BOOST_CHECK( !in->connected() );
139  BOOST_CHECK( !out1->connected() );
140  BOOST_CHECK( !out2->connected() );
141  BOOST_CHECK( !out1->getOutput() );
142  BOOST_CHECK( !out2->getOutput() );
143 
144  // connect from outputs to input
145  out1->connectTo(in);
146  out2->connectTo(in);
147  BOOST_CHECK( out1->connected() );
148  BOOST_CHECK( out2->connected() );
149  BOOST_CHECK( in->connected() );
150  BOOST_CHECK( !in->getInput() );
151  BOOST_CHECK_EQUAL( out1->getOutput(), in );
152  BOOST_CHECK_EQUAL( out2->getOutput(), in );
153 
154  // add another input that reads from in
156  after_in->connectFrom(in);
157 
158  // disconnect from inputs
159  out1->disconnect(in, true);
160  BOOST_CHECK( !out1->connected() );
161  BOOST_CHECK( out2->connected() );
162  BOOST_CHECK( in->connected() );
163  BOOST_CHECK( after_in->connected() );
164  out2->disconnect(in, true);
165  BOOST_CHECK( !out1->connected() );
166  BOOST_CHECK( !out2->connected() );
167  BOOST_CHECK( !in->connected() );
168 
169  // check that the disconnect call was forwarded to in's output after the last disconnect
170  BOOST_CHECK( !after_in->connected() );
171 }
172 
173 BOOST_AUTO_TEST_CASE( testMultipleInputsMultipleOutputsChannelElementConnection )
174 {
180 
181  BOOST_CHECK( !out1->connected() );
182  BOOST_CHECK( !out2->connected() );
183  BOOST_CHECK( !mimo->connected() );
184  BOOST_CHECK( !in1->connected() );
185  BOOST_CHECK( !in2->connected() );
186 
187  // connect from mimo to inputs
188  BOOST_CHECK( mimo->connectFrom(out1) );
189  BOOST_CHECK( mimo->connectFrom(out2) );
190  BOOST_CHECK( !mimo->connected() ); // no outputs yet
191  BOOST_CHECK( out1->connected() );
192  BOOST_CHECK( out2->connected() );
193  BOOST_CHECK( !mimo->getInput() );
194  BOOST_CHECK_EQUAL( out1->getOutput(), mimo );
195  BOOST_CHECK_EQUAL( out2->getOutput(), mimo );
196 
197  // connect from mimo to outputs
198  BOOST_CHECK( mimo->connectTo(in1) );
199  BOOST_CHECK( mimo->connected() );
200  BOOST_CHECK( mimo->connectTo(in2) );
201  BOOST_CHECK( in1->connected() );
202  BOOST_CHECK( in2->connected() );
203  BOOST_CHECK( !mimo->getOutput() );
204  BOOST_CHECK_EQUAL( in1->getInput(), mimo );
205  BOOST_CHECK_EQUAL( in2->getInput(), mimo );
206 
207  // disconnect inputs
208  mimo->disconnect(false);
209  BOOST_CHECK( !out1->connected() );
210  BOOST_CHECK( !out2->connected() );
211  BOOST_CHECK( !mimo->connected() );
212  BOOST_CHECK( !mimo->MultipleInputsChannelElementBase::connected() );
213  BOOST_CHECK( mimo->MultipleOutputsChannelElementBase::connected() );
214  BOOST_CHECK( in1->connected() );
215  BOOST_CHECK( in2->connected() );
216 
217  // disconnect outputs
218  mimo->disconnect(true);
219  BOOST_CHECK( !mimo->connected() );
220  BOOST_CHECK( !in1->connected() );
221  BOOST_CHECK( !in2->connected() );
222 
223  // connect from outputs and inputs to mimo
224  out1->connectTo(mimo);
225  out2->connectTo(mimo);
226  BOOST_CHECK( !mimo->connected() ); // no outputs yet
227  in1->connectFrom(mimo);
228  BOOST_CHECK( mimo->connected() );
229  in2->connectFrom(mimo);
230  BOOST_CHECK( out1->connected() );
231  BOOST_CHECK( out2->connected() );
232  BOOST_CHECK( in1->connected() );
233  BOOST_CHECK( in2->connected() );
234  BOOST_CHECK_EQUAL( out1->getOutput(), mimo );
235  BOOST_CHECK_EQUAL( out2->getOutput(), mimo );
236  BOOST_CHECK_EQUAL( in1->getInput(), mimo );
237  BOOST_CHECK_EQUAL( in2->getInput(), mimo );
238 
239  // disconnect from inputs and outputs
240  out1->disconnect(mimo, true);
241  BOOST_CHECK( !out1->connected() );
242  BOOST_CHECK( out2->connected() );
243  BOOST_CHECK( mimo->connected() );
244  BOOST_CHECK( in1->connected() );
245  BOOST_CHECK( in2->connected() );
246  in1->disconnect(mimo, false);
247  BOOST_CHECK( !out1->connected() );
248  BOOST_CHECK( out2->connected() );
249  BOOST_CHECK( mimo->connected() );
250  BOOST_CHECK( !in1->connected() );
251  BOOST_CHECK( in2->connected() );
252  out2->disconnect(mimo, true);
253  BOOST_CHECK( !out1->connected() );
254  BOOST_CHECK( !out2->connected() );
255  BOOST_CHECK( !mimo->connected() );
256 
257  // check that the disconnect call was forwarded to mimo's outputs after the last input has been disconnected
258  BOOST_CHECK( !in2->connected() );
259 }
260 
261 BOOST_AUTO_TEST_CASE( testDisconnectOnInvalidWrite )
262 {
268  int x = 0;
269 
270  // 1. NON-MANDATORY connections: out->in1->data1 and out->in2->data2
271  BOOST_CHECK( out->connectTo(in1, /* mandatory = */ false) );
272  BOOST_CHECK( out->connectTo(in2, /* mandatory = */ false) );
273  BOOST_CHECK( in1->connectTo(data1) );
274  BOOST_CHECK( in2->connectTo(data2) );
275  BOOST_CHECK_EQUAL( out->write(1), WriteSuccess );
276  BOOST_CHECK( data1->read(x) == NewData && x == 1 );
277  BOOST_CHECK( data2->read(x) == NewData && x == 1 );
278 
279  // disconnect in2 without backward propagation. This invalidates in2.
280  in2->disconnect(true);
281  BOOST_CHECK( out->connected() );
282  BOOST_CHECK( in1->connected() );
283  BOOST_CHECK( !in2->connected() );
284  BOOST_CHECK_EQUAL( out->write(2), WriteSuccess ); // in2 is not mandatory
285  BOOST_CHECK( data1->read(x) == NewData && x == 2 );
286  BOOST_CHECK( data2->read(x) == OldData && x == 1 );
287  BOOST_CHECK( out->connected() );
288  BOOST_CHECK( in1->connected() );
289 
290  // additionally disconnect in1 without backward propagation. This also invalidates in1.
291  in1->disconnect(true);
292  BOOST_CHECK( out->connected() );
293  BOOST_CHECK( !in1->connected() );
294  BOOST_CHECK_EQUAL( out->write(3), NotConnected ); // in1 is not mandatory, but it was the last connected output
295  BOOST_CHECK( data1->read(x) == OldData && x == 2 );
296  BOOST_CHECK( data2->read(x) == OldData && x == 1 );
297  BOOST_CHECK( !out->connected() );
298 
299  // 2. MANDATORY connections: out->in1->data1 and out->in2->data2
300  BOOST_CHECK( out->connectTo(in1, /* mandatory = */ true) );
301  BOOST_CHECK( out->connectTo(in2, /* mandatory = */ true) );
302  BOOST_CHECK( in1->connectTo(data1) );
303  BOOST_CHECK( in2->connectTo(data2) );
304  BOOST_CHECK_EQUAL( out->write(1), WriteSuccess );
305  BOOST_CHECK( data1->read(x) == NewData && x == 1 );
306  BOOST_CHECK( data2->read(x) == NewData && x == 1 );
307 
308  // disconnect in2 without backward propagation. This invalidates in2.
309  in2->disconnect(true);
310  BOOST_CHECK( out->connected() );
311  BOOST_CHECK( in1->connected() );
312  BOOST_CHECK( !in2->connected() );
313  BOOST_CHECK_EQUAL( out->write(2), NotConnected );
314  BOOST_CHECK( data1->read(x) == NewData && x == 2 );
315  BOOST_CHECK( data2->read(x) == OldData && x == 1 );
316  BOOST_CHECK( out->connected() );
317  BOOST_CHECK( in1->connected() );
318 
319  // additionally disconnect in1 without backward propagation. This also invalidates in1.
320  in1->disconnect(true);
321  BOOST_CHECK( out->connected() );
322  BOOST_CHECK( !in1->connected() );
323  BOOST_CHECK_EQUAL( out->write(3), NotConnected );
324  BOOST_CHECK( data1->read(x) == OldData && x == 2 );
325  BOOST_CHECK( data2->read(x) == OldData && x == 1 );
326  BOOST_CHECK( !out->connected() );
327 }
328 
boost::intrusive_ptr< ChannelElement< T > > shared_ptr
#define BOOST_AUTO_TEST_SUITE(suite_name)
boost::shared_ptr< DataObjectInterface< T > > shared_ptr
boost::intrusive_ptr< MultipleInputsMultipleOutputsChannelElement< T > > shared_ptr
#define BOOST_AUTO_TEST_SUITE_END()
Definition: mystd.hpp:163
virtual bool disconnect(ChannelElementBase::shared_ptr const &channel, bool forward)
virtual bool disconnect(ChannelElementBase::shared_ptr const &channel, bool forward=true)
virtual bool connectTo(ChannelElementBase::shared_ptr const &output, bool mandatory=true)
virtual void disconnect(bool forward)
virtual FlowStatus read(reference_t sample, bool copy_old_data=true)
BOOST_AUTO_TEST_CASE(testChannelElementConnection)
virtual bool connectFrom(ChannelElementBase::shared_ptr const &input)
virtual WriteStatus write(param_t sample)
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:53
boost::intrusive_ptr< MultipleInputsChannelElement< T > > shared_ptr


rtt
Author(s): RTT Developers
autogenerated on Fri Oct 25 2019 03:59:31