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
12 ******************************************************************************/
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
20 import from Custom2 all;
21 import from Custom3 all;
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
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++" };
34 var bitstring enc := encvalue(x);
36 setverdict(fail, "Expected: ", enc_exp, ", got: ", enc);
39 var integer res := decvalue(enc_exp, dec);
41 setverdict(fail, "Failed to decode ", enc_exp);
44 setverdict(fail, "Expected: ", dec_exp, ", got: ", dec);
49 // Separator for the following 2 tests
50 const bitstring c_separator := '1111'B;
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
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;
62 var bitstring enc := encvalue(x);
64 setverdict(fail, "Expected: ", enc_exp, ", got: ", enc);
67 var integer res := decvalue(enc_exp, dec);
69 setverdict(fail, "Failed to decode ", enc_exp);
72 setverdict(fail, "Expected: ", dec_exp, ", got: ", dec);
77 external function f_enc_recof(in RecOf x) return bitstring
78 with { extension "prototype(convert) encode(globalCustom)" }
80 external function f_dec_recof(inout bitstring b, out RecOf x) return integer
81 with { extension "prototype(sliding) decode(globalCustom)" }
83 type record of bitstring RecOf; // encoding type defined globally (at module level)
88 universal charstring ucs
89 } // encoding type defined globally (at module level)
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
96 var Uni x := { i := 16 };
97 var bitstring enc_exp := c_separator & int2bit(x.i, 8) & c_separator;
100 var bitstring enc := encvalue(x);
101 if (enc != enc_exp) {
102 setverdict(fail, "Expected: ", enc_exp, ", got: ", enc);
105 var integer res := decvalue(enc_exp, dec);
107 setverdict(fail, "Failed to decode ", enc_exp);
109 if (dec != dec_exp) {
110 setverdict(fail, "Expected: ", dec_exp, ", got: ", dec);
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
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++" };
125 var bitstring enc := encvalue(t);
126 if (enc != enc_exp) {
127 setverdict(fail, "Expected: ", enc_exp, ", got: ", enc);
130 if (enc != enc_exp) {
131 setverdict(fail, "Expected: ", enc_exp, ", got: ", enc);
137 execute(tc_custom1());
138 execute(tc_custom2());
139 execute(tc_custom3());
140 execute(tc_custom_temp());
145 encode "globalCustom";