From: Alexandru Ardelean Date: Tue, 20 Apr 2021 13:07:26 +0000 (+0300) Subject: tests: test1: add test cases for json_object_array_insert_idx() X-Git-Tag: json-c-0.17-20230812~10^2~12 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=d5c5b2caec671dd2235dddac5b9c790e36cb0671;p=thirdparty%2Fjson-c.git tests: test1: add test cases for json_object_array_insert_idx() This change adds a few test cases to test the behavior of the new json_object_array_insert_idx() function, to make sure it behaves according to specification in doc-string. This test uses assert() vs the old method of comparing outputs. This will cause the test to fail because the outputs won't match, since the assert() will kick in. Signed-off-by: Alexandru Ardelean --- diff --git a/tests/test1.c b/tests/test1.c index d28811bd..986861b6 100644 --- a/tests/test1.c +++ b/tests/test1.c @@ -189,6 +189,41 @@ void test_array_list_expand_internal(void) json_object_put(my_array); } +void test_array_insert_idx() +{ + json_object *my_string, *my_int, *my_null, *my_object, *my_array; + struct json_object *jo1; + + my_array = json_object_new_array(); + json_object_array_add(my_array, json_object_new_int(1)); + json_object_array_add(my_array, json_object_new_int(2)); + json_object_array_add(my_array, json_object_new_int(5)); + + json_object_array_insert_idx(my_array, 2, json_object_new_int(4)); + jo1 = json_tokener_parse("[1, 2, 4, 5]"); + assert(1 == json_object_equal(my_array, jo1)); + json_object_put(jo1); + + json_object_array_insert_idx(my_array, 2, json_object_new_int(3)); + + jo1 = json_tokener_parse("[1, 2, 3, 4, 5]"); + assert(1 == json_object_equal(my_array, jo1)); + json_object_put(jo1); + + json_object_array_insert_idx(my_array, 5, json_object_new_int(6)); + + jo1 = json_tokener_parse("[1, 2, 3, 4, 5, 6]"); + assert(1 == json_object_equal(my_array, jo1)); + json_object_put(jo1); + + json_object_array_insert_idx(my_array, 7, json_object_new_int(8)); + jo1 = json_tokener_parse("[1, 2, 3, 4, 5, 6, null, 8]"); + assert(1 == json_object_equal(my_array, jo1)); + json_object_put(jo1); + + json_object_put(my_array); +} + int main(int argc, char **argv) { json_object *my_string, *my_int, *my_null, *my_object, *my_array; @@ -253,6 +288,8 @@ int main(int argc, char **argv) json_object_put(my_array); + test_array_insert_idx(); + test_array_del_idx(); test_array_list_expand_internal();