]> mj.ucw.cz Git - libucw.git/blob - ucw-json/json-test.t
JSON: More tests and bug fixes
[libucw.git] / ucw-json / json-test.t
1 # Tests for the JSON library
2 # (c) 2015 Martin Mares <mj@ucw.cz>
3
4 ### Literals ###
5
6 Name:   Null
7 Run:    ../obj/ucw-json/json-test -rw
8 In:     null
9 Out:    null
10
11 Name:   True
12 In:     true
13 Out:    true
14
15 Name:   False
16 In:     false
17 Out:    false
18
19 Name:   Invalid literal 1
20 In:     lomikel
21 Exit:   1
22 Err:    ERROR: Invalid literal name at line 1:8
23
24 Name:   Invalid literal 2
25 In:     aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
26 Exit:   1
27 Err:    ERROR: Invalid literal name at line 1:101
28
29 ### Numbers ###
30
31 Name:   Plain number
32 In:     42
33 Out:    42
34
35 Name:   Negative number
36 In:     -42
37 Out:    -42
38
39 Name:   Zero number
40 In:     0
41 Out:    0
42
43 # The largest number guaranteed to be precise by RFC 7159
44 Name:   Large number
45 In:     9007199254740991
46 Out:    9007199254740991
47
48 Name:   Fractional number 1
49 In:     12345.54321
50 Out:    12345.54321
51
52 Name:   Fractional number 2
53 In:     0.333333333
54 Out:    0.333333333
55
56 Name:   Number in scientific notation 1
57 In:     3.14159e20
58 Out:    3.14159e+20
59
60 Name:   Number in scientific notation 2
61 In:     3.14159e+20
62 Out:    3.14159e+20
63
64 Name:   Number in scientific notation 3
65 In:     3.14159e-20
66 Out:    3.14159e-20
67
68 Name:   No leading zero allowed
69 In:     01234
70 Exit:   1
71 Err:    ERROR: Malformed number: leading zero at line 1:2
72
73 Name:   Bare sign is not a number
74 In:     -
75 Exit:   1
76 Err:    ERROR: Malformed number: just minus at line 1:2
77
78 Name:   No leading decimal point allowed
79 In:     .1234
80 Exit:   1
81 Err:    ERROR: Numbers must start with a digit at line 1:1
82
83 Name:   Digits after decimal point required
84 In:     1234.
85 Exit:   1
86 Err:    ERROR: Malformed number: no digits after decimal point at line 1:6
87
88 Name:   Exponent part must be non-empty 1
89 In:     1e
90 Exit:   1
91 Err:    ERROR: Malformed number: empty exponent at line 1:3
92
93 Name:   Exponent part must be non-empty 2
94 In:     1e+
95 Exit:   1
96 Err:    ERROR: Malformed number: empty exponent at line 1:4
97
98 Name:   Number out of range
99 In:     1e3000000
100 Exit:   1:
101 Err:    ERROR: Number out of range at line 1:10
102
103 ### Strings ###
104
105 Name:   Plain string
106 In:     "foo"
107 Out:    "foo"
108
109 Name:   Empty string
110 In:     ""
111 Out:    ""
112
113 Name:   UTF-8 string
114 In:     "šelmička"
115 Out:    "šelmička"
116
117 Name:   Unterminated string
118 In:     "infinity
119 Exit:   1
120 Err:    ERROR: Unterminated string at line 1:10
121
122 Name:   Multi-line string
123 In:     "infi
124         nity"
125 Exit:   1
126 Err:    ERROR: Unterminated string at line 1:6
127
128 # Some characters are written as \uXXXX on output, which is correct
129 Name:   Escaped characters
130 In:     "\"\\\/\b\f\n\r\t"
131 Out:    "\"\\/\u0008\u000c\n\r\t"
132
133 Name:   Improper escaped characters
134 In:     "\a"
135 Exit:   1
136 Err:    ERROR: Invalid backslash sequence in string at line 1:3
137
138 Name:   Unicode escapes
139 In:     "\u0041\u010d\u010D\u0001"
140 Out:    "Ačč\u0001"
141
142 Name:   Improper Unicode escapes 1
143 In:     "\u"
144 Exit:   1
145 Err:    ERROR: Invalid Unicode escape sequence at line 1:4
146
147 Name:   Improper Unicode escapes 2
148 In:     "\u
149 Exit:   1
150 Err:    ERROR: Invalid Unicode escape sequence at line 1:4
151
152 Name:   Improper Unicode escapes 3
153 In:     "\uABCZ"
154 Exit:   1
155 Err:    ERROR: Invalid Unicode escape sequence at line 1:7
156
157 ### Unicode magic ###
158
159 # TAB is forbidden
160 Name:   Control characters 1
161 Run:    ../obj/ucw-json/json-test -RW
162 In:     "<09>"
163 Exit:   1
164 Err:    ERROR: Invalid control character in string at line 1:2
165
166 # Surprisingly, DEL is not
167 Name:   Control characters 2
168 In:     "<7f>"
169 Out:    "<7f>"<0a>
170
171 # U+31234
172 Name:   UTF-8 outside BMP
173 In:     "<f0><b1><88><b4>"
174 Out:    "<f0><b1><88><b4>"<0a>
175
176 Name:   Private use characters in BMP
177 In:     "<ef><80><80>"
178 Exit:   1
179 Err:    ERROR: Invalid private-use character in string at line 1:2
180
181 Name:   UTF-8 outside BMP
182 In:     "<f0><b1><88><b4>"
183 Out:    "<f0><b1><88><b4>"<0a>
184
185 Name:   Private use characters outside BMP
186 In:     "<f3><b0><80><80>"
187 Exit:   1
188 Err:    ERROR: Invalid private-use character in string at line 1:2
189
190 # U+FF0000
191 Name:   UTF-8 outside UCS
192 In:     "<f8><bf><b0><80><80>"
193 Exit:   1
194 Err:    ERROR: Invalid non-Unicode character in string at line 1:2
195
196 # U+D800
197 Name:   UTF-8 surrogate 1
198 In:     "<ed><a0><80>"
199 Exit:   1
200 Err:    ERROR: Invalid surrogate character in string at line 1:2
201
202 # U+DC00
203 Name:   UTF-8 surrogate 2
204 In:     "<ed><b0><80>"
205 Exit:   1
206 Err:    ERROR: Invalid surrogate character in string at line 1:2
207
208 # U+FEFF
209 Name:   UTF-8 BOM
210 In:     <ef><bb><bf>
211 Exit:   1
212 Err:    ERROR: Misplaced byte-order mark, complain in Redmond at line 1:1
213
214 Name:   Escaped NUL
215 In:     "\u0000"
216 Exit:   1
217 Err:    ERROR: Zero bytes in strings are not supported at line 1:7
218
219 Name:   Escaped surrogate
220 In:     "\udaff\udcba"
221 Out:    "<f3><8f><b2><ba>"<0a>
222
223 Name:   Escaped surrogate malformation 1
224 In:     "\udaff"
225 Exit:   1
226 Err:    ERROR: Escaped high surrogate codepoint must be followed by a low surrogate codepoint at line 1:8
227
228 Name:   Escaped surrogate malformation 2
229 In:     "\udcff"
230 Exit:   1
231 Err:    ERROR: Invalid escaped surrogate codepoint at line 1:7
232
233 Name:   Escaped low private-use character
234 In:     "\uedac"
235 Exit:   1
236 Err:    ERROR: Invalid escaped private-use character at line 1:7
237
238 Name:   Escaped high private-use character
239 In:     "\udbff\udc00"
240 Exit:   1
241 Err:    ERROR: Invalid escaped private-use character at line 1:13
242
243 # Switch back to normal mode
244 Name:   Invalid ASCII character
245 Run:    ../obj/ucw-json/json-test -rw
246 In:     @
247 Exit:   1
248 Err:    ERROR: Invalid character at line 1:1
249
250 ### Arrays ###
251
252 Name:   Empty array
253 In:     []
254 Out:    []
255
256 Name:   One-element array
257 In:     [1]
258 Out:    [ 1 ]
259
260 Name:   Two-element array
261 In:     [1,2]
262 Out:    [ 1, 2 ]
263
264 Name:   Nested arrays
265 In:     [[1,2],["a","b"]]
266 Out:    [ [ 1, 2 ], [ "a", "b" ] ]
267
268 Name:   Multi-line array
269 In:     [
270         "a",    null,false
271         ,false
272         ]
273 Out:    [ "a", null, false, false ]
274
275 Name:   Unterminated array 1
276 In:     [1,2
277 Exit:   1
278 Err:    ERROR: Comma or right bracket expected at line 2:0
279
280 Name:   Unterminated array 2
281 In:     [1,2,
282 Exit:   1
283 Err:    ERROR: Unterminated array at line 2:0
284
285 Name:   Extra comma not allowed
286 In:     [1,2,]
287 Exit:   1
288 Err:    ERROR: Misplaced end of array at line 1:6
289
290 Name:   Solitary comma not allowed
291 In:     ,
292 Exit:   1
293 Err:    ERROR: Misplaced comma at line 1:1
294
295 Name:   Deeply nested array
296 In:     [[[[[[[[[[]]]]]]]]]]
297 Out:    [ [ [ [ [ [ [ [ [ [] ] ] ] ] ] ] ] ] ]
298
299 Name:   Deeply unclosed array
300 In:     [[[[[[[[[[]
301 Exit:   1
302 Err:    ERROR: Comma or right bracket expected at line 2:0
303
304 Name:   Missing comma
305 In:     [1 2]
306 Exit:   1
307 Err:    ERROR: Comma or right bracket expected at line 1:5
308
309 ### Objects ###
310
311 Name:   Empty object
312 In:     {}
313 Out:    {}
314
315 Name:   One-entry object
316 In:     {"a":"b"}
317 Out:    { "a": "b" }
318
319 Name:   Two-entry object
320 In:     {"a":1,"b":2}
321 Out:    { "a": 1, "b": 2 }
322
323 Name:   Nested objects
324 In:     {
325                 "a": [1,2],
326                 "b": { "x": true, "y": false }
327         }
328 Out:    { "a": [ 1, 2 ], "b": { "x": true, "y": false } }
329
330 Name:   Unterminated object 1
331 In:     {
332 Exit:   1
333 Err:    ERROR: Unterminated object at line 2:0
334
335 Name:   Unterminated object 2
336 In:     { "a"
337 Exit:   1
338 Err:    ERROR: Colon expected at line 2:0
339
340 Name:   Unterminated object 3
341 In:     { "a":
342 Exit:   1
343 Err:    ERROR: Unterminated object at line 2:0
344
345 Name:   Unterminated object 4
346 In:     { "a":1,
347 Exit:   1
348 Err:    ERROR: Unterminated object at line 2:0
349
350 Name:   Extra comma not allowed in objects
351 In:     { "a":1, }
352 Exit:   1
353 Err:    ERROR: Misplaced end of object at line 1:10
354
355 Name:   Non-string key
356 In:     {1:2}
357 Exit:   1
358 Err:    ERROR: Object key must be a string at line 1:3
359
360 Name:   Repeated key
361 In:     {"a":1, "a":2}
362 Exit:   1
363 Err:    ERROR: Key already set at line 1:14
364
365 Name:   Missing object comma
366 In:     {"a":1 "b":2}
367 Exit:   1
368 Err:    ERROR: Comma expected at line 1:10
369
370 ### Top-level problems ###
371
372 Name:   Empty input
373 Exit:   1
374 Err:    ERROR: Empty input at line 1:0
375
376 Name:   Multiple values
377 In:     1 2
378 Exit:   1
379 Err:    ERROR: Only one top-level value allowed at line 1:4