Debian Codegraph @

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

raw
1 /*
2 RUN_OUTPUT:
3 ---
4 u = 17
5 u = 1
6 u = 1
7 u = 1
8 u = 1
9 a[0] = 21
10 a[1] = 22
11 a[2] = 23
12 a[] = 21
13 a[] = 22
14 a[] = 23
15 a = 63, b = 47, c = 83
16 a = 63, b = 48, c = 83
17 Success
18 ---
19 */
20
21 import core.stdc.stdio;
22
23 /**************************************************/
24
25 void test1()
26 {
27 int i;
28
29 foreach (char c; "abcd")
30 {
31 switch (i++)
32 { case 0: assert(c == 'a'); break;
33 case 1: assert(c == 'b'); break;
34 case 2: assert(c == 'c'); break;
35 case 3: assert(c == 'd'); break;
36 default: assert(0);
37 }
38 }
39
40 i = 0;
41 foreach (wchar c; "asdf")
42 {
43 switch (i++)
44 { case 0: assert(c == 'a'); break;
45 case 1: assert(c == 's'); break;
46 case 2: assert(c == 'd'); break;
47 case 3: assert(c == 'f'); break;
48 default: assert(0);
49 }
50 }
51
52 i = 0;
53 foreach (dchar c; "bncd")
54 {
55 switch (i++)
56 { case 0: assert(c == 'b'); break;
57 case 1: assert(c == 'n'); break;
58 case 2: assert(c == 'c'); break;
59 case 3: assert(c == 'd'); break;
60 default: assert(0);
61 }
62 }
63 }
64
65 /**************************************************/
66
67 void test2()
68 {
69 int i;
70
71 uint[5] a;
72 a[0] = 16;
73 a[1] = 1;
74 a[2] = 5;
75 a[3] = 8;
76 a[4] = 3;
77
78 foreach (uint u; a)
79 {
80 switch (i++)
81 { case 0: assert(u == 16); break;
82 case 1: assert(u == 1); break;
83 case 2: assert(u == 5); break;
84 case 3: assert(u == 8); break;
85 case 4: assert(u == 3); break;
86 default: assert(0);
87 }
88 }
89
90 uint[] b = a;
91
92 i = 0;
93 foreach (uint u; b)
94 {
95 switch (i++)
96 { case 0: assert(u == 16); break;
97 case 1: assert(u == 1); break;
98 case 2: assert(u == 5); break;
99 case 3: assert(u == 8); break;
100 case 4: assert(u == 3); break;
101 default: assert(0);
102 }
103 }
104
105 test2_x(a);
106 }
107
108 void test2_x(uint[5] a)
109 {
110 int i;
111
112 foreach (uint u; a)
113 {
114 switch (i++)
115 { case 0: assert(u == 16); break;
116 case 1: assert(u == 1); break;
117 case 2: assert(u == 5); break;
118 case 3: assert(u == 8); break;
119 case 4: assert(u == 3); break;
120 default: assert(0);
121 }
122 }
123 }
124
125 /**************************************************/
126
127 void test3()
128 {
129 int i;
130
131 uint[5] a;
132 a[0] = 16;
133
134 foreach (ref uint u; a)
135 {
136 i += u;
137 u++;
138 }
139 assert(i == 16);
140 assert(a[0] == 17);
141 assert(a[4] == 1);
142
143 foreach (uint u; a)
144 {
145 printf("u = %d\n", u);
146 //u++;
147 }
148 assert(a[0] == 17);
149 assert(a[4] == 1);
150 }
151
152 /**************************************************/
153
154 enum E4 { m }
155
156 struct X4 {
157 char [] b;
158 E4 a;
159 }
160
161 void test4()
162 {
163 X4 [] x;
164 foreach (X4 w; x) {}
165 }
166
167
168 /**************************************************/
169
170 class Thing5
171 {}
172
173 class Things5
174 {
175 public:
176 int opApply(int delegate(ref Thing5 thing) dg)
177 {
178 Thing5 thing = new Thing5();
179
180 return dg(thing);
181 }
182 }
183
184 void foo5(Things5 things)
185 {
186 foreach(Thing5 t; things)
187 {
188 }
189 }
190
191 void test5()
192 {
193 }
194
195
196 /**************************************************/
197
198 void test6()
199 {
200 static long[3] a = [21,22,23];
201 long[3] b;
202 int sum;
203
204 foreach (int i, ref long v; a)
205 {
206 printf("a[%d] = %lld\n", i, v);
207 b[i] = v;
208 }
209
210 for (uint i = 0; i < 3; i++)
211 {
212 assert(b[i] == 21 + i);
213 }
214
215 foreach (ref long v; a)
216 {
217 printf("a[] = %lld\n", v);
218 sum += v;
219 }
220 assert(sum == 21 + 22 + 23);
221 }
222
223 /**************************************************/
224
225 void test7()
226 {
227 uint[string] a;
228
229 a["foo"] = 3;
230 a["bar"] = 4;
231 bool sawBar, sawFoo;
232 foreach (string s, uint v; a)
233 {
234 if (s == "bar")
235 {
236 assert(v == 4);
237 assert(!sawBar);
238 sawBar = true;
239 }
240 else if (s == "foo")
241 {
242 assert(v == 3);
243 assert(!sawFoo);
244 sawFoo = true;
245 }
246 else
247 assert(0);
248 }
249 assert(sawBar);
250 assert(sawFoo);
251 }
252
253
254 /**************************************************/
255
256 class Foo8
257 {
258 int x, y, z;
259
260 int opApply(int delegate(ref int a, ref int b, ref int c) dg)
261 {
262 int result = dg(x, y, z);
263 return 0;
264 }
265 }
266
267 void test8()
268 {
269 Foo8 f = new Foo8();
270 f.x = 63;
271 f.y = 47;
272 f.z = 83;
273 foreach (int a, ref int b, int c; f)
274 {
275 printf("a = %d, b = %d, c = %d\n", a, b, c);
276 assert(a == 63);
277 assert(b == 47);
278 assert(c == 83);
279 a++;
280 b++;
281 c++;
282 }
283 foreach (int a, ref int b, int c; f)
284 {
285 printf("a = %d, b = %d, c = %d\n", a, b, c);
286 assert(a == 63);
287 assert(b == 48);
288 assert(c == 83);
289 a++;
290 b++;
291 c++;
292 }
293 }
294
295 /**************************************************/
296
297 struct S
298 {
299 int opApply(int delegate(ref int a)) { return 0; }
300 int opApplyReverse(int delegate(ref int a)) { return 0; }
301 int dg(int delegate(ref int a)) { return 0; }
302 }
303
304 void test9()
305 {
306 S s;
307 foreach(a; s) {}
308 foreach_reverse(a; s) {}
309 foreach(a; &s.dg) {}
310 }
311
312 /**************************************************/
313
314 int main()
315 {
316 test1();
317 test2();
318 test3();
319 test4();
320 test5();
321 test6();
322 test7();
323 test8();
324 test9();
325
326 printf("Success\n");
327 return 0;
328 }