add test cases

This commit is contained in:
dota17
2020-03-30 17:32:06 +08:00
parent d1650a582e
commit 3822177473
9 changed files with 89 additions and 1 deletions

View File

@@ -656,7 +656,8 @@ int32_t json_object_get_int(const struct json_object *jso)
} else {
if (jso->o.c_int.cint.c_uint64 >= INT64_MAX)
cint64 = INT64_MAX;
cint64 = (int64_t)jso->o.c_int.cint.c_uint64;
else
cint64 = (int64_t)jso->o.c_int.cint.c_uint64;
}
if (o_type == json_type_string)

View File

@@ -208,8 +208,50 @@ int main()
else
printf("Comparing different objects is incorrect\n");
/* iterate over jso2 keys to see if any exist that are not in jso1 */
json_object_object_add(obj2, "test3", json_object_new_int(320));
json_object_object_add(obj2, "test6", json_object_new_int(321));
if (!json_object_equal(obj1, obj2))
printf("Comparing different objects is correct\n");
else
printf("Comparing different objects is incorrect\n");
/* iterate over jso1 keys and see if they exist in jso1 */
json_object_object_add(obj1, "test6", json_object_new_int(321));
if (json_object_equal(obj1, obj2))
printf("Comparing different objects is correct\n");
else
printf("Comparing different objects is incorrect\n");
json_object_object_add(obj1, "test7", json_object_new_int(322));
if (!json_object_equal(obj1, obj2))
printf("Comparing different objects is correct\n");
else
printf("Comparing different objects is incorrect\n");
json_object_put(obj1);
json_object_put(obj2);
/* different types tests */
struct json_object *int5 = json_object_new_int(0);
struct json_object *dbl5 = json_object_new_double(3.14159);
if (!json_object_equal(int5, NULL))
printf("JSON integer and NULL comparison is correct\n");
else
printf("JSON integer and NULL comparison failed\n");
if (!json_object_equal(NULL, dbl5))
printf("JSON NULL and double comparison is correct\n");
else
printf("JSON NULL and double comparison failed\n");
if (!json_object_equal(int5, dbl5))
printf("JSON integer and double comparison is correct\n");
else
printf("JSON integer and double comparison failed\n");
json_object_put(int5);
json_object_put(dbl5);
return 0;
}

View File

@@ -22,3 +22,9 @@ Comparing different arrays is correct
Comparing different arrays (one empty) is correct
Comparing JSON object with different key order is correct
Comparing different objects is correct
Comparing different objects is correct
Comparing different objects is correct
Comparing different objects is correct
JSON integer and NULL comparison is correct
JSON NULL and double comparison is correct
JSON integer and double comparison is correct

View File

@@ -19,6 +19,7 @@ static const char *json_str1 =
" \"glossary\": {"
" \"title\": \"example glossary\","
" \"GlossDiv\": {"
" \"number\": 16446744073709551615,"
" \"title\": \"S\","
" \"null_obj\": null, "
" \"exixt\": false,"

View File

@@ -11,6 +11,7 @@ Printing JSON objects for visual inspection
"glossary":{
"title":"example glossary",
"GlossDiv":{
"number":16446744073709551615,
"title":"S",
"null_obj":null,
"exixt":false,

View File

@@ -48,8 +48,17 @@ int main(int argc, char **argv)
json_object_int_inc(tmp, -200);
assert(json_object_get_int64(tmp) == 200);
assert(json_object_get_uint64(tmp) == 200);
json_object_int_inc(tmp, 200);
assert(json_object_get_int64(tmp) == 400);
assert(json_object_get_uint64(tmp) == 400);
json_object_put(tmp);
printf("UINT64 ADD PASSED\n");
tmp = json_object_new_uint64(UINT64_MAX-50);
json_object_int_inc(tmp, 100);
assert(json_object_get_int64(tmp) == INT64_MAX);
assert(json_object_get_uint64(tmp) == UINT64_MAX);
json_object_put(tmp);
printf("UINT64 ADD OVERFLOW PASSED\n");
tmp = json_object_new_uint64(100);
json_object_int_inc(tmp, -200);
assert(json_object_get_int64(tmp) == -100);

View File

@@ -5,5 +5,6 @@ INT64 ADD PASSED
INT64 ADD OVERFLOW PASSED
INT64 ADD UNDERFLOW PASSED
UINT64 ADD PASSED
UINT64 ADD OVERFLOW PASSED
UINT64 ADD UNDERFLOW PASSED
PASSED

View File

@@ -16,12 +16,15 @@ int main(int argc, char **argv)
json_object_put(tmp);
printf("INT64 PASSED\n");
tmp=json_object_new_uint64(123);
assert (json_object_get_boolean(tmp)==1);
assert (json_object_get_int(tmp)==123);
assert (json_object_get_int64(tmp)==123);
assert (json_object_get_uint64(tmp)==123);
assert (json_object_get_double(tmp)==123.000000);
json_object_set_uint64(tmp,(uint64_t)321321321);
assert (json_object_get_uint64(tmp)==321321321);
json_object_set_uint64(tmp,9223372036854775808U);
assert (json_object_get_int(tmp)==INT32_MAX);
assert (json_object_get_uint64(tmp)==9223372036854775808U);
json_object_put(tmp);
printf("UINT64 PASSED\n");
@@ -40,9 +43,11 @@ int main(int argc, char **argv)
json_object_set_double(tmp,6435.34);
assert (json_object_get_double(tmp)==6435.34);
json_object_set_double(tmp,2e21);
assert (json_object_get_int(tmp)==INT32_MAX);
assert (json_object_get_int64(tmp)==INT64_MAX);
assert (json_object_get_uint64(tmp)==UINT64_MAX);
json_object_set_double(tmp,-2e21);
assert (json_object_get_int(tmp)==INT32_MIN);
assert (json_object_get_int64(tmp)==INT64_MIN);
assert (json_object_get_uint64(tmp)==0);
json_object_put(tmp);
@@ -62,6 +67,27 @@ int main(int argc, char **argv)
json_object_put(tmp);
printf("STRING PASSED\n");
#define STR "STR"
#define DOUBLE "123.123"
#define DOUBLE_E "12E+3"
#define DOUBLE_STR "123.123STR"
#define DOUBLE_OVER "1.8E+308"
#define DOUBLE_OVER_NEGATIVE "-1.8E+308"
tmp=json_object_new_string(STR);
assert (json_object_get_double(tmp)==0.0);
json_object_set_string(tmp,DOUBLE);
assert (json_object_get_double(tmp)==123.123000);
json_object_set_string(tmp,DOUBLE_E);
assert (json_object_get_double(tmp)==12000.000000);
json_object_set_string(tmp,DOUBLE_STR);
assert (json_object_get_double(tmp)==0.0);
json_object_set_string(tmp,DOUBLE_OVER);
assert (json_object_get_double(tmp)==0.0);
json_object_set_string(tmp,DOUBLE_OVER_NEGATIVE);
assert (json_object_get_double(tmp)==0.0);
json_object_put(tmp);
printf("STRINGTODOUBLE PASSED\n");
tmp = json_tokener_parse("1.234");
json_object_set_double(tmp, 12.3);
const char *serialized = json_object_to_json_string(tmp);

View File

@@ -4,5 +4,6 @@ UINT64 PASSED
BOOL PASSED
DOUBLE PASSED
STRING PASSED
STRINGTODOUBLE PASSED
PARSE AND SET PASSED
PASSED