tests/test_json.c

changeset 977
a941b01b684f
parent 975
96bf02371aef
child 978
1da52af18ac3
equal deleted inserted replaced
976:d9063c14d3fb 977:a941b01b684f
239 CX_TEST_ASSERT(cxJsonIsInteger(d10)); 239 CX_TEST_ASSERT(cxJsonIsInteger(d10));
240 CX_TEST_ASSERT(cxJsonAsInteger(d10) == 47); 240 CX_TEST_ASSERT(cxJsonAsInteger(d10) == 47);
241 241
242 CX_TEST_ASSERT(json.states != json.states_internal); 242 CX_TEST_ASSERT(json.states != json.states_internal);
243 CX_TEST_ASSERT(json.states_alloc > cx_nmemb(json.states_internal)); 243 CX_TEST_ASSERT(json.states_alloc > cx_nmemb(json.states_internal));
244 244
245 cxJsonDestroy(&json); 245 cxJsonDestroy(&json);
246 } 246 }
247 } 247 }
248 248
249 CX_TEST(test_json_number) { 249 CX_TEST(test_json_number) {
261 cxJsonFill(&json, "-47.11e2 "); 261 cxJsonFill(&json, "-47.11e2 ");
262 result = cxJsonNext(&json, &v); 262 result = cxJsonNext(&json, &v);
263 CX_TEST_ASSERT(result == 1); 263 CX_TEST_ASSERT(result == 1);
264 CX_TEST_ASSERT(cxJsonIsNumber(v)); 264 CX_TEST_ASSERT(cxJsonIsNumber(v));
265 CX_TEST_ASSERT(cxJsonAsDouble(v) == -4711.0); 265 CX_TEST_ASSERT(cxJsonAsDouble(v) == -4711.0);
266 }
267 cxJsonDestroy(&json);
268 }
269
270 CX_TEST(test_json_multiple_values) {
271 CxJson json;
272 cxJsonInit(&json);
273 CX_TEST_DO {
274 CxJsonValue *v;
275 int result;
276
277 // read number
278 cxJsonFill(&json, "10\n");
279 result = cxJsonNext(&json, &v);
280 CX_TEST_ASSERT(result == 1);
281 CX_TEST_ASSERT(cxJsonIsNumber(v));
282 CX_TEST_ASSERT(cxJsonAsInteger(v) == 10);
283 cxJsonValueFree(v);
284 // read remaining '\n'
285 result = cxJsonNext(&json, &v);
286 CX_TEST_ASSERT(result == 0);
287 // read string
288 cxJsonFill(&json, "\"hello world\"\n");
289 result = cxJsonNext(&json, &v);
290 CX_TEST_ASSERT(result == 1);
291 CX_TEST_ASSERT(cxJsonIsString(v));
292 CX_TEST_ASSERT(!cx_strcmp(cxJsonAsCxString(v), CX_STR("hello world")));
293 cxJsonValueFree(v);
294 // don't process the remaining newline this time
295 // read obj
296 cxJsonFill(&json, "{ \"value\": \"test\" }\n");
297 result = cxJsonNext(&json, &v);
298 CX_TEST_ASSERT(result == 1);
299 CX_TEST_ASSERT(cxJsonIsObject(v));
300 CxJsonValue *value = cxJsonObjGet(v, "value");
301 CX_TEST_ASSERT(cxJsonAsString(value));
302 cxJsonValueFree(v);
303 // read array
304 cxJsonFill(&json, "[ 0, 1, 2, 3, 4, 5 ]\n");
305 result = cxJsonNext(&json, &v);
306 CX_TEST_ASSERT(result == 1);
307 CX_TEST_ASSERT(cxJsonIsArray(v));
308 CxJsonValue *a0 = cxJsonArrGet(v, 0);
309 CxJsonValue *a3 = cxJsonArrGet(v, 3);
310 CX_TEST_ASSERT(cxJsonIsNumber(a0));
311 CX_TEST_ASSERT(cxJsonAsInteger(a0) == 0);
312 CX_TEST_ASSERT(cxJsonIsNumber(a3));
313 CX_TEST_ASSERT(cxJsonAsInteger(a3) == 3);
314 cxJsonValueFree(v);
315 // read literal
316 cxJsonFill(&json, "true\n");
317 result = cxJsonNext(&json, &v);
318 CX_TEST_ASSERT(result == 1);
319 CX_TEST_ASSERT(cxJsonIsLiteral(v));
320 CX_TEST_ASSERT(cxJsonAsBool(v));
321 cxJsonValueFree(v);
322 // read null
323 cxJsonFill(&json, "null\n");
324 result = cxJsonNext(&json, &v);
325 CX_TEST_ASSERT(result == 1);
326 CX_TEST_ASSERT(cxJsonIsNull(v));
327 cxJsonValueFree(v);
266 } 328 }
267 cxJsonDestroy(&json); 329 cxJsonDestroy(&json);
268 } 330 }
269 331
270 CxTestSuite *cx_test_suite_json(void) { 332 CxTestSuite *cx_test_suite_json(void) {
274 cx_test_register(suite, test_json_simple_object); 336 cx_test_register(suite, test_json_simple_object);
275 cx_test_register(suite, test_json_object_incomplete_token); 337 cx_test_register(suite, test_json_object_incomplete_token);
276 cx_test_register(suite, test_json_object_error); 338 cx_test_register(suite, test_json_object_error);
277 cx_test_register(suite, test_json_large_nesting_depth); 339 cx_test_register(suite, test_json_large_nesting_depth);
278 cx_test_register(suite, test_json_number); 340 cx_test_register(suite, test_json_number);
279 341 cx_test_register(suite, test_json_multiple_values);
342
280 return suite; 343 return suite;
281 } 344 }
282 345

mercurial