]> git.ipfire.org Git - thirdparty/json-c.git/commitdiff
update json_object.c and testcase, delete json_object_uint_inc() 542/head
authordota17 <chenguopingdota@163.com>
Fri, 28 Feb 2020 03:18:48 +0000 (11:18 +0800)
committerdota17 <chenguopingdota@163.com>
Fri, 28 Feb 2020 09:51:56 +0000 (17:51 +0800)
json_object.c
json_object.h
tests/test_int_add.c
tests/test_int_add.expected

index bd820d2fe20e12a717a5533155e0f1b640a3f374..57d409347fa65f55f5dfd8a868c5d039e32adb7b 100644 (file)
@@ -594,8 +594,10 @@ json_bool json_object_get_boolean(const struct json_object *jso)
                switch(jso->o.c_int.cint_type) {
                case json_object_int_type_int64:
                        return (jso->o.c_int.cint.c_int64 != 0);
-               default:
+               case json_object_int_type_uint64:
                        return (jso->o.c_int.cint.c_uint64 != 0);
+               default:
+                       assert(0);
                }
        case json_type_double:
                return (jso->o.c_double != 0);
@@ -624,11 +626,9 @@ static int json_object_int_to_json_string(struct json_object* jso,
        /* room for 19 digits, the sign char, and a null term */
        char sbuf[21];
        if (jso->o.c_int.cint_type == json_object_int_type_int64)
-         snprintf(sbuf, sizeof(sbuf), "%" PRId64, jso->o.c_int.cint.c_int64);
-       else if (jso->o.c_int.cint_type == json_object_int_type_uint64)
-         snprintf(sbuf, sizeof(sbuf), "%" PRIu64, jso->o.c_int.cint.c_uint64);
+               snprintf(sbuf, sizeof(sbuf), "%" PRId64, jso->o.c_int.cint.c_int64);
        else
-               return 0;
+               snprintf(sbuf, sizeof(sbuf), "%" PRIu64, jso->o.c_int.cint.c_uint64);
        return printbuf_memappend (pb, sbuf, strlen(sbuf));
 }
 
@@ -653,7 +653,7 @@ int32_t json_object_get_int(const struct json_object *jso)
   o_type = jso->o_type;
   if (jso->o.c_int.cint_type == json_object_int_type_int64) {
     cint64 = jso->o.c_int.cint.c_int64;
-  } else if (jso->o.c_int.cint_type == json_object_int_type_uint64) {
+  } else {
     if (jso->o.c_int.cint.c_uint64 >= INT64_MAX)
       cint64 = INT64_MAX;
     cint64 = (int64_t)jso->o.c_int.cint.c_uint64;
@@ -734,7 +734,7 @@ int64_t json_object_get_int64(const struct json_object *jso)
                                return INT64_MAX;
                        return (int64_t)jso->o.c_int.cint.c_uint64;
                default:
-                       return 0;
+                       assert(0);
                }
        case json_type_double:
                // INT64_MAX can't be exactly represented as a double
@@ -772,7 +772,7 @@ uint64_t json_object_get_uint64(const struct json_object *jso)
                case json_object_int_type_uint64:
                        return jso->o.c_int.cint.c_uint64;
                default:
-                       return 0;
+                       assert(0);
                }
        case json_type_double:
                // UINT64_MAX can't be exactly represented as a double
@@ -830,40 +830,13 @@ int json_object_int_inc(struct json_object *jso, int64_t val) {
                        jso->o.c_int.cint.c_int64 = (int64_t)jso->o.c_int.cint.c_uint64 + val;
                        jso->o.c_int.cint_type = json_object_int_type_int64;
                } else if (val < 0 && jso->o.c_int.cint.c_uint64 >= (uint64_t)(-val)) {
-                       jso->o.c_int.cint.c_uint64 = jso->o.c_int.cint.c_uint64 - (uint64_t)(-val);
-               } else {
-                       jso->o.c_int.cint.c_uint64 += val;
-               }
-               return 1;
-       default:
-               return 0;
-       }
-}
-
-int json_object_uint_inc(struct json_object *jso, uint64_t val) {
-       if (!jso || jso->o_type != json_type_int)
-               return 0;
-       switch(jso->o.c_int.cint_type) {
-       case json_object_int_type_int64:
-               if ((uint64_t)jso->o.c_int.cint.c_uint64 + val> UINT64_MAX) {
-                       jso->o.c_int.cint.c_uint64 = UINT64_MAX;
-                       jso->o.c_int.cint_type = json_object_int_type_uint64;
-               } else if ((uint64_t)jso->o.c_int.cint.c_uint64 + val < INT64_MAX) {
-                       jso->o.c_int.cint.c_int64 += (int64_t)val;
-               } else {
-                       jso->o.c_int.cint.c_uint64 =(uint64_t) jso->o.c_int.cint.c_uint64 + val;
-                       jso->o.c_int.cint_type = json_object_int_type_uint64;
-               }
-               return 1;
-       case json_object_int_type_uint64:
-               if (jso->o.c_int.cint.c_uint64 > UINT64_MAX - val) {
-                       jso->o.c_int.cint.c_uint64 = UINT64_MAX;
+                       jso->o.c_int.cint.c_uint64 -= (uint64_t)(-val);
                } else {
                        jso->o.c_int.cint.c_uint64 += val;
                }
                return 1;
        default:
-               return 0;
+               assert(0);
        }
 }
 
@@ -1094,8 +1067,8 @@ double json_object_get_double(const struct json_object *jso)
     case json_object_int_type_uint64:
       return jso->o.c_int.cint.c_uint64;
     default:
-      return 0.0;
-               }
+      assert(0);
+    }
   case json_type_boolean:
     return jso->o.c_boolean;
   case json_type_string:
@@ -1475,26 +1448,22 @@ int json_object_equal(struct json_object* jso1, struct json_object* jso2)
 
                case json_type_int:
                        if (jso1->o.c_int.cint_type == json_object_int_type_int64) {
-                               if (jso2->o.c_int.cint_type == json_object_int_type_int64)
+                               if (jso2->o.c_int.cint_type == json_object_int_type_int64) {
                                        return (jso1->o.c_int.cint.c_int64 == jso2->o.c_int.cint.c_int64);
-                               if (jso2->o.c_int.cint_type == json_object_int_type_uint64) {
+                               } else {
                                        if (jso1->o.c_int.cint.c_int64 < 0)
                                                return 0;
                                        return ((uint64_t)jso1->o.c_int.cint.c_int64 == jso2->o.c_int.cint.c_uint64);
                                }
-                               return 0;
-                       }
-                       if (jso1->o.c_int.cint_type == json_object_int_type_uint64) {
-                               if (jso2->o.c_int.cint_type == json_object_int_type_uint64)
+                       } else {
+                               if (jso2->o.c_int.cint_type == json_object_int_type_uint64) {
                                        return (jso1->o.c_int.cint.c_uint64 == jso2->o.c_int.cint.c_uint64);
-                               if (jso2->o.c_int.cint_type == json_object_int_type_int64){
+                               } else {
                                        if (jso2->o.c_int.cint.c_int64 < 0)
                                                return 0;
                                        return (jso1->o.c_int.cint.c_uint64 == (uint64_t)jso2->o.c_int.cint.c_int64);
                                }
-                               return 0;
                        }
-                       return 0;
 
                case json_type_string:
                        return (jso1->o.c_string.len == jso2->o.c_string.len &&
@@ -1563,6 +1532,8 @@ int json_c_shallow_copy_default(json_object *src, json_object *parent, const cha
                case json_object_int_type_uint64:
                        *dst = json_object_new_uint64(src->o.c_int.cint.c_uint64);
                        break;
+               default:
+                       assert(0);
                }
                break;
 
index 297a2e80199e6e32c9c7a5a853c70df013aae9f7..0b8a3027b5e7dee9cdcc402abab3a739bd609963 100644 (file)
@@ -752,20 +752,6 @@ JSON_EXPORT int json_object_set_int(struct json_object *obj,int new_value);
  */
 JSON_EXPORT int json_object_int_inc(struct json_object *obj, int64_t val);
 
-/** Increment a json_type_uint object by the given amount, which may be negative.
- *
- * If the type of obj is not json_type_uint then 0 is returned with no further
- * action taken.
- * If the addition would result in a overflow, the object value
- * is set to UINT64_MAX.
- * Neither overflow nor underflow affect the return value.
- *
- * @param obj the json_object instance
- * @param val the value to add
- * @returns 1 if the increment succeded, 0 otherwise
- */
-JSON_EXPORT int json_object_uint_inc(struct json_object *obj, uint64_t val);
-
 /** Get the int value of a json_object
  *
  * The type is coerced to a int64 if the passed object is not a int64.
index bd76ac53208848b51a01757fdd7e6c6bedd6c937..82f9d992768ce55ae7c9490f6ab5cc605ed12448 100644 (file)
@@ -25,8 +25,6 @@ int main(int argc, char **argv)
        tmp = json_object_new_int64(321321321);
        json_object_int_inc(tmp, 321321321);
        assert(json_object_get_int(tmp) == 642642642);
-       json_object_uint_inc(tmp, 321321321U);
-       assert(json_object_get_int(tmp) == 963963963);
        json_object_put(tmp);
        printf("INT64 ADD PASSED\n");
        tmp = json_object_new_int64(INT64_MAX);
@@ -37,16 +35,6 @@ int main(int argc, char **argv)
        assert(json_object_get_int64(tmp) == INT64_MAX);
        assert(json_object_get_uint64(tmp) == (uint64_t)INT64_MAX);
        json_object_put(tmp);
-       tmp = json_object_new_int64(100);
-       json_object_uint_inc(tmp, 100);
-       assert(json_object_get_int64(tmp) == 200);
-       json_object_uint_inc(tmp, (uint64_t)INT64_MAX);
-       assert(json_object_get_int64(tmp) == INT64_MAX);
-       assert(json_object_get_uint64(tmp) == (uint64_t)INT64_MAX+200);
-       json_object_uint_inc(tmp, UINT64_MAX);
-       assert(json_object_get_int64(tmp) == INT64_MAX);
-       assert(json_object_get_uint64(tmp) == UINT64_MAX);
-       json_object_put(tmp);
        printf("INT64 ADD OVERFLOW PASSED\n");
        tmp = json_object_new_int64(INT64_MIN);
        json_object_int_inc(tmp, -100);
@@ -55,10 +43,6 @@ int main(int argc, char **argv)
        assert(json_object_get_int64(tmp) != INT64_MIN);
        json_object_put(tmp);
        printf("INT64 ADD UNDERFLOW PASSED\n");
-       tmp = json_object_new_uint64(321321321);
-       json_object_uint_inc(tmp, 321321321);
-       assert(json_object_get_uint64(tmp) == 642642642);
-       json_object_put(tmp);
   // uint64 + negative int64--> negative int64
        tmp = json_object_new_uint64(400);
        json_object_int_inc(tmp, -200);
@@ -66,15 +50,6 @@ int main(int argc, char **argv)
        assert(json_object_get_uint64(tmp) == 200);
        json_object_put(tmp);
        printf("UINT64 ADD PASSED\n");
-       tmp = json_object_new_uint64(UINT64_MAX);
-       json_object_uint_inc(tmp, 100);
-       assert(json_object_get_uint64(tmp) == UINT64_MAX);
-       json_object_put(tmp);
-       tmp = json_object_new_uint64(UINT64_MAX - 100);
-       json_object_uint_inc(tmp, 200);
-       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 f9348d4b097a085cd38dd99b8d411b4952dbbf3a..b2cbdb368b5d6481b9301759e898f57a07e3603c 100644 (file)
@@ -5,6 +5,5 @@ INT64 ADD PASSED
 INT64 ADD OVERFLOW PASSED
 INT64 ADD UNDERFLOW PASSED
 UINT64 ADD PASSED
-UINT64 ADD OVERFLOW PASSED
 UINT64 ADD UNDERFLOW PASSED
 PASSED