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