Titan Core Initial Contribution
[deliverable/titan.core.git] / regression_test / XML / EXER-whitepaper / UseUnion.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 UseUnion {
9 modulepar boolean UseUnion_verbose := false;
10 #define verbose UseUnion_verbose
11 #include "../macros.ttcnin"
12
13 type component UU {}
14
15 type union ProductId {
16 integer c1,
17 integer c2,
18 integer c3
19 }
20 with {
21 variant "useUnion"
22 }
23
24 type record ProductUU {
25 ProductId id,
26 float price,
27 charstring color
28 }
29 with {
30 variant "element";
31 variant "namespace as 'http://www.example.com' prefix 'exm'";
32 }
33
34 const ProductUU uuval := {
35 id := { c2 := 100 },
36 price := 25.34,
37 color := "green"
38 }
39
40 DECLARE_XER_ENCODERS(ProductUU, uu);
41 DECLARE_EXER_ENCODERS(ProductUU, uu);
42
43 const universal charstring str_uu_e :=
44 "<exm:ProductUU xmlns:exm=\'http://www.example.com\' xmlns:xsi=\'http://www.w3.org/2001/XMLSchema-instance\'>\n" &
45 "\t<id xsi:type=\'c2\'>100</id>\n" &
46 "\t<price>25.340000</price>\n" &
47 "\t<color>green</color>\n" &
48 "</exm:ProductUU>\n" &
49 "\n";
50
51 const universal charstring str_uu_b :=
52 "<ProductUU>\n" &
53 "\t<id>\n" &
54 "\t\t<c2>100</c2>\n" &
55 "\t</id>\n" &
56 "\t<price>25.340000</price>\n" &
57 "\t<color>green</color>\n" &
58 "</ProductUU>\n" &
59 "\n";
60
61 testcase encode_uu() runs on UU
62 {
63 CHECK_METHOD(bxer_enc_uu, uuval, str_uu_b);
64 CHECK_METHOD(exer_enc_uu, uuval, str_uu_e);
65 }
66
67 testcase decode_uu() runs on UU
68 {
69 CHECK_DECODE(bxer_dec_uu, str_uu_b, ProductUU, uuval);
70 CHECK_DECODE(exer_dec_uu, str_uu_e, ProductUU, uuval);
71 }
72
73 /* * * * * * * * HM81496 * * * * * * * * */
74
75 /* From the following XSD:
76
77 <xsd:simpleType name="E21named">
78 <xsd:union memberTypes="xsd:integer xsd:boolean"/>
79 </xsd:simpleType>
80 */
81 type union E21named
82 {
83 integer integer_,
84 boolean boolean_
85 }
86 with {
87 variant "useUnion";
88 variant (integer_) "name as 'integer'";
89 variant (boolean_) "name as 'boolean'";
90 variant (boolean_) "text 'true' as '1'";
91 variant (boolean_) "text 'false' as '0'";
92 };
93
94 DECLARE_XER_ENCODERS(E21named, e21n);
95 DECLARE_EXER_ENCODERS(E21named, e21n);
96
97 const E21named e21_i := { integer_ := 1 }
98 const E21named e21_b := { boolean_ := true }
99
100
101 const universal charstring estr_e21_i :=
102 "<E21named xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:type='integer'>1</E21named>\n\n";
103
104 const universal charstring estr_e21_b :=
105 "<E21named xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:type='boolean'>1</E21named>\n\n";
106
107
108 testcase encode_e21n() runs on UU
109 {
110 CHECK_METHOD(exer_enc_e21n, e21_i, estr_e21_i);
111
112 CHECK_METHOD(exer_enc_e21n, e21_b, estr_e21_b);
113 }
114
115 testcase decode_e21n() runs on UU
116 {
117 CHECK_DECODE(exer_dec_e21n, estr_e21_i, E21named, e21_i);
118
119 CHECK_DECODE(exer_dec_e21n, estr_e21_b, E21named, e21_b);
120 }
121
122 /* From the following XSD:
123
124 <xsd:simpleType name="e21unnamed">
125 <xsd:union>
126 <xsd:simpleType>
127 <xsd:restriction base="string"/>
128 </xsd:simpleType>
129 <xsd:simpleType>
130 <xsd:restriction base="float"/>
131 </xsd:simpleType>
132 </xsd:union>
133 </xsd:simpleType>
134 */
135
136 type union E21unnamed
137 {
138 charstring alt_,
139 float alt_1
140 }
141 with {
142 variant "useUnion";
143 variant (alt_) "name as ''";
144 variant (alt_1) "name as ''";
145 };
146
147 DECLARE_XER_ENCODERS(E21unnamed, e21u);
148 DECLARE_EXER_ENCODERS(E21unnamed, e21u);
149
150 const E21unnamed e21u_0 := { alt_ := "Howdy!" }
151 const E21unnamed e21u_1 := { alt_1 := 2.5 }
152 const E21unnamed e21u_misidentified := { alt_ := "2.500000" }
153
154 const universal charstring estr_e21u_0 :=
155 "<E21unnamed>Howdy!</E21unnamed>\n\n"; // no type attribute due to empty "name as ''"
156
157 const universal charstring estr_e21u_1 :=
158 "<E21unnamed>2.500000</E21unnamed>\n\n"; // no type attribute due to empty "name as ''"
159
160 testcase encode_e21u() runs on UU
161 {
162 CHECK_METHOD(exer_enc_e21u, e21u_0, estr_e21u_0);
163
164 CHECK_METHOD(exer_enc_e21u, e21u_1, estr_e21u_1);
165 }
166
167 testcase decode_e21u() runs on UU
168 {
169 CHECK_DECODE(exer_dec_e21u, estr_e21u_0, E21unnamed, e21u_0);
170
171 // this would be the correct decode
172 //CHECK_DECODE(exer_dec_e21u, estr_e21u_1, E21unnamed, e21u_1);
173
174 // unfortunately, without a type identification attribute,
175 // the decoder can't tell whether "2.500000" is a float or just some random string
176 CHECK_DECODE(exer_dec_e21u, estr_e21u_1, E21unnamed, e21u_misidentified);
177 }
178
179
180 /* * * * * * * * * USE-UNION and ATTRIBUTE * * * * * * * * */
181
182 // Simplified from vxml
183 type union Pitch {
184 float hertz_number,
185 universal charstring percent,
186 integer semitone
187 // in the original, they are all XSD.String
188 }
189 with {
190 variant "useUnion";
191 }
192
193 type universal charstring thingy;
194
195 type record Prosody {
196 record of universal charstring embed_values,
197 Pitch pitch optional,
198 record of thingy stuff
199 }
200 with {
201 variant "embedValues";
202 variant (pitch) "attribute";
203 }
204
205 DECLARE_XER_ENCODERS(Prosody, pro);
206 DECLARE_EXER_ENCODERS(Prosody, pro);
207
208 const Prosody p0 := {
209 embed_values := { "hello", "world" },
210 pitch := omit,
211 stuff := {}
212 }
213
214 const universal charstring str_p0 :=
215 "<Prosody>hello<stuff/>world</Prosody>\n";
216
217 const Prosody p1 := {
218 embed_values := { "hello", "world" },
219 pitch := omit,
220 stuff := { "some stuff" }
221 }
222
223 const universal charstring str_p1 :=
224 "<Prosody>hello<stuff><thingy>some stuff</thingy></stuff>world</Prosody>\n";
225
226 // Now the interesting stuff
227
228 const Prosody p0a := {
229 embed_values := { "hello", "world" },
230 pitch := { percent := "42 per cent" },
231 stuff := {}
232 }
233
234 const universal charstring str_p0a :=
235 "<Prosody pitch='42 per cent'>hello<stuff/>world</Prosody>\n";
236
237 testcase encode_pro() runs on UU
238 {
239 CHECK_METHOD(exer_enc_pro, p0 , str_p0 );
240 CHECK_METHOD(exer_enc_pro, p1 , str_p1 );
241 CHECK_METHOD(exer_enc_pro, p0a, str_p0a);
242 //CHECK_METHOD(exer_enc_pro, p1, str_p1);
243 }
244
245 testcase decode_pro() runs on UU
246 {
247 var Prosody expected := p0;
248 CHECK_DECODE(exer_dec_pro, str_p0 , Prosody, expected);
249
250 expected := p1;
251 CHECK_DECODE(exer_dec_pro, str_p1 , Prosody, expected);
252
253 expected := p0a;
254 //TROUBLE: CHECK_DECODE(exer_dec_pro, str_p0a, Prosody, expected);
255 }
256
257 /* * * * * * * * * * HO58864 * * * * * * * * * * */
258
259 group gmcl {
260
261 type union GmlcaddType {
262 enumerated { aLL } alt_1,
263 charstring alt_ length(3 .. 15)
264 }
265 with {
266 variant "useUnion";
267 variant (alt_) "name as '' ";
268 variant (alt_1) "name as '' ";
269 variant (alt_1) "text 'aLL' as capitalized";
270 variant (alt_, alt_1) "namespace as 'urn:gmlc' prefix 'gm' ";
271 };
272
273 type record GMLCAddressData {
274 // the original has another field
275 GmlcaddType gmlcadd
276 };
277
278 }
279 with {
280 variant "elementFormQualified";
281 }
282
283 DECLARE_EXER_ENCODERS(GMLCAddressData, gmlc);
284
285 const GMLCAddressData c_gmcl_alt := {
286 gmlcadd := { alt_ := "3141592653" }
287 }
288
289 const universal charstring str_gmcl_alt :=
290 "<GMLCAddressData xmlns:gm='urn:gmlc'>\n" &
291 "\t<gmlcadd>3141592653</gmlcadd>\n" &
292 "</GMLCAddressData>\n\n";
293
294 const GMLCAddressData c_gmcl_alt1 := {
295 gmlcadd := { alt_1 := aLL }
296 }
297
298 const universal charstring str_gmcl_alt1 :=
299 "<GMLCAddressData xmlns:gm='urn:gmlc'>\n" &
300 "\t<gmlcadd>ALL</gmlcadd>\n" &
301 "</GMLCAddressData>\n\n";
302
303
304 testcase encode_gmcl() runs on UU
305 {
306 CHECK_METHOD(exer_enc_gmlc, c_gmcl_alt , str_gmcl_alt );
307 CHECK_METHOD(exer_enc_gmlc, c_gmcl_alt1, str_gmcl_alt1);
308 }
309
310 testcase decode_gmcl() runs on UU
311 {
312 CHECK_DECODE(exer_dec_gmlc, str_gmcl_alt , GMLCAddressData, c_gmcl_alt );
313 CHECK_DECODE(exer_dec_gmlc, str_gmcl_alt1, GMLCAddressData, c_gmcl_alt1);
314 }
315
316 // == == == == == == == == == == == == == ==
317
318 // USE-UNION in a record-of (traditional or with the LIST coding instruction)
319
320 type record of integer RoI with { variant "list" }
321
322 type union UUVal {
323 bitstring bs,
324 integer i,
325 RoI roi,
326 boolean b,
327 octetstring os,
328 hexstring hs,
329 float f,
330 enumerated { Tiny, Small, Medium, Big, Huge } size,
331 verdicttype vt,
332 charstring cs,
333 universal charstring ucs
334 } with {
335 variant "name as 'UU'";
336 variant "useUnion";
337 }
338
339 type record of UUVal UURec;
340
341 type record of UUVal UUList with { variant "list" };
342
343 DECLARE_EXER_ENCODERS(UURec, uu_rec);
344 DECLARE_EXER_ENCODERS(UUList, uu_list);
345
346 const UURec c_uu_rec := {
347 { i := 19 },
348 { cs := "xyz" },
349 { size := Small },
350 { bs := '1101'B },
351 { b := true },
352 { vt := inconc },
353 { f := 1.6 },
354 { hs := '164FA'H },
355 { os := '1BC6'O },
356 { ucs := "ab" & char(0, 0, 1, 113) },
357 { roi := { 1, 4, 5, 7, 6, 6, 4 } }
358 }
359
360 const UUList c_uu_list := {
361 { i := 19 },
362 { cs := "xyz" },
363 { size := Small },
364 { bs := '1101'B },
365 { b := true },
366 { vt := inconc },
367 { f := 1.6 },
368 { hs := '164FA'H },
369 { os := '1BC6'O }
370 //{ ucs := "ab" & char(0, 0, 1, 113) }, --- would be decoded into charstring
371 //{ roi := { 1, 4, 5, 7, 6, 6, 4 } } --- would be decoded into 7 integers instead of 1 RoI
372 }
373
374 const universal charstring str_uu_rec :=
375 "<UURec xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>\n" &
376 "\t<UU xsi:type='i'>19</UU>\n" &
377 "\t<UU xsi:type='cs'>xyz</UU>\n" &
378 "\t<UU xsi:type='size'>Small</UU>\n" &
379 "\t<UU xsi:type='bs'>1101</UU>\n" &
380 "\t<UU xsi:type='b'>true</UU>\n" &
381 "\t<UU xsi:type='vt'>inconc</UU>\n" &
382 "\t<UU xsi:type='f'>1.600000</UU>\n" &
383 "\t<UU xsi:type='hs'>164FA</UU>\n" &
384 "\t<UU xsi:type='os'>1BC6</UU>\n" &
385 "\t<UU xsi:type='ucs'>ab" & char(0, 0, 1, 113) & "</UU>\n" &
386 "\t<UU xsi:type='roi'>1 4 5 7 6 6 4</UU>\n" &
387 "</UURec>\n\n";
388
389 const universal charstring str_uu_list :=
390 "<UUList xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>19 xyz Small 1101 " &
391 "true inconc 1.600000 164FA 1BC6</UUList>\n\n";
392
393 testcase encode_uu_rec() runs on UU
394 {
395 CHECK_METHOD(exer_enc_uu_rec, c_uu_rec, str_uu_rec);
396 }
397
398 testcase decode_uu_rec() runs on UU
399 {
400 CHECK_DECODE(exer_dec_uu_rec, str_uu_rec, UURec, c_uu_rec);
401 }
402
403 testcase encode_uu_list() runs on UU {
404 CHECK_METHOD(exer_enc_uu_list, c_uu_list, str_uu_list);
405 }
406
407 testcase decode_uu_list() runs on UU
408 {
409 CHECK_DECODE(exer_dec_uu_list, str_uu_list, UUList, c_uu_list);
410 }
411
412 // == == == == == == == == == == == == == ==
413
414 // Encoding and decoding USE-UNION without the type attribute (with ATTRIBUTE and UNTAGGED)
415
416 type record UUNoAttr {
417 UUVal uuatr,
418 UUVal uuelem
419 } with {
420 variant(uuatr) "attribute";
421 variant(uuelem) "untagged";
422 }
423
424 DECLARE_EXER_ENCODERS(UUNoAttr, uu_no_attr);
425
426 const UUNoAttr c_uu_na1 := { { hs := '117BA'H }, { size := Huge } };
427
428 const universal charstring str_uu_na1 :=
429 "<UUNoAttr xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' uuatr='117BA'>Huge</UUNoAttr>\n\n";
430
431 const UUNoAttr c_uu_na2 := { { roi := { 1, 2, 3, 4 } }, { roi := { 5, 6, 7, 8 } } };
432
433 const universal charstring str_uu_na2 :=
434 "<UUNoAttr xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' uuatr='1 2 3 4'>5 6 7 8</UUNoAttr>\n\n";
435
436 testcase encode_uu_no_attr() runs on UU
437 {
438 CHECK_METHOD(exer_enc_uu_no_attr, c_uu_na1, str_uu_na1);
439 CHECK_METHOD(exer_enc_uu_no_attr, c_uu_na2, str_uu_na2);
440 }
441
442 testcase decode_uu_no_attr() runs on UU
443 {
444 CHECK_DECODE(exer_dec_uu_no_attr, str_uu_na1, UUNoAttr, c_uu_na1);
445 CHECK_DECODE(exer_dec_uu_no_attr, str_uu_na2, UUNoAttr, c_uu_na2);
446 }
447
448 // == == == == == == == == == == == == == ==
449
450 control {
451 execute(encode_uu());
452 execute(decode_uu());
453
454 execute(encode_e21n());
455 execute(decode_e21n());
456
457 execute(encode_e21u());
458 execute(decode_e21u());
459
460 execute(encode_pro());
461 execute(decode_pro());
462
463 execute(encode_gmcl());
464 execute(decode_gmcl());
465
466 execute(encode_uu_rec());
467 execute(decode_uu_rec());
468 execute(encode_uu_list());
469 execute(decode_uu_list());
470
471 execute(encode_uu_no_attr());
472 execute(decode_uu_no_attr());
473 }
474
475 }
476 with {
477 encode "XML"
478 variant "controlNamespace 'http://www.w3.org/2001/XMLSchema-instance' prefix 'xsi'"
479 }
This page took 0.046748 seconds and 5 git commands to generate.