1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
|
/******************************************************************************
* Copyright (c) 2000-2021 Ericsson Telecom AB
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
*
* Contributors:
* Balasko, Jeno
* Raduly, Csaba
* Szabados, Kristof
*
******************************************************************************/
module AnytypeTest . objid { 6 6 6 } {
//import from Supplier all;
type record of charstring rof_string;
type anytype other_anytype;
type record of anytype anysequence;
type integer AT_integer;
template integer pi_t := 3+14+15+926;
type union anytype_dup {
integer integer_dup,
objid objid_dup,
rof_string foobar_dup,
fooref fooref_dup,
pifunc pifunc_dup,
integer another_integer
}
type record address {
charstring a optional,
union { boolean w } u
} ;
type component and_on {}
template anytype anytemplate := { integer := 42 }
template anytype_dup anytemplate_dup := { integer_dup := 42 }
function foonction(in charstring input) return charstring
{
var integer e := lengthof(input);
return input;
}
with { extension override "" }
type function fooref(in charstring input) return charstring;
function pi() return float
{
return 22.0/7.0;
}
type function pifunc() return float;
/// No XER encoding for anytype
///external function enc_anytype(in anytype at) return charstring
///with { extension override "prototype(convert) encode(XER)" }
testcase t1() runs on and_on
{
var anytype anyvar;
var anytype_dup anyvar_dup;
// this is not accepted by the compiler because it wants three components: if (ischosen(anyvar.integer)) {}
anyvar.rof_string := {};
anyvar .integer := 42;
if (ischosen(anyvar.integer)) { setverdict(pass)}
else { setverdict(fail); }
anyvar_dup.integer_dup := 42;
if (ischosen(anyvar_dup.foobar_dup)) { setverdict(fail)}
else { setverdict(pass); }
//DON'T DO THIS: triggers TR940
//anyvar_dup.another_integer := anyvar_dup.integer_dup;
if (anyvar.integer == anyvar_dup.integer_dup) { setverdict(pass); }
else { setverdict(fail); }
if (match (anyvar_dup, anytemplate_dup)) { setverdict(pass); }
else { setverdict(fail); }
if (match (anyvar, anytemplate)) { setverdict(pass); }
else { setverdict(fail); }
// Triggers TR940: anyvar.AT_integer := anyvar.integer ; // fields of same type
//if (ischosen(anyvar.integer)) { setverdict(fail)}
//else { setverdict(pass); }
//if (ischosen(anyvar.AT_integer)) { setverdict(pass)}
//else { setverdict(fail); }
anyvar.objid := objid { 2 2 2 }
anyvar_dup.objid_dup := objid { 2 2 2 }
if (anyvar.objid == anyvar_dup.objid_dup) { setverdict(pass); }
else { setverdict(fail); }
// now it shouldn't match
if (not match (anyvar_dup, anytemplate_dup)) { setverdict(pass); }
else { setverdict(fail); }
if (not match (anyvar, anytemplate)) { setverdict(pass); }
else { setverdict(fail); }
var anytype anys[2] := { {integer := 37}, {objid := objid{0 3 14 15 9}} }
//var anysequence aseq;
var integer thirty_seven := anys[0].integer;
if (thirty_seven == 37) { setverdict(pass); }
else { setverdict(fail); }
anyvar.bitstring := '010010001'B;
anyvar.boolean := true;
anyvar.charstring := "How are you gentlemen?";
anyvar.universal charstring := "All your base are belong to us";
// integer already fiddled with
anyvar.octetstring := 'DEADBEEF'O;
// anyvar.hexstring := 'DECAFBAD'H;
// anyvar.verdicttype := inconc;
anyvar.float := 2.718281828;
// address
// anyvar.default := null;
// objid already fiddled with
anyvar.address.a := "For great justice";
anyvar.address.u.w := true;
//semantic error: "@AnytypeTest.address cannot be indexed" : var boolean bbb := anyvar.address[0];
//that's because address in this module is a record, not a record-of
//anyvar.fooref(charstring input) := foonction;
var pifunc pf := refers(pi);
var fooref fo := refers( foonction );
anyvar.fooref := refers( foonction );
anyvar.pifunc := refers(pi);
anyvar_dup.pifunc_dup := refers(pi);
anyvar.objid := objid{0 3 14 15 9}
//var float pie := anyvar.pifunc();
/* This gives the same "Unbound left operand of integer comparison" error
as anyvar_dup. Non-const strikes again!
I think it should be "wrong alternative" !
if (anyvar.integer == 42) { setverdict(pass); }
else { setverdict(fail); }
// same for "right operand"
if (42 == anyvar.integer) { setverdict(pass); }
else { setverdict(fail); }
/**/
// misidentified as module.something:
//anyvar.pifunc( 3+2 );
// anyvar.octetstring := Supplier.epdv.data_value;
}
// Examples from "An Introduction to TTCN-3"
const anytype c := { integer := 42 }
testcase t2(in anytype pa) runs on and_on
{
var anytype anyvar := { integer := 42 }
var integer x1 := anyvar.integer;
anyvar.charstring := "fourty-two";
anyvar.float := 42.0;
//x1 := anyvar.integer; // would result in a dynamic testcase error.
// However! The error message ("Assignment of an unbound integer value")
// betrays incorrect operation: anyvar has become converted to integer
log(x1);
//x1 := pa.integer; // would result in a dynamic testcase error (non-selected field)
var charstring pacs := pa.charstring;
// compile-time error (inactive field): anyvar.float := c.float;
if (ischosen(anyvar.integer)) { setverdict(fail,"Not integer but float should be chosen"); }
else { setverdict(pass); }
var integer x2 := float2int( anyvar.float );
var anytype x_any := { charstring := "All your base are belong to us" }
//var charstring str := x_any; // a value of type charstring was expected instead of anytype
//var anytype x_str := x_any.charstring; // a value of type anytype was expected instead of charstring
var anytype x_any1 := x_any;
}
/* This is how it would look if type parameterization was supported: */
//const integer p := 7;
type record myrecord /* (integer p) */ {
integer f1 /*( 0.. p )*/
}
type union level1 {
integer i0,
float f0,
charstring s0
}
type record level2 {
level1 l1
}
type record level3 {
level2 l2
}
type union level4 {
level3 l3
}
testcase t3() runs on and_on
{
var anytype anyvar;
//misidentified as ModuleName.something:
//anyvar.myrecord(3).f1 := 3;
}
template anytype t_at := { integer := 3 };
type record of anytype rat;
testcase t4() runs on and_on
{
var rat patkany;
patkany[0].integer := 3;
// patkany[1].myrecord("WTF is this ?").f1 := 7; // Bingo! 7719
patkany[2].myrecord.f1 := 4;
var anytype ratt := patkany[0];
var level4 top;
var level1 vl_l;
var level2 vl2;
vl_l.i0 := 0;
var boolean isit;
isit := ischosen(vl_l.s0);
isit := ischosen(vl2.l1.s0);
isit := ischosen(patkany[0] /* anytype */ .address /* a record */ .u /* a union */ .w /* a boolean */ );
isit := ischosen(ratt /* anytype */ .address /* a record */ .u /* a union */ .w /* a boolean */ ); // Bingo ! 7990 anytype ispresent arg
if ( ischosen(patkany[0] /* anytype */ . level4 . l3 . l2 . l1 . f0) ) {
setverdict(fail)
}
}
control{
execute(t1());
var anytype la := { charstring := "Fourty-foo" }
execute(t2(la));
}
}
with {
extension "anytype bitstring,boolean,charstring,universal charstring,integer,octetstring,/*hexstring,verdicttype,*/float,/*default,*/objid,address, rof_string,fooref,pifunc,myrecord,level4, AT_integer"
// \-- predefined types, in the order of PredefinedType in compiler.y:6179 (address is missing here ^^) ---------/
}
|