tests/test_json.c

changeset 1547
12da0654e4a9
parent 1543
7b66371d6da3
child 1548
12315ee158ad
equal deleted inserted replaced
1546:c8dd35f3ea53 1547:12da0654e4a9
1114 1114
1115 // overwrite test2 1115 // overwrite test2
1116 cxJsonObjPutInteger(obj, "test2", 0); 1116 cxJsonObjPutInteger(obj, "test2", 0);
1117 1117
1118 // verify the values 1118 // verify the values
1119 CxIterator iter = cxJsonObjIter(obj); 1119 CxMapIterator iter = cxJsonObjIter(obj);
1120 bool found[5] = {0}; 1120 bool found[5] = {0};
1121 cx_foreach(CxJsonObjValue *, ov, iter) { 1121 cx_foreach(CxMapEntry *, ov, iter) {
1122 CxJsonValue *v = ov->value; 1122 CxJsonValue *v = ov->value;
1123 CX_TEST_ASSERT(cxJsonIsInteger(v)); 1123 CX_TEST_ASSERT(cxJsonIsInteger(v));
1124 int64_t i = cxJsonAsInteger(v); 1124 int64_t i = cxJsonAsInteger(v);
1125 CX_TEST_ASSERT(i >= 0 && i <= 4); 1125 CX_TEST_ASSERT(i >= 0 && i <= 4);
1126 found[i] = true; 1126 found[i] = true;
1190 CX_TEST_DO { 1190 CX_TEST_DO {
1191 // expected value 1191 // expected value
1192 cxstring expected = cx_str( 1192 cxstring expected = cx_str(
1193 "{\"bool\":false," 1193 "{\"bool\":false,"
1194 "\"int\":47," 1194 "\"int\":47,"
1195 "\"strings\":[\"hello\",\"world\"],"
1195 "\"nested\":{" 1196 "\"nested\":{"
1196 "\"floats\":[3.1415,47.11,8.15],"
1197 "\"ints\":[4,8,15,[16,23],42],"
1198 "\"literals\":[true,null,false],"
1199 "\"objects\":[{" 1197 "\"objects\":[{"
1200 "\"name1\":1," 1198 "\"name1\":1,"
1201 "\"name2\":3" 1199 "\"name2\":3"
1202 "},{" 1200 "},{"
1203 "\"name1\":3," 1201 "\"name2\":7,"
1204 "\"name2\":7" 1202 "\"name1\":3"
1205 "}]" 1203 "}],"
1206 "}," 1204 "\"floats\":[3.1415,47.11,8.15],"
1207 "\"strings\":[\"hello\",\"world\"]" 1205 "\"literals\":[true,null,false],"
1206 "\"ints\":[4,8,15,[16,23],42]"
1207 "}"
1208 "}" 1208 "}"
1209 ); 1209 );
1210 1210
1211 CxJsonWriter writer = cxJsonWriterCompact(); 1211 CxJsonWriter writer = cxJsonWriterCompact();
1212 CX_TEST_CALL_SUBROUTINE(test_json_write_sub, allocator, expected, &writer); 1212 CX_TEST_CALL_SUBROUTINE(test_json_write_sub, allocator, expected, &writer);
1216 } 1216 }
1217 cx_testing_allocator_destroy(&talloc); 1217 cx_testing_allocator_destroy(&talloc);
1218 } 1218 }
1219 1219
1220 CX_TEST(test_json_write_pretty_default_spaces) { 1220 CX_TEST(test_json_write_pretty_default_spaces) {
1221 CxTestingAllocator talloc;
1222 cx_testing_allocator_init(&talloc);
1223 CxAllocator *allocator = &talloc.base;
1224 CX_TEST_DO {
1225 cxstring expected = cx_str(
1226 "{\n"
1227 " \"bool\": false,\n"
1228 " \"int\": 47,\n"
1229 " \"nested\": {\n"
1230 " \"floats\": [3.1415, 47.11, 8.15],\n"
1231 " \"ints\": [4, 8, 15, [16, 23], 42],\n"
1232 " \"literals\": [true, null, false],\n"
1233 " \"objects\": [{\n"
1234 " \"name1\": 1,\n"
1235 " \"name2\": 3\n"
1236 " }, {\n"
1237 " \"name1\": 3,\n"
1238 " \"name2\": 7\n"
1239 " }]\n"
1240 " },\n"
1241 " \"strings\": [\"hello\", \"world\"]\n"
1242 "}"
1243 );
1244
1245 CxJsonWriter writer = cxJsonWriterPretty(true);
1246 CX_TEST_CALL_SUBROUTINE(test_json_write_sub, allocator, expected, &writer);
1247 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
1248 }
1249 cx_testing_allocator_destroy(&talloc);
1250 }
1251
1252 CX_TEST(test_json_write_pretty_default_tabs) {
1253 CxTestingAllocator talloc;
1254 cx_testing_allocator_init(&talloc);
1255 CxAllocator *allocator = &talloc.base;
1256 CX_TEST_DO {
1257 cxstring expected = cx_str(
1258 "{\n"
1259 "\t\"bool\": false,\n"
1260 "\t\"int\": 47,\n"
1261 "\t\"nested\": {\n"
1262 "\t\t\"floats\": [3.1415, 47.11, 8.15],\n"
1263 "\t\t\"ints\": [4, 8, 15, [16, 23], 42],\n"
1264 "\t\t\"literals\": [true, null, false],\n"
1265 "\t\t\"objects\": [{\n"
1266 "\t\t\t\"name1\": 1,\n"
1267 "\t\t\t\"name2\": 3\n"
1268 "\t\t}, {\n"
1269 "\t\t\t\"name1\": 3,\n"
1270 "\t\t\t\"name2\": 7\n"
1271 "\t\t}]\n"
1272 "\t},\n"
1273 "\t\"strings\": [\"hello\", \"world\"]\n"
1274 "}"
1275 );
1276 CxJsonWriter writer = cxJsonWriterPretty(false);
1277 CX_TEST_CALL_SUBROUTINE(test_json_write_sub, allocator, expected, &writer);
1278 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
1279 }
1280 cx_testing_allocator_destroy(&talloc);
1281 }
1282
1283 CX_TEST(test_json_write_pretty_preserve_order) {
1284 CxTestingAllocator talloc; 1221 CxTestingAllocator talloc;
1285 cx_testing_allocator_init(&talloc); 1222 cx_testing_allocator_init(&talloc);
1286 CxAllocator *allocator = &talloc.base; 1223 CxAllocator *allocator = &talloc.base;
1287 CX_TEST_DO { 1224 CX_TEST_DO {
1288 cxstring expected = cx_str( 1225 cxstring expected = cx_str(
1304 " }\n" 1241 " }\n"
1305 "}" 1242 "}"
1306 ); 1243 );
1307 1244
1308 CxJsonWriter writer = cxJsonWriterPretty(true); 1245 CxJsonWriter writer = cxJsonWriterPretty(true);
1309 writer.sort_members = false; 1246 CX_TEST_CALL_SUBROUTINE(test_json_write_sub, allocator, expected, &writer);
1247 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
1248 }
1249 cx_testing_allocator_destroy(&talloc);
1250 }
1251
1252 CX_TEST(test_json_write_pretty_default_tabs) {
1253 CxTestingAllocator talloc;
1254 cx_testing_allocator_init(&talloc);
1255 CxAllocator *allocator = &talloc.base;
1256 CX_TEST_DO {
1257 cxstring expected = cx_str(
1258 "{\n"
1259 "\t\"bool\": false,\n"
1260 "\t\"int\": 47,\n"
1261 "\t\"strings\": [\"hello\", \"world\"],\n"
1262 "\t\"nested\": {\n"
1263 "\t\t\"objects\": [{\n"
1264 "\t\t\t\"name1\": 1,\n"
1265 "\t\t\t\"name2\": 3\n"
1266 "\t\t}, {\n"
1267 "\t\t\t\"name2\": 7,\n"
1268 "\t\t\t\"name1\": 3\n"
1269 "\t\t}],\n"
1270 "\t\t\"floats\": [3.1415, 47.11, 8.15],\n"
1271 "\t\t\"literals\": [true, null, false],\n"
1272 "\t\t\"ints\": [4, 8, 15, [16, 23], 42]\n"
1273 "\t}\n"
1274 "}"
1275 );
1276 CxJsonWriter writer = cxJsonWriterPretty(false);
1310 CX_TEST_CALL_SUBROUTINE(test_json_write_sub, allocator, expected, &writer); 1277 CX_TEST_CALL_SUBROUTINE(test_json_write_sub, allocator, expected, &writer);
1311 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc)); 1278 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
1312 } 1279 }
1313 cx_testing_allocator_destroy(&talloc); 1280 cx_testing_allocator_destroy(&talloc);
1314 } 1281 }
1519 cx_test_register(suite, test_json_create_value); 1486 cx_test_register(suite, test_json_create_value);
1520 cx_test_register(suite, test_json_overwrite_value); 1487 cx_test_register(suite, test_json_overwrite_value);
1521 cx_test_register(suite, test_json_write_default_format); 1488 cx_test_register(suite, test_json_write_default_format);
1522 cx_test_register(suite, test_json_write_pretty_default_spaces); 1489 cx_test_register(suite, test_json_write_pretty_default_spaces);
1523 cx_test_register(suite, test_json_write_pretty_default_tabs); 1490 cx_test_register(suite, test_json_write_pretty_default_tabs);
1524 cx_test_register(suite, test_json_write_pretty_preserve_order);
1525 cx_test_register(suite, test_json_write_pretty_deep_nesting); 1491 cx_test_register(suite, test_json_write_pretty_deep_nesting);
1526 cx_test_register(suite, test_json_write_frac_max_digits); 1492 cx_test_register(suite, test_json_write_frac_max_digits);
1527 cx_test_register(suite, test_json_write_string_escape); 1493 cx_test_register(suite, test_json_write_string_escape);
1528 cx_test_register(suite, test_json_write_name_escape); 1494 cx_test_register(suite, test_json_write_name_escape);
1529 cx_test_register(suite, test_json_write_solidus); 1495 cx_test_register(suite, test_json_write_solidus);

mercurial