From 3628f16dd6b68ba6bc18d22c4cd75be875a5e757 Mon Sep 17 00:00:00 2001 From: Eric Haszlakiewicz Date: Mon, 27 Nov 2017 17:57:36 -0500 Subject: [PATCH] Rename json_object_add_int() to json_object_int_inc() and eliminate the "int64" variant since we store 64-bit values internally anyway. --- .gitignore | 1 + json_object.c | 16 +--------------- json_object.h | 34 +++++++++------------------------- tests/test_int_add.c | 18 ++++++++++++------ 4 files changed, 23 insertions(+), 46 deletions(-) diff --git a/.gitignore b/.gitignore index 393b6eb3b3..80a2309a56 100644 --- a/.gitignore +++ b/.gitignore @@ -27,6 +27,7 @@ /tests/test_compare /tests/test_double_serializer /tests/test_float +/tests/test_int_add /tests/test_json_pointer /tests/test_locale /tests/test_null diff --git a/json_object.c b/json_object.c index df20de1556..ee6efe5d25 100644 --- a/json_object.c +++ b/json_object.c @@ -666,20 +666,6 @@ int json_object_set_int(struct json_object *jso,int new_value){ return 1; } -int json_object_add_int(struct json_object *jso, int val) { - if (!jso || jso->o_type != json_type_int) - return 0; - if (val > 0 && jso->o.c_int64 > INT32_MAX - val) { - jso->o.c_int64 = INT32_MAX; - } else if (val < 0 && jso->o.c_int64 < INT32_MIN - val) { - jso->o.c_int64 = INT32_MIN; - } else { - jso->o.c_int64 += val; - } - return 1; -} - - struct json_object* json_object_new_int64(int64_t i) { struct json_object *jso = json_object_new(json_type_int); @@ -724,7 +710,7 @@ int json_object_set_int64(struct json_object *jso,int64_t new_value){ return 1; } -int json_object_add_int64(struct json_object *jso, int64_t val) { +int json_object_int_inc(struct json_object *jso, int64_t val) { if (!jso || jso->o_type != json_type_int) return 0; if (val > 0 && jso->o.c_int64 > INT64_MAX - val) { diff --git a/json_object.h b/json_object.h index 1f45286905..4ca1420488 100644 --- a/json_object.h +++ b/json_object.h @@ -710,7 +710,7 @@ JSON_EXPORT int32_t json_object_get_int(const struct json_object *obj); * * The type of obj is checked to be a json_type_int and 0 is returned * if it is not without any further actions. If type of obj is json_type_int - * the obect value is chaned to new_value + * the obect value is changed to new_value * * @param obj the json_object instance * @param new_value the value to be set @@ -718,21 +718,21 @@ JSON_EXPORT int32_t json_object_get_int(const struct json_object *obj); */ JSON_EXPORT int json_object_set_int(struct json_object *obj,int new_value); -/** Add the int value to the value of a json object +/** Increment a json_type_int object by the given amount, which may be negative. * - * The type of obj is checked to be a json type int and 0 is returned - * if it is not without any further actions. If the type of obj is - * json_type_int the int value is added to the object value. + * If the type of obj is not json_type_int then 0 is returned with no further + * action taken. * If the addition would result in a overflow, the object value - * is set to INT32_MAX. + * is set to INT64_MAX. * If the addition would result in a underflow, the object value - * is set to INT32_MIN. + * is set to INT64_MIN. + * Neither overflow nor underflow affect the return value. * * @param obj the json_object instance * @param val the value to add - * @returns 1 if the addition succeded, 0 otherwise + * @returns 1 if the increment succeded, 0 otherwise */ -JSON_EXPORT int json_object_add_int(struct json_object *obj, int val); +JSON_EXPORT int json_object_int_inc(struct json_object *obj, int64_t val); /** Get the int value of a json_object @@ -763,22 +763,6 @@ JSON_EXPORT int64_t json_object_get_int64(const struct json_object *obj); */ JSON_EXPORT int json_object_set_int64(struct json_object *obj,int64_t new_value); -/** Add a int64_t value to the int64_t value of a json_object - * - * The type of obj is checked to be a json_type_int and 0 is returned - * if it is not without any further actions. If the type of obj is - * json_type_int the int64 value is added to the object value. - * If the addition to the object would result in a overflow the - * object value is set to INT64_MAX. - * If the addition would result in a underflow, the - * object value is set to INT64_MIN. - * - * @param obj the json_object instance - * @param val the int64_vaule to add - * @returns 1 if the addition succeeded, 0 otherwise - */ -JSON_EXPORT int json_object_add_int64(struct json_object *obj, int64_t val); - /* double type methods */ /** Create a new empty json_object of type json_type_double diff --git a/tests/test_int_add.c b/tests/test_int_add.c index 14e935c3ff..d064005755 100644 --- a/tests/test_int_add.c +++ b/tests/test_int_add.c @@ -6,33 +6,39 @@ int main(int argc, char **argv) { json_object *tmp = json_object_new_int(123); - json_object_add_int(tmp, 123); + json_object_int_inc(tmp, 123); assert(json_object_get_int(tmp) == 246); json_object_put(tmp); printf("INT ADD PASSED\n"); tmp = json_object_new_int(INT32_MAX); - json_object_add_int(tmp, 100); + json_object_int_inc(tmp, 100); assert(json_object_get_int(tmp) == INT32_MAX); + assert(json_object_get_int64(tmp) == (long)INT32_MAX + 100L); json_object_put(tmp); printf("INT ADD OVERFLOW PASSED\n"); tmp = json_object_new_int(INT32_MIN); - json_object_add_int(tmp, -100); + json_object_int_inc(tmp, -100); assert(json_object_get_int(tmp) == INT32_MIN); + assert(json_object_get_int64(tmp) == (long)INT32_MIN - 100L); json_object_put(tmp); printf("INT ADD UNDERFLOW PASSED\n"); tmp = json_object_new_int64(321321321); - json_object_add_int(tmp, 321321321); + json_object_int_inc(tmp, 321321321); assert(json_object_get_int(tmp) == 642642642); json_object_put(tmp); printf("INT64 ADD PASSED\n"); tmp = json_object_new_int64(INT64_MAX); - json_object_add_int64(tmp, 100); + json_object_int_inc(tmp, 100); assert(json_object_get_int64(tmp) == INT64_MAX); + json_object_int_inc(tmp, -100); + assert(json_object_get_int64(tmp) != INT64_MAX); json_object_put(tmp); printf("INT64 ADD OVERFLOW PASSED\n"); tmp = json_object_new_int64(INT64_MIN); - json_object_add_int64(tmp, -100); + json_object_int_inc(tmp, -100); assert(json_object_get_int64(tmp) == INT64_MIN); + json_object_int_inc(tmp, 100); + assert(json_object_get_int64(tmp) != INT64_MIN); json_object_put(tmp); printf("INT64 ADD UNDERFLOW PASSED\n");