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