--- a/tests/test_json.c Tue Oct 22 22:42:48 2024 +0200 +++ b/tests/test_json.c Tue Oct 22 23:10:31 2024 +0200 @@ -30,6 +30,17 @@ #include "cx/json.h" +CX_TEST(test_json_init_default) { + CxJson json; + CX_TEST_DO { + cxJsonInit(&json); + CX_TEST_ASSERT(json.states == json.states_internal); + CX_TEST_ASSERT(json.nstates == 0); + CX_TEST_ASSERT(json.states_alloc == 8); + CX_TEST_ASSERT(json.reader_array_alloc == 8); + } +} + CX_TEST(test_json_simple_object) { cxstring text = cx_str( "{\n" @@ -184,12 +195,59 @@ } } +CX_TEST(test_json_large_nesting_depth) { + CxJson json; + CxJsonValue *d1; + cxstring text = cx_str("{\"test\": [{},{\"foo\": [[{\"bar\":[4, 2, [null, {\"key\": 47}]]}]]}]}"); + CX_TEST_DO { + cxJsonInit(&json); + cxJsonFill(&json, text.ptr, text.length); + cxJsonNext(&json, &d1); + + CX_TEST_ASSERT(d1 != NULL); + CX_TEST_ASSERT(cxJsonIsObject(d1)); + CxJsonValue *d2 = cxJsonObjGet(d1, "test"); + CX_TEST_ASSERT(cxJsonIsArray(d2)); + CX_TEST_ASSERT(cxJsonArrSize(d2) == 2); + CxJsonValue *d3 = cxJsonArrGet(d2, 1); + CX_TEST_ASSERT(cxJsonIsObject(d3)); + CxJsonValue *d4 = cxJsonObjGet(d3, "foo"); + CX_TEST_ASSERT(cxJsonIsArray(d4)); + CX_TEST_ASSERT(cxJsonArrSize(d4) == 1); + CxJsonValue *d5 = cxJsonArrGet(d4, 0); + CX_TEST_ASSERT(cxJsonIsArray(d5)); + CX_TEST_ASSERT(cxJsonArrSize(d5) == 1); + CxJsonValue *d6 = cxJsonArrGet(d5, 0); + CX_TEST_ASSERT(cxJsonIsObject(d6)); + CxJsonValue *d7 = cxJsonObjGet(d6, "bar"); + CX_TEST_ASSERT(cxJsonIsArray(d7)); + CX_TEST_ASSERT(cxJsonArrSize(d7) == 3); + CxJsonValue *d8 = cxJsonArrGet(d7, 2); + CX_TEST_ASSERT(cxJsonIsArray(d8)); + CX_TEST_ASSERT(cxJsonArrSize(d8) == 2); + CxJsonValue *d9a = cxJsonArrGet(d8, 0); + CX_TEST_ASSERT(cxJsonIsNull(d9a)); + CxJsonValue *d9b = cxJsonArrGet(d8, 1); + CX_TEST_ASSERT(cxJsonIsObject(d9b)); + CxJsonValue *d10 = cxJsonObjGet(d9b, "key"); + CX_TEST_ASSERT(cxJsonIsInteger(d10)); + CX_TEST_ASSERT(cxJsonAsInteger(d10) == 47); + + CX_TEST_ASSERT(json.states != json.states_internal); + CX_TEST_ASSERT(json.states_alloc > cx_nmemb(json.states_internal)); + + cxJsonDestroy(&json); + } +} + CxTestSuite *cx_test_suite_json(void) { CxTestSuite *suite = cx_test_suite_new("json"); + cx_test_register(suite, test_json_init_default); cx_test_register(suite, test_json_simple_object); cx_test_register(suite, test_json_object_incomplete_token); cx_test_register(suite, test_json_object_error); + cx_test_register(suite, test_json_large_nesting_depth); return suite; }