types_test.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002   tag: Peter Soetens  Mon Jan 10 15:59:50 CET 2005  types_test.cpp
00003 
00004                         types_test.cpp -  description
00005                            -------------------
00006     begin                : Mon January 10 2005
00007     copyright            : (C) 2005 Peter Soetens
00008     email                : peter.soetens@mech.kuleuven.ac.be
00009 
00010  ***************************************************************************
00011  *                                                                         *
00012  *   This program is free software; you can redistribute it and/or modify  *
00013  *   it under the terms of the GNU General Public License as published by  *
00014  *   the Free Software Foundation; either version 2 of the License, or     *
00015  *   (at your option) any later version.                                   *
00016  *                                                                         *
00017  ***************************************************************************/
00018 
00019 #include "unit.hpp"
00020 
00021 #include <iostream>
00022 #include <scripting/FunctionGraph.hpp>
00023 #include <OperationCaller.hpp>
00024 #include <extras/SimulationActivity.hpp>
00025 #include <extras/SimulationThread.hpp>
00026 #include <Service.hpp>
00027 #include <TaskContext.hpp>
00028 #include <scripting/ScriptingService.hpp>
00029 #include <scripting/Parser.hpp>
00030 #include <Service.hpp>
00031 #include <types/GlobalsRepository.hpp>
00032 #include <types/Types.hpp>
00033 #include <types/StructTypeInfo.hpp>
00034 #include <types/SequenceTypeInfo.hpp>
00035 
00036 #include "datasource_fixture.hpp"
00037 #include "operations_fixture.hpp"
00038 
00039 using namespace std;
00040 using namespace RTT;
00041 using namespace RTT::detail;
00042 
00043 class TypesTest : public OperationsFixture
00044 {
00045 public:
00046     Parser parser;
00047     ScriptingService::shared_ptr sa;
00048     void executePrograms(const std::string& prog);
00049     void executeStates(const std::string& state);
00050 
00051     TypesTest()
00052     : sa( ScriptingService::Create(tc) )
00053     {
00054         tc->stop();
00055         BOOST_REQUIRE( tc->setActivity(new SimulationActivity(0.01)) );
00056         BOOST_REQUIRE( tc->start() );
00057         SimulationThread::Instance()->stop();
00058     }
00059     ~TypesTest(){
00060     }
00061 };
00062 
00063 
00064 // Registers the fixture into the 'registry'
00065 BOOST_FIXTURE_TEST_SUITE(  TypesTestSuite,  TypesTest )
00066 
00067 
00068 BOOST_AUTO_TEST_CASE( testStringCapacity )
00069 {
00070     Attribute<string> str = Types()->type("string")->buildVariable("str",10);
00071     size_t strCapacity=str.get().capacity();
00072     // check size hint:
00073     BOOST_CHECK_EQUAL( str.get().size() , 10 );
00074 
00075     // False test http://www.cplusplus.com/reference/string/string/capacity/
00076     // BOOST_CHECK_EQUAL( str.get().capacity() , 10 );
00077 
00078     str.set() = "hello"; // note: assign to C string preserves capacity
00079     BOOST_CHECK_EQUAL( str.get().size() , 5 );
00080     BOOST_CHECK_EQUAL( str.get().capacity() , strCapacity );
00081 
00082     // create empty target:
00083     Attribute<string> copy("copy");
00084     BOOST_CHECK_EQUAL( copy.get().size() , 0 );
00085     // False test http://www.cplusplus.com/reference/string/string/capacity/
00086     //BOOST_CHECK_EQUAL( copy.get().capacity() , 0 );
00087 
00088     // copy str to target and check:
00089     copy.getDataSource()->update( str.getDataSource().get() );
00090 
00091     BOOST_CHECK_EQUAL( copy.get().size(), 5 );
00092     // We can't assume much here: on Linux copy.get().capacity() returns 5, on win32: 10
00093     //BOOST_CHECK_EQUAL( copy.get().capacity(), strCapacity );
00094     BOOST_CHECK_EQUAL( copy.get(), str.get() );
00095 
00096     copy.set() = "world";
00097 
00098     // now copy target back to str and check if capacity remains:
00099     str.getDataSource()->update( copy.getDataSource().get() );
00100     BOOST_CHECK_EQUAL( str.get().size() , 5 );
00101     BOOST_CHECK_EQUAL( str.get().capacity() , strCapacity );
00102     BOOST_CHECK_EQUAL( copy.get(), str.get() );
00103 
00104 
00105 
00106     // Same exercise as above, but with updateCommand():
00107     str.set() = "hello"; // note: assign to C string preserves capacity
00108     strCapacity=str.get().capacity();
00109 
00110     BOOST_CHECK_EQUAL( str.get().size() , 5 );
00111     // False test http://www.cplusplus.com/reference/string/string/capacity/
00112     //BOOST_CHECK_EQUAL( str.get().capacity() , 10 );
00113 
00114     // copy str to target and check:
00115     ActionInterface* act = copy.getDataSource()->updateAction( str.getDataSource().get() );
00116     BOOST_CHECK( act );
00117     act->readArguments();
00118     BOOST_CHECK( act->execute() );
00119     delete act;
00120 
00121     BOOST_CHECK_EQUAL( copy.get().size(), 5 );
00122     // We can't assume much here: on Linux copy.get().capacity() returns 5, on win32: 10
00123     //BOOST_CHECK_EQUAL( copy.get().capacity(), strCapacity );
00124     BOOST_CHECK_EQUAL( copy.get(), str.get() );
00125 
00126     copy.set() = "world";
00127 
00128     // now copy target back to str and check if capacity remains:
00129     act = str.getDataSource()->updateAction( copy.getDataSource().get() );
00130     BOOST_CHECK( act );
00131     act->readArguments();
00132     BOOST_CHECK( act->execute() );
00133     delete act;
00134 
00135     BOOST_CHECK_EQUAL( str.get().size() , 5 );
00136     BOOST_CHECK_EQUAL( str.get().capacity() , strCapacity );
00137     BOOST_CHECK_EQUAL( copy.get(), str.get() );
00138 
00139 }
00140 BOOST_AUTO_TEST_CASE( testTypes )
00141 {
00142     Types()->addType( new StructTypeInfo<AType,false>("astruct"));
00143     string test =
00144         // Line 2 (see below):
00145         string("var int i2 = -1, j = 10, k; set k = 20\n") +
00146         "do test.assert( i2 == -1 ) ; do test.assert( j == 10 ); do test.assert(k == 20)\n" +
00147         "var double d = 10.0\n"+
00148         "do test.assert( d == 10.0 )\n" +
00149         "var bool b = false\n"+
00150         "do test.assert( b == false )\n" +
00151         "var string s=\"string\"\n"+
00152         "do test.assert( s == \"string\" )\n" +
00153         "const int ic = i2\n" +
00154         "do test.assert( ic == 0 )\n" + // i was null at parse time !
00155         "const double dc = 10.0\n"+     // evaluate 10.0 at parse time
00156         "do test.assert( dc == 10.0 )\n" +
00157         "const bool bc = true && false\n"+
00158         "do test.assert( bc == false )\n" +
00159         "const string sc= \"hello\"\n"+
00160         "do test.assert( sc == \"hello\" )\n" +
00161         "var array ar(10)\n"+ // size hint syntax != constructor syntax
00162         "do test.assert( ar.size == 10)\n"+
00163         // 20:
00164         "do test.assert( ar.capacity == 10)\n"+
00165         "set ar[0] = 0.0\n"+
00166         "set ar[1] = 1.0\n"+
00167         "set ar[2] = 0.2\n"+
00168         "set ar[8] = 0.8\n"+
00169         "set ar[9] = 9.0\n"+
00170         "do test.assert( ar[0] == 0.0 )\n"+
00171         "do test.assert( ar[1] == 1.0 )\n"+
00172         "do test.assert( ar[2] == 0.2 )\n"+
00173         "do test.assert( ar[8] == 0.8 )\n"+
00174         // 30:
00175         "do test.assert( ar[9] == 9.0 )\n"+
00176         "do test.assert( ar[10] == 0.0 )\n"+
00177         "var array ar1 = array(12,2.0)\n"+
00178         "do test.assert(ar1.size == 12)\n"+
00179 //        "do test.print(ar1[11])\n"+
00180         "do test.assert(ar1[0] == 2.0)\n"+
00181         "var array ar2 = array(5,3.0)\n"+
00182         "do test.assert(ar2.size == 5)\n"+
00183         "do test.assert(ar2[0] == 3.0)\n"+
00184         "var array ar3(3) = array(2.0,3.0,4.0)\n"+
00185         "do test.assert(ar3.size == 3)\n"+
00186         //40:
00187         "do test.assert(ar3[0]==2.0)\n"+
00188         "do test.assert(ar3[1]==3.0)\n"+
00189         "do test.assert(ar3[2]==4.0)\n"+
00190         "var array ar4 = array(2.0,3.0,4.0,5.0)\n"+
00191         "do test.assert(ar4.size == 4)\n"+
00192         "do test.assert(ar4[0]==2.0)\n"+
00193         "do test.assert(ar4[1]==3.0)\n"+
00194         "do test.assert(ar4[2]==4.0)\n"+
00195         "do test.assert(ar4[3]==5.0)\n"+
00196         "var string str(10)\n"+
00197         "var int strCapacity = str.capacity\n"
00198         // 50:
00199 //        "do test.print(str.size)\n"+
00200 //        "do test.print(str.capacity)\n"+
00201         "do test.assertEqual( str.size, 10)\n"+
00202 //        "do test.assertEqual( str.capacity, 10)\n"
00203         "set str = \"hello\"\n"+
00204 //        "do test.print(str.size)\n"+
00205 //        "do test.print(str.capacity)\n"+
00206         "do test.assertEqual( str.size, 5)\n"+
00207         "do test.assertEqual( str.capacity, strCapacity)\n"+ // if this fails, the COW implementation of std::string fooled us again.
00208         "set str[0] = 'a'\n"+
00209         "set str[1] = 'b'\n"+
00210         "set str[2] = 'c'\n"+
00211         "do test.assert( str[0] == 'a' )\n"+
00212         "do test.assert( str[1] == 'b' )\n"+
00213         "do test.assert( str[2] == 'c' )\n"+
00214         "do test.assert( str == \"abclo\" )\n"+
00215         "do test.assert( str[20] == '\\0' )\n"+
00216         // 60:
00217         "do test.assert( str[8] == '\\0' )\n"+
00218         "do test.assert( str[9] == '\\0' )\n"+
00219         "do test.assert( str[10] == '\\0' )\n"+
00220         // various array constructors
00221         "set ar2 = array(10.,5.)\n"+ // keeps capacity
00222         "do test.assertEqual( ar2.size, 2)\n"+
00223         "do test.assertEqual( ar2.capacity, 5)\n"+
00224         "do test.assert( ar2[0] == 10.0 )\n"+
00225         "do test.assert( ar2[1] == 5.0 )\n"+
00226         "set ar3 = array(10.)\n"+
00227         "do test.assert( ar3.size == 1)\n"+
00228         // 70:
00229         "do test.assert( ar3.capacity >= 1)\n"+
00230         "do test.assert( ar3[0] == 10.0 )\n"+
00231         "set ar4 = array(2, 7.)\n"+
00232         "do test.assert( ar4.size == 2)\n"+
00233         "do test.assert( ar4.capacity >= 2)\n"+
00234         "do test.assert( ar4[0] == 7.0 )\n"+
00235         "do test.assert( ar4[1] == 7.0 )\n"+
00236         // various array assignments
00237         "set ar2 = ar4\n"+
00238         "do test.assert( ar2.size == 2)\n"+
00239         "do test.assert( ar2.capacity >= 5)\n"+
00240         // 80:
00241         "do test.assert( ar2[0] == 7.0 )\n"+
00242         "do test.assert( ar2[1] == 7.0 )\n"+
00243         "do test.assert( ar.capacity == 10)\n"+ // pre-condition
00244         "var array ar7(7) = array(7)\n"+
00245         "set ar = ar7\n"+                       // assignment must keep capacity and only change size
00246         //"do test.print( ar.size )\n"+
00247         "do test.assert( ar.size == 7)\n"+
00248         //"do test.print( ar.capacity )\n"+
00249         "do test.assertEqual( ar.capacity, 10)\n"+ // check keeping capacity: ar(10) vs ar2(2)
00250         //-- This fails because .capacity() gets a copy of the std::vector
00251         "do test.assert( ar2[0] == 7.0 )\n"+
00252         "do test.assert( ar2[1] == 7.0 )\n"+
00253         "var astruct aVar\n" +
00254         // 90:
00255         "var astruct bVar\n" +
00256         "aVar.a = 42\n" +
00257         "do test.assertMsg(aVar.a == 42, \"aVar.a != 42 assignement failed - testTypes\")\n" +
00258         "bVar = aVar\n" +
00259         "do test.assertMsg(bVar.a == 42, \"bVar.a != 42 assignement failed - testTypes\")\n" +
00260         " ";
00261 
00262     string state = string("StateMachine X { initial state Init { entry {\n")
00263         +test
00264         +"} }\n"
00265         +"final state Fini {} }\n"
00266         +"RootMachine X x\n";
00267 
00268     string prog = string("program x {\n") + test + "}\n";
00269     // execute
00270     executePrograms(prog);
00271     executeStates(state);
00272 
00273 
00274 }
00275 
00276 BOOST_AUTO_TEST_CASE( testCharType )
00277 {
00278     string test =
00279         // Line 2 (see below):
00280         string("var char c = char('c');\n") +
00281 //        "do test.assert( c == c ); \n" +
00282 //        "do test.assert( char('c') == c ); \n" +
00283         "var char d = char('d');\n" +
00284 //        "do test.assert( c != d ); \n" +
00285 //        "set c = 'a';\n" +
00286         "set c = char('a');\n" +
00287         "do test.assert( char('a') == c ); \n" +
00288         "do test.assert( c != d ); \n" +
00289         "do test.assert( c <  d ); \n" +
00290         "do test.assert( c <= d ); \n" +
00291         "do test.assert( d >  c ); \n" +
00292         "do test.assert( d >= c ); \n" +
00293         "set d = c;\n" +
00294         "do test.assert( c == d ); \n" +
00295         "do test.assert( char('a') == d ); \n" +
00296         "do test.assert( char('a') == char('a') ); \n" +
00297         "do test.assert( char('a') != char('b') ); \n" +
00298         "do test.assert( char('a') <  char('b') ); \n" +
00299         "do test.assert( char('a') <= char('b') ); \n" +
00300         "do test.assert( char('a') <= char('a') ); \n" +
00301         "do test.assert( char('z') >  char('w') ); \n" +
00302         "do test.assert( char('z') >= char('w') ); \n" +
00303         "do test.assert( char('z') >= char('z') ); \n"
00304         ;
00305 
00306     string state = string("StateMachine X { initial state Init { entry {\n")
00307         +test
00308         +"} }\n"
00309         +"final state Fini {} }\n"
00310         +"RootMachine X x\n";
00311 
00312     string prog = string("program x {\n") + test + "}\n";
00313     // execute
00314     executePrograms(prog);
00315     executeStates(state);
00316 }
00317 
00321 BOOST_AUTO_TEST_CASE( testOperators )
00322 {
00323     string prog = string("program x {\n") +
00324         "var int i = 3\n" +
00325         "var char c = 'c'\n" +
00326         "var double d = 10.0*i\n"+
00327         "do test.assert( d == 30.0 )\n" +
00328         "var bool b = false\n"+
00329         "var string s=\"string\"\n"+
00330         "set b = b || b && true && false || true\n"+
00331         "try test.assertMsg( s == \"string\", \"Unexpected string:\'\" + s +\"' instead of 'string'\")\n"+
00332         "set s = \"  \" + s + \"  \"\n"+
00333         "try test.assertMsg( s == \"  string  \", \"Unexpected string:\'\" + s +\"' instead of '  string  '\")\n"+
00334         "set s = s + int(10)\n"+
00335         "try test.assertMsg( s == \"  string  10\", \"Unexpected string:\'\" + s +\"' instead of '  string  10'\")\n"+
00336         "set s = s + \" \" + false\n"+
00337         "do  test.assertMsg( s == \"  string  10 false\", \"Unexpected string:\'\" + s +\"' instead of '  string  10 false'\")\n"+
00338         "set b = b\n ||\n b\n &&\n true\n && false\n || true\n"+
00339         "do test.assert( b == false )\n" +
00340         "var array a1 = array(2, 7.)\n"+
00341         "do test.assert( a1.size == 2 )\n" +
00342         "do test.assert( a1.capacity == 2 )\n" +
00343 //         "set s = s+\"abc\"\n"+
00344         "}";
00345     // execute
00346     executePrograms(prog);
00347 }
00348 
00353 BOOST_AUTO_TEST_CASE( testDotsAndIndexes )
00354 {
00355     Types()->addType( new SequenceTypeInfo<std::vector<std::vector<double> >,false >("matrix"));
00356     Types()->addType( new StructTypeInfo<AType,false>("astruct"));
00357     Types()->addType( new StructTypeInfo<CType,false>("cstruct"));
00358     Types()->addType( new SequenceTypeInfo<std::vector<CType>,false >("cstructv"));
00359     Types()->addType( new SequenceTypeInfo<std::vector<AType>,false >("astructv"));
00360     string prog = string("program x {\n") +
00361         "var matrix m = matrix(8,array(10))\n" + // 8 by 10 matrix
00362         "test.assertMsg(m.size == 8, \"Matrix column size is wrong.\")\n" +
00363         "test.assertMsg(m[0].size == 10, \"Matrix row size is wrong.\")\n" +
00364         "m[0][0] = 3.33\n" +
00365         "m[1][1] = 4.33\n" +
00366         "m[2][2] = 5.33\n" +
00367         "m[8][10] = 6.33\n" +
00368         "test.assertMsg(m[0][0] == 3.33, \"Matrix element assignment failed.\")\n"+
00369         "test.assertMsg(m[1][1] == 4.33, \"Matrix element assignment failed.\")\n"+
00370         "test.assertMsg(m[2][2] == 5.33, \"Matrix element assignment failed.\")\n"+
00371         "test.assertMsg(m[8][10] == 6.33, \"Matrix element assignment failed.\")\n"+
00372         "var matrix m2 = m;\n"
00373         "test.assertMsg(m2[0][0] == 3.33, \"Matrix assignment failed.\")\n"+
00374         "test.assertMsg(m2[1][1] == 4.33, \"Matrix assignment failed.\")\n"+
00375         "test.assertMsg(m2[2][2] == 5.33, \"Matrix assignment failed.\")\n"+
00376         "test.assertMsg(m2[8][10] == 6.33, \"Matrix assignment failed.\")\n"+
00377         "var cstructv structv = cstructv(3)\n"+ // vector<struct> of size 10
00378         "structv[1].a.b = 3.33\n"+
00379         "test.assertMsg(structv[1].a.b == 3.33, \"Sequence of struct element assignment failed.\")\n"+
00380         "structv[1].av[3].b = 4.33\n"+
00381         "test.assertMsg(structv[1].av[3].b == 4.33, \"Sequence of struct element assignment failed.\")\n"+
00382         "}";
00383     // execute
00384     executePrograms(prog);
00385 }
00386 
00391 BOOST_AUTO_TEST_CASE( testConversions )
00392 {
00393     string prog = string("program x {\n") +
00394         "var int i = 3.0\n" +
00395         "var double d = float(10.0*i)\n"+
00396         "do test.assert( float(d) == float(30.0) )\n" +
00397         "var float f = 5\n" +
00398         "set f = double(5) * double(-1) + i\n" +
00399         "set i = f\n" +
00400         "set f = i\n" +
00401         "set i = double(float(int(f)))\n" +
00402         "set f = int(float(double(int(3.333))))\n" +
00403         "do test.assert( f == 3 )\n" +
00404         "}";
00405     // execute
00406     executePrograms(prog);
00407 }
00408 
00412 BOOST_AUTO_TEST_CASE( testHex )
00413 {
00414     string prog = string("program x {\n") +
00415         "var uint i = 0xabc\n" +
00416         "test.assert( i == 0xabc )\n"+
00417         "test.assert( i == 2748 )\n"+
00418         "i = 0Xcba\n" +
00419         "test.assert( i == 0Xcba )\n"+
00420         "test.assert( i == 3258 )\n"+
00421         "i = 0XABC\n" +
00422         "test.assert( i == 0XABC )\n"+
00423         "test.assert( i == 2748 )\n"+
00424         "i = 0xCBA\n" +
00425         "test.assert( i == 0xCBA )\n"+
00426         "test.assert( i == 3258 )\n"+
00427         "}";
00428     // execute
00429     executePrograms(prog);
00430 }
00431 
00435 BOOST_AUTO_TEST_CASE( testProperties )
00436 {
00437     string prog = string("program x {\n") +
00438         "do test.assert( Double1 == 1.234 )\n" +
00439         "do test.assert( Double2 == 2.234 )\n" +
00440         "do test.assert( Double3 == 3.234 )\n" +
00441         "do test.assert( Collection.Double1 == 1.234 )\n" +
00442         "do test.assert( Collection.Double3 == 3.234 )\n" +
00443         "do test.assert( Collection.Collection.Double1 == 1.234 )\n" +
00444         "do test.assert( Collection.Collection.Collection.Double3 == 3.234 )\n" +
00445         "do test.assert( V[0] == 4.0 )\n" +
00446         "do test.assert( V[1] == 4.0 )\n" +
00447         "do test.assert( V[2] == 4.0 )\n" +
00448         "do test.assert( V[3] == 4.0 )\n" +
00449         //"do test.assert( V.size() == 4 )\n" +
00450         "set Double1 = 4.321\n" +
00451         "set Double2 = Double1\n" +
00452         // -> = 10
00453         "set Collection.Double3 = 0.3\n" +
00454         "set V[0] = 0.321\n" +
00455         "set V[1] = 1.321\n" +
00456         "set V[2] = 2.321\n" +
00457         "set V[3] = 3.321\n" +
00458         "do test.assert( Double1 == 4.321 )\n" +
00459         "do test.assert( Double2 == 4.321 )\n" +
00460         "do test.assert( Double3 == 0.3 )\n" +
00461         "set Collection.Collection.Collection.Double3 = 3.0\n" +
00462         "do test.assert( Double3 == 3.0 )\n" +
00463         "}";
00464 
00465     Property<PropertyBag> pb("Collection","");
00466     Property<double> pd1("Double1","",1.234);
00467     Property<double> pd2("Double2","",2.234);
00468     Property<double> pd3("Double3","",3.234);
00469 
00470     Property< std::vector<double> > pv("V","",std::vector<double>(4, 4.0) );
00471 
00472     pb.value().addProperty( pd1 );
00473     pb.value().addProperty( pd3 );
00474     pb.value().addProperty( pb ); // yep, recursive !
00475 
00476     tc->properties()->addProperty( pd1 );
00477     tc->properties()->addProperty( pd2 );
00478     tc->properties()->addProperty( pd3 );
00479     tc->properties()->addProperty( pb );
00480     tc->properties()->addProperty( pv );
00481 
00482     // execute
00483     executePrograms(prog);
00484 
00485     BOOST_CHECK_EQUAL( 4.321, pd1.get() );
00486     BOOST_CHECK_EQUAL( 3.0, pd3.get() );
00487     BOOST_CHECK_EQUAL( 0.321, pv.value()[0] );
00488     BOOST_CHECK_EQUAL( 1.321, pv.value()[1] );
00489     BOOST_CHECK_EQUAL( 2.321, pv.value()[2] );
00490     BOOST_CHECK_EQUAL( 3.321, pv.value()[3] );
00491 }
00492 
00493 BOOST_AUTO_TEST_CASE( testOperatorOrder )
00494 {
00495     string prog = string("program x {\n") +
00496         "do test.assert( 6/2*4 == 12 )\n" + // not: 0
00497         "do test.assert( 6*2/4 == 3 )\n" +  // not: 0
00498         "do test.assert( 3+2*5 == 13)\n" +  // not: 30
00499         "do test.assert( 3 < 2 != 5 > 1)\n" +
00500         "do test.assert( 3*2 <= 12/2 )\n" +
00501         "do test.assert( 3*2 < 6+1 )\n" +
00502         "do test.assert( 6 - 9 % 2*3 ==  15/3 % 3 + 1 )\n" + // 3 == 3
00503         "do test.assert( 3*(2+1) == 9 )\n" +
00504         "do test.assert( 1 - 1 + 5 == 5 )\n" +  // not: -5
00505         "var int a,b,c;\n" +
00506         " a = b = c = 3;\n" +  // not 0,0,3
00507         "do test.assertEqual( a, 3 )\n" +
00508         "do test.assertEqual( b, 3 )\n" +
00509         "do test.assertEqual( c, 3 )\n" +
00510         "}";
00511     // execute
00512     executePrograms(prog);
00513 }
00514 
00515 BOOST_AUTO_TEST_CASE( testGlobals )
00516 {
00517     GlobalsRepository::Instance()->setValue( new Constant<double>("cd_num", 3.33));
00518     GlobalsRepository::Instance()->setValue( new Constant<string>("c_string", "Hello World!"));
00519     GlobalsRepository::Instance()->setValue( new Attribute<double>("d_num", 3.33));
00520     string prog = string("program x {\n") +
00521         "do test.assert( cd_num == 3.33 )\n" +
00522         "do test.assert( cd_num == d_num )\n" +
00523         "do test.assert( c_string == \"Hello World!\")\n" +
00524         "set d_num = 6.66\n" +
00525         "do test.assert( d_num == 6.66 )\n" +
00526         "}";
00527     // execute
00528     executePrograms(prog);
00529 }
00530 
00531 BOOST_AUTO_TEST_CASE( testFlowStatus )
00532 {
00533     BOOST_CHECK (GlobalsRepository::Instance()->getValue("NewData") );
00534     BOOST_CHECK (GlobalsRepository::Instance()->getValue("OldData") );
00535     BOOST_CHECK (GlobalsRepository::Instance()->getValue("NoData") );
00536     string prog = string("program x {\n") +
00537         "do test.assert( NewData )\n" +
00538         "do test.assert( OldData )\n" +
00539         "do test.assert( !bool(NoData) )\n" +
00540         "do test.assert( NewData > NoData )\n" +
00541         "do test.assert( NewData > OldData )\n" +
00542         "do test.assert( OldData > NoData )\n" +
00543         "do test.assert( OldData == OldData )\n" +
00544         "if ( bool(NewData) && OldData ) then {\n" +
00545         "} else {\n" +
00546         "   do test.assert(false)\n" +
00547         "}\n" +
00548         "if ( bool(NoData) ) then {\n" +
00549         "   do test.assert(false)\n" +
00550         "}\n" +
00551         "if ( !bool(NoData) ) then {} else {\n" +
00552         "   do test.assert(false)\n" +
00553         "}\n" +
00554         "}";
00555     // execute
00556     executePrograms(prog);
00557 }
00558 BOOST_AUTO_TEST_SUITE_END()
00559 
00560 void TypesTest::executePrograms(const std::string& prog )
00561 {
00562     BOOST_CHECK( tc->engine() );
00563 
00564     Parser::ParsedPrograms pg_list;
00565     try {
00566         pg_list = parser.parseProgram( prog, tc );
00567     }
00568     catch( const file_parse_exception& exc )
00569         {
00570             BOOST_REQUIRE_MESSAGE( false , exc.what());
00571         }
00572     if ( pg_list.empty() )
00573         {
00574             BOOST_REQUIRE( false && "Got empty test program." );
00575         }
00576 
00577     BOOST_CHECK( sa->loadProgram( *pg_list.begin() ) );
00578     BOOST_CHECK( (*pg_list.begin())->start() );
00579 
00580     BOOST_CHECK( SimulationThread::Instance()->run(1000) );
00581 
00582     if ( (*pg_list.begin())->inError() ) {
00583         stringstream errormsg;
00584         errormsg << " Program error on line " << (*pg_list.begin())->getLineNumber() <<"."<<endl;
00585         BOOST_CHECK_MESSAGE( false, errormsg.str() );
00586     }
00587 
00588     if ( (*pg_list.begin())->isRunning() ) {
00589         stringstream errormsg;
00590         errormsg << " Program still running on line " << (*pg_list.begin())->getLineNumber() <<"."<<endl;
00591         BOOST_CHECK_MESSAGE( false, errormsg.str() );
00592     }
00593 
00594     if ( (*pg_list.begin())->isStopped() == false ) {
00595         stringstream errormsg;
00596         errormsg << " Program not stopped on line " << (*pg_list.begin())->getLineNumber() <<"."<<endl;
00597         BOOST_CHECK_MESSAGE( false , errormsg.str() );
00598     }
00599     BOOST_CHECK( (*pg_list.begin())->stop() );
00600     BOOST_CHECK( sa->unloadProgram( (*pg_list.begin())->getName() ) );
00601 }
00602 
00603 void TypesTest::executeStates(const std::string& state )
00604 {
00605     BOOST_CHECK( tc->engine() );
00606     Parser::ParsedStateMachines pg_list;
00607     try {
00608         pg_list = parser.parseStateMachine( state, tc );
00609     }
00610     catch( const file_parse_exception& exc )
00611         {
00612             BOOST_REQUIRE_MESSAGE( false , exc.what());
00613         }
00614     if ( pg_list.empty() )
00615         {
00616             BOOST_REQUIRE_MESSAGE( false, "Parser returned no state machines to execute." );
00617         }
00618 
00619     BOOST_CHECK( sa->loadStateMachine( *pg_list.begin() ) );
00620     BOOST_CHECK( (*pg_list.begin())->activate() );
00621     BOOST_CHECK( (*pg_list.begin())->start() );
00622 
00623     BOOST_CHECK( SimulationThread::Instance()->run(1000) );
00624     if ( (*pg_list.begin())->inError() ) {
00625         stringstream errormsg;
00626         errormsg << " State error on line " << (*pg_list.begin())->getLineNumber() <<"."<<endl;
00627         BOOST_CHECK_MESSAGE( false , errormsg.str());
00628     }
00629 
00630     BOOST_CHECK( (*pg_list.begin())->stop() );
00631     BOOST_CHECK( SimulationThread::Instance()->run(100) );
00632     BOOST_CHECK( (*pg_list.begin())->deactivate() );
00633 
00634     sa->unloadStateMachine( (*pg_list.begin())->getName() );
00635 }


rtt
Author(s): RTT Developers
autogenerated on Wed Aug 26 2015 16:16:20