[vala/0.36] tests: Add GenericArray (GPtrArray) tests



commit 4055e0310b2b687265caaacbdb5fb32ae56cbca3
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 c80601647..89a3f83a3 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -24,6 +24,7 @@ TESTS = \
        basic-types/sizeof.vala \
        basic-types/garray.vala \
        basic-types/glists.vala \
+       basic-types/gptrarray.vala \
        basic-types/gvariants.vala \
        basic-types/bug571486.vala \
        basic-types/bug591552.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]