types_test.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Mon Jan 10 15:59:50 CET 2005 types_test.cpp
3 
4  types_test.cpp - description
5  -------------------
6  begin : Mon January 10 2005
7  copyright : (C) 2005 Peter Soetens
8  email : peter.soetens@mech.kuleuven.ac.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 #include "unit.hpp"
20 
21 #include <iostream>
23 #include <OperationCaller.hpp>
26 #include <Service.hpp>
27 #include <TaskContext.hpp>
29 #include <scripting/Parser.hpp>
30 #include <Service.hpp>
32 #include <types/Types.hpp>
33 #include <types/StructTypeInfo.hpp>
35 
36 #include "datasource_fixture.hpp"
37 #include "operations_fixture.hpp"
38 
39 using namespace std;
40 using namespace RTT;
41 using namespace RTT::detail;
42 
44 {
45 public:
47  ScriptingService::shared_ptr sa;
48  void executePrograms(const std::string& prog);
49  void executeStates(const std::string& state);
50 
52  : sa( ScriptingService::Create(tc) )
53  {
54  tc->stop();
55  BOOST_REQUIRE( tc->setActivity(new SimulationActivity(0.01)) );
56  BOOST_REQUIRE( tc->start() );
57  SimulationThread::Instance()->stop();
58  }
60  }
61 };
62 
63 
64 // Registers the fixture into the 'registry'
65 BOOST_FIXTURE_TEST_SUITE( TypesTestSuite, TypesTest )
66 
67 BOOST_AUTO_TEST_CASE( testStringCapacity )
69 {
70  Attribute<string> str = Types()->type("string")->buildVariable("str",10);
71  size_t strCapacity=str.get().capacity();
72  // check size hint:
73  BOOST_CHECK_EQUAL( str.get().size() , 10 );
74 
75  // False test http://www.cplusplus.com/reference/string/string/capacity/
76  // BOOST_CHECK_EQUAL( str.get().capacity() , 10 );
77 
78  str.set() = "hello"; // note: assign to C string preserves capacity
79  BOOST_CHECK_EQUAL( str.get().size() , 5 );
80  BOOST_CHECK_EQUAL( str.get().capacity() , strCapacity );
81 
82  // create empty target:
83  Attribute<string> copy("copy");
84  BOOST_CHECK_EQUAL( copy.get().size() , 0 );
85  // False test http://www.cplusplus.com/reference/string/string/capacity/
86  //BOOST_CHECK_EQUAL( copy.get().capacity() , 0 );
87 
88  // copy str to target and check:
89  copy.getDataSource()->update( str.getDataSource().get() );
90 
91  BOOST_CHECK_EQUAL( copy.get().size(), 5 );
92  // We can't assume much here: on Linux copy.get().capacity() returns 5, on win32: 10
93  //BOOST_CHECK_EQUAL( copy.get().capacity(), strCapacity );
94  BOOST_CHECK_EQUAL( copy.get(), str.get() );
95 
96  copy.set() = "world";
97 
98  // now copy target back to str and check if capacity remains:
99  str.getDataSource()->update( copy.getDataSource().get() );
100  BOOST_CHECK_EQUAL( str.get().size() , 5 );
101  BOOST_CHECK_EQUAL( str.get().capacity() , strCapacity );
102  BOOST_CHECK_EQUAL( copy.get(), str.get() );
103 
104 
105 
106  // Same exercise as above, but with updateCommand():
107  str.set() = "hello"; // note: assign to C string preserves capacity
108  strCapacity=str.get().capacity();
109 
110  BOOST_CHECK_EQUAL( str.get().size() , 5 );
111  // False test http://www.cplusplus.com/reference/string/string/capacity/
112  //BOOST_CHECK_EQUAL( str.get().capacity() , 10 );
113 
114  // copy str to target and check:
115  ActionInterface* act = copy.getDataSource()->updateAction( str.getDataSource().get() );
116  BOOST_CHECK( act );
117  act->readArguments();
118  BOOST_CHECK( act->execute() );
119  delete act;
120 
121  BOOST_CHECK_EQUAL( copy.get().size(), 5 );
122  // We can't assume much here: on Linux copy.get().capacity() returns 5, on win32: 10
123  //BOOST_CHECK_EQUAL( copy.get().capacity(), strCapacity );
124  BOOST_CHECK_EQUAL( copy.get(), str.get() );
125 
126  copy.set() = "world";
127 
128  // now copy target back to str and check if capacity remains:
129  act = str.getDataSource()->updateAction( copy.getDataSource().get() );
130  BOOST_CHECK( act );
131  act->readArguments();
132  BOOST_CHECK( act->execute() );
133  delete act;
134 
135  BOOST_CHECK_EQUAL( str.get().size() , 5 );
136  BOOST_CHECK_EQUAL( str.get().capacity() , strCapacity );
137  BOOST_CHECK_EQUAL( copy.get(), str.get() );
138 
139 }
140 
142 {
143  Types()->addType( new StructTypeInfo<AType,false>("astruct"));
144  string test =
145  // Line 2 (see below):
146  string("var int i2 = -1, j = 10, k; set k = 20\n") +
147  "do test.assert( i2 == -1 ) ; do test.assert( j == 10 ); do test.assert(k == 20)\n" +
148  "var double d = 10.0\n"+
149  "do test.assert( d == 10.0 )\n" +
150  "var bool b = false\n"+
151  "do test.assert( b == false )\n" +
152  "var string s=\"string\"\n"+
153  "do test.assert( s == \"string\" )\n" +
154  "const int ic = i2\n" +
155  "do test.assert( ic == 0 )\n" + // i was null at parse time !
156  "const double dc = 10.0\n"+ // evaluate 10.0 at parse time
157  "do test.assert( dc == 10.0 )\n" +
158  "const bool bc = true && false\n"+
159  "do test.assert( bc == false )\n" +
160  "const string sc= \"hello\"\n"+
161  "do test.assert( sc == \"hello\" )\n" +
162  "var array ar(10)\n"+ // size hint syntax != constructor syntax
163  "do test.assert( ar.size == 10)\n"+
164  // 20:
165  "do test.assert( ar.capacity == 10)\n"+
166  "set ar[0] = 0.0\n"+
167  "set ar[1] = 1.0\n"+
168  "set ar[2] = 0.2\n"+
169  "set ar[8] = 0.8\n"+
170  "set ar[9] = 9.0\n"+
171  "do test.assert( ar[0] == 0.0 )\n"+
172  "do test.assert( ar[1] == 1.0 )\n"+
173  "do test.assert( ar[2] == 0.2 )\n"+
174  "do test.assert( ar[8] == 0.8 )\n"+
175  // 30:
176  "do test.assert( ar[9] == 9.0 )\n"+
177  "do test.assert( ar[10] == 0.0 )\n"+
178  "var array ar1 = array(12,2.0)\n"+
179  "do test.assert(ar1.size == 12)\n"+
180 // "do test.print(ar1[11])\n"+
181  "do test.assert(ar1[0] == 2.0)\n"+
182  "var array ar2 = array(5,3.0)\n"+
183  "do test.assert(ar2.size == 5)\n"+
184  "do test.assert(ar2[0] == 3.0)\n"+
185  "var array ar3(3) = array(2.0,3.0,4.0)\n"+
186  "do test.assert(ar3.size == 3)\n"+
187  //40:
188  "do test.assert(ar3[0]==2.0)\n"+
189  "do test.assert(ar3[1]==3.0)\n"+
190  "do test.assert(ar3[2]==4.0)\n"+
191  "var array ar4 = array(2.0,3.0,4.0,5.0)\n"+
192  "do test.assert(ar4.size == 4)\n"+
193  "do test.assert(ar4[0]==2.0)\n"+
194  "do test.assert(ar4[1]==3.0)\n"+
195  "do test.assert(ar4[2]==4.0)\n"+
196  "do test.assert(ar4[3]==5.0)\n"+
197  "var string str(10)\n"+
198  "var int strCapacity = str.capacity\n"
199  // 50:
200 // "do test.print(str.size)\n"+
201 // "do test.print(str.capacity)\n"+
202  "do test.assertEqual( str.size, 10)\n"+
203 // "do test.assertEqual( str.capacity, 10)\n"
204  "set str = \"hello\"\n"+
205 // "do test.print(str.size)\n"+
206 // "do test.print(str.capacity)\n"+
207  "do test.assertEqual( str.size, 5)\n"+
208  "do test.assertEqual( str.capacity, strCapacity)\n"+ // if this fails, the COW implementation of std::string fooled us again.
209  "set str[0] = 'a'\n"+
210  "set str[1] = 'b'\n"+
211  "set str[2] = 'c'\n"+
212  "do test.assert( str[0] == 'a' )\n"+
213  "do test.assert( str[1] == 'b' )\n"+
214  "do test.assert( str[2] == 'c' )\n"+
215  "do test.assert( str == \"abclo\" )\n"+
216  "do test.assert( str[20] == '\\0' )\n"+
217  // 60:
218  "do test.assert( str[8] == '\\0' )\n"+
219  "do test.assert( str[9] == '\\0' )\n"+
220  "do test.assert( str[10] == '\\0' )\n"+
221  // various array constructors
222  "set ar2 = array(10.,5.)\n"+ // keeps capacity
223  "do test.assertEqual( ar2.size, 2)\n"+
224  "do test.assertEqual( ar2.capacity, 5)\n"+
225  "do test.assert( ar2[0] == 10.0 )\n"+
226  "do test.assert( ar2[1] == 5.0 )\n"+
227  "set ar3 = array(10.)\n"+
228  "do test.assert( ar3.size == 1)\n"+
229  // 70:
230  "do test.assert( ar3.capacity >= 1)\n"+
231  "do test.assert( ar3[0] == 10.0 )\n"+
232  "set ar4 = array(2, 7.)\n"+
233  "do test.assert( ar4.size == 2)\n"+
234  "do test.assert( ar4.capacity >= 2)\n"+
235  "do test.assert( ar4[0] == 7.0 )\n"+
236  "do test.assert( ar4[1] == 7.0 )\n"+
237  // various array assignments
238  "set ar2 = ar4\n"+
239  "do test.assert( ar2.size == 2)\n"+
240  "do test.assert( ar2.capacity >= 5)\n"+
241  // 80:
242  "do test.assert( ar2[0] == 7.0 )\n"+
243  "do test.assert( ar2[1] == 7.0 )\n"+
244  "do test.assert( ar.capacity == 10)\n"+ // pre-condition
245  "var array ar7(7) = array(7)\n"+
246  "set ar = ar7\n"+ // assignment must keep capacity and only change size
247  //"do test.print( ar.size )\n"+
248  "do test.assert( ar.size == 7)\n"+
249  //"do test.print( ar.capacity )\n"+
250  "do test.assertEqual( ar.capacity, 10)\n"+ // check keeping capacity: ar(10) vs ar2(2)
251  //-- This fails because .capacity() gets a copy of the std::vector
252  "do test.assert( ar2[0] == 7.0 )\n"+
253  "do test.assert( ar2[1] == 7.0 )\n"+
254  "var astruct aVar\n" +
255  // 90:
256  "var astruct bVar\n" +
257  "aVar.a = 42\n" +
258  "do test.assertMsg(aVar.a == 42, \"aVar.a != 42 assignement failed - testTypes\")\n" +
259  "bVar = aVar\n" +
260  "do test.assertMsg(bVar.a == 42, \"bVar.a != 42 assignement failed - testTypes\")\n" +
261  " ";
262 
263  string state = string("StateMachine X { initial state Init { entry {\n")
264  +test
265  +"} }\n"
266  +"final state Fini {} }\n"
267  +"RootMachine X x\n";
268 
269  string prog = string("program x {\n") + test + "}\n";
270  // execute
271  executePrograms(prog);
272  executeStates(state);
273 }
274 
275 BOOST_AUTO_TEST_CASE( testAliases )
276 {
277  Types()->addType( new StructTypeInfo<AType,false>("astruct"));
278  Types()->addType( new StructTypeInfo<AType,false>("aalias1"));
279  Types()->type("astruct")->addAlias("aalias2");
280 
281  BOOST_CHECK( Types()->type("astruct") != 0 );
282  BOOST_CHECK( Types()->type("aalias1") != 0 );
283  BOOST_CHECK( Types()->type("aalias2") != 0 );
284 
285  BOOST_CHECK_EQUAL( Types()->type("astruct"), Types()->type("aalias1") );
286  BOOST_CHECK_EQUAL( Types()->type("astruct"), Types()->type("aalias2") );
287 
288  BOOST_CHECK_EQUAL( "astruct", Types()->type("astruct")->getTypeNames()[0] );
289  BOOST_CHECK_EQUAL( "aalias1", Types()->type("astruct")->getTypeNames()[1] );
290  BOOST_CHECK_EQUAL( "aalias2", Types()->type("astruct")->getTypeNames()[2] );
291 }
292 
293 BOOST_AUTO_TEST_CASE( testCharType )
294 {
295  string test =
296  // Line 2 (see below):
297  string("var char c = char('c');\n") +
298 // "do test.assert( c == c ); \n" +
299 // "do test.assert( char('c') == c ); \n" +
300  "var char d = char('d');\n" +
301 // "do test.assert( c != d ); \n" +
302 // "set c = 'a';\n" +
303  "set c = char('a');\n" +
304  "do test.assert( char('a') == c ); \n" +
305  "do test.assert( c != d ); \n" +
306  "do test.assert( c < d ); \n" +
307  "do test.assert( c <= d ); \n" +
308  "do test.assert( d > c ); \n" +
309  "do test.assert( d >= c ); \n" +
310  "set d = c;\n" +
311  "do test.assert( c == d ); \n" +
312  "do test.assert( char('a') == d ); \n" +
313  "do test.assert( char('a') == char('a') ); \n" +
314  "do test.assert( char('a') != char('b') ); \n" +
315  "do test.assert( char('a') < char('b') ); \n" +
316  "do test.assert( char('a') <= char('b') ); \n" +
317  "do test.assert( char('a') <= char('a') ); \n" +
318  "do test.assert( char('z') > char('w') ); \n" +
319  "do test.assert( char('z') >= char('w') ); \n" +
320  "do test.assert( char('z') >= char('z') ); \n"
321  ;
322 
323  string state = string("StateMachine X { initial state Init { entry {\n")
324  +test
325  +"} }\n"
326  +"final state Fini {} }\n"
327  +"RootMachine X x\n";
328 
329  string prog = string("program x {\n") + test + "}\n";
330  // execute
331  executePrograms(prog);
332  executeStates(state);
333 }
334 
338 BOOST_AUTO_TEST_CASE( testOperators )
339 {
340  string prog = string("program x {\n") +
341  "var int i = 3\n" +
342  "var char c = 'c'\n" +
343  "var double d = 10.0*i\n"+
344  "do test.assert( d == 30.0 )\n" +
345  "var bool b = false\n"+
346  "var string s=\"string\"\n"+
347  "set b = b || b && true && false || true\n"+
348  "try test.assertMsg( s == \"string\", \"Unexpected string:\'\" + s +\"' instead of 'string'\")\n"+
349  "set s = \" \" + s + \" \"\n"+
350  "try test.assertMsg( s == \" string \", \"Unexpected string:\'\" + s +\"' instead of ' string '\")\n"+
351  "set s = s + int(10)\n"+
352  "try test.assertMsg( s == \" string 10\", \"Unexpected string:\'\" + s +\"' instead of ' string 10'\")\n"+
353  "set s = s + \" \" + false\n"+
354  "do test.assertMsg( s == \" string 10 false\", \"Unexpected string:\'\" + s +\"' instead of ' string 10 false'\")\n"+
355  "set b = b\n ||\n b\n &&\n true\n && false\n || true\n"+
356  "do test.assert( b == false )\n" +
357  "var array a1 = array(2, 7.)\n"+
358  "do test.assert( a1.size == 2 )\n" +
359  "do test.assert( a1.capacity == 2 )\n" +
360 // "set s = s+\"abc\"\n"+
361  "}";
362  // execute
363  executePrograms(prog);
364 }
365 
370 BOOST_AUTO_TEST_CASE( testDotsAndIndexes )
371 {
372  Types()->addType( new SequenceTypeInfo<std::vector<std::vector<double> >,false >("matrix"));
373  Types()->addType( new StructTypeInfo<AType,false>("astruct"));
374  Types()->addType( new StructTypeInfo<CType,false>("cstruct"));
375  Types()->addType( new SequenceTypeInfo<std::vector<CType>,false >("cstructv"));
376  Types()->addType( new SequenceTypeInfo<std::vector<AType>,false >("astructv"));
377  string prog = string("program x {\n") +
378  "var matrix m = matrix(8,array(10))\n" + // 8 by 10 matrix
379  "test.assertMsg(m.size == 8, \"Matrix column size is wrong.\")\n" +
380  "test.assertMsg(m[0].size == 10, \"Matrix row size is wrong.\")\n" +
381  "m[0][0] = 3.33\n" +
382  "m[1][1] = 4.33\n" +
383  "m[2][2] = 5.33\n" +
384  "m[8][10] = 6.33\n" +
385  "test.assertMsg(m[0][0] == 3.33, \"Matrix element assignment failed.\")\n"+
386  "test.assertMsg(m[1][1] == 4.33, \"Matrix element assignment failed.\")\n"+
387  "test.assertMsg(m[2][2] == 5.33, \"Matrix element assignment failed.\")\n"+
388  "test.assertMsg(m[8][10] == 6.33, \"Matrix element assignment failed.\")\n"+
389  "var matrix m2 = m;\n"
390  "test.assertMsg(m2[0][0] == 3.33, \"Matrix assignment failed.\")\n"+
391  "test.assertMsg(m2[1][1] == 4.33, \"Matrix assignment failed.\")\n"+
392  "test.assertMsg(m2[2][2] == 5.33, \"Matrix assignment failed.\")\n"+
393  "test.assertMsg(m2[8][10] == 6.33, \"Matrix assignment failed.\")\n"+
394  "var cstructv structv = cstructv(3)\n"+ // vector<struct> of size 10
395  "structv[1].a.b = 3.33\n"+
396  "test.assertMsg(structv[1].a.b == 3.33, \"Sequence of struct element assignment failed.\")\n"+
397  "structv[1].av[3].b = 4.33\n"+
398  "test.assertMsg(structv[1].av[3].b == 4.33, \"Sequence of struct element assignment failed.\")\n"+
399  "}";
400  // execute
401  executePrograms(prog);
402 }
403 
408 BOOST_AUTO_TEST_CASE( testConversions )
409 {
410  string prog = string("program x {\n") +
411  "var int i = 3.0\n" +
412  "var double d = float(10.0*i)\n"+
413  "do test.assert( float(d) == float(30.0) )\n" +
414  "var float f = 5\n" +
415  "set f = double(5) * double(-1) + i\n" +
416  "set i = f\n" +
417  "set f = i\n" +
418  "set i = double(float(int(f)))\n" +
419  "set f = int(float(double(int(3.333))))\n" +
420  "do test.assert( f == 3 )\n" +
421  "}";
422  // execute
423  executePrograms(prog);
424 }
425 
429 BOOST_AUTO_TEST_CASE( testLongLong )
430 {
431  string prog = string("program x {\n") +
432  "var llong ll = 9223372036854775807ll\n" +
433  "do test.assert( -(ll + 2) == 9223372036854775807ll )\n" +
434  "var ullong ull = 18446744073709551615ull\n" +
435  "do test.assert( ull + 1 == 0 )\n" +
436  "var double d = ll\n" +
437  "do test.assert( d == 9.223372036854775807e+18 )\n" +
438  "set ll = 3.0f\n" +
439  "do test.assert( ll == 3ll )\n" +
440  "set ll = -1.0\n" +
441  "do test.assert( ll == -1ll )\n" +
442  "set ll = int(-1000)\n" +
443  "do test.assert( ll == -1000ll )\n" +
444  "set ull = int(1000)\n" +
445  "do test.assert( ull == 1000ull )\n" +
446  "set ull = uint(1000)\n" +
447  "do test.assert( ull == 1000ull )\n" +
448  "set ull = llong(12345)\n" +
449  "do test.assert( ull == 12345ull )\n" +
450  "}";
451  // execute
452  executePrograms(prog);
453 }
454 
459 {
460  string prog = string("program x {\n") +
461  "var uint i = 0xabc\n" +
462  "test.assert( i == 0xabc )\n"+
463  "test.assert( i == 2748 )\n"+
464  "i = 0Xcba\n" +
465  "test.assert( i == 0Xcba )\n"+
466  "test.assert( i == 3258 )\n"+
467  "i = 0XABC\n" +
468  "test.assert( i == 0XABC )\n"+
469  "test.assert( i == 2748 )\n"+
470  "i = 0xCBA\n" +
471  "test.assert( i == 0xCBA )\n"+
472  "test.assert( i == 3258 )\n"+
473  "}";
474  // execute
475  executePrograms(prog);
476 }
477 
481 BOOST_AUTO_TEST_CASE( testProperties )
482 {
483  string prog = string("program x {\n") +
484  "do test.assert( Double1 == 1.234 )\n" +
485  "do test.assert( Double2 == 2.234 )\n" +
486  "do test.assert( Double3 == 3.234 )\n" +
487  "do test.assert( Collection.Double1 == 1.234 )\n" +
488  "do test.assert( Collection.Double3 == 3.234 )\n" +
489  "do test.assert( Collection.Collection.Double1 == 1.234 )\n" +
490  "do test.assert( Collection.Collection.Collection.Double3 == 3.234 )\n" +
491  "do test.assert( V[0] == 4.0 )\n" +
492  "do test.assert( V[1] == 4.0 )\n" +
493  "do test.assert( V[2] == 4.0 )\n" +
494  "do test.assert( V[3] == 4.0 )\n" +
495  //"do test.assert( V.size() == 4 )\n" +
496  "set Double1 = 4.321\n" +
497  "set Double2 = Double1\n" +
498  // -> = 10
499  "set Collection.Double3 = 0.3\n" +
500  "set V[0] = 0.321\n" +
501  "set V[1] = 1.321\n" +
502  "set V[2] = 2.321\n" +
503  "set V[3] = 3.321\n" +
504  "do test.assert( Double1 == 4.321 )\n" +
505  "do test.assert( Double2 == 4.321 )\n" +
506  "do test.assert( Double3 == 0.3 )\n" +
507  "set Collection.Collection.Collection.Double3 = 3.0\n" +
508  "do test.assert( Double3 == 3.0 )\n" +
509  "}";
510 
511  Property<PropertyBag> pb("Collection","");
512  Property<double> pd1("Double1","",1.234);
513  Property<double> pd2("Double2","",2.234);
514  Property<double> pd3("Double3","",3.234);
515 
516  Property< std::vector<double> > pv("V","",std::vector<double>(4, 4.0) );
517 
518  pb.value().addProperty( pd1 );
519  pb.value().addProperty( pd3 );
520  pb.value().addProperty( pb ); // yep, recursive !
521 
522  tc->properties()->addProperty( pd1 );
523  tc->properties()->addProperty( pd2 );
524  tc->properties()->addProperty( pd3 );
525  tc->properties()->addProperty( pb );
526  tc->properties()->addProperty( pv );
527 
528  // execute
529  executePrograms(prog);
530 
531  BOOST_CHECK_EQUAL( 4.321, pd1.get() );
532  BOOST_CHECK_EQUAL( 3.0, pd3.get() );
533  BOOST_CHECK_EQUAL( 0.321, pv.value()[0] );
534  BOOST_CHECK_EQUAL( 1.321, pv.value()[1] );
535  BOOST_CHECK_EQUAL( 2.321, pv.value()[2] );
536  BOOST_CHECK_EQUAL( 3.321, pv.value()[3] );
537 }
538 
539 BOOST_AUTO_TEST_CASE( testOperatorOrder )
540 {
541  string prog = string("program x {\n") +
542  "do test.assert( 6/2*4 == 12 )\n" + // not: 0
543  "do test.assert( 6*2/4 == 3 )\n" + // not: 0
544  "do test.assert( 3+2*5 == 13)\n" + // not: 30
545  "do test.assert( 3 < 2 != 5 > 1)\n" +
546  "do test.assert( 3*2 <= 12/2 )\n" +
547  "do test.assert( 3*2 < 6+1 )\n" +
548  "do test.assert( 6 - 9 % 2*3 == 15/3 % 3 + 1 )\n" + // 3 == 3
549  "do test.assert( 3*(2+1) == 9 )\n" +
550  "do test.assert( 1 - 1 + 5 == 5 )\n" + // not: -5
551  "var int a,b,c;\n" +
552  " a = b = c = 3;\n" + // not 0,0,3
553  "do test.assertEqual( a, 3 )\n" +
554  "do test.assertEqual( b, 3 )\n" +
555  "do test.assertEqual( c, 3 )\n" +
556  "}";
557  // execute
558  executePrograms(prog);
559 }
560 
561 BOOST_AUTO_TEST_CASE( testGlobals )
562 {
563  GlobalsRepository::Instance()->setValue( new Constant<double>("cd_num", 3.33));
564  GlobalsRepository::Instance()->setValue( new Constant<string>("c_string", "Hello World!"));
565  GlobalsRepository::Instance()->setValue( new Attribute<double>("d_num", 3.33));
566  string prog = string("program x {\n") +
567  "do test.assert( cd_num == 3.33 )\n" +
568  "do test.assert( cd_num == d_num )\n" +
569  "do test.assert( c_string == \"Hello World!\")\n" +
570  "set d_num = 6.66\n" +
571  "do test.assert( d_num == 6.66 )\n" +
572  "}";
573  // execute
574  executePrograms(prog);
575 }
576 
577 BOOST_AUTO_TEST_CASE( testFlowStatus )
578 {
579  BOOST_CHECK (GlobalsRepository::Instance()->getValue("NewData") );
580  BOOST_CHECK (GlobalsRepository::Instance()->getValue("OldData") );
581  BOOST_CHECK (GlobalsRepository::Instance()->getValue("NoData") );
582  BOOST_CHECK (GlobalsRepository::Instance()->getValue("WriteSuccess") );
583  BOOST_CHECK (GlobalsRepository::Instance()->getValue("WriteFailure") );
584  BOOST_CHECK (GlobalsRepository::Instance()->getValue("NotConnected") );
585  string prog = string("program x {\n") +
586  "do test.assert( NewData )\n" +
587  "do test.assert( OldData )\n" +
588  "do test.assert( !bool(NoData) )\n" +
589  "do test.assert( NewData > NoData )\n" +
590  "do test.assert( NewData > OldData )\n" +
591  "do test.assert( OldData > NoData )\n" +
592  "do test.assert( OldData == OldData )\n" +
593  "if ( bool(NewData) && OldData ) then {\n" +
594  "} else {\n" +
595  " do test.assert(false)\n" +
596  "}\n" +
597  "if ( bool(NoData) ) then {\n" +
598  " do test.assert(false)\n" +
599  "}\n" +
600  "if ( !bool(NoData) ) then {} else {\n" +
601  " do test.assert(false)\n" +
602  "}\n" +
603  "}";
604  // execute
605  executePrograms(prog);
606 }
608 
609 void TypesTest::executePrograms(const std::string& prog )
610 {
611  BOOST_CHECK( tc->engine() );
612 
613  Parser::ParsedPrograms pg_list;
614  try {
615  pg_list = parser.parseProgram( prog, tc );
616  }
617  catch( const file_parse_exception& exc )
618  {
619  BOOST_REQUIRE_MESSAGE( false , exc.what());
620  }
621  if ( pg_list.empty() )
622  {
623  BOOST_REQUIRE( false && "Got empty test program." );
624  }
625 
626  BOOST_CHECK( sa->loadProgram( *pg_list.begin() ) );
627  BOOST_CHECK( (*pg_list.begin())->start() );
628 
629  BOOST_CHECK( SimulationThread::Instance()->run(1000) );
630 
631  if ( (*pg_list.begin())->inError() ) {
632  stringstream errormsg;
633  errormsg << " Program error on line " << (*pg_list.begin())->getLineNumber() <<"."<<endl;
634  BOOST_CHECK_MESSAGE( false, errormsg.str() );
635  }
636 
637  if ( (*pg_list.begin())->isRunning() ) {
638  stringstream errormsg;
639  errormsg << " Program still running on line " << (*pg_list.begin())->getLineNumber() <<"."<<endl;
640  BOOST_CHECK_MESSAGE( false, errormsg.str() );
641  }
642 
643  if ( (*pg_list.begin())->isStopped() == false ) {
644  stringstream errormsg;
645  errormsg << " Program not stopped on line " << (*pg_list.begin())->getLineNumber() <<"."<<endl;
646  BOOST_CHECK_MESSAGE( false , errormsg.str() );
647  }
648  BOOST_CHECK( (*pg_list.begin())->stop() );
649  BOOST_CHECK( sa->unloadProgram( (*pg_list.begin())->getName() ) );
650 }
651 
652 void TypesTest::executeStates(const std::string& state )
653 {
654  BOOST_CHECK( tc->engine() );
656  try {
657  pg_list = parser.parseStateMachine( state, tc );
658  }
659  catch( const file_parse_exception& exc )
660  {
661  BOOST_REQUIRE_MESSAGE( false , exc.what());
662  }
663  if ( pg_list.empty() )
664  {
665  BOOST_REQUIRE_MESSAGE( false, "Parser returned no state machines to execute." );
666  }
667 
668  BOOST_CHECK( sa->loadStateMachine( *pg_list.begin() ) );
669  BOOST_CHECK( (*pg_list.begin())->activate() );
670  BOOST_CHECK( (*pg_list.begin())->start() );
671 
672  BOOST_CHECK( SimulationThread::Instance()->run(1000) );
673  if ( (*pg_list.begin())->inError() ) {
674  stringstream errormsg;
675  errormsg << " State error on line " << (*pg_list.begin())->getLineNumber() <<"."<<endl;
676  BOOST_CHECK_MESSAGE( false , errormsg.str());
677  }
678 
679  BOOST_CHECK( (*pg_list.begin())->stop() );
680  BOOST_CHECK( SimulationThread::Instance()->run(100) );
681  BOOST_CHECK( (*pg_list.begin())->deactivate() );
682 
683  sa->unloadStateMachine( (*pg_list.begin())->getName() );
684 }
const std::string what() const
#define BOOST_FIXTURE_TEST_SUITE(suite_name, F)
DataSourceType get() const
Definition: Property.hpp:246
#define BOOST_AUTO_TEST_SUITE_END()
Definition: mystd.hpp:163
void set(T const &t)
Definition: Attribute.hpp:199
T const & get() const
Definition: Attribute.hpp:188
Parser parser
Definition: types_test.cpp:46
reference_t value()
Definition: Property.hpp:277
base::DataSourceBase::shared_ptr getDataSource() const
Definition: Attribute.hpp:214
void executeStates(const std::string &state)
Definition: types_test.cpp:652
std::vector< ProgramInterfacePtr > ParsedPrograms
Definition: Parser.hpp:101
Based on the software pattern &#39;command&#39;, this interface allows execution of action objects...
This class is the public interface to the Orocos Program Parser Framework. It parsers Orocos program ...
Definition: Parser.hpp:65
TypeInfoRepository::shared_ptr Types()
Definition: Types.cpp:48
basic_ostreams & endl(basic_ostreams &s)
Definition: rtstreams.cpp:110
BOOST_AUTO_TEST_CASE(testStringCapacity)
Tests the preservation of the capacity of a string in the type system.
Definition: types_test.cpp:68
std::vector< ParsedStateMachinePtr > ParsedStateMachines
Definition: Parser.hpp:113
ScriptingService::shared_ptr sa
Definition: types_test.cpp:47
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:53
A SimulationActivity is a PeriodicActivity which is used for simulation.


rtt
Author(s): RTT Developers
autogenerated on Tue Jun 25 2019 19:33:38