00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004
00005 #include "jsmn.c"
00006
00007 static int test_passed = 0;
00008 static int test_failed = 0;
00009
00010
00011 #define fail() return __LINE__
00012
00013
00014 #define done() return 0
00015
00016
00017 #define check(cond) do { if (!(cond)) fail(); } while (0)
00018
00019
00020 static void test(int (*func)(void), const char *name) {
00021 int r = func();
00022 if (r == 0) {
00023 test_passed++;
00024 } else {
00025 test_failed++;
00026 printf("FAILED: %s (at line %d)\n", name, r);
00027 }
00028 }
00029
00030 #define TOKEN_EQ(t, tok_start, tok_end, tok_type) \
00031 ((t).start == tok_start \
00032 && (t).end == tok_end \
00033 && (t).type == (tok_type))
00034
00035 #define TOKEN_STRING(js, t, s) \
00036 (strncmp(js+(t).start, s, (t).end - (t).start) == 0 \
00037 && strlen(s) == (t).end - (t).start)
00038
00039 #define TOKEN_PRINT(t) \
00040 printf("start: %d, end: %d, type: %d, size: %d\n", \
00041 (t).start, (t).end, (t).type, (t).size)
00042
00043 int test_empty() {
00044 const char *js;
00045 int r;
00046 jsmn_parser p;
00047 jsmntok_t t[10];
00048
00049 js = "{}";
00050 jsmn_init(&p);
00051 r = jsmn_parse(&p, js, t, 10);
00052 check(r == JSMN_SUCCESS);
00053 check(t[0].type == JSMN_OBJECT);
00054 check(t[0].start == 0 && t[0].end == 2);
00055
00056 js = "[]";
00057 jsmn_init(&p);
00058 r = jsmn_parse(&p, js, t, 10);
00059 check(r == JSMN_SUCCESS);
00060 check(t[0].type == JSMN_ARRAY);
00061 check(t[0].start == 0 && t[0].end == 2);
00062
00063 js = "{\"a\":[]}";
00064 jsmn_init(&p);
00065 r = jsmn_parse(&p, js, t, 10);
00066 check(r == JSMN_SUCCESS);
00067 check(t[0].type == JSMN_OBJECT && t[0].start == 0 && t[0].end == 8);
00068 check(t[1].type == JSMN_STRING && t[1].start == 2 && t[1].end == 3);
00069 check(t[2].type == JSMN_ARRAY && t[2].start == 5 && t[2].end == 7);
00070
00071 js = "[{},{}]";
00072 jsmn_init(&p);
00073 r = jsmn_parse(&p, js, t, 10);
00074 check(r == JSMN_SUCCESS);
00075 check(t[0].type == JSMN_ARRAY && t[0].start == 0 && t[0].end == 7);
00076 check(t[1].type == JSMN_OBJECT && t[1].start == 1 && t[1].end == 3);
00077 check(t[2].type == JSMN_OBJECT && t[2].start == 4 && t[2].end == 6);
00078 return 0;
00079 }
00080
00081 int test_simple() {
00082 const char *js;
00083 int r;
00084 jsmn_parser p;
00085 jsmntok_t tokens[10];
00086
00087 js = "{\"a\": 0}";
00088
00089 jsmn_init(&p);
00090 r = jsmn_parse(&p, js, tokens, 10);
00091 check(r == JSMN_SUCCESS);
00092 check(TOKEN_EQ(tokens[0], 0, 8, JSMN_OBJECT));
00093 check(TOKEN_EQ(tokens[1], 2, 3, JSMN_STRING));
00094 check(TOKEN_EQ(tokens[2], 6, 7, JSMN_PRIMITIVE));
00095
00096 check(TOKEN_STRING(js, tokens[0], js));
00097 check(TOKEN_STRING(js, tokens[1], "a"));
00098 check(TOKEN_STRING(js, tokens[2], "0"));
00099
00100 jsmn_init(&p);
00101 js = "[\"a\":{},\"b\":{}]";
00102 r = jsmn_parse(&p, js, tokens, 10);
00103 check(r == JSMN_SUCCESS);
00104
00105 jsmn_init(&p);
00106 js = "{\n \"Day\": 26,\n \"Month\": 9,\n \"Year\": 12\n }";
00107 r = jsmn_parse(&p, js, tokens, 10);
00108 check(r == JSMN_SUCCESS);
00109
00110 return 0;
00111 }
00112
00113 int test_primitive() {
00114 int r;
00115 jsmn_parser p;
00116 jsmntok_t tok[10];
00117 const char *js;
00118 #ifndef JSMN_STRICT
00119 js = "\"boolVar\" : true";
00120 jsmn_init(&p);
00121 r = jsmn_parse(&p, js, tok, 10);
00122 check(r == JSMN_SUCCESS && tok[0].type == JSMN_STRING
00123 && tok[1].type == JSMN_PRIMITIVE);
00124 check(TOKEN_STRING(js, tok[0], "boolVar"));
00125 check(TOKEN_STRING(js, tok[1], "true"));
00126
00127 js = "\"boolVar\" : false";
00128 jsmn_init(&p);
00129 r = jsmn_parse(&p, js, tok, 10);
00130 check(r == JSMN_SUCCESS && tok[0].type == JSMN_STRING
00131 && tok[1].type == JSMN_PRIMITIVE);
00132 check(TOKEN_STRING(js, tok[0], "boolVar"));
00133 check(TOKEN_STRING(js, tok[1], "false"));
00134
00135 js = "\"intVar\" : 12345";
00136 jsmn_init(&p);
00137 r = jsmn_parse(&p, js, tok, 10);
00138 check(r == JSMN_SUCCESS && tok[0].type == JSMN_STRING
00139 && tok[1].type == JSMN_PRIMITIVE);
00140 check(TOKEN_STRING(js, tok[0], "intVar"));
00141 check(TOKEN_STRING(js, tok[1], "12345"));
00142
00143 js = "\"floatVar\" : 12.345";
00144 jsmn_init(&p);
00145 r = jsmn_parse(&p, js, tok, 10);
00146 check(r == JSMN_SUCCESS && tok[0].type == JSMN_STRING
00147 && tok[1].type == JSMN_PRIMITIVE);
00148 check(TOKEN_STRING(js, tok[0], "floatVar"));
00149 check(TOKEN_STRING(js, tok[1], "12.345"));
00150
00151 js = "\"nullVar\" : null";
00152 jsmn_init(&p);
00153 r = jsmn_parse(&p, js, tok, 10);
00154 check(r == JSMN_SUCCESS && tok[0].type == JSMN_STRING
00155 && tok[1].type == JSMN_PRIMITIVE);
00156 check(TOKEN_STRING(js, tok[0], "nullVar"));
00157 check(TOKEN_STRING(js, tok[1], "null"));
00158 #endif
00159 return 0;
00160 }
00161
00162 int test_string() {
00163 int r;
00164 jsmn_parser p;
00165 jsmntok_t tok[10];
00166 const char *js;
00167
00168 js = "\"strVar\" : \"hello world\"";
00169 jsmn_init(&p);
00170 r = jsmn_parse(&p, js, tok, 10);
00171 check(r == JSMN_SUCCESS && tok[0].type == JSMN_STRING
00172 && tok[1].type == JSMN_STRING);
00173 check(TOKEN_STRING(js, tok[0], "strVar"));
00174 check(TOKEN_STRING(js, tok[1], "hello world"));
00175
00176 js = "\"strVar\" : \"escapes: \\/\\r\\n\\t\\b\\f\\\"\\\\\"";
00177 jsmn_init(&p);
00178 r = jsmn_parse(&p, js, tok, 10);
00179 check(r == JSMN_SUCCESS && tok[0].type == JSMN_STRING
00180 && tok[1].type == JSMN_STRING);
00181 check(TOKEN_STRING(js, tok[0], "strVar"));
00182 check(TOKEN_STRING(js, tok[1], "escapes: \\/\\r\\n\\t\\b\\f\\\"\\\\"));
00183
00184 js = "\"strVar\" : \"\"";
00185 jsmn_init(&p);
00186 r = jsmn_parse(&p, js, tok, 10);
00187 check(r == JSMN_SUCCESS && tok[0].type == JSMN_STRING
00188 && tok[1].type == JSMN_STRING);
00189 check(TOKEN_STRING(js, tok[0], "strVar"));
00190 check(TOKEN_STRING(js, tok[1], ""));
00191
00192 return 0;
00193 }
00194
00195 int test_partial_string() {
00196 int r;
00197 jsmn_parser p;
00198 jsmntok_t tok[10];
00199 const char *js;
00200
00201 jsmn_init(&p);
00202 js = "\"x\": \"va";
00203 r = jsmn_parse(&p, js, tok, 10);
00204 check(r == JSMN_ERROR_PART && tok[0].type == JSMN_STRING);
00205 check(TOKEN_STRING(js, tok[0], "x"));
00206 check(p.toknext == 1);
00207
00208 js = "\"x\": \"valu";
00209 r = jsmn_parse(&p, js, tok, 10);
00210 check(r == JSMN_ERROR_PART && tok[0].type == JSMN_STRING);
00211 check(TOKEN_STRING(js, tok[0], "x"));
00212 check(p.toknext == 1);
00213
00214 js = "\"x\": \"value\"";
00215 r = jsmn_parse(&p, js, tok, 10);
00216 check(r == JSMN_SUCCESS && tok[0].type == JSMN_STRING
00217 && tok[1].type == JSMN_STRING);
00218 check(TOKEN_STRING(js, tok[0], "x"));
00219 check(TOKEN_STRING(js, tok[1], "value"));
00220
00221 js = "\"x\": \"value\", \"y\": \"value y\"";
00222 r = jsmn_parse(&p, js, tok, 10);
00223 check(r == JSMN_SUCCESS && tok[0].type == JSMN_STRING
00224 && tok[1].type == JSMN_STRING && tok[2].type == JSMN_STRING
00225 && tok[3].type == JSMN_STRING);
00226 check(TOKEN_STRING(js, tok[0], "x"));
00227 check(TOKEN_STRING(js, tok[1], "value"));
00228 check(TOKEN_STRING(js, tok[2], "y"));
00229 check(TOKEN_STRING(js, tok[3], "value y"));
00230
00231 return 0;
00232 }
00233
00234 int test_unquoted_keys() {
00235 #ifndef JSMN_STRICT
00236 int r;
00237 jsmn_parser p;
00238 jsmntok_t tok[10];
00239 const char *js;
00240
00241 jsmn_init(&p);
00242 js = "key1: \"value\"\nkey2 : 123";
00243
00244 r = jsmn_parse(&p, js, tok, 10);
00245 check(r == JSMN_SUCCESS && tok[0].type == JSMN_PRIMITIVE
00246 && tok[1].type == JSMN_STRING && tok[2].type == JSMN_PRIMITIVE
00247 && tok[3].type == JSMN_PRIMITIVE);
00248 check(TOKEN_STRING(js, tok[0], "key1"));
00249 check(TOKEN_STRING(js, tok[1], "value"));
00250 check(TOKEN_STRING(js, tok[2], "key2"));
00251 check(TOKEN_STRING(js, tok[3], "123"));
00252 #endif
00253 return 0;
00254 }
00255
00256 int test_partial_array() {
00257 int r;
00258 jsmn_parser p;
00259 jsmntok_t tok[10];
00260 const char *js;
00261
00262 jsmn_init(&p);
00263 js = " [ 1, true, ";
00264 r = jsmn_parse(&p, js, tok, 10);
00265 check(r == JSMN_ERROR_PART && tok[0].type == JSMN_ARRAY
00266 && tok[1].type == JSMN_PRIMITIVE && tok[2].type == JSMN_PRIMITIVE);
00267
00268 js = " [ 1, true, [123, \"hello";
00269 r = jsmn_parse(&p, js, tok, 10);
00270 check(r == JSMN_ERROR_PART && tok[0].type == JSMN_ARRAY
00271 && tok[1].type == JSMN_PRIMITIVE && tok[2].type == JSMN_PRIMITIVE
00272 && tok[3].type == JSMN_ARRAY && tok[4].type == JSMN_PRIMITIVE);
00273
00274 js = " [ 1, true, [123, \"hello\"]";
00275 r = jsmn_parse(&p, js, tok, 10);
00276 check(r == JSMN_ERROR_PART && tok[0].type == JSMN_ARRAY
00277 && tok[1].type == JSMN_PRIMITIVE && tok[2].type == JSMN_PRIMITIVE
00278 && tok[3].type == JSMN_ARRAY && tok[4].type == JSMN_PRIMITIVE
00279 && tok[5].type == JSMN_STRING);
00280
00281 check(tok[3].size == 2);
00282
00283 js = " [ 1, true, [123, \"hello\"]]";
00284 r = jsmn_parse(&p, js, tok, 10);
00285 check(r == JSMN_SUCCESS && tok[0].type == JSMN_ARRAY
00286 && tok[1].type == JSMN_PRIMITIVE && tok[2].type == JSMN_PRIMITIVE
00287 && tok[3].type == JSMN_ARRAY && tok[4].type == JSMN_PRIMITIVE
00288 && tok[5].type == JSMN_STRING);
00289 check(tok[3].size == 2);
00290 check(tok[0].size == 3);
00291 return 0;
00292 }
00293
00294 int test_array_nomem() {
00295 int i;
00296 int r;
00297 jsmn_parser p;
00298 jsmntok_t toksmall[10], toklarge[10];
00299 const char *js;
00300
00301 js = " [ 1, true, [123, \"hello\"]]";
00302
00303 for (i = 0; i < 6; i++) {
00304 jsmn_init(&p);
00305 memset(toksmall, 0, sizeof(toksmall));
00306 memset(toklarge, 0, sizeof(toklarge));
00307 r = jsmn_parse(&p, js, toksmall, i);
00308 check(r == JSMN_ERROR_NOMEM);
00309
00310 memcpy(toklarge, toksmall, sizeof(toksmall));
00311
00312 r = jsmn_parse(&p, js, toklarge, 10);
00313 check(r == JSMN_SUCCESS);
00314
00315 check(toklarge[0].type == JSMN_ARRAY && toklarge[0].size == 3);
00316 check(toklarge[3].type == JSMN_ARRAY && toklarge[3].size == 2);
00317 }
00318 return 0;
00319 }
00320
00321 int test_objects_arrays() {
00322 int i;
00323 int r;
00324 jsmn_parser p;
00325 jsmntok_t tokens[10];
00326 const char *js;
00327
00328 js = "[10}";
00329 jsmn_init(&p);
00330 r = jsmn_parse(&p, js, tokens, 10);
00331 check(r == JSMN_ERROR_INVAL);
00332
00333 js = "[10]";
00334 jsmn_init(&p);
00335 r = jsmn_parse(&p, js, tokens, 10);
00336 check(r == JSMN_SUCCESS);
00337
00338 js = "{\"a\": 1]";
00339 jsmn_init(&p);
00340 r = jsmn_parse(&p, js, tokens, 10);
00341 check(r == JSMN_ERROR_INVAL);
00342
00343 js = "{\"a\": 1}";
00344 jsmn_init(&p);
00345 r = jsmn_parse(&p, js, tokens, 10);
00346 check(r == JSMN_SUCCESS);
00347
00348 return 0;
00349 }
00350
00351 int main() {
00352 test(test_empty, "general test for a empty JSON objects/arrays");
00353 test(test_simple, "general test for a simple JSON string");
00354 test(test_primitive, "test primitive JSON data types");
00355 test(test_string, "test string JSON data types");
00356 test(test_partial_string, "test partial JSON string parsing");
00357 test(test_partial_array, "test partial array reading");
00358 test(test_array_nomem, "test array reading with a smaller number of tokens");
00359 test(test_unquoted_keys, "test unquoted keys (like in JavaScript)");
00360 test(test_objects_arrays, "test objects and arrays");
00361 printf("\nPASSED: %d\nFAILED: %d\n", test_passed, test_failed);
00362 return 0;
00363 }
00364