Browse Source

Merge pull request #527 from dota17/arraylist_test

Arraylist testcase
tags/json-c-0.14-20200419
Eric Haszlakiewicz GitHub 5 years ago
parent
commit
ab5425a6a6
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 138 additions and 0 deletions
  1. +54
    -0
      tests/test1.c
  2. +14
    -0
      tests/test1.expected
  3. +14
    -0
      tests/test1Formatted_plain.expected
  4. +14
    -0
      tests/test1Formatted_pretty.expected
  5. +14
    -0
      tests/test1Formatted_spaced.expected
  6. +14
    -0
      tests/test1Formatted_spaced_pretty.expected
  7. +14
    -0
      tests/test1Formatted_spaced_pretty_pretty_tab.expected

+ 54
- 0
tests/test1.c View File

@@ -136,6 +136,53 @@ void test_array_del_idx()
json_object_put(my_array);
}

void test_array_list_expand_internal(void);
void test_array_list_expand_internal()
{
int rc;
size_t ii;
size_t idx;
json_object *my_array;
#ifdef TEST_FORMATTED
int sflags = 0;
#endif

my_array = make_array();
printf("my_array=\n");
for (ii = 0; ii < json_object_array_length(my_array); ii++)
{
json_object *obj = json_object_array_get_idx(my_array, ii);
printf("\t[%d]=%s\n", (int)ii, json_object_to_json_string(obj));
}
printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array));

/* Put iNdex < array->size, no expand. */
rc = json_object_array_put_idx(my_array, 5, json_object_new_int(6));
printf("put_idx(5,6)=%d\n", rc);

/* array->size < Put Index < array->size * 2 <= SIZE_T_MAX, the size = array->size * 2. */
idx = ARRAY_LIST_DEFAULT_SIZE * 2 - 1;
rc = json_object_array_put_idx(my_array, idx, json_object_new_int(0));
printf("put_idx(%d,0)=%d\n", (int)(idx), rc);

/* array->size * 2 < Put Index, the size = Put Index. */
idx = ARRAY_LIST_DEFAULT_SIZE * 2 * 2 + 1;
rc = json_object_array_put_idx(my_array, idx, json_object_new_int(0));
printf("put_idx(%d,0)=%d\n", (int)(idx), rc);

/* SIZE_T_MAX <= Put Index, it will fail and the size will no change. */
idx = SIZE_MAX; // SIZE_MAX = SIZE_T_MAX
json_object *tmp = json_object_new_int(10);
rc = json_object_array_put_idx(my_array, idx, tmp);
printf("put_idx(SIZE_T_MAX,0)=%d\n", rc);
if (rc == -1)
{
json_object_put(tmp);
}

json_object_put(my_array);
}

int main(int argc, char **argv)
{
json_object *my_string, *my_int, *my_null, *my_object, *my_array;
@@ -201,6 +248,7 @@ int main(int argc, char **argv)
json_object_put(my_array);

test_array_del_idx();
test_array_list_expand_internal();

my_array = json_object_new_array();
json_object_array_add(my_array, json_object_new_int(3));
@@ -223,6 +271,12 @@ int main(int argc, char **argv)
}
printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array));

json_object *one = json_object_new_int(1);
json_object *result = json_object_array_bsearch(one, my_array, sort_fn);
printf("find json_object(1) in my_array successfully: %s\n",
json_object_to_json_string(result));
json_object_put(one);

my_object = json_object_new_object();
int rc = json_object_object_add(my_object, "abc", my_object);
if (rc != -1)


+ 14
- 0
tests/test1.expected View File

@@ -41,6 +41,19 @@ after del_idx(0,7)=0, my_array.to_string()=[ ]
after del_idx(0,8)=-1, my_array.to_string()=[ 1, 2, 3, 4, 5, null, 7 ]
after del_idx(0,6)=0, my_array.to_string()=[ 7 ]
after adding more entries, my_array.to_string()=[ 7, "s1", "s2", "s3" ]
my_array=
[0]=1
[1]=2
[2]=3
[3]=4
[4]=5
[5]=null
[6]=7
my_array.to_string()=[ 1, 2, 3, 4, 5, null, 7 ]
put_idx(5,6)=0
put_idx(63,0)=0
put_idx(129,0)=0
put_idx(SIZE_T_MAX,0)=-1
my_array=
[0]=3
[1]=1
@@ -55,6 +68,7 @@ my_array=
[3]=2
[4]=3
my_array.to_string()=[ null, 0, 1, 2, 3 ]
find json_object(1) in my_array successfully: 1
baz_obj.to_string()="fark"
my_object=
abc: 12


+ 14
- 0
tests/test1Formatted_plain.expected View File

@@ -41,6 +41,19 @@ after del_idx(0,7)=0, my_array.to_string()=[]
after del_idx(0,8)=-1, my_array.to_string()=[1,2,3,4,5,null,7]
after del_idx(0,6)=0, my_array.to_string()=[7]
after adding more entries, my_array.to_string()=[7,"s1","s2","s3"]
my_array=
[0]=1
[1]=2
[2]=3
[3]=4
[4]=5
[5]=null
[6]=7
my_array.to_string()=[1,2,3,4,5,null,7]
put_idx(5,6)=0
put_idx(63,0)=0
put_idx(129,0)=0
put_idx(SIZE_T_MAX,0)=-1
my_array=
[0]=3
[1]=1
@@ -55,6 +68,7 @@ my_array=
[3]=2
[4]=3
my_array.to_string()=[null,0,1,2,3]
find json_object(1) in my_array successfully: 1
baz_obj.to_string()="fark"
my_object=
abc: 12


+ 14
- 0
tests/test1Formatted_pretty.expected View File

@@ -47,6 +47,19 @@ after del_idx(0,7)=0, my_array.to_string()=[]
after del_idx(0,8)=-1, my_array.to_string()=[1,2,3,4,5,null,7]
after del_idx(0,6)=0, my_array.to_string()=[7]
after adding more entries, my_array.to_string()=[7,"s1","s2","s3"]
my_array=
[0]=1
[1]=2
[2]=3
[3]=4
[4]=5
[5]=null
[6]=7
my_array.to_string()=[1,2,3,4,5,null,7]
put_idx(5,6)=0
put_idx(63,0)=0
put_idx(129,0)=0
put_idx(SIZE_T_MAX,0)=-1
my_array=
[0]=3
[1]=1
@@ -73,6 +86,7 @@ my_array.to_string()=[
2,
3
]
find json_object(1) in my_array successfully: 1
baz_obj.to_string()="fark"
my_object=
abc: 12


+ 14
- 0
tests/test1Formatted_spaced.expected View File

@@ -41,6 +41,19 @@ after del_idx(0,7)=0, my_array.to_string()=[]
after del_idx(0,8)=-1, my_array.to_string()=[1,2,3,4,5,null,7]
after del_idx(0,6)=0, my_array.to_string()=[7]
after adding more entries, my_array.to_string()=[7,"s1","s2","s3"]
my_array=
[0]=1
[1]=2
[2]=3
[3]=4
[4]=5
[5]=null
[6]=7
my_array.to_string()=[1,2,3,4,5,null,7]
put_idx(5,6)=0
put_idx(63,0)=0
put_idx(129,0)=0
put_idx(SIZE_T_MAX,0)=-1
my_array=
[0]=3
[1]=1
@@ -55,6 +68,7 @@ my_array=
[3]=2
[4]=3
my_array.to_string()=[ null, 0, 1, 2, 3 ]
find json_object(1) in my_array successfully: 1
baz_obj.to_string()="fark"
my_object=
abc: 12


+ 14
- 0
tests/test1Formatted_spaced_pretty.expected View File

@@ -47,6 +47,19 @@ after del_idx(0,7)=0, my_array.to_string()=[]
after del_idx(0,8)=-1, my_array.to_string()=[1,2,3,4,5,null,7]
after del_idx(0,6)=0, my_array.to_string()=[7]
after adding more entries, my_array.to_string()=[7,"s1","s2","s3"]
my_array=
[0]=1
[1]=2
[2]=3
[3]=4
[4]=5
[5]=null
[6]=7
my_array.to_string()=[1,2,3,4,5,null,7]
put_idx(5,6)=0
put_idx(63,0)=0
put_idx(129,0)=0
put_idx(SIZE_T_MAX,0)=-1
my_array=
[0]=3
[1]=1
@@ -73,6 +86,7 @@ my_array.to_string()=[
2,
3
]
find json_object(1) in my_array successfully: 1
baz_obj.to_string()="fark"
my_object=
abc: 12


+ 14
- 0
tests/test1Formatted_spaced_pretty_pretty_tab.expected View File

@@ -47,6 +47,19 @@ after del_idx(0,7)=0, my_array.to_string()=[]
after del_idx(0,8)=-1, my_array.to_string()=[1,2,3,4,5,null,7]
after del_idx(0,6)=0, my_array.to_string()=[7]
after adding more entries, my_array.to_string()=[7,"s1","s2","s3"]
my_array=
[0]=1
[1]=2
[2]=3
[3]=4
[4]=5
[5]=null
[6]=7
my_array.to_string()=[1,2,3,4,5,null,7]
put_idx(5,6)=0
put_idx(63,0)=0
put_idx(129,0)=0
put_idx(SIZE_T_MAX,0)=-1
my_array=
[0]=3
[1]=1
@@ -73,6 +86,7 @@ my_array.to_string()=[
2,
3
]
find json_object(1) in my_array successfully: 1
baz_obj.to_string()="fark"
my_object=
abc: 12


Loading…
Cancel
Save