Last sync 2016.04.01
[deliverable/titan.core.git] / regression_test / tryCatch / tryCatch_Testcases.ttcn
CommitLineData
970ed795 1/******************************************************************************
d44e3c4f 2 * Copyright (c) 2000-2016 Ericsson Telecom AB
970ed795
EL
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
d44e3c4f 7 *
8 * Contributors:
9 * Balasko, Jeno
10 * Baranyi, Botond
11 *
970ed795
EL
12 ******************************************************************************/
13module tryCatch_Testcases {
14
15import from tryCatch_Functions all;
16
17external function throw(in charstring msg);
18
19type port MyInternal_PT message {
20 inout charstring;
21} with { extension "internal"}
22
23
24type port PCOType message
25{
26 inout charstring;
27}
28
29type component MyComp {
30 port MyInternal_PT PCO;
31 port PCOType ExternalPCO;
32 var integer v_funcCallCounter; //for lazy
33}
34
35type record of integer RoI;
36
37const RoI c_roi3:={1,2,3}
38
39//Based on funtion_test :)
40type union UnionType1{
41 UnionType2 girl,
42 integer numero
43}
44
45type union UnionType2{
46 Emma emma,
47 Eszter eszter
48}
49
50type record Emma { charstring name}
51type record Eszter { charstring name}
52
53
54
55//This template cannot be sent:
56template charstring tr_recv := pattern "*";
57
58//This template can be sent:
59template charstring t_send := "This is the message";
60
61//******* Types ***************
62type integer MySmallInt(1..10);
63//******* Constants ***********
64
65//******* Functions ***********
66
67function f_divByZero() {
68 var integer i :=10, j:=0;
69 @try {
70 i:=i/j
71 }
72 @catch(dte_str){
73 setverdict(pass);
74 stop;
75 }
76 setverdict(fail); //never reach this line
77}
78
79function f_setverdict_str(in charstring pl_str, in template charstring pl_expected) {
80 if(match(pl_str,pl_expected)) {
81 setverdict(pass)
82 } else {
83 setverdict(fail, match(pl_str,pl_expected));
84 }
85} with { extension "transparent" }
86
87
88function f_tryCatch_conv() runs on MyComp {
89 var charstring vl_ch1:="H" , vl_ch2:="E";
90 @try{
91 var hexstring vl_h:=str2hex(vl_ch1&vl_ch2);
92 }
93 @catch(e){
94 if(match(e, pattern "*The argument of function str2hex\(\) shall contain hexadecimal digits only,*"))
95 { setverdict(pass) } else {setverdict(fail)};
96 }
97
98 //normal case:
99 vl_ch1:=vl_ch2&vl_ch2
100 @try{
101 var hexstring vl_h:= str2hex(vl_ch1);
102 setverdict(pass);
103 }
104 @catch(e) {
105 setverdict(fail,"exception for normal value");
106 }
107}
108
109function f_tryCatch_conv_imported() runs on MyComp {
110 var charstring vl_ch1:="H" , vl_ch2:="E";
111 template charstring tl_expected :=
112 pattern "*The argument of function str2hex\(\) shall contain hexadecimal digits only,*";
113 @try{
114 var hexstring vl_h:=str2hex(vl_ch1&vl_ch2);
115 }
116 @catch(e){
117 f_matchAndVerdict(e,tl_expected); //imported function
118 }
119}
120
121function f_tryCatch_conv_throw() runs on MyComp {
122 var charstring vl_ch1:="H" , vl_ch2:="E";
123 @try{
124 var hexstring vl_h:=str2hex(vl_ch1&vl_ch2);
125 }
126 @catch(e){
127 throw(e);
128 }
129}
130
131altstep as_tryCatch_conv() runs on MyComp {
132 [] PCO.receive {setverdict(pass);}
133 [else] {
134 log("altstep reached the else-branch");
135 var charstring vl_ch1:="H" , vl_ch2:="E";
136 @try{
137 var hexstring vl_h:=str2hex(vl_ch1&vl_ch2);
138 setverdict(fail, "Catched DTE didn't stopped the running");
139 }
140 @catch(e){
141 log("Catched DTE:",e);
142 setverdict(pass);
143
144 }
145 }
146}
147
148
149function f_behavior_sending(in template charstring pl_msg) runs on MyComp {
150 @try {
151 PCO.send(pl_msg);
152 if(isvalue(pl_msg) ) {
153 setverdict(pass, "no exception for sending real value template[sending template]")
154 } else {
155 setverdict(fail, "no exception for sending not real value template");
156 }
157 }
158 @catch(e) {
159 if( match(e, pattern "*Port PCO has neither connections nor mappings. Message cannot be sent on it.*"))
160 {
161 setverdict(pass);
162 } else {
163 log("Catched error msg:", e);
164 if( isvalue(pl_msg) ) { setverdict(fail, "exception for sending real value");}
165 f_setverdict_str(e, pattern "*Performing valueof or send operation on a non-specific charstring template.");
166 @try {
167 PCO.send("Default msg");
168 }
169 @catch(f) {
170 log("Catched DTE:",f);
171 }
172 }
173 }
174 // side-effect???? :
175 @try {
176 var charstring vl_msg:= valueof(pl_msg);
177 }
178 @catch(e) {
179 log("Catched DTE:",e);
180 f_setverdict_str(e, pattern "*Performing valueof or send operation on a non-specific charstring template.");
181
182 if(isvalue(pl_msg) ) {
183 setverdict(fail, "exception for valueof(real value template)");
184 } else {
185 setverdict(pass, "exception for valueof( not real value template[receiving template])");
186 }
187 }
188}
189
190modulepar integer tsp_counterLimit := 10000
191function f_behavior_sendingInfinite(in template charstring pl_msg) runs on MyComp {
192 var integer vl_counter:=0;
193 while(vl_counter < tsp_counterLimit) {
194 @try {
195 PCO.send(valueof(pl_msg)&int2str(vl_counter));
196 }
197 @catch(e) {
198 log("Catched DTE:",e);
199 break;
200 }
201 vl_counter:=vl_counter+1;
202
203 if ( (vl_counter mod 10000) ==0) { log(">>>Counter:",vl_counter); }
204 }
205 log("Bye");
206}
207
208
209function f_behavior_receiving(in template charstring pl_expected) runs on MyComp {
210 timer T := 2.0;
211 T.start;
212 alt {
213 [] PCO.receive { log("msg received"); setverdict(pass) };
214 [] T.timeout { setverdict(none, "timeout"); }
215 }
216}
217
218function f_behavior_notReceiving(in template charstring pl_expected) runs on MyComp {
219 timer T := 10.0;
220 T.start;
221 alt {
222 //[] PCO.receive { log("msg received"); setverdict(pass) };
223 [] T.timeout { setverdict(none, "timeout"); }
224 }
225}
226
227//sending on external port (i.e not internal port)
228function f_behavior_externalSending(in template charstring pl_msg) runs on MyComp {
229 @try {
230 ExternalPCO.send(pl_msg);
231 if(isvalue(pl_msg) ) {
232 setverdict(pass, "no exception for sending real value template[sending template]")
233 } else {
234 setverdict(fail, "no exception for sending not real value template");
235 }
236 }
237 @catch(e) {
238 log("Catched DTE:",e);
239 if( match(e, pattern "*Port PCO has neither connections nor mappings. Message cannot be sent on it.*"))
240 {
241 setverdict(pass);
242 } else {
243 if( isvalue(pl_msg) ) {
244 setverdict(fail, "exception for sending real value");
245 } else {
246 setverdict(pass, "exception for sending not real value");
247 }
248 }
249 }
250}
251
252function f_behavior_externalReceiving(in template charstring pl_expected) runs on MyComp {
253 timer T := 10.0;
254 T.start;
255 alt {
256 [] PCO.receive { log("msg received"); setverdict(pass) };
257 [] T.timeout { setverdict(none, "timeout"); }
258 }
259}
260
261function f_valueof(in template charstring pl_msg) runs on MyComp {
262 @try {
263 var charstring vl_msg:= valueof(pl_msg);
264 if(isvalue(pl_msg) ) {
265 setverdict(pass, "no exception for valueof( real value template[sending template])")
266 } else {
267 setverdict(fail, "no exception for valueof(not real value template)");
268 }
269 }
270 @catch(e) {
271 log("Catched DTE:", e);
272 f_setverdict_str(e, pattern "*Performing valueof or send operation on a non-specific charstring template.");
273
274 if(isvalue(pl_msg) ) {
275 setverdict(fail, "exception for valueof of real value template");
276 } else {
277 setverdict(pass, "exception for valueof of not real value template[receiving template]");
278 }
279 }
280}
281
282function f_assignment_int(inout integer pl_left, in integer pl_right ) {
283 @try {
284 pl_left:=pl_right;
285 if( not isbound(pl_right) ) {
286 log("right value:",pl_right);
287 setverdict(fail, "no exception for Assignment of an unbound value " )
288 } else {
289 setverdict(pass, "exception for Assignment of a bound value");//this line never reached
290 }
291 }
292 @catch(m){
293 log("Catched DTE:",m);
294 if( not isbound(pl_right) ) {
295 setverdict(pass, "exception for Assignment of an unbound value." )
296 } else {
297 setverdict(fail,"no exception for Assignment of a bound value.") }
298 f_setverdict_str(m, pattern "*Assignment of an unbound integer value.");
299 }
300}
301
302//counter counts the number of function calls
303//ther is no evaluation in try-catch and there is one evaluation if pl_evalFlag is true
304function f_returnsThreeAndCounts() runs on MyComp return integer{
305 v_funcCallCounter:=v_funcCallCounter+1;
306 return 1;
307}
308
309function f_lazy_withTryCatch_exprNotEval( in @lazy integer pl_i, in boolean pl_evalFlag) runs on MyComp {
310 var integer vl_i:=0;
311 var RoI vl_roi1:={1,2};
312 @try {
313 vl_i:=vl_roi1[10]; //DTE!!!
314 vl_i:= pl_i; //expression is not evaluated
315 setverdict(fail);
316 }
317 @catch(e) {
318 log("Catched DTE:",e);
319 setverdict(pass);
320 }
321 if(pl_evalFlag) {
322 vl_i:= pl_i;
323 }
324
325 log("Inside lazy:", vl_i);
326}
327
328
329//******* Testcases ***********
330
331//The exception is thrown by date conversion DTE
332group g_divByZero {
333testcase tc_tryCatchInTc_divByZero() runs on MyComp {
334 var integer i :=10, j:=0;
335 @try {
336 i:=i/j
337 }
338 @catch(dte_str){
339 setverdict(pass);
340 stop;
341 }
342 setverdict(fail); //never reach this line
343}
344
345testcase tc_tryCatchInFunc_divByZero() runs on MyComp {
346 f_divByZero();
347}
348
349}//g_divByZero
350
351group g_indexOverflow {
352
353testcase tc_tryCatchInTc_indexOverflow1() runs on MyComp {
354 var RoI vl_roi :={}
355 @try{
356 log(vl_roi[0]);
357 }
358 @catch(dte_str){
359 log("Catched DTE:",dte_str);
360 setverdict(pass);
361 stop;
362 }
363 setverdict(fail);
364}
365
366testcase tc_tryCatchInTc_indexOverflow2() runs on MyComp {
367 var RoI vl_roi :={1,2,3}
368 @try{
369 log(vl_roi[3]);
370 setverdict(fail);
371 }
372 @catch(dte_str){
373 log("Catched DTE:",dte_str);
374 setverdict(pass);
375 stop;
376 }
377 setverdict(fail);
378}
379
380testcase tc_tryCatchInTc_indexNegative() runs on MyComp {
381 var RoI vl_roi :={1,2,3}
382 var integer vl_i :=-1;
383 @try{
384 log(vl_roi[vl_i]);
385 setverdict(fail);
386 }
387 @catch(dte_str){
388 log("Catched DTE:",dte_str);
389 setverdict(pass);
390 stop;
391 }
392 setverdict(fail);
393}
394
395
396}//g_indexOverflow
397
398group g_union {
399
400testcase tc_tryCatchInTc_unionInvalidAccess1() runs on MyComp {
401 var UnionType1 vl_e := {girl := {emma := {name := "Emma"}}};
402 @try {
403 log(log2str("numero: ",vl_e.numero));
404 setverdict(fail, "no exception at Using non-selected union field");
405 }
406 @catch(e) {
407 log("Catched DTE:",e);
408 setverdict(pass, "exception at Using non-selected union field");
409 }
410
411}
412
413testcase tc_tryCatchInTc_unionInvalidAccess2() runs on MyComp {
414 var UnionType1 vl_e := {girl := {emma := {name := "Emma"}}};
415 var integer i:=1;
416 @try {
417 i:= vl_e.numero;
418 setverdict(fail, "no exception at Using non-selected union field" );
419 }
420 @catch(e) {
421 log("Catched DTE:",e);
422 setverdict(pass, "exception at Using non-selected union field");
423 }
424
425}
426
427testcase tc_tryCatchInTc_templatePatternInvalidAccess_hex() runs on MyComp {
428 var template hexstring vth := 'AABB?CC'H // It's a pattern, cannot be indexed, runtime error.
429 @try {
430 vth[0] := 'B'H;
431 setverdict(fail);
432 }
433 @catch(e) {
434 log("Catched DTE:",e);
435 setverdict(pass, "exception at Accessing a hexstring element of a non-specific hexstring template");
436 }
437}
438
439}//g_union
440
441group g_conversion {
442
443//this testcase checks if a try-catch works within a testcase
444testcase tc_tryCatchInTc_conv() runs on MyComp {
445 var charstring vl_ch1:= "11", vl_ch2:="a";
446 @try{
447 var integer vl_i:= str2int(vl_ch1&vl_ch2);
448 }
449 @catch(e) {
450 log("Catched DTE:",e);
451 setverdict(pass);
452 }
453
454 //normal case:
455 @try{
456 var integer vl_i:= str2int(vl_ch1);
457 setverdict(pass);
458 }
459 @catch(e) {
460 log("Catched DTE:",e);
461 setverdict(fail,"exception for normal value");
462 }
463
464}
465
466//this testcase checks if a try-catch works within a function
467testcase tc_tryCatchInFunc_conv() runs on MyComp {
468 f_tryCatch_conv();
469}
470
471//this testcase checks if a catch block can call imported functions
472testcase tc_tryCatchInFunc_conv_imported() runs on MyComp {
473 f_tryCatch_conv_imported();
474}
475
476//this testcase checks if an exception thrown from another function will be catched or not
477testcase tc_tryCatchInFunc_conv_throw() runs on MyComp {
478 @try{
479 f_tryCatch_conv_throw();
480 }
481 @catch(f){
482 if(match(f, pattern "*The argument of function str2hex\(\) shall contain hexadecimal digits only,*"))
483 {
484 setverdict(pass);
485 stop;
486 } else
487 {
488 setverdict(fail)
489 };
490 }
491 setverdict(fail); //this line must not reached!
492}
493
494//this testcase checks if a try-catch works within an altstep
495testcase tc_tryCatchInAltstep_conv() runs on MyComp {
496 as_tryCatch_conv();
497}
498
499}//g_conversion
500
501//***** send-recv ********
502
503//this testcase checks if a try-catch without DTE work or not for sending & receiving
504
505group g_send_recv {
506
507testcase tc_tryCatchInTc_send_positive() runs on MyComp {
508 var MyComp vc_A:=MyComp.create("Sending1");
509 var MyComp vc_B:=MyComp.create("Receiving1");
510 connect(vc_A:PCO,vc_B:PCO);
511 vc_B.start(f_behavior_receiving(tr_recv));
512 vc_A.start(f_behavior_sending(t_send));
513 all component.done;
514 setverdict(pass);
515
516}
517
518testcase tc_tryCatchInTc_send_neg() runs on MyComp {
519 var MyComp vc_A:=MyComp.create("Sending2");
520 var MyComp vc_B:=MyComp.create("Receiving2");
521 connect(vc_A:PCO,vc_B:PCO);
522 vc_B.start(f_behavior_receiving(tr_recv));
523 vc_A.start(f_behavior_sending(tr_recv));
524 all component.done;
525 setverdict(pass);
526}
527
528testcase tc_tryCatchInTc_send_positive_notConnected() runs on MyComp {
529 var MyComp vc_A:=MyComp.create("Sending3");
530 var MyComp vc_B:=MyComp.create("Receiving3");
531 //>>>>>>>>>>>connect(vc_A:PCO,vc_B:PCO);
532 vc_B.start(f_behavior_receiving(tr_recv));
533 vc_A.start(f_behavior_sending(t_send));
534 all component.done;
535 setverdict(pass);
536
537}
538
539testcase tc_tryCatchInTc_send_positive_noReceivingSide() runs on MyComp {
540 var MyComp vc_A:=MyComp.create("Sending4");
541 var MyComp vc_B:=MyComp.create("Receiving4");
542 connect(vc_A:PCO,vc_B:PCO);
543 //vc_B.start(f_behavior_receiving(tr_recv));
544 vc_A.start(f_behavior_sending(t_send));
545 all component.done;
546 disconnect(vc_A:PCO,vc_B:PCO);
547 setverdict(pass);
548
549}
550
551testcase tc_tryCatchInTc_send_positive_noReceivingSide_InfiniteSending() runs on MyComp {
552 var MyComp vc_A:=MyComp.create("Sending5");
553 var MyComp vc_B:=MyComp.create("Receiving5");
554 connect(vc_A:PCO,vc_B:PCO);
555 vc_B.start(f_behavior_notReceiving(tr_recv));
556 vc_A.start(f_behavior_sendingInfinite(t_send));
557 all component.done;
558 disconnect(vc_A:PCO,vc_B:PCO);
559 setverdict(pass);
560
561}
562
563testcase tc_tryCatchInTc_mapping() runs on MyComp {
564 var MyComp vc_A:=MyComp.create("Sending6");
565 var MyComp vc_B:=MyComp.create("Receiving6");
566 @try {
567 map(vc_A:ExternalPCO,vc_B:ExternalPCO);
568 }
569 @catch(e){
570 log("Catched DTE:",e);
571 setverdict(pass);
572 stop;
573 }
574 setverdict(fail);
575}
576
577testcase tc_tryCatchInTc_unmapping() runs on MyComp {
578 var MyComp vc_A:=MyComp.create("Sending6");
579 var MyComp vc_B:=MyComp.create("Receiving6");
580 connect(vc_A:PCO,vc_B:PCO);
581 map(vc_A:ExternalPCO,system:ExternalPCO);
582 map(vc_B:ExternalPCO,system:ExternalPCO);
583 @try {
584 unmap(vc_A:ExternalPCO,vc_B:ExternalPCO);
585 setverdict(fail);
586 }
587 @catch(e){
588 log("Catched DTE:",e);
589 setverdict(pass)
590 }
591}
592
593testcase tc_tryCatchInTc_externalSend_positive() runs on MyComp {
594 var MyComp vc_A:=MyComp.create("Sending6");
595 var MyComp vc_B:=MyComp.create("Receiving6");
596 connect(vc_A:PCO,vc_B:PCO);
597 map(vc_A:ExternalPCO,system:ExternalPCO);
598 map(vc_B:ExternalPCO,system:ExternalPCO);
599 vc_B.start(f_behavior_externalReceiving(tr_recv));
600 vc_A.start(f_behavior_externalSending(t_send));
601 all component.done;
602 unmap(vc_A:ExternalPCO,system:ExternalPCO);
603 unmap(vc_B:ExternalPCO,system:ExternalPCO);
604 disconnect(vc_A:PCO,vc_B:PCO);
605 setverdict(pass);
606}
607
608//this testcase checks if exception is thrown when the sending puffer is overloaded
609testcase tc_tryCatchInTc_externalSend_noReceiving_InfiniteSending() runs on MyComp {
610 map(self:ExternalPCO,system:ExternalPCO);
611 var integer i;
612 @try {
613 for(i:=0; i<1000000; i:=i+1) {
614 ExternalPCO.send("Hello"&int2str(i));
615 }
616 setverdict(pass, "No stack overflow happened");
617 }
618 @catch(e){
619 log("Catched DTE:",e);
620 setverdict(pass)
621 }
622 //f_behavior_externalSending(t_send);
623 unmap(self:ExternalPCO,system:ExternalPCO);
624}
625
626}//g_send_recv
627
628group g_valueof {
629
630testcase tc_tryCatchInFunc_valueof_pos() runs on MyComp {
631 f_valueof(t_send);
632}
633
634testcase tc_tryCatchInFunc_valueof_neg() runs on MyComp {
635 f_valueof(tr_recv);
636}
637
638}//g_valueof
639
640
641group g_assignment {
642
643testcase tc_tryCatchInTc_assignment_integer() runs on MyComp {
644 var integer i,j;
645 @try {
646 j:=i; //uninitialized right value
647 setverdict(fail,"no exception for Assignment of an unbound value" ); //do not executed!
648 }
649 @catch(m) {
650 log("Catched DTE:",m);
651 f_setverdict_str(m, pattern "*Assignment of an unbound integer value.");
652 //setverdict(pass);
653 }
654 //normal case:
655 i:=1;
656 @try {
657 j:=i;
658 setverdict(pass,"no exception for Assignment of a not unbound value" );
659 }
660 @catch(m) {
661 log("Catched DTE:",m);
662 setverdict(fail, "exception for Assignment of an unbound value");
663 }
664}
665
666testcase tc_tryCatchInFunc_assignment_integer() runs on MyComp {
667 var integer i,j;
668 f_assignment_int(i,j);//unbound assignment
669 j:=1;
670 f_assignment_int(i,j);//bound assignment
671}
672
673}//g_assignment
674
675
676//this testcase checks the cooperation of the @lazy and the @try-catch functionality
677testcase tc_tryCatchInFunc_lazy1() runs on MyComp {
678 v_funcCallCounter:=0;
679 f_lazy_withTryCatch_exprNotEval(2*f_returnsThreeAndCounts(), true);
680 if(v_funcCallCounter==1) {setverdict(pass)} else {setverdict(fail)};
681}
682
683testcase tc_tryCatchInFunc_lazy2() runs on MyComp {
684 v_funcCallCounter:=0;
685 f_lazy_withTryCatch_exprNotEval(2*f_returnsThreeAndCounts(), false);
686 if(v_funcCallCounter==0) {setverdict(pass)} else {setverdict(fail)};
687}
688
689
690//******* Control Part ********
691control {
692 var integer vl_i:=3, vl_j:=6;
693 var MySmallInt vl_s := vl_i*vl_j;// run-time not checked
694 var charstring vl_ch1:= "11", vl_ch2:="a";
695 @try{
696 vl_i:= str2int(vl_ch1&vl_ch2);
697 }
698 @catch(e) {
699 log("Catched DTE:",e);
700 //no setverdict can be applied here
701 }
702
703 execute(tc_tryCatchInTc_divByZero());
704 execute(tc_tryCatchInFunc_divByZero());
705 execute(tc_tryCatchInTc_indexOverflow1());
706 execute(tc_tryCatchInTc_indexOverflow2());
707 execute(tc_tryCatchInTc_indexNegative());
708 execute(tc_tryCatchInTc_unionInvalidAccess1());
709 execute(tc_tryCatchInTc_unionInvalidAccess2());
710 execute(tc_tryCatchInTc_templatePatternInvalidAccess_hex());
711 execute(tc_tryCatchInTc_conv());
712 execute(tc_tryCatchInFunc_conv());
713 execute(tc_tryCatchInFunc_conv_imported());
714 execute(tc_tryCatchInFunc_conv_throw());
715 execute(tc_tryCatchInAltstep_conv());
716 execute(tc_tryCatchInTc_send_positive());
717 execute(tc_tryCatchInTc_send_neg());
718 execute(tc_tryCatchInTc_send_positive_notConnected());
719 execute(tc_tryCatchInTc_send_positive_noReceivingSide());
720 //execute(tc_tryCatchInTc_send_positive_noReceivingSide_InfiniteSending());
721 execute(tc_tryCatchInTc_mapping());
722 execute(tc_tryCatchInTc_unmapping());
723 execute(tc_tryCatchInTc_externalSend_positive());
724 //execute(tc_tryCatchInTc_externalSend_noReceiving_InfiniteSending());
725 execute(tc_tryCatchInFunc_valueof_pos());
726 execute(tc_tryCatchInFunc_valueof_neg());
727 execute(tc_tryCatchInTc_assignment_integer());
728 execute(tc_tryCatchInFunc_assignment_integer());
729 execute(tc_tryCatchInFunc_lazy1());
730 execute(tc_tryCatchInFunc_lazy2());
731}
732
733}
This page took 0.051937 seconds and 5 git commands to generate.