summaryrefslogtreecommitdiff
path: root/tests/methods/iterator.vala
blob: 68d573e35d951b2597cd584e3701434c54eaf92b (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
class Foo {
}

class FooIterator {
	bool called = false;
	
	public bool next () {
		return !called;
	}

	public Foo @get () {
		assert (!called);
		called = true;
		return foo_instance;
	}
}

class FooCollection {
	public FooIterator iterator () {
		return new FooIterator ();
	}
}

class FooIterator2 {
	bool called = false;
	
	public Foo? next_value () {
		if (called)
			return null;
		called = true;
		return foo_instance;
	}
}

class FooCollection2 {
	public FooIterator2 iterator () {
		return new FooIterator2 ();
	}
}

class FooCollection3 {
	public int size { get { return 1; } }

	public Foo @get (int index) {
		assert (index == 0);
		return foo_instance;
	}
}

Foo foo_instance;

void main () {
	foo_instance = new Foo ();

	// Uses next() and get()
	var collection = new FooCollection ();
	foreach (var foo in collection) {
		assert (foo == foo_instance);
	}

	// Uses next_value()
	var collection2 = new FooCollection2 ();
	foreach (var foo2 in collection2) {
		assert (foo2 == foo_instance);
	}

	// Uses size and get()
	var collection3 = new FooCollection3 ();
	foreach (var foo3 in collection3) {
		assert (foo3 == foo_instance);
	}

	// GLib.List
	var list = new List<Foo> ();
	list.append (foo_instance);
	foreach (var e in list) {
		assert (e == foo_instance);
	}

	// GLib.SList
	var slist = new SList<Foo> ();
	slist.append (foo_instance);
	foreach (var e in slist) {
		assert (e == foo_instance);
	}
}