-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtypes_test.cpp
626 lines (565 loc) · 23 KB
/
types_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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
/***************************************************************************
tag: Peter Soetens Mon Jan 10 15:59:50 CET 2005 types_test.cpp
types_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 <iostream>
#include <scripting/FunctionGraph.hpp>
#include <OperationCaller.hpp>
#include <extras/SimulationActivity.hpp>
#include <extras/SimulationThread.hpp>
#include <Service.hpp>
#include <TaskContext.hpp>
#include <scripting/ScriptingService.hpp>
#include <scripting/Parser.hpp>
#include <Service.hpp>
#include <types/GlobalsRepository.hpp>
#include <types/Types.hpp>
#include <types/StructTypeInfo.hpp>
#include <types/SequenceTypeInfo.hpp>
#include "datasource_fixture.hpp"
#include "operations_fixture.hpp"
using namespace std;
using namespace RTT;
using namespace RTT::detail;
class TypesTest : public OperationsFixture
{
public:
Parser parser;
ScriptingService::shared_ptr sa;
void executePrograms(const std::string& prog);
void executeStates(const std::string& state);
TypesTest()
: sa( ScriptingService::Create(tc) )
{
tc->stop();
BOOST_REQUIRE( tc->setActivity(new SimulationActivity(0.01)) );
BOOST_REQUIRE( tc->start() );
SimulationThread::Instance()->stop();
}
~TypesTest(){
}
};
// Registers the fixture into the 'registry'
BOOST_FIXTURE_TEST_SUITE( TypesTestSuite, TypesTest )
//! Tests the preservation of the capacity of a string in the type system.
BOOST_AUTO_TEST_CASE( testStringCapacity )
{
Attribute<string> str = Types()->type("string")->buildVariable("str",10);
size_t strCapacity=str.get().capacity();
// check size hint:
BOOST_CHECK_EQUAL( str.get().size() , 10 );
// False test http://www.cplusplus.com/reference/string/string/capacity/
// BOOST_CHECK_EQUAL( str.get().capacity() , 10 );
str.set() = "hello"; // note: assign to C string preserves capacity
BOOST_CHECK_EQUAL( str.get().size() , 5 );
BOOST_CHECK_EQUAL( str.get().capacity() , strCapacity );
// create empty target:
Attribute<string> copy("copy");
BOOST_CHECK_EQUAL( copy.get().size() , 0 );
// False test http://www.cplusplus.com/reference/string/string/capacity/
//BOOST_CHECK_EQUAL( copy.get().capacity() , 0 );
// copy str to target and check:
copy.getDataSource()->update( str.getDataSource().get() );
BOOST_CHECK_EQUAL( copy.get().size(), 5 );
// We can't assume much here: on Linux copy.get().capacity() returns 5, on win32: 10
//BOOST_CHECK_EQUAL( copy.get().capacity(), strCapacity );
BOOST_CHECK_EQUAL( copy.get(), str.get() );
copy.set() = "world";
// now copy target back to str and check if capacity remains:
str.getDataSource()->update( copy.getDataSource().get() );
BOOST_CHECK_EQUAL( str.get().size() , 5 );
BOOST_CHECK_EQUAL( str.get().capacity() , strCapacity );
BOOST_CHECK_EQUAL( copy.get(), str.get() );
// Same exercise as above, but with updateCommand():
str.set() = "hello"; // note: assign to C string preserves capacity
strCapacity=str.get().capacity();
BOOST_CHECK_EQUAL( str.get().size() , 5 );
// False test http://www.cplusplus.com/reference/string/string/capacity/
//BOOST_CHECK_EQUAL( str.get().capacity() , 10 );
// copy str to target and check:
ActionInterface* act = copy.getDataSource()->updateAction( str.getDataSource().get() );
BOOST_CHECK( act );
act->readArguments();
BOOST_CHECK( act->execute() );
delete act;
BOOST_CHECK_EQUAL( copy.get().size(), 5 );
// We can't assume much here: on Linux copy.get().capacity() returns 5, on win32: 10
//BOOST_CHECK_EQUAL( copy.get().capacity(), strCapacity );
BOOST_CHECK_EQUAL( copy.get(), str.get() );
copy.set() = "world";
// now copy target back to str and check if capacity remains:
act = str.getDataSource()->updateAction( copy.getDataSource().get() );
BOOST_CHECK( act );
act->readArguments();
BOOST_CHECK( act->execute() );
delete act;
BOOST_CHECK_EQUAL( str.get().size() , 5 );
BOOST_CHECK_EQUAL( str.get().capacity() , strCapacity );
BOOST_CHECK_EQUAL( copy.get(), str.get() );
}
BOOST_AUTO_TEST_CASE( testTypes )
{
string test =
// Line 2 (see below):
string("var int i2 = -1, j = 10, k; set k = 20\n") +
"do test.assert( i2 == -1 ) ; do test.assert( j == 10 ); do test.assert(k == 20)\n" +
"var double d = 10.0\n"+
"do test.assert( d == 10.0 )\n" +
"var bool b = false\n"+
"do test.assert( b == false )\n" +
"var string s=\"string\"\n"+
"do test.assert( s == \"string\" )\n" +
"const int ic = i2\n" +
"do test.assert( ic == 0 )\n" + // i was null at parse time !
"const double dc = 10.0\n"+ // evaluate 10.0 at parse time
"do test.assert( dc == 10.0 )\n" +
"const bool bc = true && false\n"+
"do test.assert( bc == false )\n" +
"const string sc= \"hello\"\n"+
"do test.assert( sc == \"hello\" )\n" +
"var array ar(10)\n"+ // size hint syntax != constructor syntax
"do test.assert( ar.size == 10)\n"+
// 20:
"do test.assert( ar.capacity == 10)\n"+
"set ar[0] = 0.0\n"+
"set ar[1] = 1.0\n"+
"set ar[2] = 0.2\n"+
"set ar[8] = 0.8\n"+
"set ar[9] = 9.0\n"+
"do test.assert( ar[0] == 0.0 )\n"+
"do test.assert( ar[1] == 1.0 )\n"+
"do test.assert( ar[2] == 0.2 )\n"+
"do test.assert( ar[8] == 0.8 )\n"+
// 30:
"do test.assert( ar[9] == 9.0 )\n"+
"do test.assert( ar[10] == 0.0 )\n"+
"var array ar1 = array(12,2.0)\n"+
"do test.assert(ar1.size == 12)\n"+
// "do test.print(ar1[11])\n"+
"do test.assert(ar1[0] == 2.0)\n"+
"var array ar2 = array(5,3.0)\n"+
"do test.assert(ar2.size == 5)\n"+
"do test.assert(ar2[0] == 3.0)\n"+
"var array ar3(3) = array(2.0,3.0,4.0)\n"+
"do test.assert(ar3.size == 3)\n"+
//40:
"do test.assert(ar3[0]==2.0)\n"+
"do test.assert(ar3[1]==3.0)\n"+
"do test.assert(ar3[2]==4.0)\n"+
"var array ar4 = array(2.0,3.0,4.0,5.0)\n"+
"do test.assert(ar4.size == 4)\n"+
"do test.assert(ar4[0]==2.0)\n"+
"do test.assert(ar4[1]==3.0)\n"+
"do test.assert(ar4[2]==4.0)\n"+
"do test.assert(ar4[3]==5.0)\n"+
"var string str(10)\n"+
"var int strCapacity = str.capacity\n"
// 50:
// "do test.print(str.size)\n"+
// "do test.print(str.capacity)\n"+
"do test.assertEqual( str.size, 10)\n"+
// "do test.assertEqual( str.capacity, 10)\n"
"set str = \"hello\"\n"+
// "do test.print(str.size)\n"+
// "do test.print(str.capacity)\n"+
"do test.assertEqual( str.size, 5)\n"+
"do test.assertEqual( str.capacity, strCapacity)\n"+ // if this fails, the COW implementation of std::string fooled us again.
"set str[0] = 'a'\n"+
"set str[1] = 'b'\n"+
"set str[2] = 'c'\n"+
"do test.assert( str[0] == 'a' )\n"+
"do test.assert( str[1] == 'b' )\n"+
"do test.assert( str[2] == 'c' )\n"+
"do test.assert( str == \"abclo\" )\n"+
"do test.assert( str[20] == '\\0' )\n"+
// 60:
"do test.assert( str[8] == '\\0' )\n"+
"do test.assert( str[9] == '\\0' )\n"+
"do test.assert( str[10] == '\\0' )\n"+
// various array constructors
"set ar2 = array(10.,5.)\n"+ // keeps capacity
"do test.assertEqual( ar2.size, 2)\n"+
"do test.assertEqual( ar2.capacity, 5)\n"+
"do test.assert( ar2[0] == 10.0 )\n"+
"do test.assert( ar2[1] == 5.0 )\n"+
"set ar3 = array(10.)\n"+
"do test.assert( ar3.size == 1)\n"+
// 70:
"do test.assert( ar3.capacity >= 1)\n"+
"do test.assert( ar3[0] == 10.0 )\n"+
"set ar4 = array(2, 7.)\n"+
"do test.assert( ar4.size == 2)\n"+
"do test.assert( ar4.capacity >= 2)\n"+
"do test.assert( ar4[0] == 7.0 )\n"+
"do test.assert( ar4[1] == 7.0 )\n"+
// various array assignments
"set ar2 = ar4\n"+
"do test.assert( ar2.size == 2)\n"+
"do test.assert( ar2.capacity >= 5)\n"+
// 80:
"do test.assert( ar2[0] == 7.0 )\n"+
"do test.assert( ar2[1] == 7.0 )\n"+
"do test.assert( ar.capacity == 10)\n"+ // pre-condition
"var array ar7(7) = array(7)\n"+
"set ar = ar7\n"+ // assignment must keep capacity and only change size
//"do test.print( ar.size )\n"+
"do test.assert( ar.size == 7)\n"+
//"do test.print( ar.capacity )\n"+
"do test.assertEqual( ar.capacity, 10)\n"+ // check keeping capacity: ar(10) vs ar2(2)
//-- This fails because .capacity() gets a copy of the std::vector
"do test.assert( ar2[0] == 7.0 )\n"+
"do test.assert( ar2[1] == 7.0 )\n";
string state = string("StateMachine X { initial state Init { entry {\n")
+test
+"} }\n"
+"final state Fini {} }\n"
+"RootMachine X x\n";
string prog = string("program x {\n") + test + "}\n";
// execute
executePrograms(prog);
executeStates(state);
}
BOOST_AUTO_TEST_CASE( testCharType )
{
string test =
// Line 2 (see below):
string("var char c = char('c');\n") +
// "do test.assert( c == c ); \n" +
// "do test.assert( char('c') == c ); \n" +
"var char d = char('d');\n" +
// "do test.assert( c != d ); \n" +
// "set c = 'a';\n" +
"set c = char('a');\n" +
"do test.assert( char('a') == c ); \n" +
"do test.assert( c != d ); \n" +
"do test.assert( c < d ); \n" +
"do test.assert( c <= d ); \n" +
"do test.assert( d > c ); \n" +
"do test.assert( d >= c ); \n" +
"set d = c;\n" +
"do test.assert( c == d ); \n" +
"do test.assert( char('a') == d ); \n" +
"do test.assert( char('a') == char('a') ); \n" +
"do test.assert( char('a') != char('b') ); \n" +
"do test.assert( char('a') < char('b') ); \n" +
"do test.assert( char('a') <= char('b') ); \n" +
"do test.assert( char('a') <= char('a') ); \n" +
"do test.assert( char('z') > char('w') ); \n" +
"do test.assert( char('z') >= char('w') ); \n" +
"do test.assert( char('z') >= char('z') ); \n"
;
string state = string("StateMachine X { initial state Init { entry {\n")
+test
+"} }\n"
+"final state Fini {} }\n"
+"RootMachine X x\n";
string prog = string("program x {\n") + test + "}\n";
// execute
executePrograms(prog);
executeStates(state);
}
/**
* Tests some random operations, and alow the operator+ for strings.
*/
BOOST_AUTO_TEST_CASE( testOperators )
{
string prog = string("program x {\n") +
"var int i = 3\n" +
"var char c = 'c'\n" +
"var double d = 10.0*i\n"+
"do test.assert( d == 30.0 )\n" +
"var bool b = false\n"+
"var string s=\"string\"\n"+
"set b = b || b && true && false || true\n"+
"try test.assertMsg( s == \"string\", \"Unexpected string:\'\" + s +\"' instead of 'string'\")\n"+
"set s = \" \" + s + \" \"\n"+
"try test.assertMsg( s == \" string \", \"Unexpected string:\'\" + s +\"' instead of ' string '\")\n"+
"set s = s + int(10)\n"+
"try test.assertMsg( s == \" string 10\", \"Unexpected string:\'\" + s +\"' instead of ' string 10'\")\n"+
"set s = s + \" \" + false\n"+
"do test.assertMsg( s == \" string 10 false\", \"Unexpected string:\'\" + s +\"' instead of ' string 10 false'\")\n"+
"set b = b\n ||\n b\n &&\n true\n && false\n || true\n"+
"do test.assert( b == false )\n" +
"var array a1 = array(2, 7.)\n"+
"do test.assert( a1.size == 2 )\n" +
"do test.assert( a1.capacity == 2 )\n" +
// "set s = s+\"abc\"\n"+
"}";
// execute
executePrograms(prog);
}
/**
* Tests parsing multiple occurences of '[]' and '.' while indexing
* into structs and sequences and any combination thereof.
*/
BOOST_AUTO_TEST_CASE( testDotsAndIndexes )
{
Types()->addType( new SequenceTypeInfo<std::vector<std::vector<double> >,false >("matrix"));
Types()->addType( new StructTypeInfo<AType,false>("astruct"));
Types()->addType( new StructTypeInfo<CType,false>("cstruct"));
Types()->addType( new SequenceTypeInfo<std::vector<CType>,false >("cstructv"));
Types()->addType( new SequenceTypeInfo<std::vector<AType>,false >("astructv"));
string prog = string("program x {\n") +
"var matrix m = matrix(8,array(10))\n" + // 8 by 10 matrix
"test.assertMsg(m.size == 8, \"Matrix column size is wrong.\")\n" +
"test.assertMsg(m[0].size == 10, \"Matrix row size is wrong.\")\n" +
"m[0][0] = 3.33\n" +
"m[1][1] = 4.33\n" +
"m[2][2] = 5.33\n" +
"m[8][10] = 6.33\n" +
"test.assertMsg(m[0][0] == 3.33, \"Matrix element assignment failed.\")\n"+
"test.assertMsg(m[1][1] == 4.33, \"Matrix element assignment failed.\")\n"+
"test.assertMsg(m[2][2] == 5.33, \"Matrix element assignment failed.\")\n"+
"test.assertMsg(m[8][10] == 6.33, \"Matrix element assignment failed.\")\n"+
"var matrix m2 = m;\n"
"test.assertMsg(m2[0][0] == 3.33, \"Matrix assignment failed.\")\n"+
"test.assertMsg(m2[1][1] == 4.33, \"Matrix assignment failed.\")\n"+
"test.assertMsg(m2[2][2] == 5.33, \"Matrix assignment failed.\")\n"+
"test.assertMsg(m2[8][10] == 6.33, \"Matrix assignment failed.\")\n"+
"var cstructv structv = cstructv(3)\n"+ // vector<struct> of size 10
"structv[1].a.b = 3.33\n"+
"test.assertMsg(structv[1].a.b == 3.33, \"Sequence of struct element assignment failed.\")\n"+
"structv[1].av[3].b = 4.33\n"+
"test.assertMsg(structv[1].av[3].b == 4.33, \"Sequence of struct element assignment failed.\")\n"+
"}";
// execute
executePrograms(prog);
}
/**
* Tests converting double to float to int in several
* directions.
*/
BOOST_AUTO_TEST_CASE( testConversions )
{
string prog = string("program x {\n") +
"var int i = 3.0\n" +
"var double d = float(10.0*i)\n"+
"do test.assert( float(d) == float(30.0) )\n" +
"var float f = 5\n" +
"set f = double(5) * double(-1) + i\n" +
"set i = f\n" +
"set f = i\n" +
"set i = double(float(int(f)))\n" +
"set f = int(float(double(int(3.333))))\n" +
"do test.assert( f == 3 )\n" +
"}";
// execute
executePrograms(prog);
}
/**
* Tests hexadecimals
*/
BOOST_AUTO_TEST_CASE( testHex )
{
string prog = string("program x {\n") +
"var uint i = 0xabc\n" +
"test.assert( i == 0xabc )\n"+
"test.assert( i == 2748 )\n"+
"i = 0Xcba\n" +
"test.assert( i == 0Xcba )\n"+
"test.assert( i == 3258 )\n"+
"i = 0XABC\n" +
"test.assert( i == 0XABC )\n"+
"test.assert( i == 2748 )\n"+
"i = 0xCBA\n" +
"test.assert( i == 0xCBA )\n"+
"test.assert( i == 3258 )\n"+
"}";
// execute
executePrograms(prog);
}
/**
* Tests reading/writing property(bags) in scripting.
*/
BOOST_AUTO_TEST_CASE( testProperties )
{
string prog = string("program x {\n") +
"do test.assert( Double1 == 1.234 )\n" +
"do test.assert( Double2 == 2.234 )\n" +
"do test.assert( Double3 == 3.234 )\n" +
"do test.assert( Collection.Double1 == 1.234 )\n" +
"do test.assert( Collection.Double3 == 3.234 )\n" +
"do test.assert( Collection.Collection.Double1 == 1.234 )\n" +
"do test.assert( Collection.Collection.Collection.Double3 == 3.234 )\n" +
"do test.assert( V[0] == 4.0 )\n" +
"do test.assert( V[1] == 4.0 )\n" +
"do test.assert( V[2] == 4.0 )\n" +
"do test.assert( V[3] == 4.0 )\n" +
//"do test.assert( V.size() == 4 )\n" +
"set Double1 = 4.321\n" +
"set Double2 = Double1\n" +
// -> = 10
"set Collection.Double3 = 0.3\n" +
"set V[0] = 0.321\n" +
"set V[1] = 1.321\n" +
"set V[2] = 2.321\n" +
"set V[3] = 3.321\n" +
"do test.assert( Double1 == 4.321 )\n" +
"do test.assert( Double2 == 4.321 )\n" +
"do test.assert( Double3 == 0.3 )\n" +
"set Collection.Collection.Collection.Double3 = 3.0\n" +
"do test.assert( Double3 == 3.0 )\n" +
"}";
Property<PropertyBag> pb("Collection","");
Property<double> pd1("Double1","",1.234);
Property<double> pd2("Double2","",2.234);
Property<double> pd3("Double3","",3.234);
Property< std::vector<double> > pv("V","",std::vector<double>(4, 4.0) );
pb.value().addProperty( pd1 );
pb.value().addProperty( pd3 );
pb.value().addProperty( pb ); // yep, recursive !
tc->properties()->addProperty( pd1 );
tc->properties()->addProperty( pd2 );
tc->properties()->addProperty( pd3 );
tc->properties()->addProperty( pb );
tc->properties()->addProperty( pv );
// execute
executePrograms(prog);
BOOST_CHECK_EQUAL( 4.321, pd1.get() );
BOOST_CHECK_EQUAL( 3.0, pd3.get() );
BOOST_CHECK_EQUAL( 0.321, pv.value()[0] );
BOOST_CHECK_EQUAL( 1.321, pv.value()[1] );
BOOST_CHECK_EQUAL( 2.321, pv.value()[2] );
BOOST_CHECK_EQUAL( 3.321, pv.value()[3] );
}
BOOST_AUTO_TEST_CASE( testOperatorOrder )
{
string prog = string("program x {\n") +
"do test.assert( 6/2*4 == 12 )\n" + // not: 0
"do test.assert( 6*2/4 == 3 )\n" + // not: 0
"do test.assert( 3+2*5 == 13)\n" + // not: 30
"do test.assert( 3 < 2 != 5 > 1)\n" +
"do test.assert( 3*2 <= 12/2 )\n" +
"do test.assert( 3*2 < 6+1 )\n" +
"do test.assert( 6 - 9 % 2*3 == 15/3 % 3 + 1 )\n" + // 3 == 3
"do test.assert( 3*(2+1) == 9 )\n" +
"do test.assert( 1 - 1 + 5 == 5 )\n" + // not: -5
"var int a,b,c;\n" +
" a = b = c = 3;\n" + // not 0,0,3
"do test.assertEqual( a, 3 )\n" +
"do test.assertEqual( b, 3 )\n" +
"do test.assertEqual( c, 3 )\n" +
"}";
// execute
executePrograms(prog);
}
BOOST_AUTO_TEST_CASE( testGlobals )
{
GlobalsRepository::Instance()->setValue( new Constant<double>("cd_num", 3.33));
GlobalsRepository::Instance()->setValue( new Constant<string>("c_string", "Hello World!"));
GlobalsRepository::Instance()->setValue( new Attribute<double>("d_num", 3.33));
string prog = string("program x {\n") +
"do test.assert( cd_num == 3.33 )\n" +
"do test.assert( cd_num == d_num )\n" +
"do test.assert( c_string == \"Hello World!\")\n" +
"set d_num = 6.66\n" +
"do test.assert( d_num == 6.66 )\n" +
"}";
// execute
executePrograms(prog);
}
BOOST_AUTO_TEST_CASE( testFlowStatus )
{
BOOST_CHECK (GlobalsRepository::Instance()->getValue("NewData") );
BOOST_CHECK (GlobalsRepository::Instance()->getValue("OldData") );
BOOST_CHECK (GlobalsRepository::Instance()->getValue("NoData") );
string prog = string("program x {\n") +
"do test.assert( NewData )\n" +
"do test.assert( OldData )\n" +
"do test.assert( !bool(NoData) )\n" +
"do test.assert( NewData > NoData )\n" +
"do test.assert( NewData > OldData )\n" +
"do test.assert( OldData > NoData )\n" +
"do test.assert( OldData == OldData )\n" +
"if ( bool(NewData) && OldData ) then {\n" +
"} else {\n" +
" do test.assert(false)\n" +
"}\n" +
"if ( bool(NoData) ) then {\n" +
" do test.assert(false)\n" +
"}\n" +
"if ( !bool(NoData) ) then {} else {\n" +
" do test.assert(false)\n" +
"}\n" +
"}";
// execute
executePrograms(prog);
}
BOOST_AUTO_TEST_SUITE_END()
void TypesTest::executePrograms(const std::string& prog )
{
BOOST_CHECK( 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());
}
if ( pg_list.empty() )
{
BOOST_REQUIRE( false && "Got empty test program." );
}
BOOST_CHECK( sa->loadProgram( *pg_list.begin() ) );
BOOST_CHECK( (*pg_list.begin())->start() );
BOOST_CHECK( SimulationThread::Instance()->run(1000) );
if ( (*pg_list.begin())->inError() ) {
stringstream errormsg;
errormsg << " Program error on line " << (*pg_list.begin())->getLineNumber() <<"."<<endl;
BOOST_CHECK_MESSAGE( false, errormsg.str() );
}
if ( (*pg_list.begin())->isRunning() ) {
stringstream errormsg;
errormsg << " Program still running on line " << (*pg_list.begin())->getLineNumber() <<"."<<endl;
BOOST_CHECK_MESSAGE( false, errormsg.str() );
}
if ( (*pg_list.begin())->isStopped() == false ) {
stringstream errormsg;
errormsg << " Program not stopped on line " << (*pg_list.begin())->getLineNumber() <<"."<<endl;
BOOST_CHECK_MESSAGE( false , errormsg.str() );
}
BOOST_CHECK( (*pg_list.begin())->stop() );
BOOST_CHECK( sa->unloadProgram( (*pg_list.begin())->getName() ) );
}
void TypesTest::executeStates(const std::string& state )
{
BOOST_CHECK( tc->engine() );
Parser::ParsedStateMachines pg_list;
try {
pg_list = parser.parseStateMachine( state, tc );
}
catch( const file_parse_exception& exc )
{
BOOST_REQUIRE_MESSAGE( false , exc.what());
}
if ( pg_list.empty() )
{
BOOST_REQUIRE_MESSAGE( false, "Parser returned no state machines to execute." );
}
BOOST_CHECK( sa->loadStateMachine( *pg_list.begin() ) );
BOOST_CHECK( (*pg_list.begin())->activate() );
BOOST_CHECK( (*pg_list.begin())->start() );
BOOST_CHECK( SimulationThread::Instance()->run(1000) );
if ( (*pg_list.begin())->inError() ) {
stringstream errormsg;
errormsg << " State error on line " << (*pg_list.begin())->getLineNumber() <<"."<<endl;
BOOST_CHECK_MESSAGE( false , errormsg.str());
}
BOOST_CHECK( (*pg_list.begin())->stop() );
BOOST_CHECK( SimulationThread::Instance()->run(100) );
BOOST_CHECK( (*pg_list.begin())->deactivate() );
sa->unloadStateMachine( (*pg_list.begin())->getName() );
}