test/tools/javac/StringsInSwitch/StringSwitches.java

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 */
aoqi@0 23
aoqi@0 24 /*
aoqi@0 25 * @test
aoqi@0 26 * @bug 6827009 7071246
aoqi@0 27 * @summary Positive tests for strings in switch.
aoqi@0 28 * @author Joseph D. Darcy
aoqi@0 29 */
aoqi@0 30
aoqi@0 31 public class StringSwitches {
aoqi@0 32
aoqi@0 33 public static void main(String... args) {
aoqi@0 34 int failures = 0;
aoqi@0 35
aoqi@0 36 failures += testPileup();
aoqi@0 37 failures += testSwitchingTwoWays();
aoqi@0 38 failures += testNamedBreak();
aoqi@0 39 failures += testExtraParens();
aoqi@0 40
aoqi@0 41 if (failures > 0) {
aoqi@0 42 throw new RuntimeException();
aoqi@0 43 }
aoqi@0 44 }
aoqi@0 45
aoqi@0 46 /*
aoqi@0 47 * A zero length string and all strings consisting only of the
aoqi@0 48 * zero character \u0000 have a hash code of zero. This method
aoqi@0 49 * maps such strings to the number of times \u0000 appears for 0
aoqi@0 50 * through 6 occurrences.
aoqi@0 51 */
aoqi@0 52 private static int zeroHashes(String s) {
aoqi@0 53 int result = Integer.MAX_VALUE;
aoqi@0 54 switch(s) {
aoqi@0 55 case "":
aoqi@0 56 return 0;
aoqi@0 57
aoqi@0 58 case "\u0000":
aoqi@0 59 result = 1; break;
aoqi@0 60
aoqi@0 61 case "\u0000\u0000":
aoqi@0 62 return 2;
aoqi@0 63
aoqi@0 64 case "\u0000\u0000\u0000":
aoqi@0 65 result = 3; break;
aoqi@0 66
aoqi@0 67 case "\u0000\u0000\u0000\u0000":
aoqi@0 68 return 4;
aoqi@0 69
aoqi@0 70 case "\u0000\u0000\u0000\u0000\u0000":
aoqi@0 71 result = 5; break;
aoqi@0 72
aoqi@0 73 case "\u0000\u0000\u0000\u0000\u0000\u0000":
aoqi@0 74 return 6;
aoqi@0 75
aoqi@0 76 default:
aoqi@0 77 result = -1;
aoqi@0 78 }
aoqi@0 79 return result;
aoqi@0 80 }
aoqi@0 81
aoqi@0 82 private static int testPileup() {
aoqi@0 83 int failures = 0;
aoqi@0 84 String zero = "";
aoqi@0 85 for(int i = 0; i <= 6; i++, zero += "\u0000") {
aoqi@0 86 int result = zeroHashes(zero);
aoqi@0 87 if (result != i) {
aoqi@0 88 failures++;
aoqi@0 89 System.err.printf("For string \"%s\" unexpectedly got %d instead of %d%n.",
aoqi@0 90 zero, result, i);
aoqi@0 91 }
aoqi@0 92 }
aoqi@0 93
aoqi@0 94 if (zeroHashes("foo") != -1) {
aoqi@0 95 failures++;
aoqi@0 96 System.err.println("Failed to get -1 for input string.");
aoqi@0 97 }
aoqi@0 98
aoqi@0 99 return failures;
aoqi@0 100 }
aoqi@0 101
aoqi@0 102 /**
aoqi@0 103 * Verify that a switch on an enum and a switch with the same
aoqi@0 104 * structure on the string name of an enum compute equivalent
aoqi@0 105 * values.
aoqi@0 106 */
aoqi@0 107 private static int testSwitchingTwoWays() {
aoqi@0 108 int failures = 0;
aoqi@0 109
aoqi@0 110 for(MetaSynVar msv : MetaSynVar.values()) {
aoqi@0 111 int enumResult = enumSwitch(msv);
aoqi@0 112 int stringResult = stringSwitch(msv.name());
aoqi@0 113
aoqi@0 114 if (enumResult != stringResult) {
aoqi@0 115 failures++;
aoqi@0 116 System.err.printf("One value %s, computed 0x%x with the enum switch " +
aoqi@0 117 "and 0x%x with the string one.%n",
aoqi@0 118 msv, enumResult, stringResult);
aoqi@0 119 }
aoqi@0 120 }
aoqi@0 121
aoqi@0 122 return failures;
aoqi@0 123 }
aoqi@0 124
aoqi@0 125 private static enum MetaSynVar {
aoqi@0 126 FOO,
aoqi@0 127 BAR,
aoqi@0 128 BAZ,
aoqi@0 129 QUX,
aoqi@0 130 QUUX,
aoqi@0 131 QUUUX,
aoqi@0 132 MUMBLE,
aoqi@0 133 FOOBAR;
aoqi@0 134 }
aoqi@0 135
aoqi@0 136 private static int enumSwitch(MetaSynVar msv) {
aoqi@0 137 int result = 0;
aoqi@0 138 switch(msv) {
aoqi@0 139 case FOO:
aoqi@0 140 result |= (1<<0);
aoqi@0 141 // fallthrough:
aoqi@0 142
aoqi@0 143 case BAR:
aoqi@0 144 case BAZ:
aoqi@0 145 result |= (1<<1);
aoqi@0 146 break;
aoqi@0 147
aoqi@0 148 default:
aoqi@0 149 switch(msv) {
aoqi@0 150 case QUX:
aoqi@0 151 result |= (1<<2);
aoqi@0 152 break;
aoqi@0 153
aoqi@0 154 case QUUX:
aoqi@0 155 result |= (1<<3);
aoqi@0 156
aoqi@0 157 default:
aoqi@0 158 result |= (1<<4);
aoqi@0 159 }
aoqi@0 160 result |= (1<<5);
aoqi@0 161 break;
aoqi@0 162
aoqi@0 163 case MUMBLE:
aoqi@0 164 result |= (1<<6);
aoqi@0 165 return result;
aoqi@0 166
aoqi@0 167 case FOOBAR:
aoqi@0 168 result |= (1<<7);
aoqi@0 169 break;
aoqi@0 170 }
aoqi@0 171 result |= (1<<8);
aoqi@0 172 return result;
aoqi@0 173 }
aoqi@0 174
aoqi@0 175 private static int stringSwitch(String msvName) {
aoqi@0 176 int result = 0;
aoqi@0 177 switch(msvName) {
aoqi@0 178 case "FOO":
aoqi@0 179 result |= (1<<0);
aoqi@0 180 // fallthrough:
aoqi@0 181
aoqi@0 182 case "BAR":
aoqi@0 183 case "BAZ":
aoqi@0 184 result |= (1<<1);
aoqi@0 185 break;
aoqi@0 186
aoqi@0 187 default:
aoqi@0 188 switch(msvName) {
aoqi@0 189 case "QUX":
aoqi@0 190 result |= (1<<2);
aoqi@0 191 break;
aoqi@0 192
aoqi@0 193 case "QUUX":
aoqi@0 194 result |= (1<<3);
aoqi@0 195
aoqi@0 196 default:
aoqi@0 197 result |= (1<<4);
aoqi@0 198 }
aoqi@0 199 result |= (1<<5);
aoqi@0 200 break;
aoqi@0 201
aoqi@0 202 case "MUMBLE":
aoqi@0 203 result |= (1<<6);
aoqi@0 204 return result;
aoqi@0 205
aoqi@0 206 case "FOOBAR":
aoqi@0 207 result |= (1<<7);
aoqi@0 208 break;
aoqi@0 209 }
aoqi@0 210 result |= (1<<8);
aoqi@0 211 return result;
aoqi@0 212 }
aoqi@0 213
aoqi@0 214 private static int testNamedBreak() {
aoqi@0 215 int failures = 0;
aoqi@0 216 String[] testStrings = {"a", "b", "c", "d", "e"};
aoqi@0 217 int[] testExpected = { 0b101011, 0b101, 0b100001, 0b101000, 0b10000};
aoqi@0 218
aoqi@0 219 for(int i = 0; i < testStrings.length; i++) {
aoqi@0 220 int expected = testExpected[i];
aoqi@0 221 int result = namedBreak(testStrings[i]);
aoqi@0 222
aoqi@0 223 if (result != expected) {
aoqi@0 224 failures++;
aoqi@0 225
aoqi@0 226 System.err.printf("On input %s, got %d instead of %d.%n",
aoqi@0 227 testStrings[i], result, expected);
aoqi@0 228 }
aoqi@0 229 }
aoqi@0 230
aoqi@0 231 return failures;
aoqi@0 232 }
aoqi@0 233
aoqi@0 234 private static int namedBreak(String s) {
aoqi@0 235 int result = 0;
aoqi@0 236 outer: switch(s) {
aoqi@0 237 case "a":
aoqi@0 238 case "b":
aoqi@0 239 case "c":
aoqi@0 240 result |= (1<<0);
aoqi@0 241 inner: switch(s + s) {
aoqi@0 242 case "aa":
aoqi@0 243 result |= (1<<1);
aoqi@0 244 break inner;
aoqi@0 245
aoqi@0 246 case "cc":
aoqi@0 247 break outer;
aoqi@0 248
aoqi@0 249 default:
aoqi@0 250 result |= (1<<2);
aoqi@0 251 return result;
aoqi@0 252 }
aoqi@0 253
aoqi@0 254 case "d":
aoqi@0 255 result |= (1<<3);
aoqi@0 256 break outer;
aoqi@0 257
aoqi@0 258 default:
aoqi@0 259 return result |= (1<<4);
aoqi@0 260 }
aoqi@0 261 result |= (1<<5);
aoqi@0 262 return result;
aoqi@0 263 }
aoqi@0 264
aoqi@0 265 private static int testExtraParens() {
aoqi@0 266 int failures = 1;
aoqi@0 267 String s = "first";
aoqi@0 268
aoqi@0 269 switch(s) {
aoqi@0 270 case (("first")):
aoqi@0 271 failures = 0;
aoqi@0 272 break;
aoqi@0 273 case ("second"):
aoqi@0 274 throw new RuntimeException("Should not be reached.");
aoqi@0 275 }
aoqi@0 276
aoqi@0 277 return failures;
aoqi@0 278 }
aoqi@0 279 }

mercurial