summaryrefslogtreecommitdiff
path: root/tests/methods/iterator.vala
blob: 009d0221dc13af4b66df3054963a02fe3971aef4 (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
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;
	}
}

class FooEntry4<K,V> {
	public K key { get; private set; }
	public V value { get; private set; }

	public FooEntry4 (K _key, V _value) {
		key = _key;
		value = _value;
	}
}

class FooIterator4<G> {
	bool called = false;

	public bool next () {
		return !called;
	}

	public G @get () {
		assert (!called);
		called = true;
		return new FooEntry4<string,Foo> ("foo", foo_instance);
	}
}

class FooCollection4<K,V> {
	public int size { get { return 1; } }

	public V @get (K key) {
		assert (key == "foo");
		return foo_instance;
	}

	public FooIterator4<FooEntry4<K,V>> iterator () {
		return new FooIterator4<FooEntry4<K,V>> ();
	}
}

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);
	}

	// Uses iterator() and get()
	var collection4 = new FooCollection4<string,Foo> ();
	foreach (var fooentry4 in collection4) {
		assert (fooentry4.key == "foo");
		assert (fooentry4.value == foo_instance);
	}
	assert (collection4["foo"] == 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);
	}
}