1class Foo : Object {
2}
3
4struct FooStruct {
5	string content;
6	Foo object;
7}
8
9void test_garray () {
10	var array = new GLib.Array<Foo> ();
11
12	var foo = new Foo ();
13	assert (foo.ref_count == 1);
14
15	array.append_val (foo);
16	assert (foo.ref_count == 2);
17	assert (array.index (0) == foo);
18	array.remove_index (0);
19	assert (foo.ref_count == 1);
20
21	array.append_val (foo);
22	assert (foo.ref_count == 2);
23	assert (array.index (0) == foo);
24	array.remove_index_fast (0);
25	assert (foo.ref_count == 1);
26
27	array.append_val (foo);
28	array.append_val (foo);
29	assert (foo.ref_count == 3);
30	assert (array.index (0) == foo);
31	assert (array.index (1) == foo);
32	array.remove_range (0, 2);
33	assert (foo.ref_count == 1);
34}
35
36void test_int_garray () {
37	var array = new GLib.Array<int> ();
38	// g_array_append_val() is a macro which uses a reference to the value parameter and thus can't use constants.
39	// FIXME: allow appending constants in Vala
40	int val = 1;
41	array.prepend_val (val);
42	val++;
43	array.append_val (val);
44	val++;
45	array.insert_val (2, val);
46	assert (array.index (0) == 1);
47	assert (array.index (1) == 2);
48	assert (array.index (2) == 3);
49	assert (array.length == 3);
50}
51
52GLib.Array<FooStruct?> create_struct_garray () {
53	var array = new GLib.Array<FooStruct?> ();
54	FooStruct foo1 = { "foo", new Foo () };
55	array.append_val (foo1);
56	FooStruct foo2 = { "bar", new Foo () };
57	array.append_val (foo2);
58	return array;
59}
60
61void test_struct_garray () {
62	var array = create_struct_garray ();
63	assert (array.length == 2);
64	assert (array.index (0).content == "foo");
65	assert (array.index (0).object.ref_count == 1);
66	assert (array.index (1).content == "bar");
67	assert (array.index (1).object.ref_count == 1);
68	Foo f = array.index (0).object;
69	assert (f.ref_count == 2);
70	array = null;
71	assert (f.ref_count == 1);
72}
73
74void test_object_garray () {
75	var foo = new Foo ();
76	{
77		var array = new GLib.Array<Foo> ();
78		array.append_val (foo);
79		assert (foo.ref_count == 2);
80		array = null;
81	}
82	assert (foo.ref_count == 1);
83	{
84		var array = new GLib.Array<unowned Foo> ();
85		array.append_val (foo);
86		assert (foo.ref_count == 1);
87		array = null;
88	}
89	assert (foo.ref_count == 1);
90}
91
92void main () {
93	test_garray ();
94	test_int_garray ();
95	test_struct_garray ();
96	test_object_garray ();
97}
98