Titan Core Initial Contribution
[deliverable/titan.core.git] / regression_test / anytype / AnytypeTest.ttcnpp
1 /******************************************************************************
2 * Copyright (c) 2000-2014 Ericsson Telecom AB
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
7 ******************************************************************************/
8 module AnytypeTest . objid { 6 6 6 } {
9
10 //import from Supplier all;
11
12 type record of charstring rof_string;
13
14 type anytype other_anytype;
15
16 type record of anytype anysequence;
17
18 type integer AT_integer;
19
20 template integer pi_t := 3+14+15+926;
21
22 type union anytype_dup {
23 integer integer_dup,
24 objid objid_dup,
25 rof_string foobar_dup,
26 fooref fooref_dup,
27 pifunc pifunc_dup,
28 integer another_integer
29 }
30
31 type record address {
32 charstring a optional,
33 union { boolean w } u
34 } ;
35
36 type component and_on {}
37
38 template anytype anytemplate := { integer := 42 }
39 template anytype_dup anytemplate_dup := { integer_dup := 42 }
40
41
42 function foonction(in charstring input) return charstring
43 {
44 var integer e := lengthof(input);
45
46 return input;
47 }
48 with { extension override "" }
49
50 type function fooref(in charstring input) return charstring;
51
52 function pi() return float
53 {
54 return 22.0/7.0;
55 }
56
57 type function pifunc() return float;
58
59 /// No XER encoding for anytype
60 ///external function enc_anytype(in anytype at) return charstring
61 ///with { extension override "prototype(convert) encode(XER)" }
62
63 testcase t1() runs on and_on
64 {
65 var anytype anyvar;
66 var anytype_dup anyvar_dup;
67
68 // this is not accepted by the compiler because it wants three components: if (ischosen(anyvar.integer)) {}
69
70 anyvar.rof_string := {};
71
72 anyvar .integer := 42;
73 if (ischosen(anyvar.integer)) { setverdict(pass)}
74 else { setverdict(fail); }
75
76 anyvar_dup.integer_dup := 42;
77 if (ischosen(anyvar_dup.foobar_dup)) { setverdict(fail)}
78 else { setverdict(pass); }
79
80 //DON'T DO THIS: triggers TR940
81 //anyvar_dup.another_integer := anyvar_dup.integer_dup;
82
83 if (anyvar.integer == anyvar_dup.integer_dup) { setverdict(pass); }
84 else { setverdict(fail); }
85
86 if (match (anyvar_dup, anytemplate_dup)) { setverdict(pass); }
87 else { setverdict(fail); }
88 if (match (anyvar, anytemplate)) { setverdict(pass); }
89 else { setverdict(fail); }
90
91 // Triggers TR940: anyvar.AT_integer := anyvar.integer ; // fields of same type
92 //if (ischosen(anyvar.integer)) { setverdict(fail)}
93 //else { setverdict(pass); }
94 //if (ischosen(anyvar.AT_integer)) { setverdict(pass)}
95 //else { setverdict(fail); }
96
97
98 anyvar.objid := objid { 2 2 2 }
99 anyvar_dup.objid_dup := objid { 2 2 2 }
100
101 if (anyvar.objid == anyvar_dup.objid_dup) { setverdict(pass); }
102 else { setverdict(fail); }
103
104 // now it shouldn't match
105 if (not match (anyvar_dup, anytemplate_dup)) { setverdict(pass); }
106 else { setverdict(fail); }
107 if (not match (anyvar, anytemplate)) { setverdict(pass); }
108 else { setverdict(fail); }
109
110 var anytype anys[2] := { {integer := 37}, {objid := objid{0 3 14 15 9}} }
111 //var anysequence aseq;
112 var integer thirty_seven := anys[0].integer;
113
114 if (thirty_seven == 37) { setverdict(pass); }
115 else { setverdict(fail); }
116
117 anyvar.bitstring := '010010001'B;
118 anyvar.boolean := true;
119 anyvar.charstring := "How are you gentlemen?";
120 anyvar.universal charstring := "All your base are belong to us";
121 // integer already fiddled with
122 anyvar.octetstring := 'DEADBEEF'O;
123 // anyvar.hexstring := 'DECAFBAD'H;
124 // anyvar.verdicttype := inconc;
125 anyvar.float := 2.718281828;
126 // address
127 // anyvar.default := null;
128 // objid already fiddled with
129 anyvar.address.a := "For great justice";
130 anyvar.address.u.w := true;
131 //semantic error: "@AnytypeTest.address cannot be indexed" : var boolean bbb := anyvar.address[0];
132 //that's because address in this module is a record, not a record-of
133
134 //anyvar.fooref(charstring input) := foonction;
135 var pifunc pf := refers(pi);
136 var fooref fo := refers( foonction );
137
138 anyvar.fooref := refers( foonction );
139
140 anyvar.pifunc := refers(pi);
141 anyvar_dup.pifunc_dup := refers(pi);
142 anyvar.objid := objid{0 3 14 15 9}
143
144 //var float pie := anyvar.pifunc();
145
146 /* This gives the same "Unbound left operand of integer comparison" error
147 as anyvar_dup. Non-const strikes again!
148 I think it should be "wrong alternative" !
149
150 if (anyvar.integer == 42) { setverdict(pass); }
151 else { setverdict(fail); }
152 // same for "right operand"
153 if (42 == anyvar.integer) { setverdict(pass); }
154 else { setverdict(fail); }
155 /**/
156 // misidentified as module.something:
157 //anyvar.pifunc( 3+2 );
158
159 // anyvar.octetstring := Supplier.epdv.data_value;
160 }
161
162 // Examples from "An Introduction to TTCN-3"
163 const anytype c := { integer := 42 }
164 testcase t2(in anytype pa) runs on and_on
165 {
166 var anytype anyvar := { integer := 42 }
167 var integer x1 := anyvar.integer;
168
169 anyvar.charstring := "fourty-two";
170 anyvar.float := 42.0;
171
172 //x1 := anyvar.integer; // would result in a dynamic testcase error.
173 // However! The error message ("Assignment of an unbound integer value")
174 // betrays incorrect operation: anyvar has become converted to integer
175
176 log(x1);
177
178 //x1 := pa.integer; // would result in a dynamic testcase error (non-selected field)
179 var charstring pacs := pa.charstring;
180 // compile-time error (inactive field): anyvar.float := c.float;
181 if (ischosen(anyvar.integer)) { setverdict(fail,"Not integer but float should be chosen"); }
182 else { setverdict(pass); }
183 var integer x2 := float2int( anyvar.float );
184
185 var anytype x_any := { charstring := "All your base are belong to us" }
186 //var charstring str := x_any; // a value of type charstring was expected instead of anytype
187 //var anytype x_str := x_any.charstring; // a value of type anytype was expected instead of charstring
188 var anytype x_any1 := x_any;
189
190 }
191
192 /* This is how it would look if type parameterization was supported: */
193 //const integer p := 7;
194 type record myrecord /* (integer p) */ {
195 integer f1 /*( 0.. p )*/
196 }
197
198 type union level1 {
199 integer i0,
200 float f0,
201 charstring s0
202 }
203
204 type record level2 {
205 level1 l1
206 }
207
208 type record level3 {
209 level2 l2
210 }
211
212 type union level4 {
213 level3 l3
214 }
215
216 testcase t3() runs on and_on
217 {
218 var anytype anyvar;
219 //misidentified as ModuleName.something:
220 //anyvar.myrecord(3).f1 := 3;
221 }
222
223 template anytype t_at := { integer := 3 };
224 type record of anytype rat;
225 testcase t4() runs on and_on
226 {
227 var rat patkany;
228 patkany[0].integer := 3;
229 // patkany[1].myrecord("WTF is this ?").f1 := 7; // Bingo! 7719
230 patkany[2].myrecord.f1 := 4;
231 var anytype ratt := patkany[0];
232
233 var level4 top;
234 var level1 vl_l;
235 var level2 vl2;
236 vl_l.i0 := 0;
237 var boolean isit;
238 isit := ischosen(vl_l.s0);
239 isit := ischosen(vl2.l1.s0);
240 isit := ischosen(patkany[0] /* anytype */ .address /* a record */ .u /* a union */ .w /* a boolean */ );
241
242 isit := ischosen(ratt /* anytype */ .address /* a record */ .u /* a union */ .w /* a boolean */ ); // Bingo ! 7990 anytype ispresent arg
243
244 if ( ischosen(patkany[0] /* anytype */ . level4 . l3 . l2 . l1 . f0) ) {
245 setverdict(fail)
246 }
247
248 }
249
250 control{
251 execute(t1());
252 var anytype la := { charstring := "Fourty-foo" }
253 execute(t2(la));
254 }
255
256 }
257 with {
258 encode "RAW"
259 extension "anytype bitstring,boolean,charstring,universal charstring,integer,octetstring,/*hexstring,verdicttype,*/float,/*default,*/objid,address, rof_string,fooref,pifunc,myrecord,level4, AT_integer"
260 // \-- predefined types, in the order of PredefinedType in compiler.y:6179 (address is missing here ^^) ---------/
261
262 }
263
This page took 0.05931 seconds and 6 git commands to generate.