[vala/0.42] tests: Add GenericArray (GPtrArray) tests
- From: Rico Tzschichholz <ricotz src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [vala/0.42] tests: Add GenericArray (GPtrArray) tests
- Date: Mon, 26 Nov 2018 09:11:41 +0000 (UTC)
commit 55fe7b7aab324f9cfa279520d0bc2d70ca22ea72
Author: Rico Tzschichholz <ricotz ubuntu com>
Date: Thu Nov 8 12:10:07 2018 +0100
tests: Add GenericArray (GPtrArray) tests
tests/Makefile.am | 1 +
tests/basic-types/gptrarray.vala | 108 +++++++++++++++++++++++++++++++++++++++
2 files changed, 109 insertions(+)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index c2f538961..ee9ccd8d0 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -30,6 +30,7 @@ TESTS = \
basic-types/sizeof.vala \
basic-types/garray.vala \
basic-types/glists.vala \
+ basic-types/gptrarray.vala \
basic-types/gvariants.vala \
basic-types/bug570846.test \
basic-types/bug571486.vala \
diff --git a/tests/basic-types/gptrarray.vala b/tests/basic-types/gptrarray.vala
new file mode 100644
index 000000000..f32a7330c
--- /dev/null
+++ b/tests/basic-types/gptrarray.vala
@@ -0,0 +1,108 @@
+class Foo : Object {
+ public int i;
+ public Foo (int i) {
+ this.i = i;
+ }
+}
+
+int compare_foo (Foo a, Foo b) {
+ return b.i - a.i;
+}
+
+void main () {
+ var foo1 = new Foo (5);
+ var foo2 = new Foo (4);
+ var foo3 = new Foo (3);
+ var foo4 = new Foo (2);
+ var foo5 = new Foo (1);
+
+ {
+ var array = new GenericArray<Foo> (8);
+ array.add (foo1);
+ assert (foo1.ref_count == 2);
+ array.add (foo2);
+ assert (foo2.ref_count == 2);
+ array.add (foo3);
+ assert (foo3.ref_count == 2);
+ assert (array.length == 3);
+
+ assert (foo2 == array.get (1));
+ array.set (1, foo4);
+ assert (foo4 == array.get (1));
+ assert (foo2.ref_count == 1);
+ assert (foo4.ref_count == 2);
+ assert (array.length == 3);
+
+ array.insert (2, foo5);
+ assert (foo5.ref_count == 2);
+ assert (array.length == 4);
+
+ assert (array.remove (foo4));
+ assert (foo4.ref_count == 1);
+ assert (array.length == 3);
+
+ uint index;
+ assert (array.find (foo5, out index));
+ assert (foo5.ref_count == 2);
+ assert (index == 1);
+ assert (array.length == 3);
+
+ array.sort (compare_foo);
+ array.sort_with_data (compare_foo);
+
+ assert (array.length == 3);
+ array.length = 0;
+ assert (array.length == 0);
+ }
+
+ assert (foo1.ref_count == 1);
+ assert (foo2.ref_count == 1);
+ assert (foo3.ref_count == 1);
+ assert (foo4.ref_count == 1);
+ assert (foo5.ref_count == 1);
+
+ {
+ var array = new GenericArray<weak Foo> (8);
+ array.add (foo1);
+ assert (foo1.ref_count == 1);
+ array.add (foo2);
+ assert (foo2.ref_count == 1);
+ array.add (foo3);
+ assert (foo3.ref_count == 1);
+ assert (array.length == 3);
+
+ assert (foo2 == array.get (1));
+ array.set (1, foo4);
+ assert (foo4 == array.get (1));
+ assert (foo2.ref_count == 1);
+ assert (foo4.ref_count == 1);
+ assert (array.length == 3);
+
+ array.insert (2, foo5);
+ assert (foo5.ref_count == 1);
+ assert (array.length == 4);
+
+ assert (array.remove (foo4));
+ assert (foo4.ref_count == 1);
+ assert (array.length == 3);
+
+ uint index;
+ assert (array.find (foo5, out index));
+ assert (foo5.ref_count == 1);
+ assert (index == 1);
+ assert (array.length == 3);
+
+ array.sort (compare_foo);
+ array.sort_with_data (compare_foo);
+
+ assert (array.length == 3);
+ array.length = 0;
+ assert (array.length == 0);
+ }
+
+ assert (foo1.ref_count == 1);
+ assert (foo2.ref_count == 1);
+ assert (foo3.ref_count == 1);
+ assert (foo4.ref_count == 1);
+ assert (foo5.ref_count == 1);
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]