From: Rico Tzschichholz Date: Thu, 8 Nov 2018 11:10:07 +0000 (+0100) Subject: tests: Add GenericArray (GPtrArray) tests X-Git-Tag: 0.42.4~75 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=55fe7b7aab324f9cfa279520d0bc2d70ca22ea72;p=thirdparty%2Fvala.git tests: Add GenericArray (GPtrArray) tests --- 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 (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 (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); +}