--- a/tests/test_json.c Thu Nov 28 19:37:00 2024 +0100 +++ b/tests/test_json.c Thu Nov 28 20:53:56 2024 +0100 @@ -29,11 +29,12 @@ #include "cx/test.h" #include "cx/json.h" +#include "cx/mempool.h" CX_TEST(test_json_init_default) { CxJson json; CX_TEST_DO { - cxJsonInit(&json); + cxJsonInit(NULL, &json); CX_TEST_ASSERT(json.states == json.states_internal); CX_TEST_ASSERT(json.nstates == 0); CX_TEST_ASSERT(json.states_alloc == 8); @@ -58,7 +59,7 @@ int result; CxJson json; - cxJsonInit(&json); + cxJsonInit(NULL, &json); cxJsonFill(&json, text); // parse the big fat object @@ -125,7 +126,7 @@ int result; CxJson json; - cxJsonInit(&json); + cxJsonInit(NULL, &json); CxJsonValue *obj; size_t part = 0; @@ -190,7 +191,7 @@ CxJsonValue *obj = NULL; for(int i=0;i<5;i++) { - cxJsonInit(&json); + cxJsonInit(NULL, &json); cxJsonFill(&json, tests[i]); result = cxJsonNext(&json, &obj); @@ -206,7 +207,7 @@ CxJsonValue *d1; cxstring text = cx_str("{\"test\": [{},{\"foo\": [[{\"bar\":[4, 2, [null, {\"key\": 47}]]}]]}]}"); CX_TEST_DO { - cxJsonInit(&json); + cxJsonInit(NULL, &json); cxJsonFill(&json, text); cxJsonNext(&json, &d1); @@ -249,7 +250,7 @@ CX_TEST(test_json_number) { CxJson json; - cxJsonInit(&json); + cxJsonInit(NULL, &json); CX_TEST_DO { // TODO: find a better way to terminate values that are not arrays/objects CxJsonValue *v; @@ -272,7 +273,7 @@ CX_TEST(test_json_multiple_values) { CxJson json; - cxJsonInit(&json); + cxJsonInit(NULL, &json); CX_TEST_DO { CxJsonValue *v; int result; @@ -332,6 +333,36 @@ cxJsonDestroy(&json); } +CX_TEST(test_json_allocator) { + CxMempool *mp = cxMempoolCreate(64, NULL); + CxJson json; + cxJsonInit(mp->allocator, &json); + + cxstring text = cx_str( + "{\n" + "\t\"message\":\"success\",\n" + "\t\"data\":[\"value1\",{\"x\":123, \"y\":523 }]\n" + "}" + ); + + CX_TEST_DO { + int result; + + CxJson json; + cxJsonInit(mp->allocator, &json); + cxJsonFill(&json, text); + + CxJsonValue *obj; + result = cxJsonNext(&json, &obj); + CX_TEST_ASSERT(result == 1); + CX_TEST_ASSERT(obj->allocator == mp->allocator); + + // this recursively frees everything + cxJsonValueFree(obj); + cxJsonDestroy(&json); + } +} + CxTestSuite *cx_test_suite_json(void) { CxTestSuite *suite = cx_test_suite_new("json"); @@ -342,6 +373,7 @@ cx_test_register(suite, test_json_large_nesting_depth); cx_test_register(suite, test_json_number); cx_test_register(suite, test_json_multiple_values); + cx_test_register(suite, test_json_allocator); return suite; }