-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfunction_test.cpp
492 lines (437 loc) · 15 KB
/
function_test.cpp
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/***************************************************************************
tag: Peter Soetens Mon Jan 10 15:59:51 CET 2005 function_test.cpp
function_test.cpp - description
-------------------
begin : Mon January 10 2005
copyright : (C) 2005 Peter Soetens
email : [email protected]
***************************************************************************
* *
* 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 "unit.hpp"
#include "operations_fixture.hpp"
#include <scripting/Parser.hpp>
#include <scripting/FunctionGraph.hpp>
#include <scripting/ScriptingService.hpp>
#include <Service.hpp>
#include <OperationCaller.hpp>
class FunctionsFixture : public OperationsFixture
{
public:
FunctionsFixture()
: sa( ScriptingService::Create(tc) )
{
tc->stop();
tc->setActivity(new SimulationActivity(0.01));
SimulationThread::Instance()->stop();
tc->start();
}
Parser parser;
ScriptingService::shared_ptr sa;
void doFunction( const std::string& prog, TaskContext*, bool test=true );
void finishFunction( TaskContext* , std::string );
void loopProgram( ProgramInterfacePtr );
};
BOOST_FIXTURE_TEST_SUITE( FunctionsFixtureSuite, FunctionsFixture )
// Registers the fixture into the 'registry'
BOOST_AUTO_TEST_CASE( testSimpleFunction)
{
string prog = string("function foo { \n")
+ " do test.assert( test.isTrue( true ) )\n"
+ "}\n"
+ "program x { \n"
+ " call foo\n"
+ "}";
this->doFunction( prog, tc );
this->finishFunction( tc, "x");
}
BOOST_AUTO_TEST_CASE( testSimpleReturnFunction)
{
string prog = string("int foo { \n")
+ " test.assert( test.isTrue( true ) )\n"
+ " return 3\n"
+ "}\n"
+ "\n"
+ "program x { \n"
+ " test.assert(true);\n"
+ " call foo\n"
+ " test.assert(true);\n"
+ "}";
this->doFunction( prog, tc );
this->finishFunction( tc, "x");
}
BOOST_AUTO_TEST_CASE( testExportFunction)
{
string prog = string("export function foo { \n")
+ " do test.assert( test.isTrue( true ) )\n"
+ "}\n"
+ "export function foo_args() { \n"
+ " do test.assert( test.isTrue( true ) )\n"
+ "}\n"
+ "program x { \n"
+ " do this.foo()\n"
+ " do this.foo_args()\n"
+ "}";
this->doFunction( prog, tc );
BOOST_CHECK( tc->getOperation("foo") );
this->finishFunction( tc, "x");
}
#ifdef ORO_REMOTING
/**
* Exports foo and foo_args and calls them from C++
* Compare with testReturnExportFunction below that does
* the same, but in scripting.
* This code relies on RemoteOperationCaller and OperationCallerC
*/
BOOST_AUTO_TEST_CASE( testOnlyExportFunction)
{
string prog = string("export function foo { \n")
// + " test.print(\"foo()\")\n"
+ " do test.assert( test.isTrue( true ) )\n"
+ "}\n"
+ "export int foo_ret(double d) { \n"
// + " test.printNumber(\"foo_ret(double d), d is: \", d)\n"
+ " if (true) then\n"
+ " return 3\n"
+ " else\n"
+ " return 5\n"
+ "}\n"
+ "export int foo_args(double d, int v) { \n"
// + " test.printNumber(\"foo_args(double d, int v) v is: \", v)\n"
+ " var double r = 10\n"
+ " if ( d == 3.0 && v == 6) then\n"
+ " set r = +1\n"
+ " else\n"
+ " set r = -1\n"
+ " return r;\n"
+ "}\n";
this->doFunction( prog, tc );
BOOST_CHECK( tc->getOperation("foo") );
// Test call:
{
OperationCaller<void(void)> foo( tc->getOperation("foo"), caller->engine());
BOOST_CHECK( foo.ready() );
foo();
}
#if 0
// Test pure send:
{
OperationCaller<void(void)> foo( tc->getOperation("foo"), caller->engine());
BOOST_CHECK( foo.ready() );
foo.send();
}
// Test send + collect:
{
OperationCaller<void(void)> foo( tc->getOperation("foo"), caller->engine());
BOOST_CHECK( foo.ready() );
SendHandle<void(void)> sh = foo.send();
BOOST_CHECK_EQUAL( sh.collect(), SendSuccess);
}
#endif
// Test call:
{
OperationCaller<int(double)> foo_ret( tc->getOperation("foo_ret"), caller->engine());
BOOST_CHECK( foo_ret.ready() );
int i = 0;
i = foo_ret(3.0);
BOOST_CHECK_EQUAL( i, 3 );
}
#if 0
// Test pure send:
{
OperationCaller<int(double)> foo_ret( tc->getOperation("foo_ret"), caller->engine());
BOOST_CHECK( foo_ret.ready() );
int i = 0;
foo_ret.send(3.0);
}
// Test send + collect:
{
OperationCaller<int(double)> foo_ret( tc->getOperation("foo_ret"), caller->engine());
BOOST_CHECK( foo_ret.ready() );
int i = 0;
SendHandle<int(double)> sh = foo_ret.send(3.0);
BOOST_CHECK_EQUAL( sh.collect(i), SendSuccess);
BOOST_CHECK_EQUAL( i, 3 );
}
#endif
// Test call:
{
BOOST_CHECK( tc->getOperation("foo_args") );
OperationCaller<int(double,int)> foo_args( tc->getOperation("foo_args"), caller->engine());
BOOST_CHECK( foo_args.ready() );
int i = 0;
i = foo_args(-3.0, -6);
BOOST_CHECK_EQUAL( i, -1);
i = 0;
i = foo_args(3.0, 6);
BOOST_CHECK_EQUAL( i, +1);
}
#if 0
// Test pure send:
{
BOOST_CHECK( tc->getOperation("foo_args") );
OperationCaller<int(double,int)> foo_args( tc->getOperation("foo_args"), caller->engine());
BOOST_CHECK( foo_args.ready() );
foo_args.send(-3.0, -6);
foo_args.send(3.0, 6);
}
// Test send + collect:
{
BOOST_CHECK( tc->getOperation("foo_args") );
OperationCaller<int(double,int)> foo_args( tc->getOperation("foo_args"), caller->engine());
BOOST_CHECK( foo_args.ready() );
int i = 0;
SendHandle<int(double,int)> sh = foo_args.send(-3.0, -6);
BOOST_CHECK_EQUAL( sh.collect(i), SendSuccess);
BOOST_CHECK_EQUAL( i, -1);
i = 0;
sh = foo_args.send(3.0, 6);
BOOST_CHECK_EQUAL( sh.collect(i), SendSuccess);
BOOST_CHECK_EQUAL( i, +1);
}
#endif
}
#endif
/**
* Compare to use case above that does the same,
* but in C++
*/
BOOST_AUTO_TEST_CASE( testReturnExportFunction)
{
string prog = string("export function foo { \n")
+ " do test.assert( test.isTrue( true ) )\n"
+ "}\n"
+ "export int foo_ret() { \n"
+ " if (true) then\n"
+ " return 3\n"
+ " else\n"
+ " return 5\n"
+ "}\n"
+ "export int foo_args(double d, int v) { \n"
+ " do test.assert( test.isTrue( true ) )\n"
+ " if ( d == 3.0 && v == 6) then\n"
+ " return +1\n" // 10
+ " else\n"
+ " return -1\n"
+ " return 4\n"
+ "}\n"
+ "program x { \n"
+ " this.foo()\n" // a void function
+ " this.foo_ret()\n"
+ " this.foo_args(3.0,6)\n"
+ " test.assertEqual(this.foo_ret(), 3 )\n"
+ " test.assertEqual(this.foo_args(3.0,6), 1)\n" // 20
+ " test.assertEqual(this.foo_args(0.0,0), -1)\n"
+ "}";
this->doFunction( prog, tc );
BOOST_CHECK( tc->getOperation("foo") );
this->finishFunction( tc, "x");
}
#if 0
// Test removing exported function in infinite loop.
BOOST_AUTO_TEST_CASE( testRemoveFunction)
{
string prog = string("export function foo { \n")
+ " while (true) { do nothing }\n" // this one yiels politely
+ "}\n"
+ "program x { \n"
+ " this.foo()\n" // this will hang the program's execution being blocked in waitForMessages for foo() to return... should we use a yield point ? Was so before because it were commands...
//+ " this.foo.send()\n" // send/collect not yet supported.
+ "}";
this->doFunction( prog, tc, false );
BOOST_CHECK( tc->getOperation("foo") );
// removing the program should lead to removal of the function from the PP.
this->finishFunction( tc, "x");
}
#endif
BOOST_AUTO_TEST_CASE( testRecFunction)
{
string prog = string("export function foo { \n")
+ " do this.foo()\n" // recursive is forbidden.
+ "}\n"
+ "program x { \n"
+ " do foo\n"
+ "}";
try {
parser.parseProgram( prog, tc );
}
catch( ... )
{
prog = string("function foo { \n")
+ " call foo\n" // recursive is forbidden.
+ "}\n"
+ "program x { \n"
+ " call foo\n"
+ "}";
//progs = prog;
try {
parser.parseProgram( prog, tc );
}
catch( ... )
{
return;
}
BOOST_CHECK_MESSAGE( false, "Recursive 'call' function was accepted, while it is illegal." );
}
BOOST_CHECK_MESSAGE( false , "Recursive 'do' function was accepted, while it is illegal.");
}
BOOST_AUTO_TEST_CASE( testCallFunction)
{
string prog = string("function foo(int a, string b, bool c) { \n")
+ " do test.assert( test.isTrue( true ) )\n"
+ " if true then\n"
+ " return\n"
+ " do test.assert(false)\n" // do not reach
+ "}\n"
+ "program x { \n"
+ " call foo( 1, \"hello\", true)\n"
+ "}";
this->doFunction( prog, tc );
this->finishFunction( tc, "x");
}
BOOST_AUTO_TEST_CASE( testFunctionStack)
{
string prog = string("export function foo { \n")
+" var double a = 1.234\n"
+" var double b = 4.321\n"
+" do test.assert( a == 1.234 )\n"
+" do test.assert( b == 4.321 )\n"
+" set a = 2.134\n"
+" set b = 3.421\n"
+" do test.assert( a == 2.134 )\n"
+" do test.assert( b == 3.421 )\n"
+ "}\n"
+ "program x { \n"
+" var double b = 1.234\n" // we switch val's of a and b here
+" var double a = 4.321\n"
+ " do foo()\n"
+" do test.assert( b == 1.234 )\n"
+" do test.assert( a == 4.321 )\n"
+ " do foo()\n"
+ " set a = 0.0\n"
+ " set b = 1.0\n"
+" do test.assert( b == 1.0 )\n"
+" do test.assert( a == 0.0 )\n"
+ "}";
this->doFunction( prog, tc );
this->finishFunction( tc, "x");
}
BOOST_AUTO_TEST_CASE( testFunctionExportArgs)
{
// Test if the foo args are init'ed correctly.
string prog =
string("export function fooA(int a, string b, bool c) { \n")
+ " do test.assertMsg( c, \"c not true\" )\n"
+ " do test.assertMsg( a == 1, \"a not 1\" )\n"
+ " do test.assertMsg( b == \"A\", \"b not A\" )\n"
+ "}\n"
+ "export function fooB(int a, string b, bool c) { \n"
+ " do test.assertMsg( !c, \"c not false\" )\n"
+ " do test.assertMsg( a == -1, \"a not -1\" )\n"
+ " do test.assertMsg( b == \"B\", \"b not B\" )\n"
+ " do fooA(1, \"A\", true)\n"
+ "}\n"
+ "program x { \n"
+ " do fooA(1, \"A\", true)\n"
+ " do fooB(-1, \"B\", false)\n"
// + " call fooA(1.0, \"A\", true)\n"
// + " call fooB(-1, \"B\", false)\n"
+ "}";
this->doFunction( prog, tc );
this->finishFunction( tc, "x");
}
BOOST_AUTO_TEST_CASE( testFunctionCallArgs)
{
// Test if the foo args are init'ed correctly.
string prog =
string("function fooA(int a, string b, bool c) { \n")
+ " do test.assert( c )\n"
+ " do test.assert( a == 1 )\n"
+ " do test.assert( b == \"A\" )\n"
+ "}\n"
+ "function fooB(int a, string b, bool c) { \n"
+ " do test.assert( !c )\n"
+ " var int i = 1\n"
+ " var string s = \"A\"\n"
+ " var bool tf = true\n"
+ " call fooA(i, s, tf)\n"
+ " do test.assert( a == -1 )\n"
+ " do test.assert( b == \"B\" )\n"
+ "}\n"
+ "program x { \n"
+ " call fooA(1, \"A\", true)\n"
+ " call fooB(-1, \"B\", false)\n"
// + " call fooA(1.0, \"A\", true)\n"
// + " call fooB(-1, \"B\", false)\n"
+ "}";
this->doFunction( prog, tc );
this->finishFunction( tc, "x");
}
BOOST_AUTO_TEST_CASE( testFunctionFail)
{
// Test if obj-function error is propagated correctly to
// calling program or function
string prog =
string("export function fooA { \n")
+ " do test.assert( false )\n" // throws error
+ "}\n"
+ "export function fooB { \n"
+ " do fooA()\n"
+ "}\n"
+ "program x { \n"
+ " var bool success = false\n"
+ " try fooA()\n"
+ " catch \n"
+ " set success = true\n" // error caught.
+ " do test.assertMsg(success,\"Program script did not detect exported function failure.\")\n"
+ " set success = false\n"
+ " try fooB()\n"
+ " catch\n"
+ " set success = true\n" // error caught.
+ " do test.assertMsg(success,\"Program script did not detect exported function failure.\")\n"
+ "}";
this->doFunction( prog, tc );
this->finishFunction( tc, "x");
}
BOOST_AUTO_TEST_SUITE_END()
void FunctionsFixture::doFunction( const std::string& prog, TaskContext* tc, bool test )
{
BOOST_REQUIRE( tc->engine() );
Parser::ParsedPrograms pg_list;
try {
pg_list = parser.parseProgram( prog, tc );
}
catch( const file_parse_exception& exc )
{
BOOST_REQUIRE_MESSAGE( false , exc.what() );
}
catch( ... ) {
BOOST_REQUIRE_MESSAGE( false, "Unknown exception thrown by Parser.");
}
if ( pg_list.empty() )
{
// no program necessary here.
//BOOST_REQUIRE_MESSAGE(false , "No program parsed in test.");
return;
}
BOOST_REQUIRE( sa->loadProgram( *pg_list.begin() ) );
BOOST_CHECK( sa->getProgram( (*pg_list.begin())->getName() )->start() );
BOOST_CHECK( SimulationThread::Instance()->run(1000) );
if (test ) {
stringstream errormsg;
errormsg << " on line " << sa->getProgram("x")->getLineNumber() <<" of program 'x' (or of a function 'called' by x)."<<endl;
BOOST_REQUIRE_MESSAGE( sa->getProgramStatus("x") != ProgramInterface::Status::error , "Runtime Error Status encountered" + errormsg.str());
BOOST_REQUIRE_MESSAGE( sa->getProgramStatus("x") == ProgramInterface::Status::stopped, "Program stalled" + errormsg.str() );
}
}
void FunctionsFixture::finishFunction(TaskContext* tc, std::string prog_name)
{
BOOST_REQUIRE( sa->getProgram(prog_name) );
sa->getProgram( prog_name )->stop();
sa->unloadProgram( prog_name );
}