summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Haszlakiewicz <erh+git@nimenees.com>2020-04-14 10:28:16 -0400
committerGitHub <noreply@github.com>2020-04-14 10:28:16 -0400
commitab5425a6a600807d2c17eaa7d1dc7b4093f794aa (patch)
treea3d83cdb4437e48b0cd024c90c7690147a25a26d
parent511edb51a46bce4ba30d1daf2f76d43db58fdd22 (diff)
parent74bbe349c4cf0308ec0c9aac1ced038c73ec8545 (diff)
downloadjson-c-ab5425a6a600807d2c17eaa7d1dc7b4093f794aa.tar.gz
Merge pull request #527 from dota17/arraylist_test
Arraylist testcase
-rw-r--r--tests/test1.c54
-rw-r--r--tests/test1.expected14
-rw-r--r--tests/test1Formatted_plain.expected14
-rw-r--r--tests/test1Formatted_pretty.expected14
-rw-r--r--tests/test1Formatted_spaced.expected14
-rw-r--r--tests/test1Formatted_spaced_pretty.expected14
-rw-r--r--tests/test1Formatted_spaced_pretty_pretty_tab.expected14
7 files changed, 138 insertions, 0 deletions
diff --git a/tests/test1.c b/tests/test1.c
index 444386b..98546bf 100644
--- a/tests/test1.c
+++ b/tests/test1.c
@@ -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)
diff --git a/tests/test1.expected b/tests/test1.expected
index 4cafba6..4b6b252 100644
--- a/tests/test1.expected
+++ b/tests/test1.expected
@@ -42,6 +42,19 @@ 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
[2]=2
@@ -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
@@ -42,6 +42,19 @@ 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
[2]=2
@@ -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
@@ -48,6 +48,19 @@ 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
[2]=2
@@ -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
@@ -42,6 +42,19 @@ 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
[2]=2
@@ -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
@@ -48,6 +48,19 @@ 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
[2]=2
@@ -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
@@ -48,6 +48,19 @@ 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
[2]=2
@@ -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