public
Fork of psoetens/orocos-rtt
Description: Orocos Real-Time Toolkit
Homepage: www.orocos.org/rtt
Clone URL: git://github.com/doudou/orocos-rtt.git
Janosch Machowinski (author)
Wed Nov 26 07:50:12 -0800 2008
Sylvain Joyeux (committer)
Fri Dec 19 07:31:38 -0800 2008
orocos-rtt / tests / dispatch_test.cpp
100644 300 lines (247 sloc) 11.304 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/***************************************************************************
tag: Peter Soetens Mon Jan 10 15:59:51 CET 2005 dispatch_test.cpp
 
dispatch_test.cpp - description
-------------------
begin : Mon January 10 2005
copyright : (C) 2005 Peter Soetens
email : peter.soetens@mech.kuleuven.ac.be
 
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
 
 
 
#include "dispatch_test.hpp"
#include <unistd.h>
#include <iostream>
#include <sstream>
#include <FunctionGraph.hpp>
#include <SimulationThread.hpp>
#include <Method.hpp>
#include <Command.hpp>
 
using namespace std;
 
// Registers the fixture into the 'registry'
CPPUNIT_TEST_SUITE_REGISTRATION( DispatchTest );
 
DispatchTest::DispatchTest()
    : gtc("root"),
      mtc("space"),
      ltc("subspace"),
      gtask(0.1, gtc.engine() ),
      mtask(0.05, mtc.engine()),
      ltask(0.01, ltc.engine())
{}
 
 
void
DispatchTest::setUp()
{
    ltc.clear();
    mtc.clear();
    gtc.clear();
    // ltc has a test object
    ltc.addObject( this->createObject("test", ltc.engine()->commands()) );
    // mtc has two methods.
    mtc.addObject( this->createObject("test", mtc.engine()->commands()) );
 
    gtc.addPeer( &mtc );
    mtc.connectPeers( &ltc );
 
}
 
 
void
DispatchTest::tearDown()
{
    gtc.removePeer( "space" );
    ltc.disconnectPeers( "subspace" );
}
 
 
bool DispatchTest::assertBool( bool b) {
    return b;
}
bool DispatchTest::assertMsg( bool b, const std::string& msg) {
    if ( b == false )
        cout << "Asserted :" << b << " with :" << msg << endl;
    return b;
}
 
 
TaskObject* DispatchTest::createObject(string a, CommandProcessor* cp)
{
    TaskObject* dat = new TaskObject(a);
    dat->methods()->addMethod( method( "assert", &DispatchTest::assertBool, this), "Assert", "bool", "" );
    dat->methods()->addMethod( method( "assertMsg", &DispatchTest::assertMsg, this), "Assert message", "bool", "", "text", "text" );
    dat->methods()->addMethod( method( "isTrue", &DispatchTest::assertBool, this), "Identity function", "bool", "" );
    dat->commands()->addCommand( command( "instantDone", &DispatchTest::true_genCom,
                                      &DispatchTest::true_gen, this, cp),
                                      "returns immediately" );
    dat->commands()->addCommand( command( "neverDone", &DispatchTest::true_genCom,
                                    &DispatchTest::false_gen, this, cp),
                                    "returns never" );
    dat->commands()->addCommand( command( "instantNotDone", &DispatchTest::true_genCom,
                                         &DispatchTest::true_gen, this, cp, false),
                                         "returns never");
    dat->commands()->addCommand( command( "instantFail", &DispatchTest::false_genCom,
                                      &DispatchTest::true_gen, this, cp),
                                      "fails immediately" );
    dat->commands()->addCommand( command( "totalFail", &DispatchTest::false_genCom,
                                    &DispatchTest::false_gen, this, cp),
                                    "fails in command and condition" );
    return dat;
}
 
 
void DispatchTest::testParseDispatch()
{
    // this is a global program requesting a method on a local
    // task/processor (ie assert) and a command (instantDone)
    string prog = string("program x { do space.subspace.test.assertMsg(true,\"tpdtrue\") \n")
        + " if space.subspace.test.assert(true) then \n"
        + " do nothing\n"
        + " do space.subspace.test.instantDone() \n"
// + " do space.assertMsg(true,\"tpdfail\")\n"
// + " do this.space.assertMsg(true,\"donotreach\")\n"
        + "}";
 
    this->doDispatch( prog, &gtc );
 
    CPPUNIT_ASSERT( gtc.engine()->programs()->getProgramStatus("x") != ProgramInterface::Status::error );
    CPPUNIT_ASSERT( gtc.engine()->programs()->getProgramStatus("x") != ProgramInterface::Status::running );
    CPPUNIT_ASSERT( gtc.engine()->programs()->getProgramStatus("x") == ProgramInterface::Status::stopped );
 
    this->finishDispatch( &gtc, "x");
}
 
void DispatchTest::testDispatchFailure()
{
    // this is a global program requesting a command on a local
    // task/processor (ie instantFail).
    string prog = string("program x { do space.subspace.test.instantFail() \n")
        + "}";
 
    this->doDispatch( prog, &gtc );
 
    CPPUNIT_ASSERT( gtc.engine()->programs()->getProgramStatus("x") == ProgramInterface::Status::error );
 
    this->finishDispatch( &gtc, "x");
}
void DispatchTest::testDispatchCondition()
{
    // see if checking a remote condition works
    // also tests peerparser in expressions
    string prog = string("program x { if ( space.subspace.test.assert(true) ) then \n")
        + "do space.subspace.test.instantDone() \n"
        + "else \n"
        + "do space.subspace.test.instantFail() \n"
        + "if ( space.subspace.test.assert(false) ) then \n"
        + "do space.subspace.test.instantFail() \n"
        + "else \n"
        + "do space.subspace.test.instantDone() \n"
        + " }";
    this->doDispatch( prog, &gtc );
 
    stringstream msg;
    msg << "Status was not 'stopped', but "+gtc.engine()->programs()->getProgramStatusStr("x");
    msg << " on line " << gtc.engine()->programs()->getProgram("x")->getLineNumber();
    CPPUNIT_ASSERT_MESSAGE(msg.str(),
                           gtc.engine()->programs()->getProgramStatus("x") == ProgramInterface::Status::stopped);
 
    this->finishDispatch( &gtc, "x");
}
 
void DispatchTest::testDispatchAnd()
{
    // see if checking a remote condition works
    string prog = string("program x { do space.subspace.test.assert(true)\n")
        + "and space.subspace.test.assert(true) \n"
        + "and space.subspace.test.assert(true) \n"
        + "do space.subspace.test.instantDone() \n"
        + "and space.subspace.test.instantDone() \n"
        + "and space.subspace.test.instantDone() \n"
        + " }";
    this->doDispatch( prog, &gtc );
 
    stringstream msg;
    msg << "Status was not 'stopped', but "+gtc.engine()->programs()->getProgramStatusStr("x");
    msg << " on line " << gtc.engine()->programs()->getProgram("x")->getLineNumber();
    CPPUNIT_ASSERT_MESSAGE(msg.str(),
                           gtc.engine()->programs()->getProgramStatus("x") == ProgramInterface::Status::stopped);
 
    this->finishDispatch( &gtc, "x");
}
 
void DispatchTest::testDispatchTry()
{
    // see if checking a remote condition works
    string prog = string("program x { try space.subspace.test.assert(false)\n")
        + "try space.subspace.test.assert(true) \n"
        + "and space.subspace.test.assert(false) \n"
        + "and space.subspace.test.assert(true) \n"
        + "try space.subspace.test.instantFail()\n"
        + "try space.subspace.test.instantDone() \n"
        + "and space.subspace.test.instantFail() \n"
        + "and space.subspace.test.instantDone() \n"
        + " }";
    this->doDispatch( prog, &gtc );
 
    CPPUNIT_ASSERT( gtc.engine()->programs()->getProgramStatus("x") != ProgramInterface::Status::error );
    stringstream msg;
    msg << "Status was not 'stopped', but "+gtc.engine()->programs()->getProgramStatusStr("x");
    msg << " on line " << gtc.engine()->programs()->getProgram("x")->getLineNumber();
    CPPUNIT_ASSERT_MESSAGE(msg.str(),
                           gtc.engine()->programs()->getProgramStatus("x") == ProgramInterface::Status::stopped);
 
    this->finishDispatch( &gtc, "x");
}
 
void DispatchTest::testDispatchUntil()
{
    // see if checking a remote condition works
    string prog = string("program x { do space.subspace.test.instantDone()\n")
        + "until { \n"
        + " if time > 10 ms then continue \n" // test in simulation takes far less than 1 second
        + "} \n"
        + "do space.subspace.test.instantDone()\n"
        + "until { \n"
        + " if done then continue \n"
        + "} \n"
        + " }";
    this->doDispatch( prog, &gtc );
 
    stringstream msg;
    msg << "Status was not 'stopped', but "+gtc.engine()->programs()->getProgramStatusStr("x");
    msg << " on line " << gtc.engine()->programs()->getProgram("x")->getLineNumber();
    CPPUNIT_ASSERT_MESSAGE(msg.str(),
                           gtc.engine()->programs()->getProgramStatus("x") == ProgramInterface::Status::stopped);
 
    this->finishDispatch( &gtc, "x");
}
 
void DispatchTest::testDispatchUntilFail()
{
    // see if checking a remote condition works
    string prog = string("program x { do space.subspace.test.instantFail()\n")
        + "until { \n"
        + " if done then continue \n" // program should go into error
        + "} \n"
        + " }";
    this->doDispatch( prog, &gtc );
 
    CPPUNIT_ASSERT( gtc.engine()->programs()->getProgramStatus("x") == ProgramInterface::Status::error );
 
    this->finishDispatch( &gtc, "x");
}
 
void DispatchTest::testDispatchMany()
{
    // XXX not a valid test. send not present in Orocos, this looks like 'try'
    // a program which must not fail, even if the command failes.
    string prog = string("program x { ")
        +" do space.subspace.test.instantDone()\n"
        +" do space.subspace.test.instantDone()\n"
        +" do space.subspace.test.instantDone()\n"
        +" do space.subspace.test.instantDone()\n"
        +" }";
    this->doDispatch( prog, &gtc );
 
    CPPUNIT_ASSERT( gtc.engine()->programs()->getProgramStatus("x") != ProgramInterface::Status::error );
    CPPUNIT_ASSERT( gtc.engine()->programs()->getProgramStatus("x") == ProgramInterface::Status::stopped );
 
    this->finishDispatch( &gtc, "x" );
}
 
 
void DispatchTest::doDispatch( const std::string& prog, TaskContext* tc )
{
    Parser::ParsedPrograms pg_list;
    try {
        pg_list = parser.parseProgram( prog, tc );
    }
    catch( const file_parse_exception& exc )
        {
            CPPUNIT_ASSERT_MESSAGE( exc.what(), false );
        }
    if ( pg_list.empty() )
        {
            CPPUNIT_ASSERT( false );
        }
    CPPUNIT_ASSERT( tc->engine()->programs()->loadProgram( *pg_list.begin() ) );
    CPPUNIT_ASSERT(ltask.start());
    CPPUNIT_ASSERT(mtask.start());
    CPPUNIT_ASSERT(gtask.start());
    CPPUNIT_ASSERT( tc->engine()->programs()->getProgram( (*pg_list.begin())->getName() )->start() );
 
    SimulationThread::Instance()->run(1000);
}
 
void DispatchTest::finishDispatch(TaskContext* tc, std::string prog_name)
{
    CPPUNIT_ASSERT(gtask.stop());
    CPPUNIT_ASSERT(mtask.stop());
    CPPUNIT_ASSERT(ltask.stop());
    tc->engine()->programs()->getProgram( prog_name )->stop();
    CPPUNIT_ASSERT( tc->engine()->programs()->unloadProgram( prog_name ) );
 
}