Sync with 5.4.2
[deliverable/titan.core.git] / regression_test / customEncoding / Custom1.ttcn
1 /******************************************************************************
2 * Copyright (c) 2000-2015 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
9 // This module tests custom encoding
10 // (encvalue and decvalue encode and decode values using manually written
11 // external functions, as long as they have the same encoding name as the
12 // value's type)
13 module Custom1 {
14
15 import from Custom2 all;
16 import from Custom3 all;
17
18 type component CT {}
19
20 // Test 1.
21 // The encoded type is a record defined in another module
22 // The coding functions are declared in a 3rd module
23 testcase tc_custom1() runs on CT
24 {
25 var Rec x := { num := 3, str := "ttcn" };
26 var bitstring enc_exp := int2bit(x.num, 8);
27 var Rec dec_exp := { num := 3, str := "c++" };
28
29 var bitstring enc := encvalue(x);
30 if (enc != enc_exp) {
31 setverdict(fail, "Expected: ", enc_exp, ", got: ", enc);
32 }
33 var Rec dec;
34 var integer res := decvalue(enc_exp, dec);
35 if (res != 0) {
36 setverdict(fail, "Failed to decode ", enc_exp);
37 }
38 if (dec != dec_exp) {
39 setverdict(fail, "Expected: ", dec_exp, ", got: ", dec);
40 }
41 setverdict(pass);
42 }
43
44 // Separator for the following 2 tests
45 const bitstring c_separator := '1111'B;
46
47 // Test 2.
48 // The encoded type is a record-of. It is defined in this module.
49 // The coding functions are declared in this module, but after
50 // they are used (through encvalue and decvalue)
51 testcase tc_custom2() runs on CT
52 {
53 var RecOf x := { '1010'B, '0010'B, '01'B };
54 var bitstring enc_exp := x[0] & c_separator & x[1] & c_separator & x[2];
55 var RecOf dec_exp := x;
56
57 var bitstring enc := encvalue(x);
58 if (enc != enc_exp) {
59 setverdict(fail, "Expected: ", enc_exp, ", got: ", enc);
60 }
61 var RecOf dec;
62 var integer res := decvalue(enc_exp, dec);
63 if (res != 0) {
64 setverdict(fail, "Failed to decode ", enc_exp);
65 }
66 if (dec != dec_exp) {
67 setverdict(fail, "Expected: ", dec_exp, ", got: ", dec);
68 }
69 setverdict(pass);
70 }
71
72 external function f_enc_recof(in RecOf x) return bitstring
73 with { extension "prototype(convert) encode(globalCustom)" }
74
75 external function f_dec_recof(inout bitstring b, out RecOf x) return integer
76 with { extension "prototype(sliding) decode(globalCustom)" }
77
78 type record of bitstring RecOf; // encoding type defined globally (at module level)
79
80 type union Uni {
81 integer i,
82 octetstring os,
83 universal charstring ucs
84 } // encoding type defined globally (at module level)
85
86 // Test 3.
87 // The encoded type is a union defined in this module.
88 // The coding functions are declared in another module (circular import).
89 testcase tc_custom3() runs on CT
90 {
91 var Uni x := { i := 16 };
92 var bitstring enc_exp := c_separator & int2bit(x.i, 8) & c_separator;
93 var Uni dec_exp := x;
94
95 var bitstring enc := encvalue(x);
96 if (enc != enc_exp) {
97 setverdict(fail, "Expected: ", enc_exp, ", got: ", enc);
98 }
99 var Uni dec;
100 var integer res := decvalue(enc_exp, dec);
101 if (res != 0) {
102 setverdict(fail, "Failed to decode ", enc_exp);
103 }
104 if (dec != dec_exp) {
105 setverdict(fail, "Expected: ", dec_exp, ", got: ", dec);
106 }
107 setverdict(pass);
108 }
109
110 // Test 4.
111 // Using encvalue on templates and template variables
112 // Same type and encoding function as test 1
113 testcase tc_custom_temp() runs on CT
114 {
115 template Rec t := { num := 3, str := "ttcn" };
116 var template Rec vt := { num := 3, str := "ttcn" };
117 var bitstring enc_exp := int2bit(valueof(vt.num), 8);
118 var Rec dec_exp := { num := 3, str := "c++" };
119
120 var bitstring enc := encvalue(t);
121 if (enc != enc_exp) {
122 setverdict(fail, "Expected: ", enc_exp, ", got: ", enc);
123 }
124 enc := encvalue(vt);
125 if (enc != enc_exp) {
126 setverdict(fail, "Expected: ", enc_exp, ", got: ", enc);
127 }
128 setverdict(pass);
129 }
130
131 control {
132 execute(tc_custom1());
133 execute(tc_custom2());
134 execute(tc_custom3());
135 execute(tc_custom_temp());
136 }
137
138 }
139 with {
140 encode "globalCustom";
141 }
This page took 0.03302 seconds and 5 git commands to generate.