[vala/tintou/garray: 12/12] tests: Add more GLib.Array tests
- From: Rico Tzschichholz <ricotz src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [vala/tintou/garray: 12/12] tests: Add more GLib.Array tests
- Date: Fri, 16 Nov 2018 17:49:24 +0000 (UTC)
commit d85d5fda1b2aedd0dea3a144fa442962bb452fc1
Author: Corentin Noël <corentin elementary io>
Date: Fri Nov 16 14:55:00 2018 +0100
tests: Add more GLib.Array tests
tests/basic-types/garray.vala | 66 ++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 65 insertions(+), 1 deletion(-)
---
diff --git a/tests/basic-types/garray.vala b/tests/basic-types/garray.vala
index 055ba6a2a..2d2eb008d 100644
--- a/tests/basic-types/garray.vala
+++ b/tests/basic-types/garray.vala
@@ -1,7 +1,12 @@
class Foo : Object {
}
-void main () {
+struct FooStruct {
+ string content;
+ Foo object;
+}
+
+void test_garray () {
var array = new GLib.Array<Foo> ();
var foo = new Foo ();
@@ -22,3 +27,62 @@ void main () {
array.remove_range (0, 1);
assert (foo.ref_count == 1);
}
+
+void test_int_garray () {
+ var array = new GLib.Array<int> ();
+ // g_array_append_val() is a macro which uses a reference to the value parameter and thus can't use
constants.
+ // FIXME: allow appending constants in Vala
+ int val = 1;
+ array.prepend_val (val);
+ val++;
+ array.append_val (val);
+ val++;
+ array.insert_val (2, val);
+ assert (array.index (0) == 1);
+ assert (array.index (1) == 2);
+ assert (array.index (2) == 3);
+ assert (array.length == 3);
+}
+
+GLib.Array<FooStruct?> create_struct_garray () {
+ FooStruct foo = { "foo", new Foo () };
+ var array = new GLib.Array<FooStruct?> ();
+ array.append_val (foo);
+ return array;
+}
+
+void test_struct_garray () {
+ var array = create_struct_garray ();
+ assert (array.length == 1);
+ assert (array.index (0).content == "foo");
+ assert (array.index (0).object.ref_count == 1);
+ Foo f = array.index (0).object;
+ assert (f.ref_count == 2);
+ array = null;
+ assert (f.ref_count == 1);
+}
+
+void test_object_garray () {
+ var foo = new Foo ();
+ {
+ var array = new GLib.Array<Foo> ();
+ array.append_val (foo);
+ assert (foo.ref_count == 2);
+ array = null;
+ }
+ assert (foo.ref_count == 1);
+ {
+ var array = new GLib.Array<unowned Foo> ();
+ array.append_val (foo);
+ assert (foo.ref_count == 1);
+ array = null;
+ }
+ assert (foo.ref_count == 1);
+}
+
+void main () {
+ test_garray ();
+ test_int_garray ();
+ test_struct_garray ();
+ test_object_garray ();
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]