1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/StringsInSwitch/StringSwitches.java Mon Nov 02 21:36:59 2009 -0800 1.3 @@ -0,0 +1,263 @@ 1.4 +/* 1.5 + * Copyright 2009 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @bug 6827009 1.30 + * @summary Positive tests for strings in switch. 1.31 + * @author Joseph D. Darcy 1.32 + */ 1.33 + 1.34 +public class StringSwitches { 1.35 + 1.36 + public static void main(String... args) { 1.37 + int failures = 0; 1.38 + 1.39 + failures += testPileup(); 1.40 + failures += testSwitchingTwoWays(); 1.41 + failures += testNamedBreak(); 1.42 + 1.43 + if (failures > 0) { 1.44 + throw new RuntimeException(); 1.45 + } 1.46 + } 1.47 + 1.48 + /* 1.49 + * A zero length string and all strings consisting only of the 1.50 + * zero character \u0000 have a hash code of zero. This method 1.51 + * maps such strings to the number of times \u0000 appears for 0 1.52 + * through 6 occurrences. 1.53 + */ 1.54 + private static int zeroHashes(String s) { 1.55 + int result = Integer.MAX_VALUE; 1.56 + switch(s) { 1.57 + case "": 1.58 + return 0; 1.59 + 1.60 + case "\u0000": 1.61 + result = 1; break; 1.62 + 1.63 + case "\u0000\u0000": 1.64 + return 2; 1.65 + 1.66 + case "\u0000\u0000\u0000": 1.67 + result = 3; break; 1.68 + 1.69 + case "\u0000\u0000\u0000\u0000": 1.70 + return 4; 1.71 + 1.72 + case "\u0000\u0000\u0000\u0000\u0000": 1.73 + result = 5; break; 1.74 + 1.75 + case "\u0000\u0000\u0000\u0000\u0000\u0000": 1.76 + return 6; 1.77 + 1.78 + default: 1.79 + result = -1; 1.80 + } 1.81 + return result; 1.82 + } 1.83 + 1.84 + private static int testPileup() { 1.85 + int failures = 0; 1.86 + String zero = ""; 1.87 + for(int i = 0; i <= 6; i++, zero += "\u0000") { 1.88 + int result = zeroHashes(zero); 1.89 + if (result != i) { 1.90 + failures++; 1.91 + System.err.printf("For string \"%s\" unexpectedly got %d instead of %d%n.", 1.92 + zero, result, i); 1.93 + } 1.94 + } 1.95 + 1.96 + if (zeroHashes("foo") != -1) { 1.97 + failures++; 1.98 + System.err.println("Failed to get -1 for input string."); 1.99 + } 1.100 + 1.101 + return failures; 1.102 + } 1.103 + 1.104 + /** 1.105 + * Verify that a switch on an enum and a switch with the same 1.106 + * structure on the string name of an enum compute equivalent 1.107 + * values. 1.108 + */ 1.109 + private static int testSwitchingTwoWays() { 1.110 + int failures = 0; 1.111 + 1.112 + for(MetaSynVar msv : MetaSynVar.values()) { 1.113 + int enumResult = enumSwitch(msv); 1.114 + int stringResult = stringSwitch(msv.name()); 1.115 + 1.116 + if (enumResult != stringResult) { 1.117 + failures++; 1.118 + System.err.printf("One value %s, computed 0x%x with the enum switch " + 1.119 + "and 0x%x with the string one.%n", 1.120 + msv, enumResult, stringResult); 1.121 + } 1.122 + } 1.123 + 1.124 + return failures; 1.125 + } 1.126 + 1.127 + private static enum MetaSynVar { 1.128 + FOO, 1.129 + BAR, 1.130 + BAZ, 1.131 + QUX, 1.132 + QUUX, 1.133 + QUUUX, 1.134 + MUMBLE, 1.135 + FOOBAR; 1.136 + } 1.137 + 1.138 + private static int enumSwitch(MetaSynVar msv) { 1.139 + int result = 0; 1.140 + switch(msv) { 1.141 + case FOO: 1.142 + result |= (1<<0); 1.143 + // fallthrough: 1.144 + 1.145 + case BAR: 1.146 + case BAZ: 1.147 + result |= (1<<1); 1.148 + break; 1.149 + 1.150 + default: 1.151 + switch(msv) { 1.152 + case QUX: 1.153 + result |= (1<<2); 1.154 + break; 1.155 + 1.156 + case QUUX: 1.157 + result |= (1<<3); 1.158 + 1.159 + default: 1.160 + result |= (1<<4); 1.161 + } 1.162 + result |= (1<<5); 1.163 + break; 1.164 + 1.165 + case MUMBLE: 1.166 + result |= (1<<6); 1.167 + return result; 1.168 + 1.169 + case FOOBAR: 1.170 + result |= (1<<7); 1.171 + break; 1.172 + } 1.173 + result |= (1<<8); 1.174 + return result; 1.175 + } 1.176 + 1.177 + private static int stringSwitch(String msvName) { 1.178 + int result = 0; 1.179 + switch(msvName) { 1.180 + case "FOO": 1.181 + result |= (1<<0); 1.182 + // fallthrough: 1.183 + 1.184 + case "BAR": 1.185 + case "BAZ": 1.186 + result |= (1<<1); 1.187 + break; 1.188 + 1.189 + default: 1.190 + switch(msvName) { 1.191 + case "QUX": 1.192 + result |= (1<<2); 1.193 + break; 1.194 + 1.195 + case "QUUX": 1.196 + result |= (1<<3); 1.197 + 1.198 + default: 1.199 + result |= (1<<4); 1.200 + } 1.201 + result |= (1<<5); 1.202 + break; 1.203 + 1.204 + case "MUMBLE": 1.205 + result |= (1<<6); 1.206 + return result; 1.207 + 1.208 + case "FOOBAR": 1.209 + result |= (1<<7); 1.210 + break; 1.211 + } 1.212 + result |= (1<<8); 1.213 + return result; 1.214 + } 1.215 + 1.216 + private static int testNamedBreak() { 1.217 + int failures = 0; 1.218 + String[] testStrings = {"a", "b", "c", "d", "e"}; 1.219 + int[] testExpected = { 0b101011, 0b101, 0b100001, 0b101000, 0b10000}; 1.220 + 1.221 + for(int i = 0; i < testStrings.length; i++) { 1.222 + int expected = testExpected[i]; 1.223 + int result = namedBreak(testStrings[i]); 1.224 + 1.225 + if (result != expected) { 1.226 + failures++; 1.227 + 1.228 + System.err.printf("On input %s, got %d instead of %d.%n", 1.229 + testStrings[i], result, expected); 1.230 + } 1.231 + } 1.232 + 1.233 + return failures; 1.234 + } 1.235 + 1.236 + private static int namedBreak(String s) { 1.237 + int result = 0; 1.238 + outer: switch(s) { 1.239 + case "a": 1.240 + case "b": 1.241 + case "c": 1.242 + result |= (1<<0); 1.243 + inner: switch(s + s) { 1.244 + case "aa": 1.245 + result |= (1<<1); 1.246 + break inner; 1.247 + 1.248 + case "cc": 1.249 + break outer; 1.250 + 1.251 + default: 1.252 + result |= (1<<2); 1.253 + return result; 1.254 + } 1.255 + 1.256 + case "d": 1.257 + result |= (1<<3); 1.258 + break outer; 1.259 + 1.260 + default: 1.261 + return result |= (1<<4); 1.262 + } 1.263 + result |= (1<<5); 1.264 + return result; 1.265 + } 1.266 +}