5 static int test_passed = 0;
6 static int test_failed = 0;
8 /* Terminate current test with error */
9 #define fail() return __LINE__
11 /* Successfull end of the test case */
12 #define done() return 0
14 /* Check single condition */
15 #define check(cond) do { if (!(cond)) fail(); } while (0)
18 static void test(int (*func)(void), const char *name) {
24 printf("FAILED: %s (at line %d)\n", name, r);
28 #define TOKEN_EQ(t, tok_start, tok_end, tok_type) \
29 ((t).start == tok_start \
30 && (t).end == tok_end \
31 && (t).type == (tok_type))
33 #define TOKEN_STRING(js, t, s) \
34 (strncmp(js+(t).start, s, (t).end - (t).start) == 0 \
35 && strlen(s) == (t).end - (t).start)
37 #define TOKEN_PRINT(t) \
38 printf("start: %d, end: %d, type: %d, size: %d\n", \
39 (t).start, (t).end, (t).type, (t).size)
52 r = jsmn_parse(&p, js, strlen(js), t, 10);
54 check(t[0].type == JSMN_OBJECT);
55 check(t[0].start == 0 && t[0].end == 2);
59 r = jsmn_parse(&p, js, strlen(js), t, 10);
61 check(t[0].type == JSMN_ARRAY);
62 check(t[0].start == 0 && t[0].end == 2);
66 r = jsmn_parse(&p, js, strlen(js), t, 10);
68 check(t[0].type == JSMN_OBJECT && t[0].start == 0 && t[0].end == 8);
69 check(t[1].type == JSMN_STRING && t[1].start == 2 && t[1].end == 3);
70 check(t[2].type == JSMN_ARRAY && t[2].start == 5 && t[2].end == 7);
74 r = jsmn_parse(&p, js, strlen(js), t, 10);
76 check(t[0].type == JSMN_ARRAY && t[0].start == 0 && t[0].end == 7);
77 check(t[1].type == JSMN_OBJECT && t[1].start == 1 && t[1].end == 3);
78 check(t[2].type == JSMN_OBJECT && t[2].start == 4 && t[2].end == 6);
91 r = jsmn_parse(&p, js, strlen(js), tokens, 10);
93 check(TOKEN_EQ(tokens[0], 0, 8, JSMN_OBJECT));
94 check(TOKEN_EQ(tokens[1], 2, 3, JSMN_STRING));
95 check(TOKEN_EQ(tokens[2], 6, 7, JSMN_PRIMITIVE));
97 check(TOKEN_STRING(js, tokens[0], js));
98 check(TOKEN_STRING(js, tokens[1], "a"));
99 check(TOKEN_STRING(js, tokens[2], "0"));
102 js = "[\"a\":{},\"b\":{}]";
103 r = jsmn_parse(&p, js, strlen(js), tokens, 10);
107 js = "{\n \"Day\": 26,\n \"Month\": 9,\n \"Year\": 12\n }";
108 r = jsmn_parse(&p, js, strlen(js), tokens, 10);
114 int test_primitive() {
120 js = "\"boolVar\" : true";
122 r = jsmn_parse(&p, js, strlen(js), tok, 10);
123 check(r >= 0 && tok[0].type == JSMN_STRING
124 && tok[1].type == JSMN_PRIMITIVE);
125 check(TOKEN_STRING(js, tok[0], "boolVar"));
126 check(TOKEN_STRING(js, tok[1], "true"));
128 js = "\"boolVar\" : false";
130 r = jsmn_parse(&p, js, strlen(js), tok, 10);
131 check(r >= 0 && tok[0].type == JSMN_STRING
132 && tok[1].type == JSMN_PRIMITIVE);
133 check(TOKEN_STRING(js, tok[0], "boolVar"));
134 check(TOKEN_STRING(js, tok[1], "false"));
136 js = "\"intVar\" : 12345";
138 r = jsmn_parse(&p, js, strlen(js), tok, 10);
139 check(r >= 0 && tok[0].type == JSMN_STRING
140 && tok[1].type == JSMN_PRIMITIVE);
141 check(TOKEN_STRING(js, tok[0], "intVar"));
142 check(TOKEN_STRING(js, tok[1], "12345"));
144 js = "\"floatVar\" : 12.345";
146 r = jsmn_parse(&p, js, strlen(js), tok, 10);
147 check(r >= 0 && tok[0].type == JSMN_STRING
148 && tok[1].type == JSMN_PRIMITIVE);
149 check(TOKEN_STRING(js, tok[0], "floatVar"));
150 check(TOKEN_STRING(js, tok[1], "12.345"));
152 js = "\"nullVar\" : null";
154 r = jsmn_parse(&p, js, strlen(js), tok, 10);
155 check(r >= 0 && tok[0].type == JSMN_STRING
156 && tok[1].type == JSMN_PRIMITIVE);
157 check(TOKEN_STRING(js, tok[0], "nullVar"));
158 check(TOKEN_STRING(js, tok[1], "null"));
169 js = "\"strVar\" : \"hello world\"";
171 r = jsmn_parse(&p, js, strlen(js), tok, 10);
172 check(r >= 0 && tok[0].type == JSMN_STRING
173 && tok[1].type == JSMN_STRING);
174 check(TOKEN_STRING(js, tok[0], "strVar"));
175 check(TOKEN_STRING(js, tok[1], "hello world"));
177 js = "\"strVar\" : \"escapes: \\/\\r\\n\\t\\b\\f\\\"\\\\\"";
179 r = jsmn_parse(&p, js, strlen(js), tok, 10);
180 check(r >= 0 && tok[0].type == JSMN_STRING
181 && tok[1].type == JSMN_STRING);
182 check(TOKEN_STRING(js, tok[0], "strVar"));
183 check(TOKEN_STRING(js, tok[1], "escapes: \\/\\r\\n\\t\\b\\f\\\"\\\\"));
185 js = "\"strVar\" : \"\"";
187 r = jsmn_parse(&p, js, strlen(js), tok, 10);
188 check(r >= 0 && tok[0].type == JSMN_STRING
189 && tok[1].type == JSMN_STRING);
190 check(TOKEN_STRING(js, tok[0], "strVar"));
191 check(TOKEN_STRING(js, tok[1], ""));
196 int test_partial_string() {
204 r = jsmn_parse(&p, js, strlen(js), tok, 10);
205 check(r == JSMN_ERROR_PART && tok[0].type == JSMN_STRING);
206 check(TOKEN_STRING(js, tok[0], "x"));
207 check(p.toknext == 1);
210 char js_slash[9] = "\"x\": \"va\\";
211 r = jsmn_parse(&p, js_slash, sizeof(js_slash), tok, 10);
212 check(r == JSMN_ERROR_PART);
215 char js_unicode[10] = "\"x\": \"va\\u";
216 r = jsmn_parse(&p, js_unicode, sizeof(js_unicode), tok, 10);
217 check(r == JSMN_ERROR_PART);
219 js = "\"x\": \"valu";
220 r = jsmn_parse(&p, js, strlen(js), tok, 10);
221 check(r == JSMN_ERROR_PART && tok[0].type == JSMN_STRING);
222 check(TOKEN_STRING(js, tok[0], "x"));
223 check(p.toknext == 1);
225 js = "\"x\": \"value\"";
226 r = jsmn_parse(&p, js, strlen(js), tok, 10);
227 check(r >= 0 && tok[0].type == JSMN_STRING
228 && tok[1].type == JSMN_STRING);
229 check(TOKEN_STRING(js, tok[0], "x"));
230 check(TOKEN_STRING(js, tok[1], "value"));
232 js = "\"x\": \"value\", \"y\": \"value y\"";
233 r = jsmn_parse(&p, js, strlen(js), tok, 10);
234 check(r >= 0 && tok[0].type == JSMN_STRING
235 && tok[1].type == JSMN_STRING && tok[2].type == JSMN_STRING
236 && tok[3].type == JSMN_STRING);
237 check(TOKEN_STRING(js, tok[0], "x"));
238 check(TOKEN_STRING(js, tok[1], "value"));
239 check(TOKEN_STRING(js, tok[2], "y"));
240 check(TOKEN_STRING(js, tok[3], "value y"));
245 int test_unquoted_keys() {
253 js = "key1: \"value\"\nkey2 : 123";
255 r = jsmn_parse(&p, js, strlen(js), tok, 10);
256 check(r >= 0 && tok[0].type == JSMN_PRIMITIVE
257 && tok[1].type == JSMN_STRING && tok[2].type == JSMN_PRIMITIVE
258 && tok[3].type == JSMN_PRIMITIVE);
259 check(TOKEN_STRING(js, tok[0], "key1"));
260 check(TOKEN_STRING(js, tok[1], "value"));
261 check(TOKEN_STRING(js, tok[2], "key2"));
262 check(TOKEN_STRING(js, tok[3], "123"));
267 int test_partial_array() {
275 r = jsmn_parse(&p, js, strlen(js), tok, 10);
276 check(r == JSMN_ERROR_PART && tok[0].type == JSMN_ARRAY
277 && tok[1].type == JSMN_PRIMITIVE && tok[2].type == JSMN_PRIMITIVE);
279 js = " [ 1, true, [123, \"hello";
280 r = jsmn_parse(&p, js, strlen(js), tok, 10);
281 check(r == JSMN_ERROR_PART && tok[0].type == JSMN_ARRAY
282 && tok[1].type == JSMN_PRIMITIVE && tok[2].type == JSMN_PRIMITIVE
283 && tok[3].type == JSMN_ARRAY && tok[4].type == JSMN_PRIMITIVE);
285 js = " [ 1, true, [123, \"hello\"]";
286 r = jsmn_parse(&p, js, strlen(js), tok, 10);
287 check(r == JSMN_ERROR_PART && tok[0].type == JSMN_ARRAY
288 && tok[1].type == JSMN_PRIMITIVE && tok[2].type == JSMN_PRIMITIVE
289 && tok[3].type == JSMN_ARRAY && tok[4].type == JSMN_PRIMITIVE
290 && tok[5].type == JSMN_STRING);
291 /* check child nodes of the 2nd array */
292 check(tok[3].size == 2);
294 js = " [ 1, true, [123, \"hello\"]]";
295 r = jsmn_parse(&p, js, strlen(js), tok, 10);
296 check(r >= 0 && tok[0].type == JSMN_ARRAY
297 && tok[1].type == JSMN_PRIMITIVE && tok[2].type == JSMN_PRIMITIVE
298 && tok[3].type == JSMN_ARRAY && tok[4].type == JSMN_PRIMITIVE
299 && tok[5].type == JSMN_STRING);
300 check(tok[3].size == 2);
301 check(tok[0].size == 3);
305 int test_array_nomem() {
309 jsmntok_t toksmall[10], toklarge[10];
312 js = " [ 1, true, [123, \"hello\"]]";
314 for (i = 0; i < 6; i++) {
316 memset(toksmall, 0, sizeof(toksmall));
317 memset(toklarge, 0, sizeof(toklarge));
318 r = jsmn_parse(&p, js, strlen(js), toksmall, i);
319 check(r == JSMN_ERROR_NOMEM);
321 memcpy(toklarge, toksmall, sizeof(toksmall));
323 r = jsmn_parse(&p, js, strlen(js), toklarge, 10);
326 check(toklarge[0].type == JSMN_ARRAY && toklarge[0].size == 3);
327 check(toklarge[3].type == JSMN_ARRAY && toklarge[3].size == 2);
332 int test_objects_arrays() {
335 jsmntok_t tokens[10];
340 r = jsmn_parse(&p, js, strlen(js), tokens, 10);
341 check(r == JSMN_ERROR_INVAL);
345 r = jsmn_parse(&p, js, strlen(js), tokens, 10);
350 r = jsmn_parse(&p, js, strlen(js), tokens, 10);
351 check(r == JSMN_ERROR_INVAL);
355 r = jsmn_parse(&p, js, strlen(js), tokens, 10);
361 int test_issue_22() {
364 jsmntok_t tokens[128];
367 js = "{ \"height\":10, \"layers\":[ { \"data\":[6,6], \"height\":10, "
368 "\"name\":\"Calque de Tile 1\", \"opacity\":1, \"type\":\"tilelayer\", "
369 "\"visible\":true, \"width\":10, \"x\":0, \"y\":0 }], "
370 "\"orientation\":\"orthogonal\", \"properties\": { }, \"tileheight\":32, "
371 "\"tilesets\":[ { \"firstgid\":1, \"image\":\"..\\/images\\/tiles.png\", "
372 "\"imageheight\":64, \"imagewidth\":160, \"margin\":0, \"name\":\"Tiles\", "
373 "\"properties\":{}, \"spacing\":0, \"tileheight\":32, \"tilewidth\":32 }], "
374 "\"tilewidth\":32, \"version\":1, \"width\":10 }";
376 r = jsmn_parse(&p, js, strlen(js), tokens, 128);
379 for (i = 1; tokens[i].end < tokens[0].end; i++) {
380 if (tokens[i].type == JSMN_STRING || tokens[i].type == JSMN_PRIMITIVE) {
381 printf("%.*s\n", tokens[i].end - tokens[i].start, js + tokens[i].start);
382 } else if (tokens[i].type == JSMN_ARRAY) {
383 printf("[%d elems]\n", tokens[i].size);
384 } else if (tokens[i].type == JSMN_OBJECT) {
385 printf("{%d elems}\n", tokens[i].size);
387 TOKEN_PRINT(tokens[i]);
394 int test_unicode_characters() {
396 jsmntok_t tokens[10];
400 js = "{\"a\":\"\\uAbcD\"}";
402 r = jsmn_parse(&p, js, strlen(js), tokens, 10);
405 js = "{\"a\":\"str\\u0000\"}";
407 r = jsmn_parse(&p, js, strlen(js), tokens, 10);
410 js = "{\"a\":\"\\uFFFFstr\"}";
412 r = jsmn_parse(&p, js, strlen(js), tokens, 10);
415 js = "{\"a\":\"str\\uFFGFstr\"}";
417 r = jsmn_parse(&p, js, strlen(js), tokens, 10);
418 check(r == JSMN_ERROR_INVAL);
420 js = "{\"a\":\"str\\u@FfF\"}";
422 r = jsmn_parse(&p, js, strlen(js), tokens, 10);
423 check(r == JSMN_ERROR_INVAL);
425 js = "{\"a\":[\"\\u028\"]}";
427 r = jsmn_parse(&p, js, strlen(js), tokens, 10);
428 check(r == JSMN_ERROR_INVAL);
430 js = "{\"a\":[\"\\u0280\"]}";
432 r = jsmn_parse(&p, js, strlen(js), tokens, 10);
438 int test_input_length() {
442 jsmntok_t tokens[10];
444 js = "{\"a\": 0}garbage";
447 r = jsmn_parse(&p, js, 8, tokens, 10);
449 check(TOKEN_STRING(js, tokens[0], "{\"a\": 0}"));
450 check(TOKEN_STRING(js, tokens[1], "a"));
451 check(TOKEN_STRING(js, tokens[2], "0"));
462 check(jsmn_parse(&p, js, strlen(js), NULL, 0) == 1);
466 check(jsmn_parse(&p, js, strlen(js), NULL, 0) == 1);
470 check(jsmn_parse(&p, js, strlen(js), NULL, 0) == 2);
474 check(jsmn_parse(&p, js, strlen(js), NULL, 0) == 3);
478 check(jsmn_parse(&p, js, strlen(js), NULL, 0) == 3);
480 js = "[[], [[]], [[], []]]";
482 check(jsmn_parse(&p, js, strlen(js), NULL, 0) == 7);
484 js = "[\"a\", [[], []]]";
486 check(jsmn_parse(&p, js, strlen(js), NULL, 0) == 5);
488 js = "[[], \"[], [[]]\", [[]]]";
490 check(jsmn_parse(&p, js, strlen(js), NULL, 0) == 5);
494 check(jsmn_parse(&p, js, strlen(js), NULL, 0) == 4);
496 js = "[1, 2, [3, \"a\"], null]";
498 check(jsmn_parse(&p, js, strlen(js), NULL, 0) == 7);
503 int test_keyvalue() {
507 jsmntok_t tokens[10];
509 js = "{\"a\": 0, \"b\": \"c\"}";
512 r = jsmn_parse(&p, js, strlen(js), tokens, 10);
514 check(tokens[0].size == 2); /* two keys */
515 check(tokens[1].size == 1 && tokens[3].size == 1); /* one value per key */
516 check(tokens[2].size == 0 && tokens[4].size == 0); /* values have zero size */
520 r = jsmn_parse(&p, js, strlen(js), tokens, 10);
521 check(r == JSMN_ERROR_INVAL);
525 r = jsmn_parse(&p, js, strlen(js), tokens, 10);
526 check(r == JSMN_ERROR_INVAL);
530 r = jsmn_parse(&p, js, strlen(js), tokens, 10);
531 check(r == JSMN_ERROR_INVAL);
533 js = "{\"a\": {2: 3}}";
535 r = jsmn_parse(&p, js, strlen(js), tokens, 10);
536 check(r == JSMN_ERROR_INVAL);
539 js = "{\"a\": {\"a\": 2 3}}";
541 r = jsmn_parse(&p, js, strlen(js), tokens, 10);
542 check(r == JSMN_ERROR_INVAL);
546 /** A huge redefinition of everything to include jsmn in non-script mode */
547 #define jsmn_init jsmn_init_nonstrict
548 #define jsmn_parse jsmn_parse_nonstrict
549 #define jsmn_parser jsmn_parser_nonstrict
550 #define jsmn_alloc_token jsmn_alloc_token_nonstrict
551 #define jsmn_fill_token jsmn_fill_token_nonstrict
552 #define jsmn_parse_primitive jsmn_parse_primitive_nonstrict
553 #define jsmn_parse_string jsmn_parse_string_nonstrict
554 #define jsmntype_t jsmntype_nonstrict_t
555 #define jsmnerr_t jsmnerr_nonstrict_t
556 #define jsmntok_t jsmntok_nonstrict_t
557 #define JSMN_PRIMITIVE JSMN_PRIMITIVE_NONSTRICT
558 #define JSMN_OBJECT JSMN_OBJECT_NONSTRICT
559 #define JSMN_ARRAY JSMN_ARRAY_NONSTRICT
560 #define JSMN_STRING JSMN_STRING_NONSTRICT
561 #define JSMN_ERROR_NOMEM JSMN_ERROR_NOMEM_NONSTRICT
562 #define JSMN_ERROR_INVAL JSMN_ERROR_INVAL_NONSTRICT
563 #define JSMN_ERROR_PART JSMN_ERROR_PART_NONSTRICT
568 int test_nonstrict() {
572 jsmntok_t tokens[10];
577 r = jsmn_parse(&p, js, 4, tokens, 10);
579 check(TOKEN_STRING(js, tokens[0], "a"));
580 check(TOKEN_STRING(js, tokens[1], "0"));
582 js = "Day : 26\nMonth : Sep\n\nYear: 12";
584 r = jsmn_parse(&p, js, strlen(js), tokens, 10);
590 test(test_empty, "general test for a empty JSON objects/arrays");
591 test(test_simple, "general test for a simple JSON string");
592 test(test_primitive, "test primitive JSON data types");
593 test(test_string, "test string JSON data types");
594 test(test_partial_string, "test partial JSON string parsing");
595 test(test_partial_array, "test partial array reading");
596 test(test_array_nomem, "test array reading with a smaller number of tokens");
597 test(test_unquoted_keys, "test unquoted keys (like in JavaScript)");
598 test(test_objects_arrays, "test objects and arrays");
599 test(test_unicode_characters, "test unicode characters");
600 test(test_input_length, "test strings that are not null-terminated");
601 test(test_issue_22, "test issue #22");
602 test(test_count, "test tokens count estimation");
603 test(test_nonstrict, "test for non-strict mode");
604 test(test_keyvalue, "test for keys/values");
605 printf("\nPASSED: %d\nFAILED: %d\n", test_passed, test_failed);