]> git.ipfire.org Git - thirdparty/json-c.git/commitdiff
Add json_object_add_int functions 363/head
authorJuraj Vijtiuk <juraj.vijtiuk@sartura.hr>
Thu, 14 Sep 2017 12:05:33 +0000 (08:05 -0400)
committerJuraj Vijtiuk <juraj.vijtiuk@sartura.hr>
Thu, 14 Sep 2017 13:36:12 +0000 (09:36 -0400)
json_object.c
json_object.h
tests/Makefile.am
tests/test_int_add.c [new file with mode: 0644]
tests/test_int_add.expected [new file with mode: 0644]
tests/test_int_add.test [new symlink]

index 8cd5922e349f0d9cf4536d4ada6bd67991635f56..df20de15560c3f3220d459d3369f2c5d949eb325 100644 (file)
@@ -666,6 +666,19 @@ 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)
 {
@@ -711,6 +724,19 @@ 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) {
+       if (!jso || jso->o_type != json_type_int)
+               return 0;
+       if (val > 0 && jso->o.c_int64 > INT64_MAX - val) {
+               jso->o.c_int64 = INT64_MAX;
+       } else if (val < 0 && jso->o.c_int64 < INT64_MIN - val) {
+               jso->o.c_int64 = INT64_MIN;
+       } else {
+               jso->o.c_int64 += val;
+       }
+       return 1;
+}
+
 /* json_object_double */
 
 #if defined(HAVE___THREAD)
index 7399be8604e7e4d1539761bf7e50034c297bf39b..871fd88162e9a608b307fb7c8297e7642b607109 100644 (file)
@@ -718,6 +718,22 @@ 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
+ *
+ * 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 addition would result in a overflow, the object value
+ * is set to INT32_MAX.
+ * If the addition would result in a underflow, the object value
+ * is set to INT32_MIN.
+ *
+ * @param obj the json_object instance
+ * @param val the value to add
+ * @returns 1 if the addition succeded, 0 otherwise
+ */
+JSON_EXPORT int json_object_add_int(struct json_object *obj, int val);
+
 
 /** Get the int value of a json_object
  *
@@ -747,6 +763,21 @@ 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 */
 
index 917d20f679029aacc66d32f645ce427b38becfcb..c6a298345af3069eb232fe249bbf2aaf3d3702ab 100644 (file)
@@ -25,6 +25,7 @@ TESTS+= test_compare.test
 TESTS+= test_set_value.test
 TESTS+= test_visit.test
 TESTS+= test_json_pointer.test
+TESTS+= test_int_add.test
 
 check_PROGRAMS=
 check_PROGRAMS += $(TESTS:.test=)
diff --git a/tests/test_int_add.c b/tests/test_int_add.c
new file mode 100644 (file)
index 0000000..14e935c
--- /dev/null
@@ -0,0 +1,41 @@
+#include <assert.h>
+#include <stdio.h>
+
+#include "json.h"
+
+int main(int argc, char **argv)
+{
+       json_object *tmp = json_object_new_int(123);
+       json_object_add_int(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);
+       assert(json_object_get_int(tmp) == INT32_MAX);
+       json_object_put(tmp);
+       printf("INT ADD OVERFLOW PASSED\n");
+       tmp = json_object_new_int(INT32_MIN);
+       json_object_add_int(tmp, -100);
+       assert(json_object_get_int(tmp) == INT32_MIN);
+       json_object_put(tmp);
+       printf("INT ADD UNDERFLOW PASSED\n");
+       tmp = json_object_new_int64(321321321);
+       json_object_add_int(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);
+       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);
+       assert(json_object_get_int64(tmp) == INT64_MIN);
+       json_object_put(tmp);
+       printf("INT64 ADD UNDERFLOW PASSED\n");
+
+       printf("PASSED\n");
+       return 0;
+}
diff --git a/tests/test_int_add.expected b/tests/test_int_add.expected
new file mode 100644 (file)
index 0000000..61c205c
--- /dev/null
@@ -0,0 +1,7 @@
+INT ADD PASSED
+INT ADD OVERFLOW PASSED
+INT ADD UNDERFLOW PASSED
+INT64 ADD PASSED
+INT64 ADD OVERFLOW PASSED
+INT64 ADD UNDERFLOW PASSED
+PASSED
diff --git a/tests/test_int_add.test b/tests/test_int_add.test
new file mode 120000 (symlink)
index 0000000..58a13f4
--- /dev/null
@@ -0,0 +1 @@
+test_basic.test
\ No newline at end of file