test/tools/javac/StringsInSwitch/OneCaseSwitches.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, 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
aoqi@0 27 * @summary Positive tests for strings in switch with few alternatives.
aoqi@0 28 * @compile/fail -source 6 OneCaseSwitches.java
aoqi@0 29 * @compile OneCaseSwitches.java
aoqi@0 30 * @run main OneCaseSwitches
aoqi@0 31 * @author Joseph D. Darcy
aoqi@0 32 */
aoqi@0 33
aoqi@0 34 import java.lang.reflect.*;
aoqi@0 35 import java.lang.annotation.*;
aoqi@0 36 import java.util.*;
aoqi@0 37 import static java.lang.annotation.RetentionPolicy.*;
aoqi@0 38
aoqi@0 39 public class OneCaseSwitches {
aoqi@0 40 @Retention(RUNTIME)
aoqi@0 41 @interface TestMeForNull {}
aoqi@0 42
aoqi@0 43 @TestMeForNull
aoqi@0 44 public static int zeroCasesNoDefault(String s, Set<String> stringSet, boolean expected) {
aoqi@0 45 int failures = 0;
aoqi@0 46 switch(s) {
aoqi@0 47 }
aoqi@0 48 return failures;
aoqi@0 49 }
aoqi@0 50
aoqi@0 51 @TestMeForNull
aoqi@0 52 public static int zeroCasesWithDefault(String s, Set<String> stringSet, boolean expected) {
aoqi@0 53 int failures = 2;
aoqi@0 54 boolean addResult;
aoqi@0 55
aoqi@0 56 switch(s) {
aoqi@0 57 default:
aoqi@0 58 failures = 0;
aoqi@0 59 addResult = stringSet.add(s);
aoqi@0 60 if (addResult != expected) {
aoqi@0 61 failures++;
aoqi@0 62 System.err.println("zeroCaseWithDefault: Expectedly got add result of " + addResult +
aoqi@0 63 " on string " + s);
aoqi@0 64 }
aoqi@0 65 }
aoqi@0 66
aoqi@0 67 return failures;
aoqi@0 68 }
aoqi@0 69
aoqi@0 70 @TestMeForNull
aoqi@0 71 public static int zeroCasesWithDefaultBreak(String s, Set<String> stringSet, boolean expected) {
aoqi@0 72 int failures = 2;
aoqi@0 73 boolean addResult;
aoqi@0 74
aoqi@0 75 switch(s) {
aoqi@0 76 default:
aoqi@0 77 failures = zeroCasesWithDefault(s, stringSet, expected);
aoqi@0 78 break;
aoqi@0 79 }
aoqi@0 80
aoqi@0 81 return failures;
aoqi@0 82 }
aoqi@0 83
aoqi@0 84 @TestMeForNull
aoqi@0 85 public static int oneCaseNoDefault(String s, Set<String> stringSet, boolean expected) {
aoqi@0 86 int failures = 2;
aoqi@0 87 boolean addResult;
aoqi@0 88
aoqi@0 89 switch(s) {
aoqi@0 90 case "foo":
aoqi@0 91 failures = 0;
aoqi@0 92 addResult = stringSet.add(s);
aoqi@0 93 if (addResult != expected) {
aoqi@0 94 failures++;
aoqi@0 95 System.err.println("oneCaseNoDefault: Unexpectedly got add result of " + addResult +
aoqi@0 96 " on string " + s);
aoqi@0 97 }
aoqi@0 98 }
aoqi@0 99
aoqi@0 100 return failures;
aoqi@0 101 }
aoqi@0 102
aoqi@0 103 @TestMeForNull
aoqi@0 104 public static int oneCaseNoDefaultBreak(String s, Set<String> stringSet, boolean expected) {
aoqi@0 105 int failures = 2;
aoqi@0 106 boolean addResult;
aoqi@0 107
aoqi@0 108 switch(s) {
aoqi@0 109 case "foo":
aoqi@0 110 failures = oneCaseNoDefaultBreak(s, stringSet, expected);
aoqi@0 111 break;
aoqi@0 112 }
aoqi@0 113
aoqi@0 114 return failures;
aoqi@0 115 }
aoqi@0 116
aoqi@0 117 @TestMeForNull
aoqi@0 118 public static int oneCaseWithDefault(String s, Set<String> stringSet, boolean expected) {
aoqi@0 119 int failures = 2;
aoqi@0 120 boolean addResult;;
aoqi@0 121
aoqi@0 122 switch(s) {
aoqi@0 123 case "foo":
aoqi@0 124 failures = 0;
aoqi@0 125 addResult = stringSet.add(s);
aoqi@0 126 if (addResult != expected) {
aoqi@0 127 failures++;
aoqi@0 128 System.err.println("oneCaseNoDefault: Expectedly got add result of " + addResult +
aoqi@0 129 " on string " + s);
aoqi@0 130 }
aoqi@0 131 break;
aoqi@0 132 default:
aoqi@0 133 break;
aoqi@0 134 }
aoqi@0 135
aoqi@0 136 return failures;
aoqi@0 137 }
aoqi@0 138
aoqi@0 139 @TestMeForNull
aoqi@0 140 public static int oneCaseBreakOnly(String s, Set<String> stringSet, boolean expected) {
aoqi@0 141 int failures = 1;
aoqi@0 142 switch(s) {
aoqi@0 143 case "foo":
aoqi@0 144 break;
aoqi@0 145 }
aoqi@0 146 failures = 0;
aoqi@0 147 return failures;
aoqi@0 148 }
aoqi@0 149
aoqi@0 150 @TestMeForNull
aoqi@0 151 public static int oneCaseDefaultBreakOnly(String s, Set<String> stringSet, boolean expected) {
aoqi@0 152 int failures = 1;
aoqi@0 153 switch(s) {
aoqi@0 154 default:
aoqi@0 155 break;
aoqi@0 156 }
aoqi@0 157 failures = 0;
aoqi@0 158 return failures;
aoqi@0 159 }
aoqi@0 160
aoqi@0 161
aoqi@0 162 static int testNullBehavior() {
aoqi@0 163 int failures = 0;
aoqi@0 164 int count = 0;
aoqi@0 165
aoqi@0 166 Method[] methods = OneCaseSwitches.class.getDeclaredMethods();
aoqi@0 167
aoqi@0 168 try {
aoqi@0 169 for(Method method : methods) {
aoqi@0 170 count++;
aoqi@0 171 try {
aoqi@0 172 if (method.isAnnotationPresent(TestMeForNull.class)) {
aoqi@0 173 System.out.println("Testing method " + method);
aoqi@0 174 method.invoke(null, (String)null, emptyStringSet, false);
aoqi@0 175 failures++;
aoqi@0 176 System.err.println("Didn't get NPE as expected from " + method);
aoqi@0 177 }
aoqi@0 178 } catch (InvocationTargetException ite) { // Expected
aoqi@0 179 Throwable targetException = ite.getTargetException();
aoqi@0 180 if (! (targetException instanceof NullPointerException)) {
aoqi@0 181 failures++; // Wrong exception thrown
aoqi@0 182 System.err.println("Didn't get expected target exception NPE, got " +
aoqi@0 183 ite.getClass().getName());
aoqi@0 184 }
aoqi@0 185 }
aoqi@0 186 }
aoqi@0 187 } catch (Exception e) {
aoqi@0 188 throw new RuntimeException(e);
aoqi@0 189 }
aoqi@0 190
aoqi@0 191 if (count == 0) {
aoqi@0 192 failures++;
aoqi@0 193 System.err.println("Did not find any annotated methods.");
aoqi@0 194 }
aoqi@0 195 return failures;
aoqi@0 196 }
aoqi@0 197
aoqi@0 198 static int testZeroCases() {
aoqi@0 199 int failures = 0;
aoqi@0 200 Set<String> noDefaultSet = new HashSet<String>();
aoqi@0 201 Set<String> defaultSet = new HashSet<String>();
aoqi@0 202
aoqi@0 203 zeroCasesNoDefault(FOO, noDefaultSet, false);
aoqi@0 204 for(String word : words) {
aoqi@0 205 zeroCasesNoDefault(word, noDefaultSet, false);
aoqi@0 206 }
aoqi@0 207
aoqi@0 208 if (!noDefaultSet.isEmpty()) {
aoqi@0 209 failures++;
aoqi@0 210 System.err.println("Non-empty set after zeroCasesNoDefault");
aoqi@0 211 }
aoqi@0 212
aoqi@0 213 for(String word : words) {
aoqi@0 214 zeroCasesWithDefault(word, defaultSet, true);
aoqi@0 215 }
aoqi@0 216 if (defaultSet.size() != words.length) {
aoqi@0 217 failures++;
aoqi@0 218 System.err.println("Missing strings after zeroCasesWithDefault");
aoqi@0 219 }
aoqi@0 220
aoqi@0 221 return failures;
aoqi@0 222 }
aoqi@0 223
aoqi@0 224 static int testOneCaseNoDefault() {
aoqi@0 225 int failures = 0;
aoqi@0 226 Set<String> s = new HashSet<String>();
aoqi@0 227 s.add("foo");
aoqi@0 228 Set<String> fooSet = Collections.unmodifiableSet(s);
aoqi@0 229 Set<String> testSet = new HashSet<String>();
aoqi@0 230
aoqi@0 231 oneCaseNoDefault(FOO, testSet, true);
aoqi@0 232 if (!testSet.equals(fooSet)) {
aoqi@0 233 failures++;
aoqi@0 234 System.err.println("Unexpected result from oneCaseNoDefault: didn't get {\"Foo\"}");
aoqi@0 235 }
aoqi@0 236
aoqi@0 237 for(String word : words) {
aoqi@0 238 oneCaseNoDefault(word, testSet, false);
aoqi@0 239 }
aoqi@0 240 if (!testSet.equals(fooSet)) {
aoqi@0 241 failures++;
aoqi@0 242 System.err.println("Unexpected result from oneCaseNoDefault: didn't get {\"Foo\"}");
aoqi@0 243 }
aoqi@0 244
aoqi@0 245 return failures;
aoqi@0 246 }
aoqi@0 247
aoqi@0 248 static int testBreakOnly() {
aoqi@0 249 int failures = 0;
aoqi@0 250
aoqi@0 251 for(String word : words) {
aoqi@0 252 failures += oneCaseBreakOnly(word, emptyStringSet, true);
aoqi@0 253 failures += oneCaseDefaultBreakOnly(word, emptyStringSet, true);
aoqi@0 254 }
aoqi@0 255
aoqi@0 256 return failures;
aoqi@0 257 }
aoqi@0 258
aoqi@0 259 static int testExpressionEval() {
aoqi@0 260 String s = "a";
aoqi@0 261 int errors = 2;
aoqi@0 262
aoqi@0 263 System.out.println("Testing expression evaluation.");
aoqi@0 264
aoqi@0 265 switch (s + s) {
aoqi@0 266 case "aa":
aoqi@0 267 errors = 0;
aoqi@0 268 break;
aoqi@0 269
aoqi@0 270 case "aaaa":
aoqi@0 271 errors = 1;
aoqi@0 272 System.err.println("Suspected bad expression evaluation.");
aoqi@0 273 break;
aoqi@0 274
aoqi@0 275 default:
aoqi@0 276 throw new RuntimeException("Should not reach here.");
aoqi@0 277 }
aoqi@0 278 return errors;
aoqi@0 279 }
aoqi@0 280
aoqi@0 281 static final String FOO = "foo";
aoqi@0 282
aoqi@0 283 static final String[] words = {"baz",
aoqi@0 284 "quux",
aoqi@0 285 "wombat",
aoqi@0 286 "\u0ccc\u0012"}; // hash collision with "foo"
aoqi@0 287
aoqi@0 288 final static Set<String> emptyStringSet = Collections.emptySet();
aoqi@0 289
aoqi@0 290 public static void main(String... args) {
aoqi@0 291 int failures = 0;
aoqi@0 292
aoqi@0 293 failures += testNullBehavior();
aoqi@0 294 failures += testZeroCases();
aoqi@0 295 failures += testOneCaseNoDefault();
aoqi@0 296 failures += testBreakOnly();
aoqi@0 297 failures += testExpressionEval();
aoqi@0 298
aoqi@0 299 if (failures > 0) {
aoqi@0 300 throw new RuntimeException();
aoqi@0 301 }
aoqi@0 302 }
aoqi@0 303 }

mercurial