tests/test_json.c

changeset 1596
2d2c4ec38133
parent 1595
2e0353a9bc30
child 1597
be79be26b01d
equal deleted inserted replaced
1595:2e0353a9bc30 1596:2d2c4ec38133
123 CxJsonValue *obj = cxJsonCreateObj(NULL); 123 CxJsonValue *obj = cxJsonCreateObj(NULL);
124 CX_TEST_DO { 124 CX_TEST_DO {
125 cxJsonObjPutString(obj, "mystring", "test"); 125 cxJsonObjPutString(obj, "mystring", "test");
126 char buf[10]; 126 char buf[10];
127 for (unsigned i = 0 ; i < 300 ; i++) { 127 for (unsigned i = 0 ; i < 300 ; i++) {
128 sprintf(buf, "key %d", i); 128 snprintf(buf, 10, "key %d", i);
129 cxJsonObjPutInteger(obj, buf, i); 129 cxJsonObjPutInteger(obj, buf, i);
130 } 130 }
131 CX_TEST_ASSERT(301 == cxJsonObjSize(obj)); 131 CX_TEST_ASSERT(301 == cxJsonObjSize(obj));
132 // some samples 132 // some samples
133 CxJsonValue *v; 133 CxJsonValue *v;
141 v = cxJsonObjGet(obj, "mystring"); 141 v = cxJsonObjGet(obj, "mystring");
142 CX_TEST_ASSERT(cxJsonIsString(v)); 142 CX_TEST_ASSERT(cxJsonIsString(v));
143 CX_TEST_ASSERT(0 == cx_strcmp(cxJsonAsCxMutStr(v), "test")); 143 CX_TEST_ASSERT(0 == cx_strcmp(cxJsonAsCxMutStr(v), "test"));
144 } 144 }
145 cxJsonValueFree(obj); 145 cxJsonValueFree(obj);
146 }
147
148 CX_TEST(test_json_simple_array) {
149 cxstring empty_array = cx_str("[]");
150 cxstring int_array = cx_str("[ 0, 1, 2 ]");
151 cxstring str_array = cx_str("[ \"str1\", \"str2\" ]");
152 cxstring mixed_array = cx_str("[ true, false, 12, { \"a\": \"b\" }, [ 1,2,3,4] ]");
153
154 CxJsonValue *value;
155 CxJsonStatus result;
156 CxJson json0, json1, json2, json3;
157
158 CX_TEST_DO {
159 // empty array
160 cxJsonInit(&json0, NULL);
161 cxJsonFill(&json0, empty_array);
162 result = cxJsonNext(&json0, &value);
163 CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
164 CX_TEST_ASSERT(value);
165 CX_TEST_ASSERT(cxJsonIsArray(value));
166
167 cxJsonValueFree(value);
168 cxJsonDestroy(&json0);
169
170 // int array
171 cxJsonInit(&json1, NULL);
172 cxJsonFill(&json1, int_array);
173 result = cxJsonNext(&json1, &value);
174 CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
175 CX_TEST_ASSERT(value);
176 CX_TEST_ASSERT(cxJsonIsArray(value));
177 CX_TEST_ASSERT(value->array.data_size == 3);
178 for(int i=0;i<3;i++) {
179 CxJsonValue *v = cxJsonArrGet(value, i);
180 CX_TEST_ASSERT(v);
181 CX_TEST_ASSERT(cxJsonIsInteger(v));
182 CX_TEST_ASSERT(v->integer == i);
183 }
184
185 cxJsonValueFree(value);
186 cxJsonDestroy(&json1);
187
188 // str array
189 cxJsonInit(&json2, NULL);
190 cxJsonFill(&json2, str_array);
191 result = cxJsonNext(&json2, &value);
192 CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
193 CX_TEST_ASSERT(value);
194 CX_TEST_ASSERT(cxJsonIsArray(value));
195 CX_TEST_ASSERT(value->array.data_size == 2);
196 CxJsonValue *s0 = cxJsonArrGet(value, 0);
197 CxJsonValue *s1 = cxJsonArrGet(value, 1);
198 CX_TEST_ASSERT(s0 && s1);
199 CX_TEST_ASSERT(cxJsonIsString(s0) && cxJsonIsString(s1));
200 CX_TEST_ASSERT(cx_strcmp(s0->string, "str1") == 0);
201 CX_TEST_ASSERT(cx_strcmp(s1->string, "str2") == 0);
202
203 cxJsonValueFree(value);
204 cxJsonDestroy(&json2);
205
206 // mixed array
207 cxJsonInit(&json3, NULL);
208 cxJsonFill(&json3, mixed_array);
209 result = cxJsonNext(&json3, &value);
210 CX_TEST_ASSERT(result == CX_JSON_NO_ERROR);
211 CX_TEST_ASSERT(value);
212 CX_TEST_ASSERT(cxJsonIsArray(value));
213 CX_TEST_ASSERT(value->array.data_size == 5);
214 CxJsonValue *m0 = cxJsonArrGet(value, 0);
215 CxJsonValue *m1 = cxJsonArrGet(value, 1);
216 CxJsonValue *m2 = cxJsonArrGet(value, 2);
217 CxJsonValue *m3 = cxJsonArrGet(value, 3);
218 CxJsonValue *m4 = cxJsonArrGet(value, 4);
219 CX_TEST_ASSERT(m0 && m1 && m2 && m3 && m4);
220 CX_TEST_ASSERT(cxJsonIsLiteral(m0));
221 CX_TEST_ASSERT(cxJsonIsLiteral(m1));
222 CX_TEST_ASSERT(cxJsonIsInteger(m2));
223 CX_TEST_ASSERT(cxJsonIsObject(m3));
224 CX_TEST_ASSERT(cxJsonIsArray(m4));
225
226 cxJsonValueFree(value);
227 cxJsonDestroy(&json3);
228 }
146 } 229 }
147 230
148 CX_TEST(test_json_from_string) { 231 CX_TEST(test_json_from_string) {
149 cxstring text = cx_str( 232 cxstring text = cx_str(
150 "{\n" 233 "{\n"
1414 cxJsonValueFree(json[i]); 1497 cxJsonValueFree(json[i]);
1415 } 1498 }
1416 } 1499 }
1417 1500
1418 CX_TEST(test_json_compare_arrays) { 1501 CX_TEST(test_json_compare_arrays) {
1419 char *str[4]; 1502 char *str[6];
1420 str[0] = "[]"; 1503 str[0] = "[]";
1421 str[1] = "[ true, false ]"; 1504 str[1] = "[[]]";
1422 str[2] = "[ 0, 1, \"hello\", true, false, null, {}, {\"a\":\"b\"}]"; 1505 str[2] = "[[ {} ], { \"a\": [[[[ {} ], null]]]} ]";
1423 str[3] = "[ \"test\", [ 1, 2, [ \"sub\", \"sub1\", [{\"end\":null}]]]]"; 1506 str[3] = "[ true, false ]";
1507 str[4] = "[ 0, 1, \"hello\", true, false, null, {}, {\"a\":\"b\"}]";
1508 str[5] = "[ \"test\", [ 1, 2, [ \"sub\", \"sub1\", [{\"end\":null}]]]]";
1424 1509
1425 CxJsonValue *a[4]; 1510 CxJsonValue *a[6];
1426 CxJsonValue *b[4]; 1511 CxJsonValue *b[6];
1427 for(int i=0;i<4;i++) { 1512 for(int i=0;i<6;i++) {
1428 cxJsonFromString(NULL, str[i], &a[i]); 1513 cxJsonFromString(NULL, str[i], &a[i]);
1429 cxJsonFromString(NULL, str[i], &b[i]); 1514 cxJsonFromString(NULL, str[i], &b[i]);
1430 } 1515 }
1431 1516
1432 CX_TEST_DO { 1517 CX_TEST_DO {
1433 for(int i=0;i<4;i++) { 1518 for(int i=0;i<6;i++) {
1434 for(int j=0;j<4;j++) { 1519 // make sure the test values are arrays
1520 CX_TEST_ASSERT(cxJsonIsArray(a[i]));
1521
1522 for(int j=0;j<6;j++) {
1435 int ret = cxJsonCompare(a[i], b[j]); 1523 int ret = cxJsonCompare(a[i], b[j]);
1436 CX_TEST_ASSERT(i == j ? ret == 0 : ret != 0); 1524 CX_TEST_ASSERT(i == j ? ret == 0 : ret != 0);
1437 } 1525 }
1438 } 1526 }
1439 } 1527 }
1440 1528
1441 for(int i=0;i<4;i++) { 1529 for(int i=0;i<6;i++) {
1442 cxJsonValueFree(a[i]); 1530 cxJsonValueFree(a[i]);
1443 cxJsonValueFree(b[i]); 1531 cxJsonValueFree(b[i]);
1444 } 1532 }
1445 } 1533 }
1446 1534
1987 CxTestSuite *suite = cx_test_suite_new("json"); 2075 CxTestSuite *suite = cx_test_suite_new("json");
1988 2076
1989 cx_test_register(suite, test_json_init_default); 2077 cx_test_register(suite, test_json_init_default);
1990 cx_test_register(suite, test_json_simple_object); 2078 cx_test_register(suite, test_json_simple_object);
1991 cx_test_register(suite, test_json_large_object); 2079 cx_test_register(suite, test_json_large_object);
2080 cx_test_register(suite, test_json_simple_array);
1992 cx_test_register(suite, test_json_from_string); 2081 cx_test_register(suite, test_json_from_string);
1993 cx_test_register(suite, test_json_from_string_errors); 2082 cx_test_register(suite, test_json_from_string_errors);
1994 cx_test_register(suite, test_json_from_string_multiple_values); 2083 cx_test_register(suite, test_json_from_string_multiple_values);
1995 cx_test_register(suite, test_json_from_string_untrimmed); 2084 cx_test_register(suite, test_json_from_string_untrimmed);
1996 cx_test_register(suite, test_json_escaped_strings); 2085 cx_test_register(suite, test_json_escaped_strings);

mercurial