implemented decmatch (artf724241)
[deliverable/titan.core.git] / regression_test / templateBitstr / TtemplateBitstr.ttcn
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 * Baranyi, Botond
10 * Balasko, Jeno
11 * Kovacs, Ferenc
12 * Raduly, Csaba
13 * Szabados, Kristof
14 * Szabo, Janos Zoltan – initial implementation
15 *
16 ******************************************************************************/
17 module TtemplateBitstr {
18 type component templateBitstr_mycomp {};
19 type record templateBitstr_rec {
20 bitstring x1,
21 bitstring x2,
22 bitstring x3 optional
23 }
24 with {
25 encode "JSON";
26 variant(x1) "JSON: name as first";
27 variant(x2) "JSON: name as second";
28 variant(x3) "JSON: name as third";
29 }
30
31 type record decmatch_rec {
32 integer i,
33 charstring s
34 }
35 with {
36 encode "JSON";
37 }
38
39 type record of integer decmatch_list
40 with {
41 encode "XML";
42 variant "list";
43 }
44
45 type union decmatch_uni {
46 integer i,
47 charstring s
48 }
49 with {
50 encode "RAW";
51 variant(i) "FIELDLENGTH(16)";
52 }
53
54
55 template templateBitstr_rec templateBitstr_tSpec :={ //specific values
56 x1:='001'B,
57 x2:='010'B,
58 x3:='100'B };
59 template templateBitstr_rec templateBitstr_tList :={ //specific value and value list
60 x1:='001'B,
61 x2:=('010'B,'011'B,'1100'B),
62 x3:='100'B };
63 template templateBitstr_rec templateBitstr_tComp :={ //specific value and compl. list
64 x1:='001'B,
65 x2:=complement ('11'B,'00'B,'1000'B),
66 x3:='100'B };
67 template templateBitstr_rec templateBitstr_tOmit :={ //omitting values
68 x1:='001'B,
69 x2:='010'B,
70 x3:=omit } ;
71 template templateBitstr_rec templateBitstr_tAny :={ //specific and any value
72 x1:='001'B,
73 x2:='010'B,
74 x3:=? } ;
75 template templateBitstr_rec templateBitstr_tAnyorNone :={ //specific and AnyorNone value
76 x1:='001'B,
77 x2:='010'B,
78 x3:=* };
79 template templateBitstr_rec templateBitstr_tLength1 :={ //specific value and fix length
80 x1:='001'B,
81 x2:='010'B,
82 x3:=* length(3) };
83 template templateBitstr_rec templateBitstr_tLength2 :={ //specific value and length (range)
84 x1:='001'B,
85 x2:='010'B,
86 x3:=? length(2..4) };
87 template templateBitstr_rec templateBitstr_tLength3 :={ //specific value and length (range, infinity)
88 x1:='001'B,
89 x2:='010'B,
90 x3:=? length(2..infinity) };
91 template templateBitstr_rec templateBitstr_tIfpresent :={ //specific value and ifpresent
92 x1:='001'B,
93 x2:='010'B,
94 x3:='100'B ifpresent };
95 template templateBitstr_rec templateBitstr_tLengthIfp :={ //specific value and fix length and ifpresent
96 x1:='001'B,
97 x2:='010'B,
98 x3:= ? length(3) ifpresent};
99 template templateBitstr_rec templateBitstr_tAnyEl :={ //specific value and any element inside value
100 x1:='001'B,
101 x2:='010'B,
102 x3:='10?'B } ;
103 template templateBitstr_rec templateBitstr_tAnyorNoneEl :={ //specific value and Any number of elements or none inside value
104 x1:='001'B,
105 x2:='010'B,
106 x3:='10*'B };
107
108 template bitstring t_param1(templateBitstr_rec par) := substr(par.x1, 0, 1); // parameterised template
109
110 template decmatch_uni decmatch_tUnion := { i := ? };
111
112 template decmatch_rec decmatch_tRecord := { i := (0..infinity), s := ? };
113
114 template templateBitstr_rec templateBitstr_tDecmatch := { // decoded content match
115 x1 := decmatch decmatch_list: { (1..10), (11..20), (21..30) },
116 x2 := decmatch decmatch_tUnion,
117 x3 := decmatch modifies decmatch_tRecord := { s := "abc" }
118 };
119
120 template templateBitstr_rec templateBitstr_tDecmatchSelfRef := { // decoded content match with self-reference
121 x1 := '1011'B,
122 x2 := decmatch templateBitstr_tDecmatchSelfRef.x1,
123 x3 := decmatch templateBitstr_rec: { x1 := templateBitstr_tDecmatchSelfRef.x1, x2 := ?, x3 := * }
124 };
125
126 testcase templateBitstrSpec() runs on templateBitstr_mycomp {
127 var templateBitstr_rec x1,x2; //specific value
128 x1:={ x1:='001'B, x2:='010'B, x3:='100'B };
129 x2:={ x1:='00'B, x2:='010'B, x3:='100'B };
130 //match
131 if (match(x1,templateBitstr_tSpec)) {setverdict(pass);}
132 else {setverdict(fail);}
133 //no match
134 if (not(match(x2,templateBitstr_tSpec))) {setverdict(pass);}
135 else {setverdict(fail);}
136 }
137
138 testcase templateBitstrList() runs on templateBitstr_mycomp {
139 var templateBitstr_rec x1,x2,x3; //value list
140 x1:={ x1:='001'B, x2:='010'B, x3:='100'B };
141 x2:={ x1:='001'B, x2:='00'B, x3:='100'B };
142 x3:={ x1:='1'B, x2:='010'B, x3:='100'B };
143 //match
144 if (match(x1,templateBitstr_tList)) {setverdict(pass);}
145 else {setverdict(fail);}
146 //no match: out of list
147 if (not(match(x2,templateBitstr_tList))) {setverdict(pass);}
148 else {setverdict(fail);}
149 //no match: other field
150 if (not(match(x3,templateBitstr_tList))) {setverdict(pass);}
151 else {setverdict(fail);}
152 }
153
154 testcase templateBitstrComp() runs on templateBitstr_mycomp {
155 var templateBitstr_rec x1,x2,x3; //complemented list
156 x1:={ x1:='001'B, x2:='010'B, x3:='100'B };
157 x2:={ x1:='001'B, x2:='11'B, x3:='100'B };
158 x3:={ x1:='11'B, x2:='010'B, x3:='100'B };
159 //match
160 if (match(x1,templateBitstr_tComp)) {setverdict(pass);}
161 else {setverdict(fail);}
162 //no match: in the list
163 if (not(match(x2,templateBitstr_tComp))) {setverdict(pass);}
164 else {setverdict(fail);}
165 //no match: other field
166 if (not(match(x3,templateBitstr_tComp))) {setverdict(pass);}
167 else {setverdict(fail);}
168 }
169
170 testcase templateBitstrOmit() runs on templateBitstr_mycomp {
171 var templateBitstr_rec x1,x2,x3; //omitting value
172 x1:={ x1:='001'B, x2:='010'B, x3:=omit };
173 x2:={ x1:='001'B, x2:='010'B, x3:='000'B };
174 x3:={ x1:='00'B, x2:='010'B, x3:=omit };
175 //match
176 if (match(x1,templateBitstr_tOmit)) {setverdict(pass);}
177 else {setverdict(fail);}
178 //no match: not omitted
179 if (not(match(x2,templateBitstr_tOmit))) {setverdict(pass);}
180 else {setverdict(fail);}
181 //no match: other field
182 if (not(match(x3,templateBitstr_tOmit))) {setverdict(pass);}
183 else {setverdict(fail);}
184 }
185
186 testcase templateBitstrAny() runs on templateBitstr_mycomp {
187 var templateBitstr_rec x1,x2,x3; //any value
188 x1:={ x1:='001'B, x2:='010'B, x3:='111'B };
189 x2:={ x1:='001'B, x2:='010'B, x3:=omit };
190 x3:={ x1:='0'B, x2:='010'B, x3:='10'B };
191 //match
192 if (match(x1,templateBitstr_tAny)) {setverdict(pass);}
193 else {setverdict(fail);}
194 //no match: field omitted
195 if (not(match(x2,templateBitstr_tAny))) {setverdict(pass);}
196 else {setverdict(fail);}
197 //no match: other field
198 if (not(match(x3,templateBitstr_tAny))) {setverdict(pass);}
199 else {setverdict(fail);}
200 }
201
202 testcase templateBitstrAnyorNone() runs on templateBitstr_mycomp {
203 var templateBitstr_rec x1,x2,x3; //AnyorNone value
204 x1:={ x1:='001'B, x2:='010'B, x3:=omit };
205 x2:={ x1:='001'B, x2:='010'B, x3:='100'B };
206 x3:={ x1:='1'B, x2:='010'B, x3:=omit };
207 //match: omitted
208 if (match(x1,templateBitstr_tAnyorNone)) {setverdict(pass);}
209 else {setverdict(fail);}
210 //match: value
211 if (match(x2,templateBitstr_tAnyorNone)) {setverdict(pass);}
212 else {setverdict(fail);}
213 //no match: other field
214 if (not(match(x3,templateBitstr_tAnyorNone))) {setverdict(pass);}
215 else {setverdict(fail);}
216 }
217
218 testcase templateBitstrLength1() runs on templateBitstr_mycomp {
219 var templateBitstr_rec x1,x2,x3,x4; //length (fix)
220 x1:={ x1:='001'B, x2:='010'B, x3:='111'B };
221 x2:={ x1:='001'B, x2:='010'B, x3:=omit };
222 x3:={ x1:='001'B, x2:='010'B, x3:='10'B };
223 x4:={ x1:='0010'B, x2:='010'B, x3:='111'B };
224 //match: proper length
225 if (match(x1,templateBitstr_tLength1)) {setverdict(pass);}
226 else {setverdict(fail);}
227 //match: omitted
228 if (match(x2,templateBitstr_tLength1)) {setverdict(pass);}
229 else {setverdict(fail);}
230 // no match: not proper length
231 if (not(match(x3,templateBitstr_tLength1))) {setverdict(pass);}
232 else {setverdict(fail);}
233 //no match: other field
234 if (not(match(x4,templateBitstr_tLength1))) {setverdict(pass);}
235 else {setverdict(fail);}
236 }
237
238 testcase templateBitstrLength2() runs on templateBitstr_mycomp {
239 var templateBitstr_rec x1,x2,x3; //length (range)
240 x1:={ x1:='001'B, x2:='010'B, x3:='111'B };
241 x2:={ x1:='001'B, x2:='010'B, x3:='1'B };
242 x3:={ x1:='0010'B, x2:='010'B, x3:='111'B };
243 //match
244 if (match(x1,templateBitstr_tLength2)) {setverdict(pass);}
245 else {setverdict(fail);}
246 // no match: not proper length
247 if (not(match(x2,templateBitstr_tLength2))) {setverdict(pass);}
248 else {setverdict(fail);}
249 //no match: other field
250 if (not(match(x3,templateBitstr_tLength2))) {setverdict(pass);}
251 else {setverdict(fail);}
252 }
253
254 testcase templateBitstrLength3() runs on templateBitstr_mycomp {
255 var templateBitstr_rec x1,x2,x3; //length (range, infinity)
256 x1:={ x1:='001'B, x2:='010'B, x3:='111'B };
257 x2:={ x1:='001'B, x2:='010'B, x3:='1'B };
258 x3:={ x1:='0010'B, x2:='010'B, x3:='111'B };
259 //match
260 if (match(x1,templateBitstr_tLength3)) {setverdict(pass);}
261 else {setverdict(fail);}
262 // no match: not proper length
263 if (not(match(x2,templateBitstr_tLength3))) {setverdict(pass);}
264 else {setverdict(fail);}
265 //no match: other field
266 if (not(match(x3,templateBitstr_tLength3))) {setverdict(pass);}
267 else {setverdict(fail);}
268 }
269
270 testcase templateBitstrIfpresent() runs on templateBitstr_mycomp {
271 var templateBitstr_rec x1,x2,x3,x4; //ifpresent
272 x1:={ x1:='001'B, x2:='010'B, x3:='100'B };
273 x2:={ x1:='001'B, x2:='010'B, x3:=omit };
274 x3:={ x1:='001'B, x2:='010'B, x3:='000'B };
275 x4:={ x1:='00'B, x2:='010'B, x3:=omit };
276 //match: present and match
277 if (match(x1,templateBitstr_tIfpresent)) {setverdict(pass);}
278 else {setverdict(fail);}
279 //match: not present
280 if (match(x2,templateBitstr_tIfpresent)) {setverdict(pass);}
281 else {setverdict(fail);}
282 //no match: present and not match
283 if (not(match(x3,templateBitstr_tIfpresent))) {setverdict(pass);}
284 else {setverdict(fail);}
285 //no match: other field
286 if (not(match(x4,templateBitstr_tIfpresent))) {setverdict(pass);}
287 else {setverdict(fail);}
288 }
289
290 testcase templateBitstrLengthIfp() runs on templateBitstr_mycomp {
291 var templateBitstr_rec x1,x2,x3,x4; //length (fix), ifpresent
292 x1:={ x1:='001'B, x2:='010'B, x3:='111'B };
293 x2:={ x1:='001'B, x2:='010'B, x3:=omit };
294 x3:={ x1:='001'B, x2:='010'B, x3:='10'B };
295 x4:={ x1:='0010'B, x2:='010'B, x3:='111'B };
296 //match: proper length
297 if (match(x1,templateBitstr_tLengthIfp)) {setverdict(pass);}
298 else {setverdict(fail);}
299 //match: omitted
300 if (match(x2,templateBitstr_tLengthIfp)) {setverdict(pass);}
301 else {setverdict(fail);}
302 // no match: not proper length
303 if (not(match(x3,templateBitstr_tLengthIfp))) {setverdict(pass);}
304 else {setverdict(fail);}
305 //no match: other field
306 if (not(match(x4,templateBitstr_tLengthIfp))) {setverdict(pass);}
307 else {setverdict(fail);}
308 }
309
310 testcase templateBitstrAnyEl() runs on templateBitstr_mycomp {
311 var templateBitstr_rec x1,x2,x3,x4,x5; //any element
312 x1:={ x1:='001'B, x2:='010'B, x3:='101'B };
313 x2:={ x1:='001'B, x2:='010'B, x3:='10'B };
314 x3:={ x1:='001'B, x2:='010'B, x3:='1000'B };
315 x4:={ x1:='001'B, x2:='010'B, x3:='001'B };
316 x5:={ x1:='0'B, x2:='010'B, x3:='101'B };
317 //match
318 if (match(x1,templateBitstr_tAnyEl)) {setverdict(pass);}
319 else {setverdict(fail);}
320 //no match: no element
321 if (not(match(x2,templateBitstr_tAnyEl))) {setverdict(pass);}
322 else {setverdict(fail);}
323 //no match: two element
324 if (not(match(x3,templateBitstr_tAnyEl))) {setverdict(pass);}
325 else {setverdict(fail);}
326 //no match: wrong element
327 if (not(match(x4,templateBitstr_tAnyEl))) {setverdict(pass);}
328 else {setverdict(fail);}
329 //no match: other field
330 if (not(match(x5,templateBitstr_tAnyEl))) {setverdict(pass);}
331 else {setverdict(fail);}
332 }
333
334 testcase templateBitstrAnyorNoneEl() runs on templateBitstr_mycomp {
335 var templateBitstr_rec x1,x2,x3,x4,x5; //Any number of elements or none
336 x1:={ x1:='001'B, x2:='010'B, x3:='10'B };
337 x2:={ x1:='001'B, x2:='010'B, x3:='100'B };
338 x3:={ x1:='001'B, x2:='010'B, x3:='1011'B };
339 x4:={ x1:='001'B, x2:='010'B, x3:='110'B };
340 x5:={ x1:='1'B, x2:='010'B, x3:='100'B };
341 //match: no element
342 if (match(x1,templateBitstr_tAnyorNoneEl)) {setverdict(pass);}
343 else {setverdict(fail);}
344 //match: one element
345 if (match(x2,templateBitstr_tAnyorNoneEl)) {setverdict(pass);}
346 else {setverdict(fail);}
347 //match: two element
348 if (match(x3,templateBitstr_tAnyorNoneEl)) {setverdict(pass);}
349 else {setverdict(fail);}
350 //no match: other element
351 if (not(match(x4,templateBitstr_tAnyorNoneEl))) {setverdict(pass);}
352 else {setverdict(fail);}
353 //no match: other field
354 if (not(match(x5,templateBitstr_tAnyorNoneEl))) {setverdict(pass);}
355 else {setverdict(fail);}
356 }
357
358 testcase CR_TR00018474() runs on templateBitstr_mycomp {
359 // Indexing of string template variables.
360 var template bitstring vtb1 := '0011110000'B
361 var template bitstring vtb2 := '0*?1'B // It's a pattern, cannot be indexed, runtime error.
362 vtb1[0] := '1'B
363 if (match('1011110000'B, vtb1)) { setverdict(pass) } else { setverdict(fail) }
364 vtb1[0] := '1'B // Indexed assignment notation cannot be used here.
365 vtb1[1] := '0'B // Still works, nothing special.
366 if (match('1011110000'B, vtb1)) { setverdict(pass) } else { setverdict(fail) }
367 }
368
369 testcase templateBitstrDecmatch() runs on templateBitstr_mycomp {
370 var decmatch_rec bad_rec, good_rec;
371 bad_rec := { i := 11, s := "xyz" };
372 good_rec := { i := 3, s := "abc" };
373 var decmatch_list bad_list, good_list;
374 bad_list := { 4, 7, 10 };
375 good_list := { 2, 15, 28 };
376 var decmatch_uni bad_uni, good_uni;
377 bad_uni := { s := "five" };
378 good_uni := { i := 5 };
379 var bitstring bad_rec_enc, good_rec_enc, bad_list_enc, good_list_enc, bad_uni_enc, good_uni_enc;
380 bad_rec_enc := encvalue(bad_rec);
381 good_rec_enc := encvalue(good_rec);
382 bad_list_enc := encvalue(bad_list);
383 good_list_enc := encvalue(good_list);
384 bad_uni_enc := encvalue(bad_uni);
385 good_uni_enc := encvalue(good_uni);
386 var templateBitstr_rec r1, r2, r3, r4, r5;
387 r1 := { x1 := good_list_enc, x2 := good_uni_enc, x3 := good_rec_enc };
388 r2 := { x1 := bad_list_enc, x2 := good_uni_enc, x3 := good_rec_enc };
389 r3 := { x1 := good_list_enc, x2 := bad_uni_enc, x3 := good_rec_enc };
390 r4 := { x1 := good_list_enc, x2 := good_uni_enc, x3 := bad_rec_enc };
391 r5 := { x1 := good_list_enc, x2 := good_uni_enc, x3 := '11110011'B };
392 // match: all 3 are good
393 if (match(r1, templateBitstr_tDecmatch)) { setverdict(pass); }
394 else { setverdict(fail, 1); }
395 // no match: decoded list does not match
396 if (not match(r2, templateBitstr_tDecmatch)) { setverdict(pass); }
397 else { setverdict(fail, 2); }
398 // no match: decoded union does not match
399 if (not match(r3, templateBitstr_tDecmatch)) { setverdict(pass); }
400 else { setverdict(fail, 3); }
401 // no match: decoded record does not match
402 if (not match(r4, templateBitstr_tDecmatch)) { setverdict(pass); }
403 else { setverdict(fail, 4); }
404 // no match: x3 is not a valid encoded record value
405 if (not match(r5, templateBitstr_tDecmatch)) { setverdict(pass); }
406 else { setverdict(fail, 5); }
407 // match r1 with the same template declared as an in-line template
408 if (match(r1, templateBitstr_rec: {
409 x1 := decmatch decmatch_list: { (1..10), (11..20), (21..30) },
410 x2 := decmatch decmatch_tUnion,
411 x3 := decmatch modifies decmatch_tRecord := { s := "abc" }
412 })) { setverdict(pass); }
413 else { setverdict(fail, 6); }
414 }
415
416 external function ef_enc_rec_x1(in templateBitstr_rec.x1 x) return octetstring
417 with { extension "prototype(convert) encode(JSON)" }
418
419 testcase templateBitstrDecmatchSelfRef() runs on templateBitstr_mycomp {
420 // global self-referencing template
421 var templateBitstr_rec.x1 bad_bs, good_bs;
422 bad_bs := '11'B;
423 good_bs := '1011'B;
424 var templateBitstr_rec bad_rec, good_rec;
425 bad_rec := { x1 := '01'B, x2 := '10'B, x3 := '11'B };
426 good_rec := { x1 := '1011'B, x2 := '10'B, x3 := '11'B };
427 var bitstring bad_bs_enc, good_bs_enc, bad_rec_enc, good_rec_enc;
428 bad_bs_enc := oct2bit(ef_enc_rec_x1(bad_bs));
429 good_bs_enc := oct2bit(ef_enc_rec_x1(good_bs));
430 bad_rec_enc := encvalue(bad_rec);
431 good_rec_enc := encvalue(good_rec);
432 var templateBitstr_rec r1, r2, r3;
433 r1 := { x1 := '1011'B, x2 := good_bs_enc, x3 := good_rec_enc };
434 r2 := { x1 := '1011'B, x2 := bad_bs_enc, x3 := good_rec_enc };
435 r3 := { x1 := '1011'B, x2 := good_bs_enc, x3 := bad_rec_enc };
436 // match: all 2 are good
437 if (match(r1, templateBitstr_tDecmatchSelfRef)) { setverdict(pass); }
438 else { setverdict(fail, 1); }
439 // no match: decoded octetstring does not match
440 if (not match(r2, templateBitstr_tDecmatchSelfRef)) { setverdict(pass); }
441 else { setverdict(fail, 2); }
442 // no match: decoded record does not match
443 if (not match(r3, templateBitstr_tDecmatchSelfRef)) { setverdict(pass); }
444 else { setverdict(fail, 3); }
445
446 // local self-referencing template
447 var template templateBitstr_rec t := { x1 := '1011'B, x2 := ?, x3 := ? };
448 t.x1 := decmatch t;
449 var templateBitstr_rec r4, r5;
450 r4 := { x1 := good_rec_enc, x2 := '10'B, x3 := '11'B };
451 r5 := { x1 := bad_rec_enc, x2 := '10'B, x3 := '11'B };
452 if (match(r4, t)) { setverdict(pass); }
453 else { setverdict(fail, 4); }
454 if (not match(r5, t)) { setverdict(pass); }
455 else { setverdict(fail, 5); }
456 }
457
458 control {
459 execute(templateBitstrSpec());
460 execute(templateBitstrList());
461 execute(templateBitstrComp());
462 execute(templateBitstrOmit());
463 execute(templateBitstrAny());
464 execute(templateBitstrAnyorNone());
465 execute(templateBitstrLength1());
466 execute(templateBitstrLength2());
467 execute(templateBitstrLength3());
468 execute(templateBitstrIfpresent());
469 execute(templateBitstrLengthIfp());
470 execute(templateBitstrAnyEl());
471 execute(templateBitstrAnyorNoneEl());
472 execute(CR_TR00018474());
473 execute(templateBitstrDecmatch());
474 execute(templateBitstrDecmatchSelfRef());
475 }
476 }
This page took 0.04234 seconds and 5 git commands to generate.