Debian Codegraph @

gcc/testsuite/gdc.test/runnable/foreach3.d

raw
1 /*
2 RUN_OUTPUT:
3 ---
4 Success
5 ---
6 */
7
8 import core.stdc.stdio;
9
10 struct Foo
11 {
12 uint[2] array;
13
14 int opApply(int delegate(ref uint) dg)
15 {
16 int result;
17 for (int i = 0; i < array.length; i++)
18 {
19 result = dg(array[i]);
20 if (result)
21 break;
22 }
23 return result;
24 }
25 }
26
27
28 /**************************************************/
29
30 void test1()
31 {
32 Foo a;
33 int i;
34
35 a.array[0] = 73;
36 a.array[1] = 82;
37
38 foreach (uint u; a)
39 {
40 i++;
41 u++;
42 }
43 assert(i == 2);
44 assert(a.array[0] == 73);
45 assert(a.array[1] == 82);
46 }
47
48 /**************************************************/
49
50 void test2()
51 {
52 Foo a;
53 int i;
54
55 a.array[0] = 73;
56 a.array[1] = 82;
57
58 foreach (ref uint u; a)
59 {
60 i++;
61 u++;
62 }
63 assert(i == 2);
64 assert(a.array[0] == 74);
65 assert(a.array[1] == 83);
66 }
67
68 /**************************************************/
69
70 void test3()
71 {
72 Foo a;
73 int i;
74
75 a.array[0] = 73;
76 a.array[1] = 82;
77
78 foreach (ref uint u; a)
79 {
80 i++;
81 if (i)
82 break;
83 u++;
84 }
85 assert(i == 1);
86 assert(a.array[0] == 73);
87 assert(a.array[1] == 82);
88 }
89
90 /**************************************************/
91
92 void test4()
93 {
94 Foo a;
95 int i;
96
97 a.array[0] = 73;
98 a.array[1] = 82;
99
100 foreach (ref uint u; a)
101 {
102 i++;
103 if (i == 1)
104 continue;
105 u++;
106 }
107 assert(i == 2);
108 assert(a.array[0] == 73 && a.array[1] == 83);
109 }
110
111 /**************************************************/
112
113 void test5()
114 {
115 Foo a;
116 int i;
117
118 a.array[0] = 73;
119 a.array[1] = 82;
120
121 Loop:
122 while (1)
123 {
124 foreach (ref uint u; a)
125 {
126 i++;
127 if (i)
128 break Loop;
129 u++;
130 }
131 }
132 assert(i == 1);
133 assert(a.array[0] == 73);
134 assert(a.array[1] == 82);
135 }
136
137 /**************************************************/
138
139 void test6()
140 {
141 Foo a;
142 int i;
143
144 a.array[0] = 73;
145 a.array[1] = 82;
146
147 Loop:
148 while (1)
149 {
150 foreach (ref uint u; a)
151 {
152 i++;
153 if (i == 1)
154 continue Loop;
155 u++;
156 }
157 break;
158 }
159 assert(i == 3);
160 assert(a.array[0] == 74);
161 assert(a.array[1] == 83);
162 }
163
164 /**************************************************/
165
166 void test7()
167 {
168 Foo a;
169 int i;
170
171 a.array[0] = 73;
172 a.array[1] = 82;
173
174 foreach (ref uint u; a)
175 {
176 i++;
177 if (i)
178 goto Label;
179 u++;
180 }
181 assert(0);
182 Label:
183 assert(i == 1);
184 assert(a.array[0] == 73);
185 assert(a.array[1] == 82);
186 }
187
188 /**************************************************/
189
190 void test8_x(Foo a)
191 {
192 int i;
193 foreach (ref uint u; a)
194 {
195 i++;
196 if (i)
197 return;
198 u++;
199 }
200 }
201
202 void test8()
203 {
204 Foo a;
205 int i;
206
207 a.array[0] = 73;
208 a.array[1] = 82;
209
210 test8_x(a);
211 assert(i == 0);
212 assert(a.array[0] == 73);
213 assert(a.array[1] == 82);
214 }
215
216 /**************************************************/
217
218 int test9_x(Foo a)
219 {
220 int i;
221 foreach (ref uint u; a)
222 {
223 i++;
224 if (i)
225 return 67;
226 u++;
227 }
228 return 23;
229 }
230
231 void test9()
232 {
233 Foo a;
234 int i;
235
236 a.array[0] = 73;
237 a.array[1] = 82;
238
239 i = test9_x(a);
240 assert(i == 67);
241 assert(a.array[0] == 73);
242 assert(a.array[1] == 82);
243 }
244
245 /**************************************************/
246
247 int test10_x(Foo a)
248 {
249 int i;
250 foreach (ref uint u; a)
251 {
252 i++;
253 if (i)
254 return i;
255 u++;
256 }
257 return 23;
258 }
259
260 void test10()
261 {
262 Foo a;
263 int i;
264
265 a.array[0] = 73;
266 a.array[1] = 82;
267
268 i = test10_x(a);
269 assert(i == 1);
270 assert(a.array[0] == 73);
271 assert(a.array[1] == 82);
272 }
273
274 /**************************************************/
275
276 int main()
277 {
278 test1();
279 test2();
280 test3();
281 test4();
282 test5();
283 test6();
284 test7();
285 test8();
286 test9();
287 test10();
288
289 printf("Success\n");
290 return 0;
291 }