rtstring_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 
12 #include <rtt-config.h>
17 #include <scripting/DumpObject.hpp>
18 #include <scripting/Parser.hpp>
19 
20 #include <Service.hpp>
21 #include <TaskContext.hpp>
22 #include <OperationCaller.hpp>
23 #include <Port.hpp>
25 #include "operations_fixture.hpp"
26 
27 #include <string>
28 #include <iostream>
29 #include <sstream>
30 
31 using namespace RTT;
32 using namespace RTT::detail;
33 using namespace std;
34 
36  : public OperationsFixture
37 {
38 public:
41 
42  void doState(const std::string& name, const std::string& prog, TaskContext*, bool test=true );
43  void parseState( const std::string& prog, TaskContext*, bool test=true );
44  void runState(const std::string& name, TaskContext*, bool test=true );
45  void checkState( const std::string& name, TaskContext*, bool test=true );
46  void finishState( std::string const& name, TaskContext*, bool test=true );
47 
48  std::string sline;
49 public:
51  :
52  sa( ScriptingService::Create(tc) )
53  {
54  tc->stop();
55  tc->setActivity( new SimulationActivity(0.001) );
56 
57  tc->start();
58  i = 0;
60  }
62  }
63 };
64 
65 BOOST_FIXTURE_TEST_SUITE( RtStringTestSuite, RtStringStateTest )
66 
67 BOOST_AUTO_TEST_CASE( testCreateRtString )
68 {
69  string prog = string("StateMachine X {\n")
70  + " initial state INIT {\n"
71  + " var rt_string s1\n"
72  + " transitions { select FINI }\n"
73  + " }\n"
74  + " final state FINI {\n"
75  + " }\n"
76  + " }\n"
77  + " RootMachine X x\n" // instantiate a non hierarchical SC
78  ;
79 
80  this->doState("x", prog, tc );
81  this->finishState( "x", tc );
82 }
83 
84 BOOST_AUTO_TEST_CASE( testCreateRtstringOfFixedSize )
85 {
86  string prog = string("StateMachine X {\n")
87  + " initial state INIT {\n"
88  + " var rt_string s1(22)\n"
89  + " transitions { select FINI }\n"
90  + " }\n"
91  + " final state FINI {\n"
92  + " }\n"
93  + " }\n"
94  + " RootMachine X x\n" // instantiate a non hierarchical SC
95  ;
96 
97  this->doState("x", prog, tc );
98  this->finishState( "x", tc );
99 }
100 
101 #if 0
102 // this is incorrect syntax:
103 BOOST_AUTO_TEST_CASE( testCreateRtstringFromCharPtr )
104 {
105  string prog = string("StateMachine X {\n")
106  + " initial state INIT {\n"
107  + " var rt_string s1(\"hello world\")\n"
108  + " transitions { select FINI }\n"
109  + " }\n"
110  + " final state FINI {\n"
111  + " }\n"
112  + " }\n"
113  + " RootMachine X x\n" // instantiate a non hierarchical SC
114  ;
115  this->doState("x", prog, tc );
116  this->finishState( "x", tc );
117 }
118 #endif
119 
120 BOOST_AUTO_TEST_CASE( testCreateRtstringFromRtString )
121 {
122  string prog = string("StateMachine X {\n")
123  + " initial state INIT {\n"
124  + " var rt_string s1 = rt_string(\"hello world\")\n"
125  + " transitions { select FINI }\n"
126  + " }\n"
127  + " final state FINI {\n"
128  + " }\n"
129  + " }\n"
130  + " RootMachine X x\n" // instantiate a non hierarchical SC
131  ;
132 
133  this->doState("x", prog, tc );
134  this->finishState( "x", tc );
135 }
136 
137 BOOST_AUTO_TEST_CASE( testConcatRtstring )
138 {
139  string prog = string("StateMachine X {\n")
140  + " initial state INIT {\n"
141  + " var rt_string s1\n"
142  + " entry {\n"
143  + " s1 = rt_string(\"Multiply \")\n"
144  + " s1 = s1 + 10\n"
145  + " s1 = s1 + rt_string(\" times \")\n"
146  + " s1 = s1 + 33.3\n"
147  + " s1 = rt_string(\"Now: \") + 1 + rt_string(\"st \") + s1 + '!'\n"
148  + " }\n"
149  + " transitions { select FINI }\n"
150  + " }\n"
151  + " final state FINI {\n"
152  + " }\n"
153  + " }\n"
154  + " RootMachine X x\n" // instantiate a non hierarchical SC
155  ;
156 
157  this->doState("x", prog, tc );
158  this->finishState( "x", tc );
159 }
160 
161 BOOST_AUTO_TEST_CASE( testRtstringConversion )
162 {
163  string prog = string("StateMachine X {\n")
164  + " initial state INIT {\n"
165  + " var rt_string rts1\n"
166  + " var string s1\n"
167  + " entry {\n"
168  + " s1 = \"s1\"\n"
169  + " rts1 = rt_string(\"rts1\")\n"
170  + " test.assert(s1 == \"s1\")\n"
171  + " test.assert(rts1 == rt_string(\"rts1\"))\n"
172  + " s1 = string(rts1)\n"
173  + " test.assert(s1 == string(rts1))\n"
174  + " test.assert(s1 == \"rts1\")\n"
175  + " s1 = \"s1\"\n"
176  + " rts1 = rt_string(s1)\n"
177  + " test.assert(rts1 == rt_string(\"s1\"))\n"
178  + " s1 = string( rt_string(\" s1 \") )\n"
179  + " rts1 = rt_string( string(\" rts1 \") )\n"
180  + " test.assert(s1 == \" s1 \")\n"
181  + " test.assert(rts1 == rt_string(\" rts1 \"))\n"
182  + " }\n"
183  + " transitions { select FINI }\n"
184  + " }\n"
185  + " final state FINI {\n"
186  + " }\n"
187  + " }\n"
188  + " RootMachine X x\n" // instantiate a non hierarchical SC
189  ;
190 
191  this->doState("x", prog, tc );
192  this->finishState( "x", tc );
193 }
194 
195 
197 
198 void RtStringStateTest::doState( const std::string& name, const std::string& prog, TaskContext* tc, bool test )
199 {
200  BOOST_CHECK( tc->engine() );
201 
202  parseState( prog, tc, test);
203  runState(name, tc, test);
204  checkState(name, tc, test);
205 }
206 
207 void RtStringStateTest::parseState(const std::string& prog, TaskContext* tc, bool test )
208 {
209  // Alternative way: test ScriptingService as well.
210  try {
211  sa->loadStateMachines( prog, std::string("state_test.cpp"), true );
212  }
213  catch( const file_parse_exception& exc )
214  {
215  BOOST_REQUIRE_MESSAGE( !test, exc.what() );
216  }
217  catch( const parse_exception& exc )
218  {
219  BOOST_REQUIRE_MESSAGE( !test, exc.what() );
220  }
221  catch( const program_load_exception& e)
222  {
223  BOOST_REQUIRE_MESSAGE( !test, e.what() );
224  }
225  catch( const std::exception& e ) {
226  BOOST_CHECK_MESSAGE( !test , e.what());
227  BOOST_REQUIRE_MESSAGE( !test, "Uncaught Processor load exception" );
228  }
229 }
230 
231 void RtStringStateTest::runState(const std::string& name, TaskContext* tc, bool test )
232 {
233  StateMachinePtr sm = sa->getStateMachine(name);
234  BOOST_REQUIRE( sm );
235  sm->trace(true);
236  OperationCaller<bool(StateMachine*)> act = tc->provides(name)->getOperation("activate");
237  OperationCaller<bool(StateMachine*)> autom = tc->provides(name)->getOperation("automatic");
238  BOOST_CHECK( act(sm.get()) );
239  BOOST_CHECK( SimulationThread::Instance()->run(1) );
240  BOOST_CHECK_MESSAGE( sm->isActive(), "Error : Activate Command for '"+sm->getName()+"' did not have effect." );
241  BOOST_CHECK( autom(sm.get()) || !test );
242 
243  BOOST_CHECK( SimulationThread::Instance()->run(1000) );
244 }
245 
246 void RtStringStateTest::checkState(const std::string& name, TaskContext* tc, bool test )
247 {
248  StateMachinePtr sm = sa->getStateMachine(name);
249  BOOST_REQUIRE( sm );
250  if ( test ) {
251  // check error status of parent :
252  BOOST_CHECK_MESSAGE( sm->isActive(), "Error : State Context '"+sm->getName()+"' did not get activated." );
253  stringstream errormsg;
254  int line = sm->getLineNumber();
255  errormsg <<" in StateMachine "+sm->getName()
256  <<" in state "<< (sm->currentState() ? sm->currentState()->getName() : "(null)")
257  <<" on line " << line <<" of that StateMachine:"<<endl;
258  {
259  stringstream sctext( sm->getText() );
260  int cnt = 1;
261  while ( cnt++ <line && sctext ) {
262  string garbage;
263  getline( sctext, garbage, '\n' );
264  }
265  getline( sctext, sline, '\n' );
266  }
267  errormsg <<"here > " << sline << endl;
268  if ( sm->inError() ) {
271  }
272  BOOST_CHECK_MESSAGE( sm->inError() == false, "Runtime error (inError() == true) encountered" + errormsg.str() );
273  // check error status of all children:
274  StateMachine::ChildList cl = sm->getChildren();
275  StateMachine::ChildList::iterator clit = cl.begin();
276  while( clit != cl.end() ) {
277  line = (*clit)->getLineNumber();
278  {
279  stringstream sctext( (*clit)->getText() );
280  int cnt = 1;
281  while ( cnt++ <line && sctext ) {
282  string garbage;
283  getline( sctext, garbage, '\n' );
284  }
285  getline( sctext, sline, '\n' );
286  }
287  stringstream cerrormsg;
288  if ( (*clit)->currentState() )
289  cerrormsg <<" in child "<< (*clit)->getName() <<" in state "<<(*clit)->currentState()->getName()<< " on line " << (*clit)->getLineNumber() <<" of that StateMachine."<<endl <<"here > " << sline << endl;
290  else
291  cerrormsg <<" child "<< (*clit)->getName() << " (deactivated) on line " << (*clit)->getLineNumber() <<" of that StateMachine."<<endl<<"here > " << sline << endl;
292 
293  BOOST_CHECK_MESSAGE( (*clit)->inError() == false, "Runtime error (inError() == true) encountered" + cerrormsg.str() );
294  if ( (*clit)->inError() == false && sm->inError() == true) {
295  cout << "Child Status:" << cerrormsg.str() << endl;
296  }
297  ++clit;
298  }
299  }
300 }
301 
302 void RtStringStateTest::finishState(std::string const& name, TaskContext* tc, bool test)
303 {
304  StateMachinePtr sm = sa->getStateMachine(name);
305  BOOST_REQUIRE( sm );
306  BOOST_CHECK( sa->getStateMachine( name )->stop() );
307  BOOST_CHECK( SimulationThread::Instance()->run(500) );
308  if (test) {
309  stringstream errormsg;
310  errormsg << " on line " << sm->getLineNumber() <<", status is "<< sa->getStateMachineStatusStr(name) <<endl <<"here > " << sline << endl;;
311  BOOST_CHECK_MESSAGE( sm->isStopped(), "StateMachine stalled " + errormsg.str() );
312  }
313  // you can call deactivate even when the proc is not running.
314  // but deactivation may be 'in progress if exit state has commands in it.
315  BOOST_CHECK( sa->getStateMachine( name )->deactivate() );
316  BOOST_CHECK( SimulationThread::Instance()->run(200) );
317  if ( sm->isActive() )
318  BOOST_CHECK( sa->getStateMachine( name )->deactivate() );
319  BOOST_CHECK( SimulationThread::Instance()->run(200) );
320  BOOST_CHECK( sa->getStateMachine( name )->isActive() == false );
321 
322  // only stop now, since deactivate won't work if simtask not running.
323  tc->stop();
324 
325  try {
326  BOOST_CHECK( sa->unloadStateMachine( name ) );
327  }
328  catch( const program_unload_exception& e)
329  {
330  BOOST_REQUIRE_MESSAGE( false, e.what() );
331  }
332  catch( ... ) {
333  BOOST_REQUIRE_MESSAGE( false, "Uncaught Processor unload exception" );
334  }
335 
336 }
void checkState(const std::string &name, TaskContext *, bool test=true)
const std::string what() const
#define BOOST_FIXTURE_TEST_SUITE(suite_name, F)
boost::shared_ptr< ScriptingService > shared_ptr
Service::shared_ptr provides()
virtual const std::string what() const =0
void parseState(const std::string &prog, TaskContext *, bool test=true)
virtual bool stop()
#define BOOST_AUTO_TEST_SUITE_END()
Definition: mystd.hpp:163
void runState(const std::string &name, TaskContext *, bool test=true)
static SimulationThreadPtr Instance(double period=0.001)
void DumpObject(Service::shared_ptr peer)
Definition: DumpObject.cpp:54
printstream cout
Definition: rtstreams.cpp:45
void finishState(std::string const &name, TaskContext *, bool test=true)
This class is the public interface to the Orocos Program Parser Framework. It parsers Orocos program ...
Definition: Parser.hpp:65
boost::shared_ptr< StateMachine > StateMachinePtr
basic_ostreams & endl(basic_ostreams &s)
Definition: rtstreams.cpp:110
std::vector< StateMachinePtr > ChildList
ScriptingService::shared_ptr sa
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:53
A SimulationActivity is a PeriodicActivity which is used for simulation.
BOOST_AUTO_TEST_CASE(testCreateRtString)


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