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