summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCorentin Noël <corentin@elementary.io>2020-11-09 15:17:37 +0100
committerRico Tzschichholz <ricotz@ubuntu.com>2020-11-10 18:14:55 +0100
commit3a695246fed3eed1966d37fbeffbc1c48cd6c773 (patch)
treef01cb5260539472fe868f3dc617066d42fdcc4bf
parent21028c0f59b72e4538c33a7b9618e71474a665db (diff)
downloadvala-wip/foreach.tar.gz
tests: Add "GenericArray foreach" tests to increase coveragewip/foreach
-rw-r--r--tests/basic-types/gptrarray.vala48
-rw-r--r--tests/control-flow/foreach.vala36
2 files changed, 84 insertions, 0 deletions
diff --git a/tests/basic-types/gptrarray.vala b/tests/basic-types/gptrarray.vala
index 6d8aa2d1a..1b5d6c622 100644
--- a/tests/basic-types/gptrarray.vala
+++ b/tests/basic-types/gptrarray.vala
@@ -26,6 +26,30 @@ void main () {
assert (foo3.ref_count == 2);
assert (array.length == 3);
+ int loop_size = 0;
+ foreach (weak Foo element in array) {
+ loop_size++;
+ assert (element.ref_count == 2);
+ switch (loop_size) {
+ case 1: assert (element == foo1); break;
+ case 2: assert (element == foo2); break;
+ case 3: assert (element == foo3); break;
+ }
+ }
+ assert (loop_size == 3);
+
+ loop_size = 0;
+ foreach (Foo element in array) {
+ loop_size++;
+ assert (element.ref_count == 3);
+ switch (loop_size) {
+ case 1: assert (element == foo1); break;
+ case 2: assert (element == foo2); break;
+ case 3: assert (element == foo3); break;
+ }
+ }
+ assert (loop_size == 3);
+
assert (foo2 == array.get (1));
array.set (1, foo4);
assert (foo4 == array.get (1));
@@ -73,6 +97,30 @@ void main () {
assert (foo3.ref_count == 1);
assert (array.length == 3);
+ int loop_size = 0;
+ foreach (weak Foo element in array) {
+ loop_size++;
+ assert (element.ref_count == 1);
+ switch (loop_size) {
+ case 1: assert (element == foo1); break;
+ case 2: assert (element == foo2); break;
+ case 3: assert (element == foo3); break;
+ }
+ }
+ assert (loop_size == 3);
+
+ loop_size = 0;
+ foreach (Foo element in array) {
+ loop_size++;
+ assert (element.ref_count == 2);
+ switch (loop_size) {
+ case 1: assert (element == foo1); break;
+ case 2: assert (element == foo2); break;
+ case 3: assert (element == foo3); break;
+ }
+ }
+ assert (loop_size == 3);
+
assert (foo2 == array.get (1));
array.set (1, foo4);
assert (foo4 == array.get (1));
diff --git a/tests/control-flow/foreach.vala b/tests/control-flow/foreach.vala
index 6a36453fd..052e03d3c 100644
--- a/tests/control-flow/foreach.vala
+++ b/tests/control-flow/foreach.vala
@@ -33,6 +33,41 @@ void test_foreach_gvaluearray () {
test_unowned (array);
}
+void test_generic_array_owned (GenericArray<Value?> array) {
+ uint i = 0;
+
+ foreach (Value? item in array) {
+ i++;
+ }
+
+ assert (i == 3);
+}
+
+void test_generic_array_unowned (GenericArray<Value?> array) {
+ uint i = 0;
+
+ foreach (unowned Value? item in array) {
+ i++;
+ }
+
+ assert (i == 3);
+}
+
+void test_foreach_genericarray () {
+ Value value;
+ var array = new GenericArray<Value?> ();
+
+ value = 1;
+ array.add (value);
+ value = 2.0;
+ array.add (value);
+ value = "three";
+ array.add (value);
+
+ test_generic_array_owned (array);
+ test_generic_array_unowned (array);
+}
+
void test_foreach_multidim_array () {
int[,] foo = { { 1, 2 }, { 3, 4 }, { 5, 6 } };
string result = "";
@@ -70,6 +105,7 @@ void test_foreach_slice_array () {
void main () {
test_foreach_gvaluearray ();
+ test_foreach_genericarray ();
test_foreach_const_array ();
test_foreach_multidim_array ();
test_foreach_slice_array ();