--- a/tests/test_json.c Thu Dec 05 01:51:47 2024 +0100 +++ b/tests/test_json.c Thu Dec 05 01:54:12 2024 +0100 @@ -26,6 +26,7 @@ * POSSIBILITY OF SUCH DAMAGE. */ +#include "util_allocator.h" #include "cx/test.h" #include "cx/json.h" @@ -34,11 +35,13 @@ CX_TEST(test_json_init_default) { CxJson json; CX_TEST_DO { - cxJsonInit(NULL, &json); + cxJsonInit(&json, NULL); 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_ASSERT(json.states_size == 1); + CX_TEST_ASSERT(json.states_capacity >= 8); + CX_TEST_ASSERT(json.vbuf == json.vbuf_internal); + CX_TEST_ASSERT(json.vbuf_size == 0); + CX_TEST_ASSERT(json.vbuf_capacity >= 8); } } @@ -59,7 +62,7 @@ int result; CxJson json; - cxJsonInit(NULL, &json); + cxJsonInit(&json, NULL); cxJsonFill(&json, text); // parse the big fat object @@ -126,7 +129,7 @@ int result; CxJson json; - cxJsonInit(NULL, &json); + cxJsonInit(&json, NULL); CxJsonValue *obj; size_t part = 0; @@ -191,12 +194,12 @@ CxJsonValue *obj = NULL; for(int i=0;i<5;i++) { - cxJsonInit(NULL, &json); + cxJsonInit(&json, NULL); cxJsonFill(&json, tests[i]); result = cxJsonNext(&json, &obj); CX_TEST_ASSERT(result == -1); - CX_TEST_ASSERT(obj == NULL); + CX_TEST_ASSERT(obj != NULL && obj->type == CX_JSON_NOTHING); cxJsonDestroy(&json); } } @@ -207,7 +210,7 @@ CxJsonValue *d1; cxstring text = cx_str("{\"test\": [{},{\"foo\": [[{\"bar\":[4, 2, [null, {\"key\": 47}]]}]]}]}"); CX_TEST_DO { - cxJsonInit(NULL, &json); + cxJsonInit(&json, NULL); cxJsonFill(&json, text); cxJsonNext(&json, &d1); @@ -241,7 +244,7 @@ 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)); + CX_TEST_ASSERT(json.states_capacity > cx_nmemb(json.states_internal)); cxJsonValueFree(d1); cxJsonDestroy(&json); @@ -250,7 +253,7 @@ CX_TEST(test_json_number) { CxJson json; - cxJsonInit(NULL, &json); + cxJsonInit(&json, NULL); CX_TEST_DO { // TODO: find a better way to terminate values that are not arrays/objects CxJsonValue *v; @@ -273,7 +276,7 @@ CX_TEST(test_json_multiple_values) { CxJson json; - cxJsonInit(NULL, &json); + cxJsonInit(&json, NULL); CX_TEST_DO { CxJsonValue *v; int result; @@ -334,10 +337,10 @@ } CX_TEST(test_json_allocator) { - CxMempool *mp = cxMempoolCreate(64, NULL); - CxJson json; - cxJsonInit(mp->allocator, &json); - + CxTestingAllocator talloc; + cx_testing_allocator_init(&talloc); + CxAllocator *allocator = &talloc.base; + cxstring text = cx_str( "{\n" "\t\"message\":\"success\",\n" @@ -346,22 +349,52 @@ ); CX_TEST_DO { - int result; - CxJson json; - cxJsonInit(mp->allocator, &json); + cxJsonInit(&json, allocator); cxJsonFill(&json, text); CxJsonValue *obj; - result = cxJsonNext(&json, &obj); + int result = cxJsonNext(&json, &obj); CX_TEST_ASSERT(result == 1); - CX_TEST_ASSERT(obj->allocator == mp->allocator); + CX_TEST_ASSERT(obj->allocator == allocator); // this recursively frees everything cxJsonValueFree(obj); cxJsonDestroy(&json); - cxMempoolFree(mp); + + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); } + cx_testing_allocator_destroy(&talloc); +} + +CX_TEST(test_json_allocator_parse_error) { + CxTestingAllocator talloc; + cx_testing_allocator_init(&talloc); + CxAllocator *allocator = &talloc.base; + + cxstring text = cx_str( + "{\n" + "\t\"message\":\"success\"\n" // <-- missing comma + "\t\"data\":[\"value1\",{\"x\":123, \"y\":523 }]\n" + "}" + ); + + CX_TEST_DO { + CxJson json; + cxJsonInit(&json, allocator); + cxJsonFill(&json, text); + + CxJsonValue *obj = NULL; + int result = cxJsonNext(&json, &obj); + CX_TEST_ASSERT(result == -1); + CX_TEST_ASSERT(obj != NULL && obj->type == CX_JSON_NOTHING); + + // clean-up any left-over memory + cxJsonDestroy(&json); + + CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); + } + cx_testing_allocator_destroy(&talloc); } CxTestSuite *cx_test_suite_json(void) { @@ -375,6 +408,7 @@ cx_test_register(suite, test_json_number); cx_test_register(suite, test_json_multiple_values); cx_test_register(suite, test_json_allocator); + cx_test_register(suite, test_json_allocator_parse_error); return suite; }