File: JsonBasicTest_ucharstring.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 (368 lines) | stat: -rwxr-xr-x 15,697 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
/******************************************************************************
* 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:
*   Botond Baranyi – initial implementation
*   Jeno Balasko - rework for JSON
*
******************************************************************************/

module JsonBasicTest_ucharstring {

type component CT {}

type record Rec {
  integer num,
  universal charstring ustr
}

external function f_enc_rec(in Rec x) return octetstring
with { extension "prototype(convert) encode(JSON)" }

external function f_dec_rec(in octetstring x) return Rec
with { extension "prototype(convert) decode(JSON)" }

external function f_enc_ustr(in universal charstring x) return octetstring
with { extension "prototype(convert) encode(JSON)" }

external function f_dec_ustr(in octetstring x) return universal charstring
with { extension "prototype(convert) decode(JSON)" }

const universal charstring c_ustr := "árvíztűrő tükörfúrógép"; // 31 UTF-8 characters
const octetstring c_enc_ustr := '22C3A17276C3AD7A74C5B172C5912074C3BC6BC3B67266C3BA72C3B367C3A97022'O
//================================  
//local constant, var:
testcase tc_JSON_ustr_local1() runs on CT {
  //local const:
  const universal charstring cl_ustr := char(0,0,0,1);
  const octetstring cl_enc_ustr := '225C753030303122'O; //("\"\\u0001\"")
  var octetstring v_enc := f_enc_ustr(cl_ustr);
  log("Expected: ", cl_enc_ustr, " unichar: ", oct2unichar(v_enc))
  if (v_enc != cl_enc_ustr) {
    setverdict(fail, "Encoding failed. Expected: ", cl_enc_ustr, ", got: ", v_enc);
  }
  //var:
  var universal charstring v_ustr := char(0,0,0,1);
  var octetstring v_enc1 := f_enc_ustr(v_ustr);
  log("Expected: ", cl_enc_ustr, " unichar: ", oct2unichar(v_enc1))
  if (v_enc1 != cl_enc_ustr) {
    setverdict(fail, "Encoding failed. Expected: ", cl_enc_ustr, ", got: ", v_enc);
  }

  //decode
  var universal charstring v_dec := f_dec_ustr(cl_enc_ustr);
  if (v_dec != cl_ustr) {
    setverdict(fail, "Decoding failed. Expected: ", cl_ustr, ", got: ", v_dec);
  }
  setverdict(pass);
}

//TODO: Perhaps this is not the correct behavior, too big input!
testcase tc_JSON_ustr_too_big_char() runs on CT {
  //local const:
  const universal charstring cl_ustr := char(1,0,0,0); // max:1FFFFF - illegal input for Json encoder !!!
  const octetstring cl_enc_ustr := '22F98080808022'O //got:'22F98080808022'O , code point: 01 00 00 00 
  var octetstring v_enc := f_enc_ustr(cl_ustr); // max:1FFFFF - illegal input for Json encoder !!!
  log("Expected: ", cl_enc_ustr, " unichar: ", oct2unichar(v_enc))
  if (v_enc != cl_enc_ustr) {
    setverdict(fail, "Encoding failed. Expected: ", cl_enc_ustr, ", got: ", v_enc);
  }
  //var:
  var universal charstring v_ustr := char(1,0,0,0);
  var octetstring v_enc1 := f_enc_ustr(v_ustr);
  log("Expected: ", cl_enc_ustr, " unichar: ", oct2unichar(v_enc1))
  if (v_enc1 != cl_enc_ustr) {
    setverdict(fail, "Encoding failed. Expected: ", cl_enc_ustr, ", got: ", v_enc);
  }

  //decode
  var universal charstring v_dec := f_dec_ustr(cl_enc_ustr);
  if (v_dec != cl_ustr) {
    setverdict(fail, "Decoding failed. Expected: ", cl_ustr, ", got: ", v_dec);
  }
  setverdict(pass);
}
//==
//local constant, var:
testcase tc_JSON_ustr_local3() runs on CT {
  //local const:
  const universal charstring cl_ustr := char(0,0,0,128);
  const octetstring cl_enc_ustr := '225C753030383022'O; //("\"\\u0080\"")
  var octetstring v_enc := f_enc_ustr(cl_ustr);
  log("Expected: ", cl_enc_ustr, " unichar: ", oct2unichar(v_enc))
  if (v_enc != cl_enc_ustr) {
    setverdict(fail, "Encoding failed. Expected: ", cl_enc_ustr, ", got: ", v_enc);//'22C28022'O = "128" in utf-8
  }
  //var:
  var universal charstring v_ustr := char(0,0,0,128);
  var octetstring v_enc1 := f_enc_ustr(v_ustr);
  log("Expected: ", cl_enc_ustr, " unichar: ", oct2unichar(v_enc1))
  if (v_enc1 != cl_enc_ustr) {
    setverdict(fail, "Encoding failed. Expected: ", cl_enc_ustr, ", got: ", v_enc);
  }

  //decode
  var universal charstring v_dec := f_dec_ustr(cl_enc_ustr);
  if (v_dec != cl_ustr) {
    setverdict(fail, "Decoding failed. Expected: ", cl_ustr, ", got: ", v_dec);
  }
  setverdict(pass);
}


//===== Bulk processing =====
//short:"Uses the JSON short escape sequences for any characters that have them (i.e. '\n', '\t', '\r',
//'\f', '\b', '\"', '\\' and '\/'), and uses USI-like escape sequences (i.e. '\u' followed by 4 hex digits
//containing the character’s ASCII code) for all other characters between char(U0) and char(U1F),
//and for char(U7F)."

//Encoding data record
type record E {
  universal charstring u, //data to be encoded
  octetstring o // expected output
}

// record of input-output data for encoding
type record of E RoE;

//
testcase tc_JSON_ustr_local_char1() runs on CT {
  //local const:
  const RoE cl_data := {
    [0]:= { u := char(0,0,0,0), o:= '225C753030303022'O}, // ("\"\\u0000\"")
    [1]:= { u := char(0,0,0,1), o:= '225C753030303122'O}, // ("\"\\u0001\"")
    [2]:= { u := char(0,0,0,2), o:= '225C753030303222'O}, // ("\"\\u0002\"")
    [3]:= { u := char(0,0,0,3), o:= '225C753030303322'O}, // ("\"\\u0003\"")
    [4]:= { u := char(0,0,0,8), o:= '225C6222'O }, //("\"\\b\"") backspace, \b
    [5]:= { u := char(0,0,0,9), o:= '225C7422'O }, //("\"\\t\""), horizontal tab \t
    [6]:= { u := char(0,0,0,10), o:= '225C6E22'O }, //("\"\\n\"") line feed \n
    [7]:= { u := char(0,0,0,11), o:= '225C753030304222'O}, //("\"\\u000B\"")" vertical tab \v -Not applied
    [8]:= { u := char(0,0,0,12), o:= '225C6622'O}, //form feed, \f
    [9]:= { u := char(0,0,0,13), o:= '225C7222'O}, //carriage return,\r
    [10]:= { u := char(0,0,0,31), o:= '225C753030314622'O}, // //("\"\\u001F\""), unit separator

    //from this point, UTF-8 applied, perhaps with escape
    [11]:= { u := char(0,0,0,32), o:= '222022'O}, //'222022'O space, got: '222022'O
    [12]:= { u := char(0,0,0,34), o:= '225C2222'O }, // ("\"\\\"\"")  quotation mark, escaped
    [13]:= { u := char(0,0,0,47), o:= '225C2F22'O },// ("\"\\/\"") slash
    [14]:= { u := char(0,0,0,92), o:= '225C5C22'O }, //("\"\\\\\"") back slash
    [15]:= { u := char(0,0,0,125), o:= '227D22'O }, // }
    [16]:= { u := char(0,0,0,126), o:= '227E22'O }, // ~
    [17]:= { u := char(0,0,0,127), o:= '225C753030374622'O } //[DEL], ("\"\\u007F\"")

  }
  const integer N := sizeof(cl_data);
  for(var integer i:= 0; i<N;i:=i+1 ) {
    var octetstring v_enc := f_enc_ustr(cl_data[i].u);
    log(i,": Expected: ", cl_data[i].o, " unichar: ", oct2unichar(cl_data[i].o))
    if (v_enc != cl_data[i].o) {
      setverdict(fail, "Encoding failed. Expected: ", cl_data[i].o, ", got: ", v_enc);
    }
    //var:
    var universal charstring v_ustr := cl_data[i].u;
    var octetstring v_enc1 := f_enc_ustr(v_ustr);
    log("Expected: ", cl_data[i].o, " unichar: ", oct2unichar(cl_data[i].o))
    if (v_enc1 != cl_data[i].o) {
      setverdict(fail, "Encoding failed. Expected: ", cl_data[i].o, ", got: ", v_enc1);
    }

    //decode
    var universal charstring v_dec := f_dec_ustr(v_enc1);
    if (v_dec != cl_data[i].u) {
      setverdict(fail, "Decoding failed. Expected: ", cl_data[i].u, ", got: ", v_dec);
    }
  }
  setverdict(pass);
}

testcase tc_JSON_ustr_local_char2() runs on CT {
  //local const:
  const RoE cl_data := {
    //one byte is not enough:
    //2 bytes utf8 length:
    [0]:= { u := char(0,0,0,128), o:= '22C28022'O }, //First case upper 7bit
    [1]:= { u := char(0,0,0,193), o:= '22C38122'O }, //Á, 00C1, utf8:C381
    [2]:= { u := char(0,0,0,201), o:= '22C38922'O }, //É, 00C9, utf8:C389
    [3]:= { u := char(0,0,0,205), o:= '22C38D22'O }, //Í, 00CD, utf8:C38D
    [4]:= { u := char(0,0,0,211), o:= '22C39322'O }, //Ó, 00D3, utf8:C393
    [5]:= { u := char(0,0,0,214), o:= '22C39622'O }, //Ö, 00D6, utf8:C396
    [6]:= { u := char(0,0,0,255), o:= '22C3BF22'O }, //Latin Small Letter Y with Diaeresis "ÿ" //07ff
	[7]:= { u := char(0,0,1,0),   o:= '22C48022'O }, //Latin Capital Letter a with Macron, 256dec=0100 utf8:C480
	[8]:= { u := char(0,0,7,255), o:= '22DFBF22'O },  //2047dec=07FF, utf8:DFBF  (last 2bytes utf8)
	
	[9]:= { u := char(0,0,8,0),   o:= '22E0A08022'O }, //Samaritan Letter Alaf, 0800
	[10]:= { u := char(0,0,255,255), o:= '22EFBFBF22'O }, //very last 3 bytes code
	
	//utf8 length: 4 bytes
	[11]:= { u := char(0,1,0,0), o:= '22F090808022'O } //first 4 bytes utf8
	
  }
  const integer N := sizeof(cl_data);
  for(var integer i:= 0; i<N;i:=i+1 ) {
    var octetstring v_enc := f_enc_ustr(cl_data[i].u);
    log(i,": Expected: ", cl_data[i].o, " unichar: ", oct2unichar(cl_data[i].o))
    if (v_enc != cl_data[i].o) {
      setverdict(fail, "Encoding failed. Expected: ", cl_data[i].o, ", got: ", v_enc);
    }
    //var:
    var universal charstring v_ustr := cl_data[i].u;
    var octetstring v_enc1 := f_enc_ustr(v_ustr);
    log("Expected: ", cl_data[i].o, " unichar: ", oct2unichar(cl_data[i].o))
    if (v_enc1 != cl_data[i].o) {
      setverdict(fail, "Encoding failed. Expected: ", cl_data[i].o, ", got: ", v_enc1);
    }

    //decode
    var universal charstring v_dec := f_dec_ustr(v_enc1);
    if (v_dec != cl_data[i].u) {
      setverdict(fail, "Decoding failed. Expected: ", cl_data[i].u, ", got: ", v_dec);
    }
  }
  setverdict(pass);
}

//TODO: Check Higher plane codepoints !

testcase tc_JSON_ustr_local_char3() runs on CT {
  //max number encodable in UTF-8 is '00011111 11111111 11111111'B='1F FF FF'O=char(0,31,255,255)
  //https://hu.wikipedia.org/wiki/UTF-8
  //local const:
  const RoE cl_data := {
	//utf8 length: 4 bytes
	[0]:= { u := char(0,1,0,0), o:= '22F090808022'O }, //first 4 bytes utf8
	[1]:= { u := char(0,1,1,1), o:= '22F090848122'O }, // Aegean Word Separator Dot, U+10101, utf8: F0908481
	[2]:= { u := char(0,1,1,127), o:= '22F09085BF22'O }, //Greek Four Obols Sign, U+1017F, utf-8:
	[3]:= { u := char(0,1,1,255), o:= '22F09087BF22'O }, //Phaistos Disc. last char U+101FF, utf-8:F0 90 87 BF
	[4]:= { u := char(0,1,1,255), o:= '22F09087BF22'O },
	[5]:= { u := char(0,15,1,239), o:= '22F3B087AF22'O }, //last defined unicode point, U+F01EF  utf-8:F3B087AF ok, manually tested
	[6]:= { u := char(0,31,255,255), o:= '22F7BFBFBF22'O} //'1F FF FF'O very last code point permitted in UTF-8, manually checked
  }
  const integer N := sizeof(cl_data);
  for(var integer i:= 0; i<N;i:=i+1 ) {
    log("==== i=",i,"========================================================");
    var octetstring v_enc := f_enc_ustr(cl_data[i].u);
    log(i,": Expected: ", cl_data[i].o, " unichar: ", oct2unichar(cl_data[i].o))
    if (v_enc != cl_data[i].o) {
      setverdict(fail, "Encoding failed. Expected: ", cl_data[i].o, ", got: ", v_enc);
    }
    //var:
    var universal charstring v_ustr := cl_data[i].u;
    var octetstring v_enc1 := f_enc_ustr(v_ustr);
    log("Expected: ", cl_data[i].o, " unichar: ", oct2unichar(cl_data[i].o))
    if (v_enc1 != cl_data[i].o) {
      setverdict(fail, "Encoding failed. Expected: ", cl_data[i].o, ", got: ", v_enc1);
    }

    //decode
    var universal charstring v_dec := f_dec_ustr(v_enc1);
    if (v_dec != cl_data[i].u) {
      setverdict(fail, "Decoding failed. Expected: ", cl_data[i].u, ", got: ", v_dec);
    }
  }
  setverdict(pass);
}

//================================
//global constant:
testcase tc_JSON_ustr_gc() runs on CT {
  var octetstring v_enc := f_enc_ustr(c_ustr);
  log("Expected: ", c_enc_ustr, oct2unichar(v_enc))
  if (v_enc != c_enc_ustr) {
    setverdict(fail, "Encoding failed. Expected: ", c_enc_ustr, ", got: ", v_enc);
  }

  //var:
  var universal charstring v_ustr := "árvíztűrő tükörfúrógép";
  var octetstring v_enc1 := f_enc_ustr(v_ustr);
  log("Expected: ", c_enc_ustr, oct2unichar(v_enc1))
  if (v_enc1 != c_enc_ustr) {
    setverdict(fail, "Encoding failed. Expected: ", c_enc_ustr, ", got: ", v_enc);
  }
  var universal charstring v_dec := f_dec_ustr(c_enc_ustr);
  if (v_dec != c_ustr) {
    setverdict(fail, "Decoding failed. Expected: ", c_ustr, ", got: ", v_dec);
  }
  setverdict(pass);
}

//local constant:
testcase tc_JSON_ustr_lc() runs on CT {
  const universal charstring cl_ustr := "árvíztűrő tükörfúrógép"; // 31 UTF-8 characters
  const octetstring cl_enc_ustr := '22C3A17276C3AD7A74C5B172C5912074C3BC6BC3B67266C3BA72C3B367C3A97022'O  
  var octetstring v_enc := f_enc_ustr(cl_ustr);
  //cpp log: DEBUG ../src/JsonBasicTest_ucharstring.ttcn:39(testcase:tc_JSON_ustr) f_enc_ustr(): Encoding universal charstring: char(0, 0, 0, 225) & "rv" & char(0, 0, 0, 237) & "zt" & char(0, 0, 1, 113) & "r" & char(0, 0, 1, 81) & " t" & char(0, 0, 0, 252) & "k" & char(0, 0, 0, 246) & "rf" & char(0, 0, 0, 250) & "r" & char(0, 0, 0, 243) & "g" & char(0, 0, 0, 233) & "p"
  log("Expected: ", cl_enc_ustr, oct2unichar(v_enc))
  if (v_enc != c_enc_ustr) {
    setverdict(fail, "Encoding failed. Expected: ", c_enc_ustr, ", got: ", v_enc);
  }
  //var:
  var universal charstring v_ustr := "árvíztűrő tükörfúrógép";
  var octetstring v_enc1 := f_enc_ustr(v_ustr);
  log("Expected: ", cl_enc_ustr, oct2unichar(v_enc1))
  if (v_enc1 != cl_enc_ustr) {
    setverdict(fail, "Encoding failed. Expected: ", cl_enc_ustr, ", got: ", v_enc);
  }

  //decode
  var universal charstring v_dec := f_dec_ustr(c_enc_ustr);
  if (v_dec != cl_ustr) {
    setverdict(fail, "Decoding failed. Expected: ", cl_ustr, ", got: ", v_dec);
  }
  setverdict(pass);
}

//expected value calculated
testcase tc_JSON_ustr_in_rec1() runs on CT {
  var Rec v_rec := { 10, c_ustr };
  var octetstring v_enc_expected := unichar2oct("{\"num\":10,\"ustr\":\"" & char(0, 0, 0, 225) & "rv" & char(0, 0, 0, 237) & "zt" & char(0, 0, 1, 113) & "r" & char(0, 0, 1, 81) & " t" & char(0, 0, 0, 252) & "k" & char(0, 0, 0, 246) & "rf" & char(0, 0, 0, 250) & "r" & char(0, 0, 0, 243) & "g" & char(0, 0, 0, 233) & "p\"}");
  var octetstring v_enc := f_enc_rec(v_rec);
  log("Expected:", v_enc_expected, " char: ", oct2unichar(v_enc_expected));
  if (v_enc != v_enc_expected) {
    setverdict(fail, "Encoding failed. Expected: ", v_enc_expected, ", got: ", v_enc, oct2unichar(v_enc));
  }
  var Rec v_dec := f_dec_rec(v_enc_expected);
  var Rec v_dec_rec := { 10, c_ustr };
  if (v_dec != v_dec_rec) {
    setverdict(fail, "Decoding failed. Expected: ", v_dec_rec, ", got: ", v_dec);
  }
  setverdict(pass);
}

//expected value with octetstring
testcase tc_JSON_ustr_in_rec2() runs on CT {
  var Rec v_rec := { 10, c_ustr };
  var octetstring v_enc_expected := '7B226E756D223A31302C2275737472223A22C3A17276C3AD7A74C5B172C5912074C3BC6BC3B67266C3BA72C3B367C3A970227D'O //{xxx}
  var octetstring v_enc := f_enc_rec(v_rec);
  log("Expected:", v_enc_expected, " char: ",oct2unichar(v_enc_expected));
  if (v_enc != v_enc_expected) {
    setverdict(fail, "Encoding failed. Expected: ", v_enc_expected, ", got: ", v_enc, oct2unichar(v_enc));
  }
  var Rec v_dec := f_dec_rec(v_enc_expected);
  var Rec v_dec_rec := { 10, c_ustr };
  if (v_dec != v_dec_rec) {
    setverdict(fail, "Decoding failed. Expected: ", v_dec_rec, ", got: ", v_dec);
  }
  setverdict(pass);
}


control {
  execute(tc_JSON_ustr_local1());
  execute(tc_JSON_ustr_too_big_char());
  execute(tc_JSON_ustr_local_char1());
  execute(tc_JSON_ustr_local_char2());
  execute(tc_JSON_ustr_local_char3());
  execute(tc_JSON_ustr_gc());
  execute(tc_JSON_ustr_lc());
//  execute(tc_JSON_ustr_in_rec1());
//  execute(tc_JSON_ustr_in_rec2());
}

}
with {
encode "JSON"
}