tests/test_json.c

changeset 1057
4e8436c3e806
parent 1051
7d17bd1103d7
child 1060
0a7c1bb2372d
equal deleted inserted replaced
1056:e180bd389fbc 1057:4e8436c3e806
616 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 616 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
617 } 617 }
618 cx_testing_allocator_destroy(&talloc); 618 cx_testing_allocator_destroy(&talloc);
619 } 619 }
620 620
621 CX_TEST(test_json_create_value) {
622 CxTestingAllocator talloc;
623 cx_testing_allocator_init(&talloc);
624 CxAllocator *allocator = &talloc.base;
625 CX_TEST_DO {
626 /*
627 * This is the value we want to create in this test:
628 *
629 * {
630 * "bool": false,
631 * "int": 47,
632 * "strings": [ "hello", "world" ],
633 * "nested": {
634 * "string": "test",
635 * "floats": [ 3.1415, 47.11, 8.15 ],
636 * "ints": [ 4, 8, 15, 16, 23, 42 ],
637 * "literals": [ true, null, false ]
638 * }
639 * }
640 */
641
642
643 // create the object
644 CxJsonValue *obj = cxJsonCreateObj(allocator);
645 CX_TEST_ASSERT(obj != NULL);
646 CX_TEST_ASSERT(cxJsonIsObject(obj));
647 CX_TEST_ASSERT(obj->allocator == allocator);
648
649 // add the members
650 {
651 cxJsonObjPutLiteral(obj, CX_STR("bool"), CX_JSON_FALSE);
652 cxJsonObjPutInteger(obj, CX_STR("int"), 47);
653 CxJsonValue *strings = cxJsonObjPutArr(obj, CX_STR("strings"));
654 CX_TEST_ASSERT(strings != NULL);
655 CX_TEST_ASSERT(cxJsonIsArray(strings));
656 const char* str[] = {"hello", "world"};
657 CX_TEST_ASSERT(0 == cxJsonArrAddStrings(strings, str, 2));
658
659 CxJsonValue *nested = cxJsonObjPutObj(obj, CX_STR("nested"));
660 CX_TEST_ASSERT(nested != NULL);
661 CX_TEST_ASSERT(cxJsonIsObject(nested));
662 cxJsonObjPutCxString(nested, CX_STR("string"), CX_STR("test"));
663
664 cxJsonArrAddNumbers(cxJsonObjPutArr(nested, CX_STR("floats")),
665 (double[]){3.1415, 47.11, 8.15}, 3);
666 cxJsonArrAddIntegers(cxJsonObjPutArr(nested, CX_STR("ints")),
667 (int64_t[]){4, 8, 15, 16, 23, 42}, 6);
668 cxJsonArrAddLiterals(cxJsonObjPutArr(nested, CX_STR("literals")),
669 (CxJsonLiteral[]){CX_JSON_TRUE, CX_JSON_NULL, CX_JSON_FALSE}, 3);
670 }
671
672 // verify the contents
673 {
674 CX_TEST_ASSERT(cxJsonIsFalse(cxJsonObjGet(obj, "bool")));
675 CX_TEST_ASSERT(47 == cxJsonAsInteger(cxJsonObjGet(obj, "int")));
676 CxJsonValue *strings = cxJsonObjGet(obj, "strings");
677 CX_TEST_ASSERT(cxJsonIsArray(strings));
678 CX_TEST_ASSERT(2 == cxJsonArrSize(strings));
679 CX_TEST_ASSERT(0 == cx_strcmp(CX_STR("hello"), cxJsonAsCxString(cxJsonArrGet(strings, 0))));
680 CX_TEST_ASSERT(0 == cx_strcmp(CX_STR("world"), cxJsonAsCxString(cxJsonArrGet(strings, 1))));
681
682 CxJsonValue *nested = cxJsonObjGet(obj, "nested");
683 CX_TEST_ASSERT(cxJsonIsObject(nested));
684 CX_TEST_ASSERT(0 == strcmp("test", cxJsonAsString(cxJsonObjGet(nested, "string"))));
685 CxJsonValue *floats = cxJsonObjGet(nested, "floats");
686 CX_TEST_ASSERT(cxJsonIsArray(floats));
687 CX_TEST_ASSERT(3 == cxJsonArrSize(floats));
688 CX_TEST_ASSERT(3.1415 == cxJsonAsDouble(cxJsonArrGet(floats, 0)));
689 CX_TEST_ASSERT(47.11 == cxJsonAsDouble(cxJsonArrGet(floats, 1)));
690 CX_TEST_ASSERT(8.15 == cxJsonAsDouble(cxJsonArrGet(floats, 2)));
691 CxJsonValue *ints = cxJsonObjGet(nested, "ints");
692 CX_TEST_ASSERT(cxJsonIsArray(ints));
693 CX_TEST_ASSERT(6 == cxJsonArrSize(ints));
694 CX_TEST_ASSERT(4 == cxJsonAsInteger(cxJsonArrGet(ints, 0)));
695 CX_TEST_ASSERT(8 == cxJsonAsInteger(cxJsonArrGet(ints, 1)));
696 CX_TEST_ASSERT(15 == cxJsonAsInteger(cxJsonArrGet(ints, 2)));
697 CX_TEST_ASSERT(16 == cxJsonAsInteger(cxJsonArrGet(ints, 3)));
698 CX_TEST_ASSERT(23 == cxJsonAsInteger(cxJsonArrGet(ints, 4)));
699 CX_TEST_ASSERT(42 == cxJsonAsInteger(cxJsonArrGet(ints, 5)));
700 CxJsonValue *literals = cxJsonObjGet(nested, "literals");
701 CX_TEST_ASSERT(cxJsonIsArray(literals));
702 CX_TEST_ASSERT(3 == cxJsonArrSize(literals));
703 CX_TEST_ASSERT(cxJsonIsTrue(cxJsonArrGet(literals, 0)));
704 CX_TEST_ASSERT(cxJsonIsNull(cxJsonArrGet(literals, 1)));
705 CX_TEST_ASSERT(cxJsonIsFalse(cxJsonArrGet(literals, 2)));
706 }
707
708 // destroy the value and verify the allocations
709 cxJsonValueFree(obj);
710 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
711 }
712 cx_testing_allocator_destroy(&talloc);
713 }
714
621 CxTestSuite *cx_test_suite_json(void) { 715 CxTestSuite *cx_test_suite_json(void) {
622 CxTestSuite *suite = cx_test_suite_new("json"); 716 CxTestSuite *suite = cx_test_suite_new("json");
623 717
624 cx_test_register(suite, test_json_init_default); 718 cx_test_register(suite, test_json_init_default);
625 cx_test_register(suite, test_json_simple_object); 719 cx_test_register(suite, test_json_simple_object);
633 cx_test_register(suite, test_json_number_format_errors); 727 cx_test_register(suite, test_json_number_format_errors);
634 cx_test_register(suite, test_json_multiple_values); 728 cx_test_register(suite, test_json_multiple_values);
635 cx_test_register(suite, test_json_array_iterator); 729 cx_test_register(suite, test_json_array_iterator);
636 cx_test_register(suite, test_json_allocator); 730 cx_test_register(suite, test_json_allocator);
637 cx_test_register(suite, test_json_allocator_parse_error); 731 cx_test_register(suite, test_json_allocator_parse_error);
732 cx_test_register(suite, test_json_create_value);
638 733
639 return suite; 734 return suite;
640 } 735 }
641 736

mercurial