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