operation_test.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: The SourceWorks Tue Sep 7 00:54:57 CEST 2010 operation_test.cpp
3 
4  operation_test.cpp - description
5  -------------------
6  begin : Tue September 07 2010
7  copyright : (C) 2010 The SourceWorks
8  email : peter@thesourceworks.com
9 
10  ***************************************************************************
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * *
17  ***************************************************************************/
18 
19 
20 #include "unit.hpp"
21 
22 #include <rtt-fwd.hpp>
23 #include <rtt/Operation.hpp>
24 #include <rtt/Service.hpp>
25 #include <rtt/OperationCaller.hpp>
26 #include <rtt/TaskContext.hpp>
27 
28 using namespace std;
29 using namespace RTT::detail;
30 using namespace RTT;
31 
38 {
39 public:
40  OperationTest() : tc("TC"),
41  op0r("op0r"), op0cr("op0cr"), op1r("op1r"), op1cr("op1cr"), op0("op0"), op1("op1"), op2("op2"), op3("op3"), op4("op4"), op5("op5"),
42  opc0r("op0r"), opc0cr("op0cr"), opc1r("op1r"), opc1cr("op1cr"), opc0("op0"), opc1("op1"), opc2("op2"), opc3("op3"), opc4("op4"), opc5("op5")
43  {
44 
45  tc.provides()->addOperation("op", &OperationTest::func, this);
46  tc.provides()->addOperation("op0", &OperationTest::func0, this);
47  tc.provides()->addOperation("op1", &OperationTest::func1, this);
48  tc.provides()->addOperation("op2", &OperationTest::func2, this);
49  tc.provides()->addOperation("op3", &OperationTest::func3, this);
50  tc.provides()->addOperation("op4", &OperationTest::func4, this);
51  tc.provides()->addOperation("op5", &OperationTest::func5, this);
52  BOOST_CHECK( tc.provides()->getOperation("op0") );
53  BOOST_CHECK( tc.provides()->getOperation("op1") );
54  BOOST_CHECK( tc.provides()->getOperation("op2") );
55  BOOST_CHECK( tc.provides()->getOperation("op3") );
56  BOOST_CHECK( tc.provides()->getOperation("op4") );
57  BOOST_CHECK( tc.provides()->getOperation("op5") );
58 
59  tc.provides()->addOperation("op0r", &OperationTest::func0r, this);
60 
61  tc.provides()->addOperation("op1r", &OperationTest::func1r, this);
62  tc.provides()->addOperation("op1cr", &OperationTest::func1cr, this);
63  }
64 
66 
67  }
68  double ret;
69  double& func0r(void) { return ret; }
70  const double& func0cr(void) { return ret; }
71 
72  double func1r(double& a) { a = 2*a; return a; }
73  double func1cr(const double& a) { return a; }
74 
75  // plain argument tests:
76  void func() { return; }
77  double func0() { return 1.0; }
78  double func1(int i) { return 2.0; }
79  double func2(int i, double d) { return 3.0; }
80  double func3(int i, double d, bool c) { return 4.0; }
81  double func4(int i, double d, bool c, std::string s) { return 5.0; }
82  double func5(int i, double d, bool c, std::string s, float f) { return 6.0; }
83 
84  // The return values of signals are intentionally distinct than these above.
85  double sig0() { return sig=-1.0; }
86  double sig1(int i) { return (sig=-2.0); }
87  double sig2(int i, double d) { return sig=-3.0; }
88  double sig3(int i, double d, bool c) { return (sig=-4.0); }
89  double sig4(int i, double d, bool c, std::string s) { return sig=-5.0; }
90  double sig5(int i, double d, bool c, std::string s, float f) { return sig=-6.0; }
91 
92  static double freefunc0(void) { return 1.0; }
93  static double freefunc1(int i) { return 2.0; }
94 
96 
97  // check value for signals
98  double sig;
99 
110 
121 };
122 
123 // Registers the fixture into the 'registry'
124 BOOST_FIXTURE_TEST_SUITE( OperationTestSuite, OperationTest )
125 
126 // Test default properties of an operation.
127 BOOST_AUTO_TEST_CASE( testOperationCreate )
128 {
129  // name
130  BOOST_CHECK( op0.getName() == "op0" );
131  // not in interface, not ready.
132  BOOST_CHECK( op0.ready() == false);
133 
134 }
135 
136 // Test adding and calling an operation (internal API)
137 BOOST_AUTO_TEST_CASE( testOperationCall )
138 {
139  Service::shared_ptr s = boost::make_shared<Service>("Service");
140 
141  tc.provides()->addService( s );
142 
143  op0.calls( &OperationTest::func0, this);
144  op1.calls( &OperationTest::func1, this);
145  op2.calls( &OperationTest::func2, this);
146  op3.calls( &OperationTest::func3, this);
147  op4.calls( &OperationTest::func4, this);
148  op5.calls( &OperationTest::func5, this);
149 
150  s->addOperation( op0 );
151  s->addOperation( op1 );
152  s->addOperation( op2 );
153  s->addOperation( op3 );
154  s->addOperation( op4 );
155  s->addOperation( op5 );
156 
157  // Test calling an operation using a OperationCaller.
159  BOOST_CHECK( m0.ready() == false );
160  m0 = op0.getImplementation();
161  BOOST_CHECK( m0.ready() );
162  BOOST_CHECK_EQUAL( 1.0, m0.call() );
163 }
164 
165 // Test calling an operation (user API)
166 BOOST_AUTO_TEST_CASE( testOperationCall2 )
167 {
168  Service::shared_ptr s = boost::make_shared<Service>("Service");
169 
170  tc.provides()->addService( s );
171 
172  s->addOperation("op0", &OperationTest::func0, this);
173  s->addOperation("op1", &OperationTest::func1, this);
174  s->addOperation("op2", &OperationTest::func2, this);
175  s->addOperation("op3", &OperationTest::func3, this);
176  s->addOperation("op4", &OperationTest::func4, this);
177  s->addOperation("op5", &OperationTest::func5, this);
178 
179  // Test calling an operation using a OperationCaller.
181  BOOST_CHECK( m0.ready() == false );
182  m0 = s->getOperation("op0");
183  BOOST_CHECK( m0.ready() );
184  BOOST_CHECK_EQUAL( 1.0, m0.call() );
185 }
186 
187 // Test adding a C++ function to the services.
188 BOOST_AUTO_TEST_CASE( testOperationAddCpp )
189 {
190  // Add to custom service:
191  Service::shared_ptr s = boost::make_shared<Service>("Service");
192  tc.provides()->addService( s );
193  s->addOperation("top0", &OperationTest::func0, this);
194  s->addOperation("top1", &OperationTest::func1, this);
195  BOOST_CHECK( s->getOperation("top0") );
196  BOOST_CHECK( s->getOperation("top1") );
197  opc0 = s->getOperation("top0");
198  opc1 = s->getOperation("top1");
199  BOOST_CHECK( opc0.ready() );
200  BOOST_CHECK( opc1.ready() );
201  BOOST_CHECK_EQUAL(opc0(), 1.0);
202  BOOST_CHECK_EQUAL(opc1(1), 2.0);
203 
204  // Add to default service:
205  tc.addOperation("tcop0", &OperationTest::func0, this);
206  tc.addOperation("tcop1", &OperationTest::func1, this);
207  BOOST_CHECK( tc.provides()->getOperation("tcop0") );
208  BOOST_CHECK( tc.provides()->getOperation("tcop1") );
209  opc0 = tc.provides()->getOperation("tcop0");
210  opc1 = tc.provides()->getOperation("tcop1");
211  BOOST_CHECK( opc0.ready() );
212  BOOST_CHECK( opc1.ready() );
213  BOOST_CHECK_EQUAL(opc0(), 1.0);
214  BOOST_CHECK_EQUAL(opc1(1), 2.0);
215 
216  // Override existing ops:
217  // note: we add different signature with same name to check if new op was used
218  tc.addOperation("tcop0", &OperationTest::func1, this);
219  tc.addOperation("tcop1", &OperationTest::func2, this);
220  BOOST_CHECK( tc.provides()->getOperation("tcop0") );
221  BOOST_CHECK( tc.provides()->getOperation("tcop1") );
222  opc1 = tc.provides()->getOperation("tcop0");
223  opc2 = tc.provides()->getOperation("tcop1");
224  BOOST_CHECK( opc1.ready() );
225  BOOST_CHECK( opc2.ready() );
226  BOOST_CHECK_EQUAL(opc1(1), 2.0);
227  BOOST_CHECK_EQUAL(opc2(1,2.0), 3.0);
228 }
229 
230 // Test adding a C function to the services.
231 BOOST_AUTO_TEST_CASE( testOperationAddC )
232 {
233  // Using custom Service
234  Service::shared_ptr s = boost::make_shared<Service>("Service");
235  tc.provides()->addService( s );
236  s->addOperation("top0", &OperationTest::freefunc0);
237  s->addOperation("top1", &OperationTest::freefunc1);
238  BOOST_CHECK( s->getOperation("top0") );
239  BOOST_CHECK( s->getOperation("top1") );
240  opc0 = s->getOperation("top0");
241  opc1 = s->getOperation("top1");
242  BOOST_CHECK( opc0.ready() );
243  BOOST_CHECK( opc1.ready() );
244  BOOST_CHECK_EQUAL(opc0(), 1.0);
245  BOOST_CHECK_EQUAL(opc1(1), 2.0);
246 
247  // Using TaskContext API
248  tc.addOperation("top0", &OperationTest::freefunc0);
249  tc.addOperation("top1", &OperationTest::freefunc1);
250  BOOST_CHECK( tc.getOperation("top0") );
251  BOOST_CHECK( tc.getOperation("top1") );
252  opc0 = tc.getOperation("top0");
253  opc1 = tc.getOperation("top1");
254  BOOST_CHECK( opc0.ready() );
255  BOOST_CHECK( opc1.ready() );
256  BOOST_CHECK_EQUAL(opc0(), 1.0);
257  BOOST_CHECK_EQUAL(opc1(1), 2.0);
258 }
259 
260 // Test calling an operation of the default service.
261 BOOST_AUTO_TEST_CASE( testOperationCall0 )
262 {
263  // Test calling an operation using a OperationCaller.
265  BOOST_CHECK( m0.ready() == false );
266  m0 = tc.getOperation("op0");
267  BOOST_CHECK( m0.ready() );
268  BOOST_CHECK_EQUAL( 1.0, m0.call() );
269 }
270 
271 #ifdef ORO_SIGNALLING_OPERATIONS
272 // Test adding and signalling an operation without an implementation
273 BOOST_AUTO_TEST_CASE( testOperationSignal )
274 {
275  // invoke sig0 according to call/thread specification.
276  Handle h = op0.signals(&OperationTest::sig0, this);
277 
278  BOOST_CHECK( h.connected() );
279  // Test signal when calling a method:
281  BOOST_CHECK( m0.ready() == false );
282  m0 = op0.getImplementation();
283  BOOST_CHECK( m0.ready() );
284 
285  try {
286  m0.call(); // return unspecified number (no implementation!).
287  } catch (SendStatus sf) {
288  BOOST_FAIL("Should not throw a SendFailure!");
289  }
290 
291  BOOST_CHECK_EQUAL( -1.0, sig );
292 
293  // Test direct calling of op.signal
294  sig = 0.0;
295  op0.signal->emit();
296  BOOST_CHECK_EQUAL( -1.0, sig );
297 }
298 
299 BOOST_AUTO_TEST_CASE( testOperationCallAndSignal )
300 {
301  // invoke sig0 according to call/thread specification.
302  Handle h = op0.signals(&OperationTest::sig0, this);
303  op0.calls(&OperationTest::func0, this);
304 
305  BOOST_CHECK( h.connected() );
306  // Test signal when calling a method:
308  BOOST_CHECK( m0.ready() == false );
309  m0 = op0.getImplementation();
310  BOOST_CHECK( m0.ready() );
311 
312  BOOST_CHECK_EQUAL( 1.0, m0.call() ); // return result of func0().
313 
314  BOOST_CHECK_EQUAL( -1.0, sig );
315 
316  // Test direct calling of op.signal
317  sig = 0.0;
318  op0.signal->emit();
319  BOOST_CHECK_EQUAL( -1.0, sig );
320 }
321 #endif
323 
double func1cr(const double &a)
double sig1(int i)
OperationCaller< const double &(void)> opc0cr
#define BOOST_FIXTURE_TEST_SUITE(suite_name, F)
OperationCaller< double &(void)> opc0r
BOOST_AUTO_TEST_CASE(testOperationCreate)
OperationCaller< double(int, double, bool, std::string)> opc4
#define BOOST_AUTO_TEST_SUITE_END()
Definition: mystd.hpp:163
bool connected() const
Definition: Handle.cpp:79
Operation< double(int, double, bool, std::string, float)> op5
static double freefunc1(int i)
TaskContext tc
OperationCaller< double(int, double, bool, std::string, float)> opc5
double func1(int i)
double func1r(double &a)
Operation< double(double &)> op1r
double func2(int i, double d)
OperationCaller< double(int, double, bool)> opc3
double func4(int i, double d, bool c, std::string s)
Operation< double &(const double &)> op1cr
SendStatus
Definition: SendStatus.hpp:53
boost::shared_ptr< Service > shared_ptr
Definition: Service.hpp:101
OperationCaller< double(double &)> opc1r
static double freefunc0(void)
Operation< const double &(void)> op0cr
double & func0r(void)
double sig4(int i, double d, bool c, std::string s)
Operation< double(int, double, bool)> op3
double sig5(int i, double d, bool c, std::string s, float f)
double func5(int i, double d, bool c, std::string s, float f)
Operation< double(int)> op1
OperationCaller< double &(const double &)> opc1cr
double sig3(int i, double d, bool c)
const double & func0cr(void)
OperationCaller< double(int, double)> opc2
Operation< double(int, double)> op2
Operation< double(int, double, bool, std::string)> op4
OperationCaller< double(int)> opc1
OperationCaller< double(void)> opc0
Operation< double(void)> op0
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:53
double sig2(int i, double d)
Operation< double &(void)> op0r
The Handle holds the information, and allows manipulation, of a connection between a internal::Signal...
Definition: Handle.hpp:66
double func3(int i, double d, bool c)


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