tests/test_json.c

changeset 946
b428424c0214
parent 941
9077724b75a0
equal deleted inserted replaced
945:84a5fab8a47c 946:b428424c0214
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
33 CX_TEST(test_json_init_default) {
34 CxJson json;
35 CX_TEST_DO {
36 cxJsonInit(&json);
37 CX_TEST_ASSERT(json.states == json.states_internal);
38 CX_TEST_ASSERT(json.nstates == 0);
39 CX_TEST_ASSERT(json.states_alloc == 8);
40 CX_TEST_ASSERT(json.reader_array_alloc == 8);
41 }
42 }
32 43
33 CX_TEST(test_json_simple_object) { 44 CX_TEST(test_json_simple_object) {
34 cxstring text = cx_str( 45 cxstring text = cx_str(
35 "{\n" 46 "{\n"
36 "\t\"message\":\"success\",\n" 47 "\t\"message\":\"success\",\n"
182 cxJsonDestroy(&json); 193 cxJsonDestroy(&json);
183 } 194 }
184 } 195 }
185 } 196 }
186 197
198 CX_TEST(test_json_large_nesting_depth) {
199 CxJson json;
200 CxJsonValue *d1;
201 cxstring text = cx_str("{\"test\": [{},{\"foo\": [[{\"bar\":[4, 2, [null, {\"key\": 47}]]}]]}]}");
202 CX_TEST_DO {
203 cxJsonInit(&json);
204 cxJsonFill(&json, text.ptr, text.length);
205 cxJsonNext(&json, &d1);
206
207 CX_TEST_ASSERT(d1 != NULL);
208 CX_TEST_ASSERT(cxJsonIsObject(d1));
209 CxJsonValue *d2 = cxJsonObjGet(d1, "test");
210 CX_TEST_ASSERT(cxJsonIsArray(d2));
211 CX_TEST_ASSERT(cxJsonArrSize(d2) == 2);
212 CxJsonValue *d3 = cxJsonArrGet(d2, 1);
213 CX_TEST_ASSERT(cxJsonIsObject(d3));
214 CxJsonValue *d4 = cxJsonObjGet(d3, "foo");
215 CX_TEST_ASSERT(cxJsonIsArray(d4));
216 CX_TEST_ASSERT(cxJsonArrSize(d4) == 1);
217 CxJsonValue *d5 = cxJsonArrGet(d4, 0);
218 CX_TEST_ASSERT(cxJsonIsArray(d5));
219 CX_TEST_ASSERT(cxJsonArrSize(d5) == 1);
220 CxJsonValue *d6 = cxJsonArrGet(d5, 0);
221 CX_TEST_ASSERT(cxJsonIsObject(d6));
222 CxJsonValue *d7 = cxJsonObjGet(d6, "bar");
223 CX_TEST_ASSERT(cxJsonIsArray(d7));
224 CX_TEST_ASSERT(cxJsonArrSize(d7) == 3);
225 CxJsonValue *d8 = cxJsonArrGet(d7, 2);
226 CX_TEST_ASSERT(cxJsonIsArray(d8));
227 CX_TEST_ASSERT(cxJsonArrSize(d8) == 2);
228 CxJsonValue *d9a = cxJsonArrGet(d8, 0);
229 CX_TEST_ASSERT(cxJsonIsNull(d9a));
230 CxJsonValue *d9b = cxJsonArrGet(d8, 1);
231 CX_TEST_ASSERT(cxJsonIsObject(d9b));
232 CxJsonValue *d10 = cxJsonObjGet(d9b, "key");
233 CX_TEST_ASSERT(cxJsonIsInteger(d10));
234 CX_TEST_ASSERT(cxJsonAsInteger(d10) == 47);
235
236 CX_TEST_ASSERT(json.states != json.states_internal);
237 CX_TEST_ASSERT(json.states_alloc > cx_nmemb(json.states_internal));
238
239 cxJsonDestroy(&json);
240 }
241 }
242
187 CxTestSuite *cx_test_suite_json(void) { 243 CxTestSuite *cx_test_suite_json(void) {
188 CxTestSuite *suite = cx_test_suite_new("json"); 244 CxTestSuite *suite = cx_test_suite_new("json");
189 245
246 cx_test_register(suite, test_json_init_default);
190 cx_test_register(suite, test_json_simple_object); 247 cx_test_register(suite, test_json_simple_object);
191 cx_test_register(suite, test_json_object_incomplete_token); 248 cx_test_register(suite, test_json_object_incomplete_token);
192 cx_test_register(suite, test_json_object_error); 249 cx_test_register(suite, test_json_object_error);
250 cx_test_register(suite, test_json_large_nesting_depth);
193 251
194 return suite; 252 return suite;
195 } 253 }
196 254

mercurial