blob: 2d2eb008d2a7c32873cc6494be9e96b434be72ea (
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
|
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);
array.remove_index (0);
assert (foo.ref_count == 1);
array.append_val (foo);
assert (foo.ref_count == 2);
array.remove_index_fast (0);
assert (foo.ref_count == 1);
array.append_val (foo);
assert (foo.ref_count == 2);
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 ();
}
|