]> granicus.if.org Git - json-c/commitdiff
add test cases
authordota17 <chenguopingdota@163.com>
Mon, 30 Mar 2020 09:32:06 +0000 (17:32 +0800)
committerdota17 <chenguopingdota@163.com>
Tue, 31 Mar 2020 11:12:45 +0000 (19:12 +0800)
json_object.c
tests/test_compare.c
tests/test_compare.expected
tests/test_deep_copy.c
tests/test_deep_copy.expected
tests/test_int_add.c
tests/test_int_add.expected
tests/test_set_value.c
tests/test_set_value.expected

index 139415063d132d85cb325edceed7a1dc1fd94319..96461641859cb38aa53101eb19ebfbcf8007f871 100644 (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)
index cbb7cccd2529766d8ab454162c29d40e1d7096f7..37f2e65de5c8f1dbb7185a4765936dfacaad015f 100644 (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;
 }
index a08fab9cae95eb942330fef1a4507020a3a08dc8..51366d9c8b2164cfeabef811d9f2981548c9ea19 100644 (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
index a91301521c171e384cdd6bd8856d6de2a30b3a24..8e67f86f667b51f4f2f8c455d9a5d8df92202fa4 100644 (file)
@@ -19,6 +19,7 @@ static const char *json_str1 =
 "    \"glossary\": {"
 "        \"title\": \"example glossary\","
 "        \"GlossDiv\": {"
+"            \"number\": 16446744073709551615,"
 "            \"title\": \"S\","
 "            \"null_obj\": null, "
 "            \"exixt\": false,"
index 4f0c39d14f7d1310d2b483a921efcfda17adb410..3580ddc2394825739115305b32bf1e9c7cfdb8bd 100644 (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,
index 82f9d992768ce55ae7c9490f6ab5cc605ed12448..0cb98c179263da93cb939263059b1eb753a8774e 100644 (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);
index b2cbdb368b5d6481b9301759e898f57a07e3603c..f9348d4b097a085cd38dd99b8d411b4952dbbf3a 100644 (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
index 25124be1d3d39e2d2933b680d8bd86cc8ba90daf..622be93f996d9e306b710b461ee37c46070a1ae9 100644 (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);
index 18dd49e304e7d07aeca7a4cb971f238831487ac2..7b43eec497ec436ad592f60558d08d820b148f28 100644 (file)
@@ -4,5 +4,6 @@ UINT64 PASSED
 BOOL PASSED
 DOUBLE PASSED
 STRING PASSED
+STRINGTODOUBLE PASSED
 PARSE AND SET PASSED
 PASSED