From: dota17 Date: Fri, 28 Feb 2020 03:18:48 +0000 (+0800) Subject: update json_object.c and testcase, delete json_object_uint_inc() X-Git-Tag: json-c-0.14-20200419~52^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F542%2Fhead;p=thirdparty%2Fjson-c.git update json_object.c and testcase, delete json_object_uint_inc() --- diff --git a/json_object.c b/json_object.c index bd820d2f..57d40934 100644 --- a/json_object.c +++ b/json_object.c @@ -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; diff --git a/json_object.h b/json_object.h index 297a2e80..0b8a3027 100644 --- a/json_object.h +++ b/json_object.h @@ -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. diff --git a/tests/test_int_add.c b/tests/test_int_add.c index bd76ac53..82f9d992 100644 --- a/tests/test_int_add.c +++ b/tests/test_int_add.c @@ -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); diff --git a/tests/test_int_add.expected b/tests/test_int_add.expected index f9348d4b..b2cbdb36 100644 --- a/tests/test_int_add.expected +++ b/tests/test_int_add.expected @@ -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