27 */ |
27 */ |
28 |
28 |
29 #include "cx/test.h" |
29 #include "cx/test.h" |
30 |
30 |
31 #include "cx/json.h" |
31 #include "cx/json.h" |
|
32 #include "cx/mempool.h" |
32 |
33 |
33 CX_TEST(test_json_init_default) { |
34 CX_TEST(test_json_init_default) { |
34 CxJson json; |
35 CxJson json; |
35 CX_TEST_DO { |
36 CX_TEST_DO { |
36 cxJsonInit(&json); |
37 cxJsonInit(NULL, &json); |
37 CX_TEST_ASSERT(json.states == json.states_internal); |
38 CX_TEST_ASSERT(json.states == json.states_internal); |
38 CX_TEST_ASSERT(json.nstates == 0); |
39 CX_TEST_ASSERT(json.nstates == 0); |
39 CX_TEST_ASSERT(json.states_alloc == 8); |
40 CX_TEST_ASSERT(json.states_alloc == 8); |
40 CX_TEST_ASSERT(json.reader_array_alloc == 8); |
41 CX_TEST_ASSERT(json.reader_array_alloc == 8); |
41 } |
42 } |
188 int result; |
189 int result; |
189 CxJson json; |
190 CxJson json; |
190 CxJsonValue *obj = NULL; |
191 CxJsonValue *obj = NULL; |
191 |
192 |
192 for(int i=0;i<5;i++) { |
193 for(int i=0;i<5;i++) { |
193 cxJsonInit(&json); |
194 cxJsonInit(NULL, &json); |
194 cxJsonFill(&json, tests[i]); |
195 cxJsonFill(&json, tests[i]); |
195 result = cxJsonNext(&json, &obj); |
196 result = cxJsonNext(&json, &obj); |
196 |
197 |
197 CX_TEST_ASSERT(result == -1); |
198 CX_TEST_ASSERT(result == -1); |
198 CX_TEST_ASSERT(obj == NULL); |
199 CX_TEST_ASSERT(obj == NULL); |
204 CX_TEST(test_json_large_nesting_depth) { |
205 CX_TEST(test_json_large_nesting_depth) { |
205 CxJson json; |
206 CxJson json; |
206 CxJsonValue *d1; |
207 CxJsonValue *d1; |
207 cxstring text = cx_str("{\"test\": [{},{\"foo\": [[{\"bar\":[4, 2, [null, {\"key\": 47}]]}]]}]}"); |
208 cxstring text = cx_str("{\"test\": [{},{\"foo\": [[{\"bar\":[4, 2, [null, {\"key\": 47}]]}]]}]}"); |
208 CX_TEST_DO { |
209 CX_TEST_DO { |
209 cxJsonInit(&json); |
210 cxJsonInit(NULL, &json); |
210 cxJsonFill(&json, text); |
211 cxJsonFill(&json, text); |
211 cxJsonNext(&json, &d1); |
212 cxJsonNext(&json, &d1); |
212 |
213 |
213 CX_TEST_ASSERT(d1 != NULL); |
214 CX_TEST_ASSERT(d1 != NULL); |
214 CX_TEST_ASSERT(cxJsonIsObject(d1)); |
215 CX_TEST_ASSERT(cxJsonIsObject(d1)); |
330 cxJsonValueFree(v); |
331 cxJsonValueFree(v); |
331 } |
332 } |
332 cxJsonDestroy(&json); |
333 cxJsonDestroy(&json); |
333 } |
334 } |
334 |
335 |
|
336 CX_TEST(test_json_allocator) { |
|
337 CxMempool *mp = cxMempoolCreate(64, NULL); |
|
338 CxJson json; |
|
339 cxJsonInit(mp->allocator, &json); |
|
340 |
|
341 cxstring text = cx_str( |
|
342 "{\n" |
|
343 "\t\"message\":\"success\",\n" |
|
344 "\t\"data\":[\"value1\",{\"x\":123, \"y\":523 }]\n" |
|
345 "}" |
|
346 ); |
|
347 |
|
348 CX_TEST_DO { |
|
349 int result; |
|
350 |
|
351 CxJson json; |
|
352 cxJsonInit(mp->allocator, &json); |
|
353 cxJsonFill(&json, text); |
|
354 |
|
355 CxJsonValue *obj; |
|
356 result = cxJsonNext(&json, &obj); |
|
357 CX_TEST_ASSERT(result == 1); |
|
358 CX_TEST_ASSERT(obj->allocator == mp->allocator); |
|
359 |
|
360 // this recursively frees everything |
|
361 cxJsonValueFree(obj); |
|
362 cxJsonDestroy(&json); |
|
363 } |
|
364 } |
|
365 |
335 CxTestSuite *cx_test_suite_json(void) { |
366 CxTestSuite *cx_test_suite_json(void) { |
336 CxTestSuite *suite = cx_test_suite_new("json"); |
367 CxTestSuite *suite = cx_test_suite_new("json"); |
337 |
368 |
338 cx_test_register(suite, test_json_init_default); |
369 cx_test_register(suite, test_json_init_default); |
339 cx_test_register(suite, test_json_simple_object); |
370 cx_test_register(suite, test_json_simple_object); |
340 cx_test_register(suite, test_json_object_incomplete_token); |
371 cx_test_register(suite, test_json_object_incomplete_token); |
341 cx_test_register(suite, test_json_object_error); |
372 cx_test_register(suite, test_json_object_error); |
342 cx_test_register(suite, test_json_large_nesting_depth); |
373 cx_test_register(suite, test_json_large_nesting_depth); |
343 cx_test_register(suite, test_json_number); |
374 cx_test_register(suite, test_json_number); |
344 cx_test_register(suite, test_json_multiple_values); |
375 cx_test_register(suite, test_json_multiple_values); |
|
376 cx_test_register(suite, test_json_allocator); |
345 |
377 |
346 return suite; |
378 return suite; |
347 } |
379 } |
348 |
380 |