test/langtools/tools/javac/StringsInSwitch/OneCaseSwitches.java
author prr
Fri, 25 May 2018 12:12:24 -0700
changeset 50347 b2f046ae8eb6
parent 47216 71c04702a3d5
child 50949 999f09bf3464
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4142
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
     1
/*
30715
099641abfb05 8074387: Group 11: golden files for coin tests in tools/javac dir
sogoel
parents: 5520
diff changeset
     2
 * @test /nodynamiccopyright/
40504
0a01f6710c84 8078561: Error message should be generated once when -source 6 is specified
jlahoda
parents: 30715
diff changeset
     3
 * @bug 6827009 8078561
4142
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
     4
 * @summary Positive tests for strings in switch with few alternatives.
30715
099641abfb05 8074387: Group 11: golden files for coin tests in tools/javac dir
sogoel
parents: 5520
diff changeset
     5
 * @compile/fail/ref=OneCaseSwitches.out -XDrawDiagnostics -source 6 OneCaseSwitches.java
099641abfb05 8074387: Group 11: golden files for coin tests in tools/javac dir
sogoel
parents: 5520
diff changeset
     6
 * @compile          OneCaseSwitches.java
4142
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
     7
 * @run main OneCaseSwitches
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
     8
 * @author  Joseph D. Darcy
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
     9
 */
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    10
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    11
import java.lang.reflect.*;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    12
import java.lang.annotation.*;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    13
import java.util.*;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    14
import static java.lang.annotation.RetentionPolicy.*;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    15
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    16
public class OneCaseSwitches {
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    17
    @Retention(RUNTIME)
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    18
    @interface TestMeForNull {}
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    19
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    20
    @TestMeForNull
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    21
    public static int zeroCasesNoDefault(String s, Set<String> stringSet, boolean expected) {
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    22
        int failures = 0;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    23
        switch(s) {
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    24
        }
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    25
        return failures;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    26
    }
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    27
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    28
    @TestMeForNull
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    29
    public static int zeroCasesWithDefault(String s, Set<String> stringSet, boolean expected) {
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    30
        int failures = 2;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    31
        boolean addResult;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    32
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    33
        switch(s) {
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    34
        default:
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    35
            failures = 0;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    36
            addResult = stringSet.add(s);
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    37
            if (addResult != expected) {
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    38
                failures++;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    39
                System.err.println("zeroCaseWithDefault: Expectedly got add result of " + addResult +
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    40
                                   " on string " + s);
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    41
            }
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    42
        }
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    43
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    44
        return failures;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    45
    }
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    46
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    47
    @TestMeForNull
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    48
    public static int zeroCasesWithDefaultBreak(String s, Set<String> stringSet, boolean expected) {
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    49
        int failures = 2;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    50
        boolean addResult;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    51
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    52
        switch(s) {
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    53
        default:
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    54
            failures = zeroCasesWithDefault(s, stringSet, expected);
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    55
            break;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    56
        }
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    57
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    58
        return failures;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    59
    }
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    60
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    61
    @TestMeForNull
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    62
    public static int oneCaseNoDefault(String s, Set<String> stringSet, boolean expected) {
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    63
        int failures = 2;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    64
        boolean addResult;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    65
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    66
        switch(s) {
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    67
        case "foo":
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    68
            failures = 0;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    69
            addResult = stringSet.add(s);
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    70
            if (addResult != expected) {
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    71
                failures++;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    72
                System.err.println("oneCaseNoDefault: Unexpectedly got add result of " + addResult +
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    73
                                   " on string " + s);
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    74
            }
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    75
        }
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    76
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    77
        return failures;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    78
    }
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    79
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    80
    @TestMeForNull
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    81
    public static int oneCaseNoDefaultBreak(String s, Set<String> stringSet, boolean expected) {
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    82
        int failures = 2;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    83
        boolean addResult;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    84
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    85
        switch(s) {
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    86
        case "foo":
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    87
            failures = oneCaseNoDefaultBreak(s, stringSet, expected);
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    88
            break;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    89
        }
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    90
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    91
        return failures;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    92
    }
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    93
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    94
    @TestMeForNull
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    95
    public static int oneCaseWithDefault(String s, Set<String> stringSet, boolean expected) {
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    96
        int failures = 2;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    97
        boolean addResult;;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    98
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
    99
        switch(s) {
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   100
        case "foo":
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   101
            failures = 0;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   102
            addResult = stringSet.add(s);
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   103
            if (addResult != expected) {
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   104
                failures++;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   105
                System.err.println("oneCaseNoDefault: Expectedly got add result of " + addResult +
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   106
                                   " on string " + s);
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   107
            }
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   108
            break;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   109
        default:
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   110
            break;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   111
        }
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   112
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   113
        return failures;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   114
    }
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   115
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   116
    @TestMeForNull
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   117
    public static int oneCaseBreakOnly(String s, Set<String> stringSet, boolean expected) {
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   118
        int failures = 1;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   119
        switch(s) {
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   120
        case "foo":
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   121
            break;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   122
        }
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   123
        failures = 0;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   124
        return failures;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   125
    }
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   126
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   127
    @TestMeForNull
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   128
    public static int oneCaseDefaultBreakOnly(String s, Set<String> stringSet, boolean expected) {
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   129
        int failures = 1;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   130
        switch(s) {
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   131
        default:
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   132
            break;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   133
        }
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   134
        failures = 0;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   135
        return failures;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   136
    }
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   137
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   138
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   139
    static int testNullBehavior() {
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   140
        int failures = 0;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   141
        int count = 0;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   142
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   143
        Method[] methods = OneCaseSwitches.class.getDeclaredMethods();
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   144
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   145
        try {
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   146
            for(Method method : methods) {
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   147
                count++;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   148
                try {
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   149
                    if (method.isAnnotationPresent(TestMeForNull.class)) {
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   150
                        System.out.println("Testing method " + method);
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   151
                        method.invoke(null, (String)null, emptyStringSet, false);
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   152
                        failures++;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   153
                        System.err.println("Didn't get NPE as expected from " + method);
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   154
                    }
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   155
                } catch (InvocationTargetException ite) { // Expected
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   156
                    Throwable targetException = ite.getTargetException();
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   157
                    if (! (targetException instanceof NullPointerException)) {
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   158
                        failures++; // Wrong exception thrown
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   159
                        System.err.println("Didn't get expected target exception NPE, got " +
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   160
                                           ite.getClass().getName());
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   161
                    }
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   162
                }
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   163
            }
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   164
        } catch (Exception e) {
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   165
            throw new RuntimeException(e);
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   166
        }
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   167
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   168
        if (count == 0) {
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   169
            failures++;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   170
            System.err.println("Did not find any annotated methods.");
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   171
        }
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   172
        return failures;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   173
    }
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   174
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   175
    static int testZeroCases() {
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   176
        int failures = 0;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   177
        Set<String> noDefaultSet = new HashSet<String>();
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   178
        Set<String> defaultSet   = new HashSet<String>();
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   179
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   180
        zeroCasesNoDefault(FOO, noDefaultSet, false);
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   181
        for(String word : words) {
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   182
            zeroCasesNoDefault(word, noDefaultSet, false);
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   183
        }
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   184
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   185
        if (!noDefaultSet.isEmpty()) {
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   186
            failures++;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   187
            System.err.println("Non-empty set after zeroCasesNoDefault");
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   188
        }
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   189
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   190
        for(String word : words) {
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   191
            zeroCasesWithDefault(word, defaultSet, true);
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   192
        }
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   193
        if (defaultSet.size() != words.length) {
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   194
            failures++;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   195
            System.err.println("Missing strings after zeroCasesWithDefault");
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   196
        }
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   197
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   198
        return failures;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   199
    }
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   200
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   201
    static int testOneCaseNoDefault() {
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   202
        int failures = 0;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   203
        Set<String> s = new HashSet<String>();
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   204
        s.add("foo");
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   205
        Set<String> fooSet = Collections.unmodifiableSet(s);
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   206
        Set<String> testSet   = new HashSet<String>();
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   207
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   208
        oneCaseNoDefault(FOO, testSet, true);
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   209
        if (!testSet.equals(fooSet)) {
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   210
            failures++;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   211
            System.err.println("Unexpected result from oneCaseNoDefault: didn't get {\"Foo\"}");
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   212
        }
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   213
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   214
        for(String word : words) {
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   215
            oneCaseNoDefault(word, testSet, false);
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   216
        }
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   217
        if (!testSet.equals(fooSet)) {
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   218
            failures++;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   219
            System.err.println("Unexpected result from oneCaseNoDefault: didn't get {\"Foo\"}");
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   220
        }
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   221
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   222
        return failures;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   223
    }
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   224
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   225
    static int testBreakOnly() {
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   226
        int failures = 0;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   227
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   228
        for(String word : words) {
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   229
            failures += oneCaseBreakOnly(word, emptyStringSet, true);
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   230
            failures += oneCaseDefaultBreakOnly(word, emptyStringSet, true);
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   231
        }
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   232
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   233
        return failures;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   234
    }
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   235
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   236
    static int testExpressionEval() {
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   237
        String s = "a";
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   238
        int errors = 2;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   239
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   240
        System.out.println("Testing expression evaluation.");
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   241
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   242
        switch (s + s) {
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   243
        case "aa":
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   244
            errors = 0;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   245
            break;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   246
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   247
        case "aaaa":
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   248
            errors = 1;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   249
            System.err.println("Suspected bad expression evaluation.");
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   250
            break;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   251
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   252
        default:
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   253
             throw new RuntimeException("Should not reach here.");
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   254
        }
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   255
        return errors;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   256
    }
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   257
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   258
    static final String FOO = "foo";
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   259
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   260
    static final String[] words = {"baz",
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   261
                                   "quux",
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   262
                                   "wombat",
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   263
                                   "\u0ccc\u0012"}; // hash collision with "foo"
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   264
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   265
    final static Set<String> emptyStringSet = Collections.emptySet();
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   266
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   267
    public static void main(String... args) {
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   268
        int failures = 0;
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   269
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   270
        failures += testNullBehavior();
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   271
        failures += testZeroCases();
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   272
        failures += testOneCaseNoDefault();
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   273
        failures += testBreakOnly();
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   274
        failures += testExpressionEval();
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   275
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   276
        if (failures > 0) {
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   277
            throw new RuntimeException();
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   278
        }
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   279
    }
bd950c8f4fb3 6827009: Project Coin: Strings in Switch
darcy
parents:
diff changeset
   280
}