tests/test_json.c

changeset 996
333155f234c4
parent 978
1da52af18ac3
child 997
d14f3d5f47d1
equal deleted inserted replaced
995:d3d4f245b843 996:333155f234c4
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 }
56 57
57 CX_TEST_DO { 58 CX_TEST_DO {
58 int result; 59 int result;
59 60
60 CxJson json; 61 CxJson json;
61 cxJsonInit(&json); 62 cxJsonInit(NULL, &json);
62 cxJsonFill(&json, text); 63 cxJsonFill(&json, text);
63 64
64 // parse the big fat object 65 // parse the big fat object
65 CxJsonValue *obj; 66 CxJsonValue *obj;
66 result = cxJsonNext(&json, &obj); 67 result = cxJsonNext(&json, &obj);
123 124
124 CX_TEST_DO { 125 CX_TEST_DO {
125 int result; 126 int result;
126 127
127 CxJson json; 128 CxJson json;
128 cxJsonInit(&json); 129 cxJsonInit(NULL, &json);
129 CxJsonValue *obj; 130 CxJsonValue *obj;
130 131
131 size_t part = 0; 132 size_t part = 0;
132 while(part < nparts) { 133 while(part < nparts) {
133 cxJsonFill(&json, parts[part]); 134 cxJsonFill(&json, parts[part]);
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));
247 } 248 }
248 } 249 }
249 250
250 CX_TEST(test_json_number) { 251 CX_TEST(test_json_number) {
251 CxJson json; 252 CxJson json;
252 cxJsonInit(&json); 253 cxJsonInit(NULL, &json);
253 CX_TEST_DO { 254 CX_TEST_DO {
254 // TODO: find a better way to terminate values that are not arrays/objects 255 // TODO: find a better way to terminate values that are not arrays/objects
255 CxJsonValue *v; 256 CxJsonValue *v;
256 int result; 257 int result;
257 cxJsonFill(&json, "3.1415 "); 258 cxJsonFill(&json, "3.1415 ");
270 cxJsonDestroy(&json); 271 cxJsonDestroy(&json);
271 } 272 }
272 273
273 CX_TEST(test_json_multiple_values) { 274 CX_TEST(test_json_multiple_values) {
274 CxJson json; 275 CxJson json;
275 cxJsonInit(&json); 276 cxJsonInit(NULL, &json);
276 CX_TEST_DO { 277 CX_TEST_DO {
277 CxJsonValue *v; 278 CxJsonValue *v;
278 int result; 279 int result;
279 280
280 // read number 281 // read number
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

mercurial