generictask_test.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Mon Jun 26 13:26:02 CEST 2006 generictask_test.cpp
3 
4  generictask_test.cpp - description
5  -------------------
6  begin : Mon June 26 2006
7  copyright : (C) 2006 Peter Soetens
8  email : peter.soetens@fmtc.be
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 #include "generictask_test.hpp"
23 #include <OperationCaller.hpp>
24 #include <Service.hpp>
26 
29 
30 #include <boost/function_types/function_type.hpp>
31 
32 #include <rtt-config.h>
33 #include <Service.hpp>
34 
35 using namespace std;
36 
37 void
39 {
40  tc = new TaskContext( "root" );
41  tc->addService( this->createOperationCallerFactory() );
42  tsim = new SimulationActivity(0.001, tc->engine() );
43 }
44 
45 
46 void
48 {
49 // if ( tc->getPeer("programs") )
50 // delete tc->getPeer("programs");
51  tsim->stop();
52  SimulationThread::Instance()->stop();
53  delete tc;
54  delete tsim;
55 }
56 
58  return b;
59 }
60 
62 {
63  Service* to = new Service("methods");
64 
65  to->addOperation("assert", &Generic_TaskTest::assertBool, this).doc("assert").arg("b", "bd");
66 
67  to->addOperation("m0", &Generic_TaskTest::m0, this).doc("M0");
68  to->addOperation("m1", &Generic_TaskTest::m1, this).doc("M1").arg("a", "ad");
69  to->addOperation("m2", &Generic_TaskTest::m2, this).doc("M2").arg("a", "ad").arg("a", "ad");
70  to->addOperation("m3", &Generic_TaskTest::m3, this).doc("M3").arg("a", "ad").arg("a", "ad").arg("a", "ad");
71  to->addOperation("m4", &Generic_TaskTest::m4, this).doc("M4").arg("a", "ad").arg("a", "ad").arg("a", "ad").arg("a", "ad");
72  return to;
73 }
74 
75 // Registers the fixture into the 'registry'
76 BOOST_FIXTURE_TEST_SUITE( Generic_TaskTestSuite, Generic_TaskTest )
77 
78 
79 BOOST_AUTO_TEST_CASE(testRemoteOperationCaller)
80 {
82  boost::shared_ptr<ActionInterface> implementation( new detail::RemoteOperationCaller<double(void)>(tc->provides("methods"),"m0") );
83  m0 = implementation;
84  BOOST_CHECK( m0.ready() );
85 
87  implementation.reset( new detail::RemoteOperationCaller<double(int)>(tc->provides("methods"),"m1") );
88  m1 = implementation;
89  BOOST_CHECK( m1.ready() );
90 
91  BOOST_CHECK_EQUAL( -2.0, m1(1) );
92  BOOST_CHECK_EQUAL( -1.0, m0() );
93 }
94 
95 BOOST_AUTO_TEST_CASE(testOperationCallersC)
96 {
98  double r = 0.0;
99  mc = tc->provides("methods")->create("m0").ret( r );
100  BOOST_CHECK( mc.execute() );
101  BOOST_CHECK( r == -1.0 );
102 
103  mc = tc->provides("methods")->create("m2").argC(1).argC(1.0).ret( r );
104  BOOST_CHECK( mc.execute() );
105  BOOST_CHECK( r == -3.0 );
106 
107  mc = tc->provides("methods")->create("m3").ret( r ).argC(1).argC(1.0).argC(true);
108  BOOST_CHECK( mc.execute() );
109  BOOST_CHECK( r == -4.0 );
110 
111 #if 0
112  +" set r = methods.m0()\n"
113  +" do methods.assert( r == -1.0 )\n"
114  +" set r = methods.m1(1)\n"
115  +" do methods.assert( r == -2.0 )\n"
116  +" set r = methods.m2(1, 1.0)\n"
117  +" do methods.assert( r == -3.0 )\n"
118  +" set r = methods.m3(1, 1.0, true)\n"
119  +" do methods.assert( r == -4.0 )\n"
120  +" set r = methods.m4(1, 1.0, true, \"hello\")\n"
121  +" do methods.assert( r == -5.0 )\n"
122 #endif
123 }
124 
125 BOOST_AUTO_TEST_CASE(testOperationCaller)
126 {
132 
133  BOOST_CHECK_EQUAL( -1.0, m0() );
134  BOOST_CHECK_EQUAL( -2.0, m1(1) );
135  BOOST_CHECK_EQUAL( -3.0, m2(1, 2.0) );
136  BOOST_CHECK_EQUAL( -4.0, m3(1, 2.0, false) );
137  BOOST_CHECK_EQUAL( -5.0, m4(1, 2.0, false,"hello") );
138 }
139 
140 BOOST_AUTO_TEST_CASE(testOperationCallerFactory)
141 {
142  // Test the addition of 'simple' methods to the operation interface,
143  // and retrieving it back in a new OperationCaller object.
144 
148 
149  Service to("task");
150 
151  BOOST_CHECK( to.addOperationCaller(&m0) );
152  BOOST_CHECK( ! to.addOperationCaller(&m0) );
153  BOOST_CHECK( to.addOperationCaller(&m1) );
154  BOOST_CHECK( to.addOperationCaller(&m2) );
155 
156  // test constructor
157  Operation<double(void)> mm0 = to.getOperationCaller<double(void)>("m0");
158  BOOST_CHECK( mm0.getOperationCallerImpl() );
159  BOOST_CHECK( mm0.ready() );
160 
161  // test operator=()
163  mm1 = to.getOperationCaller<double(int)>("m1");
164  BOOST_CHECK( mm1.getOperationCallerImpl() );
165  BOOST_CHECK( mm1.ready() );
166 
167  Operation<double(int,double)> mm2 = to.getOperationCaller<double(int,double)>("m2");
168  BOOST_CHECK( mm2.getOperationCallerImpl() );
169  BOOST_CHECK( mm2.ready() );
170 
171  // start the activity, such that methods are accepted.
172  BOOST_CHECK( tsim->start()) ;
173  // execute methods and check status:
174  BOOST_CHECK_EQUAL( -1.0, mm0() );
175 
176  BOOST_CHECK_EQUAL( -2.0, mm1(1) );
177  BOOST_CHECK_EQUAL( -3.0, mm2(1, 2.0) );
178 
179  // test error cases:
180  // Add uninitialised method:
181  Operation<void(void)> mvoid;
182  BOOST_CHECK(to.addOperationCaller( &mvoid ) == false);
183  mvoid = Operation<void(void)>("voidm");
184  BOOST_CHECK(to.addOperationCaller( &mvoid ) == false);
185 
186  // wrong type 1:
187  mvoid = to.getOperationCaller<void(void)>("m1");
188  BOOST_CHECK( mvoid.ready() == false );
189  // wrong type 2:
190  mvoid = to.getOperationCaller<void(bool)>("m1");
191  // wrong type 3:
192  mvoid = to.getOperationCaller<double(void)>("m0");
193  BOOST_CHECK( mvoid.ready() == false );
194  // non existing
195  mvoid = to.getOperationCaller<void(void)>("voidm");
196  BOOST_CHECK( mvoid.ready() == false );
197 
198  // this line may not crash:
199  mvoid();
200 
201 }
202 
203 BOOST_AUTO_TEST_CASE(testCROperationCaller)
204 {
205  this->ret = -3.3;
206 
209 
212 
213  BOOST_CHECK_EQUAL( -3.3, m0r() );
214  BOOST_CHECK_EQUAL( -3.3, m0cr() );
215 
216  double value = 5.3;
217  BOOST_CHECK_EQUAL( 5.3*2, m1r(value) );
218  BOOST_CHECK_EQUAL( 5.3*2, value );
219  BOOST_CHECK_EQUAL( 5.3, m1cr(5.3) );
220 }
221 
222 
223 BOOST_AUTO_TEST_CASE(testOperationCallerFromDS)
224 {
225  Service to("task");
226 
232 
233  to.addOperation( &m0 ).doc("desc");
234  to.addOperation( &m1 ).doc("desc").arg("a1", "d1");
235  to.addOperation( &m2 ).doc("desc").arg("a1", "d1").arg("a2", "d2");
236  to.addOperation( &m3 ).doc("desc").arg("a1", "d1").arg("a2", "d2").arg("a3", "d3");
237  to.addOperation( &m4 ).doc("desc").arg("a1", "d1").arg("a2", "d2").arg("a3", "d3").arg("a4", "d4");
238 
239  double ret;
240  OperationCallerC mc0( to.methods(), "m0");
241  mc0.ret(ret);
242  OperationCallerC mc1( to.methods(), "m1");
243  mc1.argC(1).ret(ret);
244  OperationCallerC mc2( to.methods(), "m2");
245  mc2.argC(1).argC(2.0).ret(ret);
246  OperationCallerC mc3( to.methods(), "m3");
247  mc3.argC(1).argC(2.0).argC(false).ret(ret);
248  OperationCallerC mc4( to.methods(), "m4");
249  mc4.argC(1).argC(2.0).argC(false).argC(std::string("hello")).ret(ret);
250 
251  BOOST_CHECK( mc0.execute() );
252  BOOST_CHECK_EQUAL(-1.0, ret);
253  BOOST_CHECK( mc1.execute() );
254  BOOST_CHECK_EQUAL(-2.0, ret);
255  BOOST_CHECK( mc2.execute() );
256  BOOST_CHECK_EQUAL(-3.0, ret);
257  BOOST_CHECK( mc3.execute() );
258  BOOST_CHECK_EQUAL(-4.0, ret);
259  BOOST_CHECK( mc4.execute() );
260  BOOST_CHECK_EQUAL(-5.0, ret);
261 }
262 
263 BOOST_AUTO_TEST_CASE(testDSOperationCaller)
264 {
265  Service to("task");
266 
267  // A method of which the first argument type is a pointer to the object
268  // on which it must be invoked. The pointer is internally stored as a weak_ptr,
269  // thus the object must be stored in a shared_ptr, in a DataSource. Scripting
270  // requires this for copying state machines.
271 
274 
275  method_ds("m0", &Generic_TaskTest::m0);
276 
279 
280  method_ds("m1", &Generic_TaskTest::m1);
281  method_ds("ms",&Generic_TaskTest::comstr );
282 
283  boost::shared_ptr<Generic_TaskTest> ptr( new Generic_TaskTest() );
285  BOOST_CHECK( to.addOperationCallerDS( wp.get(), meth0, "desc" ) );
286  BOOST_CHECK( to.addOperationCallerDS( wp.get(), meth1, "desc", "a1", "d1" ) );
287 
288  // this actually works ! the method will detect the deleted pointer.
289  //ptr.reset();
290 
291  BOOST_CHECK( tsim->start()) ;
292 
293  double ret;
294  OperationCallerC c0 = to.create("m0").ret(ret);
295  BOOST_CHECK( c0.execute() );
296  BOOST_CHECK_EQUAL( -1.0, ret );
297  OperationCallerC c1 = to.create("m1").argC(1).ret(ret);
298  BOOST_CHECK( c1.execute() );
299  BOOST_CHECK_EQUAL( -2.0, ret );
300 
301  BOOST_CHECK( tsim->stop()) ;
302 
303 }
304 
305 BOOST_AUTO_TEST_CASE(testAddOperationCaller)
306 {
307  Operation<double(void)> m0 = method("m0", &Generic_TaskTest::m0, this);
308 
309  Operation<double(int)> m1 = method("m1", &Generic_TaskTest::m1, this);
310  Operation<double(int,double)> m2 = method("m2", &Generic_TaskTest::m2, this);
313 
314  BOOST_CHECK_EQUAL( -1.0, m0() );
315  BOOST_CHECK_EQUAL( -2.0, m1(1) );
316  BOOST_CHECK_EQUAL( -3.0, m2(1, 2.0) );
317  BOOST_CHECK_EQUAL( -4.0, m3(1, 2.0, false) );
318  BOOST_CHECK_EQUAL( -5.0, m4(1, 2.0, false,"hello") );
319 }
320 
#define BOOST_FIXTURE_TEST_SUITE(suite_name, F)
double m1cr(const double &a)
Service * createOperationCallerFactory()
BOOST_AUTO_TEST_CASE(testRemoteOperationCaller)
#define BOOST_AUTO_TEST_SUITE_END()
Definition: mystd.hpp:163
Operation< Signature > & addOperation(Operation< Signature > &op)
Definition: Service.hpp:341
const double & m0cr()
bool comstr(const std::string &cs)
OperationCallerC & ret(base::AttributeBase *r)
double m1r(double &a)
DataSource< T >::result_t get() const
Definition: DataSources.hpp:78
OperationCallerC & argC(const ArgT a)
double m3(int i, double d, bool c)
double m4(int i, double d, bool c, std::string s)
A SimulationActivity is a PeriodicActivity which is used for simulation.
internal::OperationCallerC create(std::string name, ExecutionEngine *caller)
Definition: Service.cpp:268
double m2(int i, double d)


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