703 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
703 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
704 } |
704 } |
705 cx_testing_allocator_destroy(&talloc); |
705 cx_testing_allocator_destroy(&talloc); |
706 } |
706 } |
707 |
707 |
|
708 CX_TEST(test_json_write_default_format) { |
|
709 CxTestingAllocator talloc; |
|
710 cx_testing_allocator_init(&talloc); |
|
711 CxAllocator *allocator = &talloc.base; |
|
712 CX_TEST_DO { |
|
713 // expected value |
|
714 cxstring expected = CX_STR("{\"bool\":false,\"nested\":{\"floats\":[3.1415,47.11,8.15],\"ints\":[4,8,15,16,23,42],\"literals\":[true,null,false],\"string\":\"test\"},\"num\":47.11,\"strings\":[\"hello\",\"world\"]}"); |
|
715 |
|
716 // create the value |
|
717 CxJsonValue *obj = cxJsonCreateObj(allocator); |
|
718 cxJsonObjPutLiteral(obj, CX_STR("bool"), CX_JSON_FALSE); |
|
719 cxJsonObjPutNumber(obj, CX_STR("num"), 47.11); |
|
720 CxJsonValue *strings = cxJsonObjPutArr(obj, CX_STR("strings")); |
|
721 cxJsonArrAddCxStrings(strings, (cxstring[]) {CX_STR("hello"), CX_STR("world")}, 2); |
|
722 CxJsonValue *nested = cxJsonObjPutObj(obj, CX_STR("nested")); |
|
723 cxJsonObjPutString(nested, CX_STR("string"), "test"); |
|
724 cxJsonArrAddNumbers(cxJsonObjPutArr(nested, CX_STR("floats")), |
|
725 (double[]){3.1415, 47.11, 8.15}, 3); |
|
726 cxJsonArrAddLiterals(cxJsonObjPutArr(nested, CX_STR("literals")), |
|
727 (CxJsonLiteral[]){CX_JSON_TRUE, CX_JSON_NULL, CX_JSON_FALSE}, 3); |
|
728 cxJsonArrAddIntegers(cxJsonObjPutArr(nested, CX_STR("ints")), |
|
729 (int64_t[]){4, 8, 15, 16, 23, 42}, 6); |
|
730 |
|
731 // write it to a buffer |
|
732 CxBuffer buf; |
|
733 cxBufferInit(&buf, NULL, 256, NULL, CX_BUFFER_DEFAULT); |
|
734 int result = cxJsonWrite(&buf, obj, (cx_write_func) cxBufferWrite, NULL); |
|
735 CX_TEST_ASSERT(result == 0); |
|
736 |
|
737 // compare the string |
|
738 CX_TEST_ASSERT(0 == cx_strcmp(cx_strn(buf.space, buf.size), expected)); |
|
739 |
|
740 // destroy everything |
|
741 cxBufferDestroy(&buf); |
|
742 cxJsonValueFree(obj); |
|
743 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); |
|
744 } |
|
745 cx_testing_allocator_destroy(&talloc); |
|
746 } |
|
747 |
708 CxTestSuite *cx_test_suite_json(void) { |
748 CxTestSuite *cx_test_suite_json(void) { |
709 CxTestSuite *suite = cx_test_suite_new("json"); |
749 CxTestSuite *suite = cx_test_suite_new("json"); |
710 |
750 |
711 cx_test_register(suite, test_json_init_default); |
751 cx_test_register(suite, test_json_init_default); |
712 cx_test_register(suite, test_json_simple_object); |
752 cx_test_register(suite, test_json_simple_object); |
721 cx_test_register(suite, test_json_multiple_values); |
761 cx_test_register(suite, test_json_multiple_values); |
722 cx_test_register(suite, test_json_array_iterator); |
762 cx_test_register(suite, test_json_array_iterator); |
723 cx_test_register(suite, test_json_allocator); |
763 cx_test_register(suite, test_json_allocator); |
724 cx_test_register(suite, test_json_allocator_parse_error); |
764 cx_test_register(suite, test_json_allocator_parse_error); |
725 cx_test_register(suite, test_json_create_value); |
765 cx_test_register(suite, test_json_create_value); |
|
766 cx_test_register(suite, test_json_write_default_format); |
726 |
767 |
727 return suite; |
768 return suite; |
728 } |
769 } |
729 |
770 |