summaryrefslogtreecommitdiff
path: root/tests/basic-types/garray.vala
blob: 4f66c56af1acc9120be022c4916bdedc5b0e4900 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
class Foo : Object {
}

struct FooStruct {
	string content;
	Foo object;
}

void test_garray () {
	var array = new GLib.Array<Foo> ();

	var foo = new Foo ();
	assert (foo.ref_count == 1);

	array.append_val (foo);
	assert (foo.ref_count == 2);
	assert (array.index (0) == foo);
	array.remove_index (0);
	assert (foo.ref_count == 1);

	array.append_val (foo);
	assert (foo.ref_count == 2);
	assert (array.index (0) == foo);
	array.remove_index_fast (0);
	assert (foo.ref_count == 1);

	array.append_val (foo);
	array.append_val (foo);
	assert (foo.ref_count == 3);
	assert (array.index (0) == foo);
	assert (array.index (1) == foo);
	array.remove_range (0, 2);
	assert (foo.ref_count == 1);
}

void test_garray_foreach () {
	var array = new GLib.Array<Foo> ();

	var foo1 = new Foo ();
	var foo2 = new Foo ();
	var foo3 = new Foo ();

	array.append_val (foo1);
	assert (foo1.ref_count == 2);
	array.append_val (foo2);
	assert (foo2.ref_count == 2);
	array.append_val (foo3);
	assert (foo3.ref_count == 2);
	assert (array.length == 3);

	int loop_size = 0;
	foreach (weak Foo element in array) {
		loop_size++;
		assert (element.ref_count == 2);
		switch (loop_size) {
			case 1: assert (element == foo1); break;
			case 2: assert (element == foo2); break;
			case 3: assert (element == foo3); break;
		}
	}
	assert (loop_size == 3);

	loop_size = 0;
	foreach (Foo element in array) {
		loop_size++;
		assert (element.ref_count == 3);
		switch (loop_size) {
			case 1: assert (element == foo1); break;
			case 2: assert (element == foo2); break;
			case 3: assert (element == foo3); break;
		}
	}
	assert (loop_size == 3);
	assert (foo1.ref_count == 2);
	assert (foo2.ref_count == 2);
	assert (foo3.ref_count == 2);
}

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 () {
	var array = new GLib.Array<FooStruct?> ();
	FooStruct foo1 = { "foo", new Foo () };
	array.append_val (foo1);
	FooStruct foo2 = { "bar", new Foo () };
	array.append_val (foo2);
	return array;
}

void test_struct_garray () {
	var array = create_struct_garray ();
	assert (array.length == 2);
	assert (array.index (0).content == "foo");
	assert (array.index (0).object.ref_count == 1);
	assert (array.index (1).content == "bar");
	assert (array.index (1).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);
}

unowned Array<Value> check_gvalue_garray (Array<Value> vals) {
	assert (vals.index (0) == "foo");
	assert (vals.index (1) == 42);
	assert (vals.index (2) == 3.1415);
	return vals;
}

void test_gvalue_garray () {
	{
		var foo = new Array<Value> ();
		foo.append_val ("foo");
		foo.append_val (42);
		foo.append_val (3.1415);
		check_gvalue_garray (foo);
	}
	{
		Array<Value> foo = new Array<Value> ();
		foo.append_val ("foo");
		foo.append_val (42);
		foo.append_val (3.1415);
		check_gvalue_garray (foo);
	}
}

void main () {
	test_garray ();
	test_garray_foreach ();
	test_int_garray ();
	test_struct_garray ();
	test_object_garray ();
	test_gvalue_garray ();
}