/****************************************************************************** * Copyright (c) 2000-2016 Ericsson Telecom AB * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Balasko, Jeno * Baranyi, Botond * ******************************************************************************/ // This module tests custom encoding // (encvalue and decvalue encode and decode values using manually written // external functions, as long as they have the same encoding name as the // value's type) module Custom1 { import from Custom2 all; import from Custom3 all; type component CT {} // Test 1. // The encoded type is a record defined in another module // The coding functions are declared in a 3rd module testcase tc_custom1() runs on CT { var Rec x := { num := 3, str := "ttcn" }; var bitstring enc_exp := int2bit(x.num, 8); var Rec dec_exp := { num := 3, str := "c++" }; var bitstring enc := encvalue(x); if (enc != enc_exp) { setverdict(fail, "Expected: ", enc_exp, ", got: ", enc); } var Rec dec; var integer res := decvalue(enc_exp, dec); if (res != 0) { setverdict(fail, "Failed to decode ", enc_exp); } if (dec != dec_exp) { setverdict(fail, "Expected: ", dec_exp, ", got: ", dec); } setverdict(pass); } // Separator for the following 2 tests const bitstring c_separator := '1111'B; // Test 2. // The encoded type is a record-of. It is defined in this module. // The coding functions are declared in this module, but after // they are used (through encvalue and decvalue) testcase tc_custom2() runs on CT { var RecOf x := { '1010'B, '0010'B, '01'B }; var bitstring enc_exp := x[0] & c_separator & x[1] & c_separator & x[2]; var RecOf dec_exp := x; var bitstring enc := encvalue(x); if (enc != enc_exp) { setverdict(fail, "Expected: ", enc_exp, ", got: ", enc); } var RecOf dec; var integer res := decvalue(enc_exp, dec); if (res != 0) { setverdict(fail, "Failed to decode ", enc_exp); } if (dec != dec_exp) { setverdict(fail, "Expected: ", dec_exp, ", got: ", dec); } setverdict(pass); } external function f_enc_recof(in RecOf x) return bitstring with { extension "prototype(convert) encode(globalCustom)" } external function f_dec_recof(inout bitstring b, out RecOf x) return integer with { extension "prototype(sliding) decode(globalCustom)" } type record of bitstring RecOf; // encoding type defined globally (at module level) type union Uni { integer i, octetstring os, universal charstring ucs } // encoding type defined globally (at module level) // Test 3. // The encoded type is a union defined in this module. // The coding functions are declared in another module (circular import). testcase tc_custom3() runs on CT { var Uni x := { i := 16 }; var bitstring enc_exp := c_separator & int2bit(x.i, 8) & c_separator; var Uni dec_exp := x; var bitstring enc := encvalue(x); if (enc != enc_exp) { setverdict(fail, "Expected: ", enc_exp, ", got: ", enc); } var Uni dec; var integer res := decvalue(enc_exp, dec); if (res != 0) { setverdict(fail, "Failed to decode ", enc_exp); } if (dec != dec_exp) { setverdict(fail, "Expected: ", dec_exp, ", got: ", dec); } setverdict(pass); } // Test 4. // Using encvalue on templates and template variables // Same type and encoding function as test 1 testcase tc_custom_temp() runs on CT { template Rec t := { num := 3, str := "ttcn" }; var template Rec vt := { num := 3, str := "ttcn" }; var bitstring enc_exp := int2bit(valueof(vt.num), 8); var Rec dec_exp := { num := 3, str := "c++" }; var bitstring enc := encvalue(t); if (enc != enc_exp) { setverdict(fail, "Expected: ", enc_exp, ", got: ", enc); } enc := encvalue(vt); if (enc != enc_exp) { setverdict(fail, "Expected: ", enc_exp, ", got: ", enc); } setverdict(pass); } control { execute(tc_custom1()); execute(tc_custom2()); execute(tc_custom3()); execute(tc_custom_temp()); } } with { encode "globalCustom"; }