1/* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17/** 18 * Test switch() blocks 19 */ 20public class Main { 21 22 // TODO: This should be translated to smali tests, so it is guaranteed we have the right kind 23 // of switch. 24 25 // Simple packed-switch. 26 public static void packedSwitch(int value) { 27 switch (value) { 28 case 0: 29 System.out.println("0"); break; 30 case 1: 31 System.out.println("1"); break; 32 case 2: 33 System.out.println("2"); break; 34 case 3: 35 System.out.println("3"); break; 36 case 4: 37 System.out.println("4"); break; 38 default: 39 System.out.println("default"); break; 40 } 41 } 42 43 // Simple packed-switch starting at a negative index. 44 public static void packedSwitch2(int value) { 45 switch (value) { 46 case -3: 47 System.out.println("-3"); break; 48 case -2: 49 System.out.println("-2"); break; 50 case -1: 51 System.out.println("-1"); break; 52 case 0: 53 System.out.println("0"); break; 54 case 1: 55 System.out.println("1"); break; 56 case 2: 57 System.out.println("2"); break; 58 default: 59 System.out.println("default"); break; 60 } 61 } 62 63 // Simple packed-switch starting above 0. 64 public static void packedSwitch3(int value) { 65 switch (value) { 66 case 2: 67 System.out.println("2"); break; 68 case 3: 69 System.out.println("3"); break; 70 case 4: 71 System.out.println("4"); break; 72 case 5: 73 System.out.println("5"); break; 74 case 6: 75 System.out.println("6"); break; 76 default: 77 System.out.println("default"); break; 78 } 79 } 80 81 // Simple packed-switch going up to max_int. 82 public static void packedSwitch4(int value) { 83 switch (value) { 84 case Integer.MAX_VALUE - 1: 85 System.out.println(Integer.MAX_VALUE - 1); break; 86 case Integer.MAX_VALUE: 87 System.out.println(Integer.MAX_VALUE); break; 88 default: 89 System.out.println("default"); break; 90 } 91 } 92 93 // Simple packed-switch starting at min_int. 94 public static void packedSwitch5(int value) { 95 switch (value) { 96 case Integer.MIN_VALUE: 97 System.out.println(Integer.MIN_VALUE); break; 98 case Integer.MIN_VALUE + 1: 99 System.out.println(Integer.MIN_VALUE + 1); break; 100 default: 101 System.out.println("default"); break; 102 } 103 } 104 105 // Simple (packed-)switch with only min_int. 106 public static void packedSwitch6(int value) { 107 switch (value) { 108 case Integer.MIN_VALUE: 109 System.out.println(Integer.MIN_VALUE); break; 110 default: 111 System.out.println("default"); break; 112 } 113 } 114 115 // Long packed-switch that might lead to not creating chained-ifs. 116 public static void packedSwitch7(int value) { 117 switch (value) { 118 case 1: 119 System.out.println(1); break; 120 case 2: 121 System.out.println(2); break; 122 case 3: 123 System.out.println(3); break; 124 case 4: 125 System.out.println(4); break; 126 case 5: 127 System.out.println(5); break; 128 case 6: 129 System.out.println(6); break; 130 case 7: 131 System.out.println(7); break; 132 case 8: 133 System.out.println(8); break; 134 case 9: 135 System.out.println(9); break; 136 case 10: 137 System.out.println(10); break; 138 case 11: 139 System.out.println(11); break; 140 case 12: 141 System.out.println(12); break; 142 case 13: 143 System.out.println(13); break; 144 case 14: 145 System.out.println(14); break; 146 case 15: 147 System.out.println(15); break; 148 default: 149 System.out.println("default"); break; 150 } 151 } 152 153 // Sparse switch, just leave a gap. 154 public static void sparseSwitch(int value) { 155 switch (value) { 156 case 0: 157 System.out.println("0"); break; 158 case 1: 159 System.out.println("1"); break; 160 case 3: 161 System.out.println("3"); break; 162 case 4: 163 System.out.println("4"); break; 164 default: 165 System.out.println("default"); break; 166 } 167 } 168 169 // Simple sparse-switch starting at a negative index. 170 public static void sparseSwitch2(int value) { 171 switch (value) { 172 case -3: 173 System.out.println("-3"); break; 174 case -2: 175 System.out.println("-2"); break; 176 case -1: 177 System.out.println("-1"); break; 178 case 0: 179 System.out.println("0"); break; 180 case 2: 181 System.out.println("2"); break; 182 default: 183 System.out.println("default"); break; 184 } 185 } 186 187 // Simple sparse-switch starting above 0. 188 public static void sparseSwitch3(int value) { 189 switch (value) { 190 case 2: 191 System.out.println("2"); break; 192 case 4: 193 System.out.println("4"); break; 194 case 5: 195 System.out.println("5"); break; 196 case 6: 197 System.out.println("6"); break; 198 default: 199 System.out.println("default"); break; 200 } 201 } 202 203 // Simple sparse-switch going up to max_int. 204 public static void sparseSwitch4(int value) { 205 switch (value) { 206 case Integer.MAX_VALUE - 2: 207 System.out.println(Integer.MAX_VALUE - 2); break; 208 case Integer.MAX_VALUE: 209 System.out.println(Integer.MAX_VALUE); break; 210 default: 211 System.out.println("default"); break; 212 } 213 } 214 215 // Simple sparse-switch starting at min_int. 216 public static void sparseSwitch5(int value) { 217 switch (value) { 218 case Integer.MIN_VALUE: 219 System.out.println(Integer.MIN_VALUE); break; 220 case Integer.MIN_VALUE + 2: 221 System.out.println(Integer.MIN_VALUE + 2); break; 222 default: 223 System.out.println("default"); break; 224 } 225 } 226 227 // Long sparse-switch that might lead to not creating chained-ifs. 228 public static void sparseSwitch7(int value) { 229 switch (value) { 230 case 1: 231 System.out.println(1); break; 232 case 2: 233 System.out.println(2); break; 234 case 4: 235 System.out.println(4); break; 236 case 5: 237 System.out.println(5); break; 238 case 6: 239 System.out.println(6); break; 240 case 7: 241 System.out.println(7); break; 242 case 8: 243 System.out.println(8); break; 244 case 9: 245 System.out.println(9); break; 246 case 10: 247 System.out.println(10); break; 248 case 11: 249 System.out.println(11); break; 250 case 12: 251 System.out.println(12); break; 252 case 13: 253 System.out.println(13); break; 254 case 14: 255 System.out.println(14); break; 256 case 15: 257 System.out.println(15); break; 258 default: 259 System.out.println("default"); break; 260 } 261 } 262 263 public static void main(String args[]) { 264 /* 265 * Note: We are using for loops and calls to hopefully avoid simplifying the switch 266 * structure from constant propagation. When inlining is supported, this needs to 267 * be revisited. 268 */ 269 270 System.out.println("packed"); 271 for (int i = -2; i < 3; i++) { 272 packedSwitch(i); 273 } 274 packedSwitch(Integer.MIN_VALUE); 275 packedSwitch(Integer.MAX_VALUE); 276 277 System.out.println("packed2"); 278 for (int i = -2; i < 3; i++) { 279 packedSwitch2(i); 280 } 281 packedSwitch2(Integer.MIN_VALUE); 282 packedSwitch2(Integer.MAX_VALUE); 283 284 System.out.println("packed3"); 285 for (int i = -2; i < 7; i++) { 286 packedSwitch3(i); 287 } 288 packedSwitch3(Integer.MIN_VALUE); 289 packedSwitch3(Integer.MAX_VALUE); 290 291 System.out.println("packed4"); 292 for (int i = Integer.MAX_VALUE - 2; i > 0; i++) { 293 packedSwitch4(i); 294 } 295 packedSwitch4(Integer.MIN_VALUE); 296 297 System.out.println("packed5"); 298 for (int i = Integer.MIN_VALUE; i < Integer.MIN_VALUE + 2; i++) { 299 packedSwitch5(i); 300 } 301 packedSwitch5(Integer.MAX_VALUE); 302 303 System.out.println("packed6"); 304 packedSwitch6(Integer.MIN_VALUE); 305 packedSwitch6(Integer.MAX_VALUE); 306 307 System.out.println("packed7"); 308 for (int i = -1; i < 17; i++) { 309 packedSwitch7(i); 310 } 311 312 313 System.out.println("sparse"); 314 for (int i = -2; i < 4; i++) { 315 sparseSwitch(i); 316 } 317 sparseSwitch(Integer.MIN_VALUE); 318 sparseSwitch(Integer.MAX_VALUE); 319 320 System.out.println("sparse2"); 321 for (int i = -2; i < 3; i++) { 322 sparseSwitch2(i); 323 } 324 sparseSwitch2(Integer.MIN_VALUE); 325 sparseSwitch2(Integer.MAX_VALUE); 326 327 System.out.println("sparse3"); 328 for (int i = -2; i < 7; i++) { 329 sparseSwitch3(i); 330 } 331 sparseSwitch3(Integer.MIN_VALUE); 332 sparseSwitch3(Integer.MAX_VALUE); 333 334 System.out.println("sparse4"); 335 for (int i = Integer.MAX_VALUE - 2; i > 0; i++) { 336 sparseSwitch4(i); 337 } 338 sparseSwitch4(Integer.MIN_VALUE); 339 340 System.out.println("sparse5"); 341 for (int i = Integer.MIN_VALUE; i < Integer.MIN_VALUE + 2; i++) { 342 sparseSwitch5(i); 343 } 344 sparseSwitch5(Integer.MAX_VALUE); 345 346 System.out.println("sparse7"); 347 for (int i = -1; i < 17; i++) { 348 sparseSwitch7(i); 349 } 350 351 // Older tests. 352 353 int a = 1; 354 355 switch (a) { 356 case -1: System.out.print("neg one\n"); break; 357 case 0: System.out.print("zero\n"); break; 358 case 1: System.out.print("CORRECT (one)\n"); break; 359 case 2: System.out.print("two\n"); break; 360 case 3: System.out.print("three\n"); break; 361 case 4: System.out.print("four\n"); break; 362 default: System.out.print("???\n"); break; 363 } 364 switch (a) { 365 case 3: System.out.print("three\n"); break; 366 case 4: System.out.print("four\n"); break; 367 default: System.out.print("CORRECT (not found)\n"); break; 368 } 369 370 a = 0x12345678; 371 372 switch (a) { 373 case 0x12345678: System.out.print("CORRECT (large)\n"); break; 374 case 0x12345679: System.out.print("large+1\n"); break; 375 default: System.out.print("nuts\n"); break; 376 } 377 switch (a) { 378 case 0x12345678: System.out.print("CORRECT (large2)\n"); break; 379 case 0x12345700: System.out.print("large+many\n"); break; 380 default: System.out.print("nuts\n"); break; 381 } 382 switch (a) { 383 case 57: System.out.print("fifty-seven!\n"); break; 384 case -6: System.out.print("neg six!\n"); break; 385 case 0x12345678: System.out.print("CORRECT (large3)\n"); break; 386 case 22: System.out.print("twenty-two!\n"); break; 387 case 3: System.out.print("three!\n"); break; 388 default: System.out.print("huh?\n"); break; 389 } 390 switch (a) { 391 case -6: System.out.print("neg six!\n"); break; 392 case 3: System.out.print("three!\n"); break; 393 default: System.out.print("CORRECT (not found)\n"); break; 394 } 395 396 a = -5; 397 switch (a) { 398 case 12: System.out.print("twelve\n"); break; 399 case -5: System.out.print("CORRECT (not found)\n"); break; 400 case 0: System.out.print("zero\n"); break; 401 default: System.out.print("wah?\n"); break; 402 } 403 404 switch (a) { 405 default: System.out.print("CORRECT (default only)\n"); break; 406 } 407 408 a = -10; 409 switch (a) { 410 case -10: System.out.print("CORRECT big sparse / first\n"); break; 411 case -5: System.out.print("neg five\n"); break; 412 case 0: System.out.print("zero\n"); break; 413 case 5: System.out.print("five\n"); break; 414 case 10: System.out.print("ten\n"); break; 415 case 15: System.out.print("fifteen\n"); break; 416 case 20: System.out.print("twenty\n"); break; 417 case 50: System.out.print("fifty\n"); break; 418 case 100: System.out.print("hundred\n"); break; 419 default: System.out.print("blah!\n"); break; 420 } 421 422 a = 100; 423 switch (a) { 424 case -10: System.out.print("neg ten\n"); break; 425 case -5: System.out.print("neg five\n"); break; 426 case 0: System.out.print("zero\n"); break; 427 case 5: System.out.print("five\n"); break; 428 case 10: System.out.print("ten\n"); break; 429 case 15: System.out.print("fifteen\n"); break; 430 case 20: System.out.print("twenty\n"); break; 431 case 50: System.out.print("fifty\n"); break; 432 case 100: System.out.print("CORRECT big sparse / last\n"); break; 433 default: System.out.print("blah!\n"); break; 434 } 435 436 for (a = 253; a <= 258; a++) { 437 switch (a) { 438 case 254: System.out.println("254"); break; 439 case 255: System.out.println("255"); break; 440 case 256: System.out.println("256"); break; 441 case 257: System.out.println("257"); break; 442 default: System.out.println("default"); break; 443 } 444 } 445 } 446} 447