1 /* 2 * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 6827009 27 * @summary Positive tests for strings in switch. 28 * @author Joseph D. Darcy 29 */ 30 31 public class StringSwitches { 32 33 public static void main(String... args) { 34 int failures = 0; 35 36 failures += testPileup(); 37 failures += testSwitchingTwoWays(); 38 failures += testNamedBreak(); 39 40 if (failures > 0) { 41 throw new RuntimeException(); 42 } 43 } 44 45 /* 46 * A zero length string and all strings consisting only of the 47 * zero character \u0000 have a hash code of zero. This method 48 * maps such strings to the number of times \u0000 appears for 0 49 * through 6 occurrences. 50 */ 51 private static int zeroHashes(String s) { 52 int result = Integer.MAX_VALUE; 53 switch(s) { 54 case "": 55 return 0; 56 57 case "\u0000": 58 result = 1; break; 243 break inner; 244 245 case "cc": 246 break outer; 247 248 default: 249 result |= (1<<2); 250 return result; 251 } 252 253 case "d": 254 result |= (1<<3); 255 break outer; 256 257 default: 258 return result |= (1<<4); 259 } 260 result |= (1<<5); 261 return result; 262 } 263 } | 1 /* 2 * Copyright (c) 2009, 2011 Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 6827009 7071246 27 * @summary Positive tests for strings in switch. 28 * @author Joseph D. Darcy 29 */ 30 31 public class StringSwitches { 32 33 public static void main(String... args) { 34 int failures = 0; 35 36 failures += testPileup(); 37 failures += testSwitchingTwoWays(); 38 failures += testNamedBreak(); 39 failures += testExtraParens(); 40 41 if (failures > 0) { 42 throw new RuntimeException(); 43 } 44 } 45 46 /* 47 * A zero length string and all strings consisting only of the 48 * zero character \u0000 have a hash code of zero. This method 49 * maps such strings to the number of times \u0000 appears for 0 50 * through 6 occurrences. 51 */ 52 private static int zeroHashes(String s) { 53 int result = Integer.MAX_VALUE; 54 switch(s) { 55 case "": 56 return 0; 57 58 case "\u0000": 59 result = 1; break; 244 break inner; 245 246 case "cc": 247 break outer; 248 249 default: 250 result |= (1<<2); 251 return result; 252 } 253 254 case "d": 255 result |= (1<<3); 256 break outer; 257 258 default: 259 return result |= (1<<4); 260 } 261 result |= (1<<5); 262 return result; 263 } 264 265 private static int testExtraParens() { 266 int failures = 1; 267 String s = "first"; 268 269 switch(s) { 270 case (("first")): 271 failures = 0; 272 break; 273 case ("second"): 274 throw new RuntimeException("Should not be reached."); 275 } 276 277 return failures; 278 } 279 } |