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