From 929e2337562c21d17b4ee31b1db90ccf0fa09d8d Mon Sep 17 00:00:00 2001 From: "Serge A. Zaitsev" Date: Wed, 1 Feb 2012 16:03:36 +0200 Subject: added tests for primitive types, primitive types now can be stored outside the objects without braces --- jsmn_test.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) (limited to 'jsmn_test.c') diff --git a/jsmn_test.c b/jsmn_test.c index 7c7e31018..6a0b3407e 100644 --- a/jsmn_test.c +++ b/jsmn_test.c @@ -64,15 +64,66 @@ int test_simple() { } int test_primitive() { + int r; jsmn_parser p; - jsmntok_t tokens[10]; + jsmntok_t tok[10]; const char *js; + + js = "\"boolVar\" : true"; + jsmn_init(&p); + r = jsmn_parse(&p, js, tok, 10); + check(r == JSMN_SUCCESS && tok[0].type == JSMN_STRING + && tok[1].type == JSMN_PRIMITIVE); + check(TOKEN_STIRNG(js, tok[0], "boolVar")); + check(TOKEN_STIRNG(js, tok[1], "true")); + + js = "\"boolVar\" : false"; + jsmn_init(&p); + r = jsmn_parse(&p, js, tok, 10); + check(r == JSMN_SUCCESS && tok[0].type == JSMN_STRING + && tok[1].type == JSMN_PRIMITIVE); + check(TOKEN_STIRNG(js, tok[0], "boolVar")); + check(TOKEN_STIRNG(js, tok[1], "false")); + + js = "\"intVar\" : 12345"; + jsmn_init(&p); + r = jsmn_parse(&p, js, tok, 10); + check(r == JSMN_SUCCESS && tok[0].type == JSMN_STRING + && tok[1].type == JSMN_PRIMITIVE); + check(TOKEN_STIRNG(js, tok[0], "intVar")); + check(TOKEN_STIRNG(js, tok[1], "12345")); + + js = "\"floatVar\" : 12.345"; + jsmn_init(&p); + r = jsmn_parse(&p, js, tok, 10); + check(r == JSMN_SUCCESS && tok[0].type == JSMN_STRING + && tok[1].type == JSMN_PRIMITIVE); + check(TOKEN_STIRNG(js, tok[0], "floatVar")); + check(TOKEN_STIRNG(js, tok[1], "12.345")); + + js = "\"nullVar\" : null"; + jsmn_init(&p); + r = jsmn_parse(&p, js, tok, 10); + check(r == JSMN_SUCCESS && tok[0].type == JSMN_STRING + && tok[1].type == JSMN_PRIMITIVE); + check(TOKEN_STIRNG(js, tok[0], "nullVar")); + check(TOKEN_STIRNG(js, tok[1], "null")); + + js = "\"strVar\" : \"hello world\""; + jsmn_init(&p); + r = jsmn_parse(&p, js, tok, 10); + check(r == JSMN_SUCCESS && tok[0].type == JSMN_STRING + && tok[1].type == JSMN_STRING); + check(TOKEN_STIRNG(js, tok[0], "strVar")); + check(TOKEN_STIRNG(js, tok[1], "hello world")); + return 0; } int main() { test(test_simple, "general test for a simple JSON string"); test(test_primitive, "test primitive JSON data types"); + printf("\nPASSED: %d\nFAILED: %d\n", test_passed, test_failed); return 0; } -- cgit v1.2.3