OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 // Dart test program to test check that we can parse labels. | 4 // Dart test program to test check that we can parse labels. |
5 | 5 |
6 import "package:expect/expect.dart"; | 6 import "package:expect/expect.dart"; |
7 | 7 |
8 | |
9 class Helper { | 8 class Helper { |
10 | |
11 static int ticks; | 9 static int ticks; |
12 | 10 |
13 // Helper function to prevent endless loops in case labels or | 11 // Helper function to prevent endless loops in case labels or |
14 // break/continue is broken. | 12 // break/continue is broken. |
15 static doAgain() { | 13 static doAgain() { |
16 ++ticks; | 14 ++ticks; |
17 if (ticks > 300) { | 15 if (ticks > 300) { |
18 // obfuscating man's assert(false) | 16 // obfuscating man's assert(false) |
19 Expect.equals(true, false); | 17 Expect.equals(true, false); |
20 } | 18 } |
21 return true; | 19 return true; |
22 } | 20 } |
23 | 21 |
24 static test1() { | 22 static test1() { |
25 var i = 1; | 23 var i = 1; |
26 while (doAgain()) { | 24 while (doAgain()) { |
27 if (i > 0) break; | 25 if (i > 0) break; |
28 return 0; | 26 return 0; |
29 } | 27 } |
30 return 111; | 28 return 111; |
31 } | 29 } |
32 | 30 |
33 static test2() { | 31 static test2() { |
34 // Make sure we break out to default label. | 32 // Make sure we break out to default label. |
35 var i = 1; | 33 var i = 1; |
36 L: while (doAgain()) { // unused label | 34 L: |
| 35 while (doAgain()) { |
| 36 // unused label |
37 if (i > 0) break; | 37 if (i > 0) break; |
38 return 0; | 38 return 0; |
39 } | 39 } |
40 return 111; | 40 return 111; |
41 } | 41 } |
42 | 42 |
43 static test3() { | 43 static test3() { |
44 // Make sure we break out of outer loop. | 44 // Make sure we break out of outer loop. |
45 var i = 1; | 45 var i = 1; |
46 L: while (doAgain()) { | 46 L: |
| 47 while (doAgain()) { |
47 while (doAgain()) { | 48 while (doAgain()) { |
48 if (i > 0) break L; | 49 if (i > 0) break L; |
49 return 0; | 50 return 0; |
50 } | 51 } |
51 return 1; | 52 return 1; |
52 } | 53 } |
53 return 111; | 54 return 111; |
54 } | 55 } |
55 | 56 |
56 static test4() { | 57 static test4() { |
57 // Make sure we break out of inner loop. | 58 // Make sure we break out of inner loop. |
58 var i = 100; | 59 var i = 100; |
59 L: while (doAgain()) { // unused label | 60 L: |
| 61 while (doAgain()) { |
| 62 // unused label |
60 while (doAgain()) { | 63 while (doAgain()) { |
61 if (i > 0) break; | 64 if (i > 0) break; |
62 return 0; | 65 return 0; |
63 } | 66 } |
64 return 111; | 67 return 111; |
65 } | 68 } |
66 return 1; | 69 return 1; |
67 } | 70 } |
68 | 71 |
69 static test5() { | 72 static test5() { |
70 // Make sure we jump to loop condition. | 73 // Make sure we jump to loop condition. |
71 var i = 10; | 74 var i = 10; |
72 while (i > 0) { | 75 while (i > 0) { |
73 i--; | 76 i--; |
74 if (true) continue; // without the if the following return is dead code. | 77 if (true) continue; // without the if the following return is dead code. |
75 return 0; | 78 return 0; |
76 } | 79 } |
77 return 111; | 80 return 111; |
78 } | 81 } |
79 | 82 |
80 static test6() { | 83 static test6() { |
81 // Make sure we jump to loop condition. | 84 // Make sure we jump to loop condition. |
82 L: for (int i = 10; i > 0; i--) { // unreferenced label, should warn | 85 L: |
| 86 for (int i = 10; i > 0; i--) { |
| 87 // unreferenced label, should warn |
83 if (true) continue; // without the if the following return is dead code. | 88 if (true) continue; // without the if the following return is dead code. |
84 return 0; | 89 return 0; |
85 } | 90 } |
86 // Make sure this L does not conflict with previous L. | 91 // Make sure this L does not conflict with previous L. |
87 var k = 20; | 92 var k = 20; |
88 L: while (doAgain()) { | 93 L: |
89 L0: while (doAgain()) break L; // unreferenced label L0, should warn | 94 while (doAgain()) { |
| 95 L0: |
| 96 while (doAgain()) break L; // unreferenced label L0, should warn |
90 return 1; | 97 return 1; |
91 } | 98 } |
92 return 111; | 99 return 111; |
93 } | 100 } |
94 | 101 |
95 static test7() { | 102 static test7() { |
96 // Just weird stuff. | 103 // Just weird stuff. |
97 var i = 10; | 104 var i = 10; |
98 L: do { | 105 L: |
99 L: while (doAgain()) { | 106 do { |
| 107 L: |
| 108 while (doAgain()) { |
100 if (true) break L; // without the if the following line is dead code. | 109 if (true) break L; // without the if the following line is dead code. |
101 continue L; | 110 continue L; |
102 } | 111 } |
103 i = 0; | 112 i = 0; |
104 continue L; | 113 continue L; |
105 } while (i == 10 && doAgain()); | 114 } while (i == 10 && doAgain()); |
106 return 111; | 115 return 111; |
107 } | 116 } |
108 | 117 |
109 static test8() { | 118 static test8() { |
110 L: while (false) { | 119 L: |
| 120 while (false) { |
111 var L = 33; // OK, shouldn't collide with label. | 121 var L = 33; // OK, shouldn't collide with label. |
112 if (true) break L; | 122 if (true) break L; |
113 } | 123 } |
114 return 111; | 124 return 111; |
115 } | 125 } |
116 | 126 |
117 static test9() { | 127 static test9() { |
118 var i = 111; | 128 var i = 111; |
119 L1: if (i == 0) { // unreferenced label, should warn | 129 L1: |
| 130 if (i == 0) { |
| 131 // unreferenced label, should warn |
120 return 0; | 132 return 0; |
121 } | 133 } |
122 | 134 |
123 L2: while (i == 0) { // unreferenced label, should warn | 135 L2: |
| 136 while (i == 0) { |
| 137 // unreferenced label, should warn |
124 return 0; | 138 return 0; |
125 } | 139 } |
126 | 140 |
127 L3: // useless label, should warn | 141 L3: // useless label, should warn |
128 return i; | 142 return i; |
129 } | 143 } |
130 | 144 |
131 // Labels should be allowed on block/if/for/switch/while/do stmts. | 145 // Labels should be allowed on block/if/for/switch/while/do stmts. |
132 static test10() { | 146 static test10() { |
133 int i = 111; | 147 int i = 111; |
134 // block | 148 // block |
135 while (doAgain()) { | 149 while (doAgain()) { |
136 L: { | 150 L: |
| 151 { |
137 while (doAgain()) { | 152 while (doAgain()) { |
138 break L; | 153 break L; |
139 } | 154 } |
140 i--; | 155 i--; |
141 } | 156 } |
142 break; | 157 break; |
143 } | 158 } |
144 Expect.equals(111, i); | 159 Expect.equals(111, i); |
145 | 160 |
146 while(doAgain()) { | 161 while (doAgain()) { |
147 L: if (doAgain()) { | 162 L: |
148 while(doAgain()) { | 163 if (doAgain()) { |
| 164 while (doAgain()) { |
149 break L; | 165 break L; |
150 } | 166 } |
151 i--; | 167 i--; |
152 } | 168 } |
153 break; | 169 break; |
154 } | 170 } |
155 Expect.equals(111, i); | 171 Expect.equals(111, i); |
156 | 172 |
157 while(doAgain()) { | 173 while (doAgain()) { |
158 L: for (;doAgain();) { | 174 L: |
| 175 for (; doAgain();) { |
159 while (doAgain()) { | 176 while (doAgain()) { |
160 break L; | 177 break L; |
161 } | 178 } |
162 i--; | 179 i--; |
163 } | 180 } |
164 break; | 181 break; |
165 } | 182 } |
166 Expect.equals(111, i); | 183 Expect.equals(111, i); |
167 | 184 |
168 L: for (i in [111]) { | 185 L: |
169 while(doAgain()) { | 186 for (i in [111]) { |
170 break L; | 187 while (doAgain()) { |
171 } | 188 break L; |
172 i--; | 189 } |
173 break; | 190 i--; |
174 } | |
175 Expect.equals(111, i); | |
176 | |
177 L: for (var j in [111]) { | |
178 while(doAgain()) { | |
179 break L; | |
180 } | |
181 i--; | |
182 break; | 191 break; |
183 } | 192 } |
184 Expect.equals(111, i); | 193 Expect.equals(111, i); |
185 | 194 |
186 while(doAgain()) { | 195 L: |
187 L: switch (i) { | 196 for (var j in [111]) { |
| 197 while (doAgain()) { |
| 198 break L; |
| 199 } |
| 200 i--; |
| 201 break; |
| 202 } |
| 203 Expect.equals(111, i); |
| 204 |
| 205 while (doAgain()) { |
| 206 L: |
| 207 switch (i) { |
188 case 111: | 208 case 111: |
189 while(doAgain()) { | 209 while (doAgain()) { |
190 break L; | 210 break L; |
191 } | 211 } |
192 default: | 212 default: |
193 i--; | 213 i--; |
194 } | 214 } |
195 break; | 215 break; |
196 } | 216 } |
197 Expect.equals(111, i); | 217 Expect.equals(111, i); |
198 | 218 |
199 while(doAgain()) { | 219 while (doAgain()) { |
200 L: do { | 220 L: |
201 while(doAgain()) { | 221 do { |
| 222 while (doAgain()) { |
202 break L; | 223 break L; |
203 } | 224 } |
204 i--; | 225 i--; |
205 } while (doAgain()); | 226 } while (doAgain()); |
206 break; | 227 break; |
207 } | 228 } |
208 Expect.equals(111, i); | 229 Expect.equals(111, i); |
209 | 230 |
210 while(doAgain()) { | 231 while (doAgain()) { |
211 L: try { | 232 L: |
212 while(doAgain()) { | 233 try { |
| 234 while (doAgain()) { |
213 break L; | 235 break L; |
214 } | 236 } |
215 i--; | 237 i--; |
216 } finally { | 238 } finally {} |
217 } | |
218 break; | 239 break; |
219 } | 240 } |
220 Expect.equals(111, i); | 241 Expect.equals(111, i); |
221 | 242 |
222 return i; | 243 return i; |
223 } | 244 } |
224 | 245 |
225 static test11() { | 246 static test11() { |
226 // Kind of odd, but is valid and shouldn't be flagged as useless either. | 247 // Kind of odd, but is valid and shouldn't be flagged as useless either. |
227 L: break L; | 248 L: |
228 return 111; | 249 break L; |
229 } | 250 return 111; |
| 251 } |
230 | 252 |
231 static test12() { | 253 static test12() { |
232 int i = 111; | 254 int i = 111; |
233 | 255 |
234 // label the inner block on compound stmts | 256 // label the inner block on compound stmts |
235 if (true) L: { | 257 if (true) |
236 while (doAgain()) { | 258 L: |
237 break L; | 259 { |
238 } | 260 while (doAgain()) { |
239 i--; | 261 break L; |
240 } | 262 } |
241 Expect.equals(111, i); | 263 i--; |
| 264 } |
| 265 Expect.equals(111, i); |
242 | 266 |
243 // loop will execute each time, but won't execute code below the break | 267 // loop will execute each time, but won't execute code below the break |
244 var forCount = 0; | 268 var forCount = 0; |
245 for (forCount = 0 ; forCount < 2 ; forCount++) L: { | 269 for (forCount = 0; forCount < 2; forCount++) |
246 while(doAgain()) { | 270 L: |
247 break L; | 271 { |
248 } | 272 while (doAgain()) { |
249 i--; | 273 break L; |
250 break; | |
251 } | |
252 Expect.equals(111, i); | |
253 Expect.equals(forCount, 2); | |
254 | |
255 for (i in [111]) L: { | |
256 while(doAgain()) { | |
257 break L; | |
258 } | |
259 i--; | |
260 break; | |
261 } | |
262 Expect.equals(111, i); | |
263 | |
264 for (var j in [111]) L: { | |
265 while(doAgain()) { | |
266 break L; | |
267 } | |
268 i--; | |
269 break; | |
270 } | |
271 Expect.equals(111, i); | |
272 | |
273 if (false) { | |
274 } else L: { | |
275 while (doAgain()) { | |
276 break L; | |
277 } | |
278 i--; | |
279 } | |
280 Expect.equals(111, i); | |
281 | |
282 int whileCount = 0; | |
283 while (whileCount < 2) L: { | |
284 whileCount++; | |
285 while(doAgain()) { | |
286 » break L; | |
287 } | 274 } |
288 i--; | 275 i--; |
289 break; | 276 break; |
290 } | 277 } |
291 Expect.equals(111, i); | 278 Expect.equals(111, i); |
292 Expect.equals(2, whileCount); | 279 Expect.equals(forCount, 2); |
293 | 280 |
294 return i; | 281 for (i in [111]) |
295 } | 282 L: |
| 283 { |
| 284 while (doAgain()) { |
| 285 break L; |
| 286 } |
| 287 i--; |
| 288 break; |
| 289 } |
| 290 Expect.equals(111, i); |
| 291 |
| 292 for (var j in [111]) |
| 293 L: |
| 294 { |
| 295 while (doAgain()) { |
| 296 break L; |
| 297 } |
| 298 i--; |
| 299 break; |
| 300 } |
| 301 Expect.equals(111, i); |
| 302 |
| 303 if (false) {} else |
| 304 L: |
| 305 { |
| 306 while (doAgain()) { |
| 307 break L; |
| 308 } |
| 309 i--; |
| 310 } |
| 311 Expect.equals(111, i); |
| 312 |
| 313 int whileCount = 0; |
| 314 while (whileCount < 2) |
| 315 L: |
| 316 { |
| 317 whileCount++; |
| 318 while (doAgain()) { |
| 319 break L; |
| 320 } |
| 321 i--; |
| 322 break; |
| 323 } |
| 324 Expect.equals(111, i); |
| 325 Expect.equals(2, whileCount); |
| 326 |
| 327 return i; |
| 328 } |
296 } | 329 } |
297 | 330 |
298 class LabelTest { | 331 class LabelTest { |
299 static testMain() { | 332 static testMain() { |
300 Helper.ticks = 0; | 333 Helper.ticks = 0; |
301 Expect.equals(111, Helper.test1()); | 334 Expect.equals(111, Helper.test1()); |
302 Expect.equals(111, Helper.test2()); | 335 Expect.equals(111, Helper.test2()); |
303 Expect.equals(111, Helper.test3()); | 336 Expect.equals(111, Helper.test3()); |
304 Expect.equals(111, Helper.test4()); | 337 Expect.equals(111, Helper.test4()); |
305 Expect.equals(111, Helper.test5()); | 338 Expect.equals(111, Helper.test5()); |
306 Expect.equals(111, Helper.test6()); | 339 Expect.equals(111, Helper.test6()); |
307 Expect.equals(111, Helper.test7()); | 340 Expect.equals(111, Helper.test7()); |
308 Expect.equals(111, Helper.test8()); | 341 Expect.equals(111, Helper.test8()); |
309 Expect.equals(111, Helper.test9()); | 342 Expect.equals(111, Helper.test9()); |
310 Expect.equals(111, Helper.test10()); | 343 Expect.equals(111, Helper.test10()); |
311 Expect.equals(111, Helper.test11()); | 344 Expect.equals(111, Helper.test11()); |
312 Expect.equals(111, Helper.test12()); | 345 Expect.equals(111, Helper.test12()); |
313 } | 346 } |
314 } | 347 } |
315 | 348 |
316 main() { | 349 main() { |
317 LabelTest.testMain(); | 350 LabelTest.testMain(); |
318 } | 351 } |
OLD | NEW |