Debian Codegraph @

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

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