Browse Source

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 <ardeleanalex@gmail.com>
tags/json-c-0.17-20230812
Alexandru Ardelean Eric Hawicz 4 years ago
parent
commit
d5c5b2caec
1 changed files with 37 additions and 0 deletions
  1. +37
    -0
      tests/test1.c

+ 37
- 0
tests/test1.c View 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();



Loading…
Cancel
Save