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 ******************************************************************************/
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
15 import from Custom2 all;
16 import from Custom3 all;
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
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++" };
29 var bitstring enc := encvalue(x);
31 setverdict(fail, "Expected: ", enc_exp, ", got: ", enc);
34 var integer res := decvalue(enc_exp, dec);
36 setverdict(fail, "Failed to decode ", enc_exp);
39 setverdict(fail, "Expected: ", dec_exp, ", got: ", dec);
44 // Separator for the following 2 tests
45 const bitstring c_separator := '1111'B;
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
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;
57 var bitstring enc := encvalue(x);
59 setverdict(fail, "Expected: ", enc_exp, ", got: ", enc);
62 var integer res := decvalue(enc_exp, dec);
64 setverdict(fail, "Failed to decode ", enc_exp);
67 setverdict(fail, "Expected: ", dec_exp, ", got: ", dec);
72 external function f_enc_recof(in RecOf x) return bitstring
73 with { extension "prototype(convert) encode(globalCustom)" }
75 external function f_dec_recof(inout bitstring b, out RecOf x) return integer
76 with { extension "prototype(sliding) decode(globalCustom)" }
78 type record of bitstring RecOf; // encoding type defined globally (at module level)
83 universal charstring ucs
84 } // encoding type defined globally (at module level)
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
91 var Uni x := { i := 16 };
92 var bitstring enc_exp := c_separator & int2bit(x.i, 8) & c_separator;
95 var bitstring enc := encvalue(x);
97 setverdict(fail, "Expected: ", enc_exp, ", got: ", enc);
100 var integer res := decvalue(enc_exp, dec);
102 setverdict(fail, "Failed to decode ", enc_exp);
104 if (dec != dec_exp) {
105 setverdict(fail, "Expected: ", dec_exp, ", got: ", dec);
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
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++" };
120 var bitstring enc := encvalue(t);
121 if (enc != enc_exp) {
122 setverdict(fail, "Expected: ", enc_exp, ", got: ", enc);
125 if (enc != enc_exp) {
126 setverdict(fail, "Expected: ", enc_exp, ", got: ", enc);
132 execute(tc_custom1());
133 execute(tc_custom2());
134 execute(tc_custom3());
135 execute(tc_custom_temp());
140 encode "globalCustom";