From: dota17 Date: Mon, 30 Mar 2020 09:32:06 +0000 (+0800) Subject: add test cases X-Git-Tag: json-c-0.14-20200419~44^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=38221774731c8ff95fe10fe37de8b198a5c71416;p=json-c add test cases --- diff --git a/json_object.c b/json_object.c index 1394150..9646164 100644 --- a/json_object.c +++ b/json_object.c @@ -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) diff --git a/tests/test_compare.c b/tests/test_compare.c index cbb7ccc..37f2e65 100644 --- a/tests/test_compare.c +++ b/tests/test_compare.c @@ -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; } diff --git a/tests/test_compare.expected b/tests/test_compare.expected index a08fab9..51366d9 100644 --- a/tests/test_compare.expected +++ b/tests/test_compare.expected @@ -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 diff --git a/tests/test_deep_copy.c b/tests/test_deep_copy.c index a913015..8e67f86 100644 --- a/tests/test_deep_copy.c +++ b/tests/test_deep_copy.c @@ -19,6 +19,7 @@ static const char *json_str1 = " \"glossary\": {" " \"title\": \"example glossary\"," " \"GlossDiv\": {" +" \"number\": 16446744073709551615," " \"title\": \"S\"," " \"null_obj\": null, " " \"exixt\": false," diff --git a/tests/test_deep_copy.expected b/tests/test_deep_copy.expected index 4f0c39d..3580ddc 100644 --- a/tests/test_deep_copy.expected +++ b/tests/test_deep_copy.expected @@ -11,6 +11,7 @@ Printing JSON objects for visual inspection "glossary":{ "title":"example glossary", "GlossDiv":{ + "number":16446744073709551615, "title":"S", "null_obj":null, "exixt":false, diff --git a/tests/test_int_add.c b/tests/test_int_add.c index 82f9d99..0cb98c1 100644 --- a/tests/test_int_add.c +++ b/tests/test_int_add.c @@ -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); diff --git a/tests/test_int_add.expected b/tests/test_int_add.expected index b2cbdb3..f9348d4 100644 --- a/tests/test_int_add.expected +++ b/tests/test_int_add.expected @@ -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 diff --git a/tests/test_set_value.c b/tests/test_set_value.c index 25124be..622be93 100644 --- a/tests/test_set_value.c +++ b/tests/test_set_value.c @@ -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); diff --git a/tests/test_set_value.expected b/tests/test_set_value.expected index 18dd49e..7b43eec 100644 --- a/tests/test_set_value.expected +++ b/tests/test_set_value.expected @@ -4,5 +4,6 @@ UINT64 PASSED BOOL PASSED DOUBLE PASSED STRING PASSED +STRINGTODOUBLE PASSED PARSE AND SET PASSED PASSED