changeset 51539:10a0cdfd83ad switch

Added more tests for switch expressions
author gbierman
date Mon, 09 Jul 2018 15:02:42 +0100
parents 27247bfeaf89
children 36a1c43285a6
files test/langtools/tools/javac/expswitch/ExpSwitchNestingTest.java test/langtools/tools/javac/switchexpr/ExpressionSwitchCodeFromJLS.java test/langtools/tools/javac/switchexpr/ExpressionSwitchDA.java test/langtools/tools/javac/switchexpr/ExpressionSwitchUnreachable.java test/langtools/tools/javac/switchexpr/ExpressionSwitchUnreachable.out
diffstat 5 files changed, 382 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/test/langtools/tools/javac/expswitch/ExpSwitchNestingTest.java	Fri Jun 29 16:40:37 2018 +0200
+++ b/test/langtools/tools/javac/expswitch/ExpSwitchNestingTest.java	Mon Jul 09 15:02:42 2018 +0100
@@ -41,14 +41,20 @@
     private static final String WHILE = "while (cond) { # }";
     private static final String DO = "do { # } while (cond);";
     private static final String SSWITCH = "switch (x) { case 0: # };";
-    private static final String ESWITCH = "int res = switch (x) { case 0 -> { # } default -> 0; };";
+    private static final String ESWITCH_Z = "int res = switch (x) { case 0 -> { # } default -> 0; };";
+    private static final String ESWITCH_S = "String res_string = switch (x) { case 0 -> { # } default -> \"default\"; };";
+    private static final String INT_FN_ESWITCH = "java.util.function.IntSupplier r = switch (x) { case 0 -> { # } default -> null; };";
+    private static final String ESWITCH_DEFAULT = "int res = switch (x) { case 0 -> 0; default -> { # }; };";
     private static final String IF = "if (cond) { # }";
     private static final String BLOCK = "{ # }";
     private static final String BREAK_Z = "break 0;";
+    private static final String BREAK_S = "break \"hello world\";";
+    private static final String BREAK_INT_FN = "break () -> 0 ;";
     private static final String BREAK_N = "break;";
     private static final String BREAK_L = "break label;";
     private static final String RETURN_Z = "return 0;";
     private static final String RETURN_N = "return;";
+    private static final String RETURN_S = "return \"Hello\";";
     private static final String CONTINUE_N = "continue;";
     private static final String CONTINUE_L = "continue label;";
     private static final String NOTHING = "System.out.println();";
@@ -135,30 +141,79 @@
     }
 
     public void testEswitch() {
-        assertOK(ESWITCH, BREAK_Z);
-        assertOK(LABEL, BLOCK, ESWITCH, BREAK_Z);
-        assertFail("compiler.err.break.missing.value", ESWITCH, BREAK_N);
-        assertFail("compiler.err.cant.resolve.location", ESWITCH, BREAK_L);
-        assertFail("compiler.err.break.outside.switch.expression", LABEL, BLOCK, ESWITCH, BREAK_L);
-        assertFail("compiler.err.undef.label", ESWITCH, CONTINUE_L);
-        assertFail("compiler.err.cont.outside.loop", ESWITCH, CONTINUE_N);
-        assertFail("compiler.err.return.outside.switch.expression", ESWITCH, RETURN_N);
-        assertFail("compiler.err.return.outside.switch.expression", ESWITCH, RETURN_Z);
+        //Int-valued switch expressions
+        assertOK(ESWITCH_Z, BREAK_Z);
+        assertOK(LABEL, BLOCK, ESWITCH_Z, BREAK_Z);
+        assertFail("compiler.err.break.missing.value", ESWITCH_Z, BREAK_N);
+        assertFail("compiler.err.cant.resolve.location", ESWITCH_Z, BREAK_L);
+        assertFail("compiler.err.break.outside.switch.expression", LABEL, BLOCK, ESWITCH_Z, BREAK_L);
+        assertFail("compiler.err.undef.label", ESWITCH_Z, CONTINUE_L);
+        assertFail("compiler.err.cont.outside.loop", ESWITCH_Z, CONTINUE_N);
+        assertFail("compiler.err.return.outside.switch.expression", ESWITCH_Z, RETURN_N);
+        assertFail("compiler.err.return.outside.switch.expression", ESWITCH_Z, RETURN_Z);
+        // String-valued switch expressions
+        assertOK(ESWITCH_S, BREAK_S);
+        assertOK(LABEL, BLOCK, ESWITCH_S, BREAK_S);
+        assertFail("compiler.err.break.missing.value", ESWITCH_S, BREAK_N);
+        assertFail("compiler.err.cant.resolve.location", ESWITCH_S, BREAK_L);
+        assertFail("compiler.err.break.outside.switch.expression", LABEL, BLOCK, ESWITCH_S, BREAK_L);
+        assertFail("compiler.err.undef.label", ESWITCH_S, CONTINUE_L);
+        assertFail("compiler.err.cont.outside.loop", ESWITCH_S, CONTINUE_N);
+        assertFail("compiler.err.return.outside.switch.expression", ESWITCH_S, RETURN_N);
+        assertFail("compiler.err.return.outside.switch.expression", ESWITCH_S, RETURN_S); //? which error message is right here?
+        // Function-valued switch expression
+        assertOK(INT_FN_ESWITCH, BREAK_INT_FN);
+        assertFail("compiler.err.break.missing.value", INT_FN_ESWITCH, BREAK_N);
+        assertFail("compiler.err.cant.resolve.location", INT_FN_ESWITCH, BREAK_L);
+        assertFail("compiler.err.break.outside.switch.expression", LABEL, BLOCK, INT_FN_ESWITCH, BREAK_L);
+        assertFail("compiler.err.undef.label", INT_FN_ESWITCH, CONTINUE_L);
+        assertFail("compiler.err.cont.outside.loop", INT_FN_ESWITCH, CONTINUE_N);
+        assertFail("compiler.err.return.outside.switch.expression", INT_FN_ESWITCH, RETURN_N);
+        assertFail("compiler.err.return.outside.switch.expression", INT_FN_ESWITCH, RETURN_S); //? which error message is right here?
+
     }
 
     public void testNestedInExpSwitch() {
-        assertOK(ESWITCH, IF, BREAK_Z);
-        assertOK(ESWITCH, BLOCK, BREAK_Z);
-        assertFail("compiler.err.break.expr.not.immediate", ESWITCH, SSWITCH, BREAK_Z);
-        assertFail("compiler.err.break.expr.not.immediate", ESWITCH, FOR, BREAK_Z);
-        assertFail("compiler.err.break.expr.not.immediate", ESWITCH, WHILE, BREAK_Z);
-        assertFail("compiler.err.break.expr.not.immediate", ESWITCH, DO, BREAK_Z);
+        assertOK(ESWITCH_Z, IF,     BREAK_Z);
+        assertOK(ESWITCH_Z, BLOCK,  BREAK_Z);
+        //
+        assertOK(ESWITCH_Z, IF,     IF,     BREAK_Z);
+        assertOK(ESWITCH_Z, IF,     BLOCK,  BREAK_Z);
+        assertOK(ESWITCH_Z, BLOCK,  IF,     BREAK_Z);
+        assertOK(ESWITCH_Z, BLOCK,  BLOCK,  BREAK_Z);
+        //
+        assertOK(ESWITCH_Z, IF,     IF,     IF,     BREAK_Z);
+        assertOK(ESWITCH_Z, IF,     IF,     BLOCK,  BREAK_Z);
+        assertOK(ESWITCH_Z, IF,     BLOCK,  IF,     BREAK_Z);
+        assertOK(ESWITCH_Z, IF,     BLOCK,  BLOCK,  BREAK_Z);
+        assertOK(ESWITCH_Z, BLOCK,  IF,     IF,     BREAK_Z);
+        assertOK(ESWITCH_Z, BLOCK,  IF,     BLOCK,  BREAK_Z);
+        assertOK(ESWITCH_Z, BLOCK,  BLOCK,  IF,     BREAK_Z);
+        assertOK(ESWITCH_Z, BLOCK,  BLOCK,  BLOCK,  BREAK_Z);
+        //
+        assertFail("compiler.err.break.expr.not.immediate", ESWITCH_Z, SSWITCH, BREAK_Z);
+        assertFail("compiler.err.break.expr.not.immediate", ESWITCH_Z, FOR, BREAK_Z);
+        assertFail("compiler.err.break.expr.not.immediate", ESWITCH_Z, WHILE, BREAK_Z);
+        assertFail("compiler.err.break.expr.not.immediate", ESWITCH_Z, DO, BREAK_Z);
+        assertFail("compiler.err.break.complex.value.no.switch.expression", ESWITCH_Z, INT_FN, BREAK_Z);
+        assertFail("compiler.err.break.expr.not.immediate", ESWITCH_Z, SSWITCH, IF, BREAK_Z);
+        assertFail("compiler.err.break.expr.not.immediate", ESWITCH_Z, FOR, IF, BREAK_Z);
+        assertFail("compiler.err.break.expr.not.immediate", ESWITCH_Z, WHILE, IF, BREAK_Z);
+        assertFail("compiler.err.break.expr.not.immediate", ESWITCH_Z, DO, IF, BREAK_Z);
+        assertFail("compiler.err.break.expr.not.immediate", ESWITCH_Z, BLOCK, SSWITCH, IF, BREAK_Z);
+        assertFail("compiler.err.break.expr.not.immediate", ESWITCH_Z, BLOCK, FOR, IF, BREAK_Z);
+        assertFail("compiler.err.break.expr.not.immediate", ESWITCH_Z, BLOCK, WHILE, IF, BREAK_Z);
+        assertFail("compiler.err.break.expr.not.immediate", ESWITCH_Z, BLOCK, DO, IF, BREAK_Z);
     }
 
     public void testBreakExpressionLabelDisambiguation() {
-        assertOK(DEF_LABEL_VAR, ESWITCH, BREAK_L);
-        assertOKWithWarning("compiler.warn.break.ambiguous.target", DEF_LABEL_VAR, ESWITCH, LABEL, FOR, BREAK_L); //label break
-        assertOKWithWarning("compiler.warn.break.ambiguous.target", DEF_LABEL_VAR, LABEL, BLOCK, ESWITCH, BREAK_L); //expression break
+        assertOK(DEF_LABEL_VAR, ESWITCH_Z, BREAK_L);
+        assertOKWithWarning("compiler.warn.break.ambiguous.target", DEF_LABEL_VAR, ESWITCH_Z, LABEL, FOR, BREAK_L); //label break
+        assertOKWithWarning("compiler.warn.break.ambiguous.target", DEF_LABEL_VAR, LABEL, BLOCK, ESWITCH_Z, BREAK_L); //expression break
         //
     }
+
+    public void testFunReturningSwitchExp() {
+        assertOK(INT_FN_ESWITCH, BREAK_INT_FN);
+    }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/switchexpr/ExpressionSwitchCodeFromJLS.java	Mon Jul 09 15:02:42 2018 +0100
@@ -0,0 +1,68 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * @test
+ * @compile --enable-preview -source 12 ExpressionSwitchCodeFromJLS.java
+ * @run main/othervm --enable-preview ExpressionSwitchCodeFromJLS
+ */
+
+public class ExpressionSwitchCodeFromJLS {
+    static void howMany(int k) {
+        switch (k) {
+            case 1: System.out.print("one ");
+            case 2: System.out.print("too ");
+            case 3: System.out.println("many");
+        }
+    }
+    static void howManyRule(int k) {
+        switch (k) {
+            case 1 -> System.out.println("one");
+            case 2 -> System.out.println("two");
+            case 3 -> System.out.println("many");
+        }
+    }
+    static void howManyGroup(int k) {
+        switch (k) {
+            case 1: System.out.println("one");
+                break;  // exit the switch
+            case 2: System.out.println("two");
+                break;  // exit the switch
+            case 3: System.out.println("many");
+                break;  // not needed, but good style
+        }
+    }
+    public static void main(String[] args) {
+        howMany(3);
+        howMany(2);
+        howMany(1);
+        howManyRule(1);
+        howManyRule(2);
+        howManyRule(3);
+        howManyGroup(1);
+        howManyGroup(2);
+        howManyGroup(3);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/switchexpr/ExpressionSwitchDA.java	Mon Jul 09 15:02:42 2018 +0100
@@ -0,0 +1,176 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * @test
+ * @compile --enable-preview -source 12 ExpressionSwitchDA.java
+ * @run main/othervm --enable-preview ExpressionSwitchDA
+ */
+
+public class ExpressionSwitchDA {
+    public static void test1() {
+        int i;
+        int j = 0;
+        switch (j) {
+            case 0 : i=42; break;
+            default: i=42;
+        }
+        System.out.println(i);
+    }
+    public static void test2(){
+        int i;
+        int j = 0;
+        switch (j) {
+            case 0  -> i=42;
+            default -> i=42;
+        }
+        System.out.println(i);
+    }
+    public static void test3(){
+        int i;
+        int j = 0;
+        switch (j) {
+            case 0  -> { i=42; }
+            default -> { i=42; }
+        }
+        System.out.println(i);
+    }
+    public static void test4(){
+        int i;
+        int j = 0;
+        int k = switch (j) {
+            case 0  -> i=42;
+            default -> i=42;
+        };
+        System.out.println(i);
+    }
+    public static void test5(){
+        int i;
+        int j = 0;
+        int k = switch (j) {
+            case 0  -> { i=42; break 42; }
+            default -> i=42;
+        };
+        System.out.println(i);
+    }
+    public static void test6(){
+        int i;
+        int j = 0;
+        int k = switch (j) {
+            case 0  -> i=42;
+            default -> { i=42; break 42; }
+        };
+        System.out.println(i);
+    }
+    public static void test7(){
+        int i;
+        int j = 0;
+        int k = switch (j) {
+            case 0  -> { i=42; break 42; }
+            default -> { i=42; break 42; }
+        };
+        System.out.println(i);
+    }
+    public static void test8() {
+        int i;
+        int j = 0;
+        switch (j) {
+            case 0 : i=42; break;
+            default: throw new NullPointerException();
+        }
+        System.out.println(i);
+    }
+    // These two should pass but fail :-( Bug in reachability
+    public static void test9() {
+        int i;
+        int j = 0;
+        switch (j) {
+            case 0 -> i=42;
+            default ->  throw new NullPointerException();
+        }
+        System.out.println(i);
+    }
+    public static void test10() {
+        int i;
+        int j = 0;
+        switch (j) {
+            case 0 -> { i=42; System.out.print(i);}
+            default ->  throw new NullPointerException();
+        }
+        System.out.println(i);
+    }
+    public static void test11() {
+        int i;
+        int j = 0;
+        switch (j) {
+            case 0 -> { i=42; System.out.print(i);}
+            default ->  { throw new NullPointerException(); }
+        }
+        System.out.println(i);
+    }
+    public static void test12() {
+        int i;
+        int j = 0;
+        switch (j) {
+            case 0 : i=42; break;
+            default: return;
+        }
+        System.out.println(i);
+    }
+    public static void test13() {
+        int i;
+        int j = 0;
+        switch (j) {
+            case 0 -> i=42;
+            default -> { return; }
+        }
+        System.out.println(i);
+    }
+    public static void test14() {
+        int i;
+        int j = 0;
+        switch (j) {
+            case 0 -> { i=42; }
+            default -> { return; }
+        }
+        System.out.println(i);
+    }
+    public static void main(String[] args) {
+        test1();
+        test2();
+        test3();
+        test4();
+        test5();
+        test6();
+        test7();
+        test8();
+        test9();
+        test10();
+        test11();
+        test12();
+        test13();
+        test14();
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/switchexpr/ExpressionSwitchUnreachable.java	Mon Jul 09 15:02:42 2018 +0100
@@ -0,0 +1,55 @@
+/**
+ * @test /nodynamiccopyright/
+ * @compile/fail/ref=ExpressionSwitchUnreachable.out -XDrawDiagnostics --enable-preview -source 12 ExpressionSwitchUnreachable.java
+ */
+
+public class ExpressionSwitchUnreachable {
+
+    public static void main(String[] args) {
+        int z = 42;
+        int i = switch (z) {
+            case 0 -> {
+                break 42;
+                System.out.println("Unreachable");  //Unreachable
+            }
+            default -> 0;
+        };
+        i = switch (z) {
+            case 0 -> {
+                break 42;
+                break 42; //Unreachable
+            }
+            default -> 0;
+        };
+        i = switch (z) {
+            case 0:
+                System.out.println("0");
+                break 42;
+                System.out.println("1");    //Unreachable
+            default : break 42;
+        };
+        i = switch (z) {
+            case 0 -> 42;
+            default -> {
+                break 42;
+                System.out.println("Unreachable"); //Unreachable
+            }
+        };
+        i = switch (z) {
+            case 0: break 42;
+            default:
+                System.out.println("0");
+                break 42;
+                System.out.println("1");    //Unreachable
+        };
+        i = switch (z) {
+            case 0:
+            default:
+                System.out.println("0");
+                break 42;
+                System.out.println("1");    //Unreachable
+        };
+    }
+
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/switchexpr/ExpressionSwitchUnreachable.out	Mon Jul 09 15:02:42 2018 +0100
@@ -0,0 +1,9 @@
+ExpressionSwitchUnreachable.java:13:17: compiler.err.unreachable.stmt
+ExpressionSwitchUnreachable.java:20:17: compiler.err.unreachable.stmt
+ExpressionSwitchUnreachable.java:28:17: compiler.err.unreachable.stmt
+ExpressionSwitchUnreachable.java:35:17: compiler.err.unreachable.stmt
+ExpressionSwitchUnreachable.java:43:17: compiler.err.unreachable.stmt
+ExpressionSwitchUnreachable.java:50:17: compiler.err.unreachable.stmt
+- compiler.note.preview.filename: ExpressionSwitchUnreachable.java
+- compiler.note.preview.recompile
+6 errors
\ No newline at end of file