]> git.ipfire.org Git - thirdparty/json-c.git/commitdiff
tests: test1: add test cases for json_object_array_insert_idx()
authorAlexandru Ardelean <ardeleanalex@gmail.com>
Tue, 20 Apr 2021 13:07:26 +0000 (16:07 +0300)
committerEric Hawicz <erh+git@nimenees.com>
Tue, 1 Aug 2023 02:17:30 +0000 (22:17 -0400)
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 <ardeleanalex@gmail.com>
tests/test1.c

index d28811bd5238435a163174ff4cd295451b668ae8..986861b62bf4d19ea6ee9ce5a118b6629871d700 100644 (file)
@@ -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();