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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
void test_owned (ValueArray array) {
uint i = 0;
foreach (Value item in array) {
i++;
}
assert (i == 3);
}
void test_unowned (ValueArray array) {
uint i = 0;
foreach (unowned Value item in array) {
i++;
}
assert (i == 3);
}
void test_foreach_gvaluearray () {
Value value;
ValueArray array = new ValueArray (3);
value = 1;
array.append (value);
value = 2.0;
array.append (value);
value = "three";
array.append (value);
test_owned (array);
test_unowned (array);
}
void test_generic_array_owned (GenericArray<Value?> array) {
uint i = 0;
foreach (Value? item in array) {
i++;
}
assert (i == 3);
}
void test_generic_array_unowned (GenericArray<Value?> array) {
uint i = 0;
foreach (unowned Value? item in array) {
i++;
}
assert (i == 3);
}
void test_foreach_genericarray () {
Value value;
var array = new GenericArray<Value?> ();
value = 1;
array.add (value);
value = 2.0;
array.add (value);
value = "three";
array.add (value);
test_generic_array_owned (array);
test_generic_array_unowned (array);
}
void test_gsequence_owned (Sequence<Value?> sequence) {
uint i = 0;
foreach (Value? item in sequence) {
i++;
}
assert (i == 3);
}
void test_gsequence_unowned (Sequence<Value?> sequence) {
uint i = 0;
foreach (unowned Value? item in sequence) {
i++;
}
assert (i == 3);
}
void test_foreach_gsequence () {
Value value;
var sequence = new Sequence<Value?> ();
value = 1;
sequence.append (value);
value = 2.0;
sequence.append (value);
value = "three";
sequence.append (value);
test_gsequence_owned (sequence);
test_gsequence_unowned (sequence);
}
void test_garray_owned (Array<Value?> array) {
uint i = 0;
foreach (Value? item in array) {
i++;
}
assert (i == 3);
}
void test_garray_unowned (Array<Value?> array) {
uint i = 0;
foreach (unowned Value? item in array) {
i++;
}
assert (i == 3);
}
void test_foreach_garray () {
Value value;
var array = new Array<Value?> ();
value = 1;
array.append_val (value);
value = 2.0;
array.append_val (value);
value = "three";
array.append_val (value);
test_garray_owned (array);
test_garray_unowned (array);
}
void test_foreach_multidim_array () {
int[,] foo = { { 1, 2 }, { 3, 4 }, { 5, 6 } };
string result = "";
foreach (var i in foo) {
result += i.to_string ();
}
assert (result == "123456");
}
const int[] FOO = { 1, 2, 3, 4, 5, 6 };
const int BAR[] = { 6, 5, 4, 3, 2, 1 };
void test_foreach_const_array () {
string result = "";
foreach (var i in FOO) {
result += i.to_string ();
}
assert (result == "123456");
string result2 = "";
foreach (var i in BAR) {
result2 += i.to_string ();
}
assert (result2 == "654321");
}
void test_foreach_slice_array () {
int[] foo = { 1, 2, 3, 4, 5, 6 };
string result = "";
foreach (var i in foo[1:foo.length - 1]) {
result += i.to_string ();
}
assert (result == "2345");
}
void test_foreach_string () {
uint i = 0;
string s = "abc àçêö 你好"; // Ni hao
foreach (unichar c in s) {
i++;
}
assert (i == 11);
}
void main () {
test_foreach_gvaluearray ();
test_foreach_garray ();
test_foreach_genericarray ();
test_foreach_gsequence ();
test_foreach_const_array ();
test_foreach_multidim_array ();
test_foreach_slice_array ();
test_foreach_string ();
}
|