]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
tests: Add GenericArray (GPtrArray) tests
authorRico Tzschichholz <ricotz@ubuntu.com>
Thu, 8 Nov 2018 11:10:07 +0000 (12:10 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Thu, 8 Nov 2018 11:12:31 +0000 (12:12 +0100)
tests/Makefile.am
tests/basic-types/gptrarray.vala [new file with mode: 0644]

index 8002849423046445d6df38922a5458fd5d84a000..7b0628ca9979ccf9a65f4baa7074489e911dfc28 100644 (file)
@@ -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 (file)
index 0000000..f32a733
--- /dev/null
@@ -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);
+}