From 76e1472808d84ea50fe9b540874783f88bc8fa37 Mon Sep 17 00:00:00 2001 From: chenguoping Date: Thu, 2 Jan 2020 19:30:00 +0800 Subject: [PATCH 1/3] testcase for array_list --- tests/test1.c | 46 +++++++++++++++++++ tests/test1.expected | 14 ++++++ tests/test1Formatted_plain.expected | 14 ++++++ tests/test1Formatted_pretty.expected | 14 ++++++ tests/test1Formatted_spaced.expected | 14 ++++++ tests/test1Formatted_spaced_pretty.expected | 14 ++++++ ...ormatted_spaced_pretty_pretty_tab.expected | 14 ++++++ 7 files changed, 130 insertions(+) diff --git a/tests/test1.c b/tests/test1.c index 444386b..68f4aaa 100644 --- a/tests/test1.c +++ b/tests/test1.c @@ -136,6 +136,48 @@ 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 + rc = json_object_array_put_idx(my_array, idx, json_object_new_int(0)); + printf("put_idx(SIZE_T_MAX,0)=%d\n", rc); + + 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 +243,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)); @@ -222,6 +265,9 @@ int main(int argc, char **argv) printf("\t[%d]=%s\n", (int)i, json_object_to_json_string(obj)); } printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array)); + + json_object* result = json_object_array_bsearch(json_object_new_int(1), my_array, sort_fn); + printf("find json_object(1) in my_array successfully: %s\n", json_object_to_json_string(result)); my_object = json_object_new_object(); int rc = json_object_object_add(my_object, "abc", my_object); diff --git a/tests/test1.expected b/tests/test1.expected index 4cafba6..4b6b252 100644 --- a/tests/test1.expected +++ b/tests/test1.expected @@ -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 diff --git a/tests/test1Formatted_plain.expected b/tests/test1Formatted_plain.expected index 6cbf356..128e274 100644 --- a/tests/test1Formatted_plain.expected +++ b/tests/test1Formatted_plain.expected @@ -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 diff --git a/tests/test1Formatted_pretty.expected b/tests/test1Formatted_pretty.expected index 766b04f..b67185f 100644 --- a/tests/test1Formatted_pretty.expected +++ b/tests/test1Formatted_pretty.expected @@ -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 diff --git a/tests/test1Formatted_spaced.expected b/tests/test1Formatted_spaced.expected index 7ac0fb2..fe6979d 100644 --- a/tests/test1Formatted_spaced.expected +++ b/tests/test1Formatted_spaced.expected @@ -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 diff --git a/tests/test1Formatted_spaced_pretty.expected b/tests/test1Formatted_spaced_pretty.expected index 0b1f220..104a554 100644 --- a/tests/test1Formatted_spaced_pretty.expected +++ b/tests/test1Formatted_spaced_pretty.expected @@ -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 diff --git a/tests/test1Formatted_spaced_pretty_pretty_tab.expected b/tests/test1Formatted_spaced_pretty_pretty_tab.expected index 5434544..f9a8e87 100644 --- a/tests/test1Formatted_spaced_pretty_pretty_tab.expected +++ b/tests/test1Formatted_spaced_pretty_pretty_tab.expected @@ -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 From 110c60fcdd1742c1a7f9dbee5c9fb971790bb9e7 Mon Sep 17 00:00:00 2001 From: chenguoping Date: Fri, 3 Jan 2020 16:56:43 +0800 Subject: [PATCH 2/3] fix valgrind errors --- tests/test1.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/tests/test1.c b/tests/test1.c index 68f4aaa..7cf6aba 100644 --- a/tests/test1.c +++ b/tests/test1.c @@ -172,8 +172,13 @@ void test_array_list_expand_internal() /* SIZE_T_MAX <= Put Index, it will fail and the size will no change. */ idx = SIZE_MAX; // SIZE_MAX = SIZE_T_MAX - rc = json_object_array_put_idx(my_array, idx, json_object_new_int(0)); + 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); } @@ -266,8 +271,10 @@ int main(int argc, char **argv) } printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array)); - json_object* result = json_object_array_bsearch(json_object_new_int(1), my_array, sort_fn); + 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); From 74bbe349c4cf0308ec0c9aac1ced038c73ec8545 Mon Sep 17 00:00:00 2001 From: dota17 Date: Tue, 14 Apr 2020 09:20:51 +0800 Subject: [PATCH 3/3] clang-format test1.c --- tests/test1.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/tests/test1.c b/tests/test1.c index 7cf6aba..98546bf 100644 --- a/tests/test1.c +++ b/tests/test1.c @@ -149,37 +149,37 @@ void test_array_list_expand_internal() my_array = make_array(); printf("my_array=\n"); - for(ii = 0; ii < json_object_array_length(my_array); ii++) + 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); + 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); } @@ -270,10 +270,11 @@ int main(int argc, char **argv) printf("\t[%d]=%s\n", (int)i, json_object_to_json_string(obj)); } 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 *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();