File: JsonBasicAttributeTest.ttcn

package info (click to toggle)
eclipse-titan 8.2.0-1
  • links: PTS
  • area: main
  • in suites: bookworm, sid
  • size: 103,544 kB
  • sloc: cpp: 271,008; ansic: 33,683; yacc: 23,419; makefile: 15,483; lex: 9,204; java: 4,848; perl: 4,555; sh: 2,242; xml: 1,378; javascript: 85; awk: 48; php: 32; python: 13
file content (371 lines) | stat: -rw-r--r-- 10,002 bytes parent folder | download
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/******************************************************************************
* 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
*
******************************************************************************/
//cpp: 16 pass
//java:10 pass
module JsonBasicAttributeTest {

type component CT {}

function f_compare_bitstring(in bitstring pl_val, in bitstring pl_expected) {
  if ( pl_val == pl_expected ){
    setverdict(pass);
  } else {
    setverdict( fail, "expected: ", pl_expected, " got: ", pl_val)
  }
} with { extension "transparent"}

//=======================================
//============= Types ===================
//=======================================

type enumerated E { e1,e2,e3 }

type enumerated Etext { One, Two, Three}
with {
  variant "text 'One' as '1'"
  variant "text 'Two' as '2'"
  variant "text 'Three' as '3'"
}


//======= Record with one field =========
//moved down
//======= Record with more fields =========

//type record R10_json {
//  integer i optional,
//  float    f,
//  E       e
//} with { encode "JSON" }
//
//type record R11_json {
//  integer i optional
//} with { encode "JSON"; variant(i) "JSON: omit as null"; }
//
//type record R12_json {
//  integer i optional
//} with { encode "JSON"; variant(i) "JSON: name as Integer"; }
//
//type record R13_json {
//  integer i optional
//} with { encode "JSON"; variant(i) "JSON: name as Integer"; variant(i) "JSON: omit as null";}
//
//type record R14_json {
//  integer i optional
//} with { encode "JSON"; variant "JSON: as value";}

//=================================
//======= Testcases ===============
//=================================

//=== Enum ====

testcase tc_attr_enum_no_attr() runs on CT {
  var E x := e2, z;
  var bitstring expected := oct2bit(char2oct("\"e2\""));
  var bitstring encoded := encvalue(x);
  log(x);
  log(bit2oct(encoded));
  f_compare_bitstring(expected,encoded);

  var integer r := decvalue(expected, z);
  if (r==0 and x==z){
    setverdict(pass);
  } else {
    setverdict(fail);
  }
}

//Status: fail in java
testcase tc_attr_enum_text_attr() runs on CT {
  var Etext x := One, z;
  var bitstring expected := oct2bit(char2oct("\"1\""));
  var bitstring encoded := encvalue(x);
  log(x);
  log(bit2oct(encoded));
  f_compare_bitstring(expected,encoded);

  var integer r := decvalue(expected, z);
  if (r==0 and x==z){
    setverdict(pass);
  } else {
    setverdict(fail);
  }
}

//========= no other attr than JSON =======
type record R0_json {
  integer i optional
} with { encode "JSON" }

testcase tc_rec_attr_no_attr_omit() runs on CT {
  var R0_json x := { i:=omit }
  var bitstring expected := oct2bit(char2oct("{}"));
  var bitstring encoded := encvalue(x);
  log(x);
  log(bit2oct(encoded));
  f_compare_bitstring(expected,encoded);//TODO fail
}

testcase tc_rec_attr_no_attr_int() runs on CT {
  var R0_json x := { i:= 1 }
  var bitstring expected := oct2bit(char2oct("{\"i\":1}"));
  var bitstring encoded := encvalue(x);
  log(x);
  log(bit2oct(encoded));
  f_compare_bitstring(expected,encoded);
}


//======== omit as null ======

type record R1_json {
  integer i optional
} with { encode "JSON"; variant(i) "JSON: omit as null"; }

//Attribute: omit as null
testcase tc_rec_attr_omit_as_null_omitvalue() runs on CT {
  var R1_json x := { i:=omit }
  var bitstring expected := oct2bit(char2oct("{\"i\":null}"));
  var bitstring encoded := encvalue(x);
  log(x);
  log(bit2oct(encoded));
  f_compare_bitstring(expected,encoded);
}

testcase tc_rec_attr_omit_as_null_intvalue() runs on CT {
  var R1_json x := { i:= 1 }
  var bitstring expected := oct2bit(char2oct("{\"i\":1}"));
  var bitstring encoded := encvalue(x);
  log(x);
  log(bit2oct(encoded));
  f_compare_bitstring(expected,encoded);
}

//========= Attribute: name as ======
type record R2_json {
  integer i optional
} with { encode "JSON"; variant(i) "JSON: name as Integer"; }

testcase tc_rec_attr_name_as_omitvalue() runs on CT {
  var R2_json x := { i:=omit }
  var bitstring expected := oct2bit(char2oct("{}"));
  var bitstring encoded := encvalue(x);
  log(x);
  log(bit2oct(encoded));
  f_compare_bitstring(expected,encoded);
}

testcase tc_rec_attr_name_as_intvalue() runs on CT {
  var R2_json x := { i:= 1 }
  var bitstring expected := oct2bit(char2oct("{\"Integer\":1}"));
  var bitstring encoded := encvalue(x);
  log(x);
  log(bit2oct(encoded));
  f_compare_bitstring(expected,encoded);
}

testcase tc_rec_attr_name_as_uninit_value() runs on CT {
  var R2_json x;
  var bitstring expected := oct2bit(char2oct("{\"Integer\":1}"));
  log(x);
  @try {
    var bitstring encoded := encvalue(x);
    setverdict(fail, "DTE expected")
  } @catch(e){
    setverdict(pass)
  }
}

//Attribute: name as & omit as null
type record R3_json {
  integer i optional
} with { encode "JSON"; variant(i) "JSON: name as Integer"; variant(i) "JSON: omit as null";}

testcase tc_rec_attr_name_as_and_omit_as_null_omitvalue() runs on CT {
  var R3_json x := { i:=omit }
  var bitstring expected := oct2bit(char2oct("{\"Integer\":null}")); 
  var bitstring encoded := encvalue(x);
  log(x);
  log(bit2oct(encoded));
  f_compare_bitstring(expected,encoded);
}

testcase tc_rec_attr_name_as_and_omit_as_null_intvalue() runs on CT {
  var R3_json x := { i:= 1 }
  var bitstring expected := oct2bit(char2oct("{\"Integer\":1}"));
  var bitstring encoded := encvalue(x);
  log(x);
  log(bit2oct(encoded));
  f_compare_bitstring(expected,encoded);
}

//============= as value =====
type record R4_json {
  integer i
} with { encode "JSON"; variant "JSON: as value";}

testcase tc_rec_attr_as_value_intvalue() runs on CT {
  var R4_json x := { i:= 1 }
  var bitstring expected := oct2bit(char2oct("1"));
  var bitstring encoded := encvalue(x);
  log(x);
  log(bit2oct(encoded));
  f_compare_bitstring(expected,encoded);
}

// TODO: much more tests for "as value" !

//============= default =====================
type record R5_json {
  integer i,
  charstring cs
} with { encode "JSON";
  variant(i) "JSON: default (127)"
  variant(cs) "JSON: default (Abba)"
}

testcase tc_rec_attr_default_values() runs on CT {
  var R5_json x := { i:=1, cs := "Covid-19"},z;
  var bitstring expected := oct2bit(char2oct("{\"i\":1,\"cs\":\"Covid-19\"}"));
  var bitstring encoded := encvalue(x);
  log(x);
  log(bit2oct(encoded));
  f_compare_bitstring(expected,encoded);

  //decode
  var integer r := decvalue(expected, z);
  if (r==0 and x==z){
    setverdict(pass);
  } else {
    setverdict(fail);
  }
}

testcase tc_rec_attr_default_novalues() runs on CT {
  var R5_json x := { i:=127, cs := "Abba"}, z; //default values
  var bitstring expected := oct2bit(char2oct("{\"i\":127,\"cs\":\"Abba\"}"));
  var bitstring encoded := encvalue(x);
  log(x);
  log(bit2oct(encoded));
  f_compare_bitstring(expected,encoded);

  //decode: missing i field
  var bitstring d := oct2bit(char2oct("{\"cs\":\"Abba\"}"));
  var integer r := decvalue(d, z);
  if (r==0 and x==z){
    setverdict(pass);
  } else {
    setverdict(fail);
  }
  //cs is missing
  d := oct2bit(char2oct("{\"i\":127}"));
  r := decvalue(d, z);
  if (r==0 and x==z){
    setverdict(pass);
  } else {
    setverdict(fail);
  }

  //both missing
  d := oct2bit(char2oct("{}"));
  r := decvalue(expected, z);
  if (r==0 and x==z){
    setverdict(pass);
  } else {
    setverdict(fail);
  }
}

//========= Attribute: extend =======

// The specification of this attribute is unclear and there is no example
// TODO: clarify it!

//========= Attribute: metainfo for unbound=======
type record R6_json {
  integer i,
  charstring cs
} with { encode "JSON";
  variant(i) "JSON: metainfo for unbound";
}

testcase tc_rec_attr_metainfo_int() runs on CT {
  var R6_json x := { cs := "Covid-19"}, z;
  var bitstring expected := oct2bit(char2oct("{\"i\":null,\"metainfo i\":\"unbound\",\"cs\":\"Covid-19\"}"));
  var bitstring encoded := encvalue(x);
  log(x);
  log(bit2oct(encoded));
  f_compare_bitstring(expected,encoded);

  //decode
  var integer r := decvalue(expected, z);
  log(z);
  if (r==0 and log2str(x)==log2str(z)){
    setverdict(pass);
  } else {
    setverdict(fail);
  }
}
//====
type record R7_json {
  integer i,
  charstring cs,
  octetstring os
} with { encode "JSON";
  variant(i) "JSON: metainfo for unbound";
  variant(i) "JSON: name as Int";
  variant(os) "JSON: metainfo for unbound";
}

testcase tc_rec_attr_metainfo_nameas() runs on CT {
  var R7_json x := { cs := "Covid-19"}, z;
  var bitstring expected := oct2bit(char2oct("{\"Int\":null,\"metainfo Int\":\"unbound\",\"cs\":\"Covid-19\",\"os\":null,\"metainfo os\":\"unbound\"}"));
  var bitstring encoded := encvalue(x);
  log(x);
  log(bit2oct(encoded));
  f_compare_bitstring(expected,encoded);

  //decode
  var integer r := decvalue(expected, z);
  if (r==0 and log2str(x)==log2str(z)){
    setverdict(pass);
  } else {
    setverdict(fail);
  }
}

//========= As number =========

//========= control part ============
control {
  execute(tc_attr_enum_no_attr());
  execute(tc_attr_enum_text_attr()); //fails in java
  execute(tc_rec_attr_no_attr_omit());
  execute(tc_rec_attr_no_attr_int());
  execute(tc_rec_attr_omit_as_null_omitvalue());
  execute(tc_rec_attr_omit_as_null_intvalue());
  execute(tc_rec_attr_name_as_omitvalue());
  execute(tc_rec_attr_name_as_intvalue());
  execute(tc_rec_attr_name_as_uninit_value());
  execute(tc_rec_attr_name_as_and_omit_as_null_omitvalue());
  execute(tc_rec_attr_name_as_and_omit_as_null_intvalue());
  execute(tc_rec_attr_as_value_intvalue());
  execute( tc_rec_attr_default_values() );
  execute( tc_rec_attr_default_novalues() );
  execute(tc_rec_attr_metainfo_int());
  execute(tc_rec_attr_metainfo_nameas());
}

} with {
encode "JSON";
}