changeset 51549:5e622e823b4e switch

More tests for switch expression
author gbierman
date Wed, 11 Jul 2018 10:42:58 +0100
parents bd1b9d9af00d
children 39947391f99c
files test/langtools/tools/javac/expswitch/ExpSwitchNestingTest.java test/langtools/tools/javac/switchexpr/ExpressionSwitchInExpressionSwitch.java
diffstat 2 files changed, 85 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/test/langtools/tools/javac/expswitch/ExpSwitchNestingTest.java	Wed Jul 11 11:07:00 2018 +0200
+++ b/test/langtools/tools/javac/expswitch/ExpSwitchNestingTest.java	Wed Jul 11 10:42:58 2018 +0100
@@ -44,7 +44,7 @@
     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 INT_ESWITCH_DEFAULT = "int res = switch (x) { default -> { # } };";
     private static final String IF = "if (cond) { # }";
     private static final String BLOCK = "{ # }";
     private static final String BREAK_Z = "break 0;";
@@ -132,12 +132,21 @@
         assertOK(RUNNABLE, NOTHING);
         assertOK(INT_FN, RETURN_Z);
         assertFail("compiler.err.break.outside.switch.loop", RUNNABLE, BREAK_N);
+        assertFail("compiler.err.break.complex.value.no.switch.expression", RUNNABLE, BREAK_Z);
+        assertFail("compiler.err.break.complex.value.no.switch.expression", RUNNABLE, BREAK_S);
+        assertFail("compiler.err.break.outside.switch.loop", INT_FN, BREAK_N);
         assertFail("compiler.err.break.complex.value.no.switch.expression", INT_FN, BREAK_Z);
+        assertFail("compiler.err.break.complex.value.no.switch.expression", INT_FN, BREAK_S);
         assertFail("compiler.err.cont.outside.loop", RUNNABLE, CONTINUE_N);
         assertFail("compiler.err.undef.label", RUNNABLE, BREAK_L);
         assertFail("compiler.err.undef.label", RUNNABLE, CONTINUE_L);
+        assertFail("compiler.err.cont.outside.loop", INT_FN, CONTINUE_N);
+        assertFail("compiler.err.undef.label", INT_FN, BREAK_L);
+        assertFail("compiler.err.undef.label", INT_FN, CONTINUE_L);
         assertFail("compiler.err.undef.label", LABEL, BLOCK, RUNNABLE, BREAK_L);
         assertFail("compiler.err.undef.label", LABEL, BLOCK, RUNNABLE, CONTINUE_L);
+        assertFail("compiler.err.undef.label", LABEL, BLOCK, INT_FN, BREAK_L);
+        assertFail("compiler.err.undef.label", LABEL, BLOCK, INT_FN, CONTINUE_L);
     }
 
     public void testEswitch() {
@@ -145,31 +154,43 @@
         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.prob.found.req", ESWITCH_Z, BREAK_S);
         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);
+
+        assertOK(INT_ESWITCH_DEFAULT, BREAK_Z);
+        assertFail("compiler.err.break.missing.value", INT_ESWITCH_DEFAULT, BREAK_N);
+        assertFail("compiler.err.prob.found.req", INT_ESWITCH_DEFAULT, BREAK_S);
+        assertFail("compiler.err.cant.resolve.location", INT_ESWITCH_DEFAULT, BREAK_L);
+
+
         // 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.prob.found.req", ESWITCH_S, BREAK_Z);
         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?
+        assertFail("compiler.err.return.outside.switch.expression", ESWITCH_S, RETURN_S); 
         // 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.prob.found.req", INT_FN_ESWITCH, BREAK_Z);
+        assertFail("compiler.err.prob.found.req", INT_FN_ESWITCH, BREAK_S);
+
         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?
+        assertFail("compiler.err.return.outside.switch.expression", INT_FN_ESWITCH, RETURN_S); 
 
     }
 
@@ -208,8 +229,10 @@
 
     public void testBreakExpressionLabelDisambiguation() {
         assertOK(DEF_LABEL_VAR, ESWITCH_Z, BREAK_L);
-        assertFail("compiler.err.break.ambiguous.target", DEF_LABEL_VAR, ESWITCH_Z, LABEL, FOR, BREAK_L);
-        assertFail("compiler.err.break.ambiguous.target", DEF_LABEL_VAR, LABEL, BLOCK, ESWITCH_Z, BREAK_L);
+        assertFail("compiler.err.break.ambiguous.target", LABEL, FOR, BLOCK, DEF_LABEL_VAR, ESWITCH_Z, BREAK_L);
+        assertFail("compiler.err.break.ambiguous.target", DEF_LABEL_VAR, ESWITCH_Z, LABEL, FOR, BREAK_L); //label break
+        assertFail("compiler.err.break.ambiguous.target", DEF_LABEL_VAR, LABEL, BLOCK, ESWITCH_Z, BREAK_L); //expression break
+        //
     }
 
     public void testFunReturningSwitchExp() {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/langtools/tools/javac/switchexpr/ExpressionSwitchInExpressionSwitch.java	Wed Jul 11 10:42:58 2018 +0100
@@ -0,0 +1,57 @@
+/*
+ * 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 ExpressionSwitchInExpressionSwitch.java
+ * @run main/othervm --enable-preview ExpressionSwitchInExpressionSwitch
+ */
+
+public class ExpressionSwitchInExpressionSwitch {
+    public static void main(String[] args) {
+
+        int j = 42;
+        int i = switch (j) {
+            default -> (switch (j) { default -> 0; } )+1;
+        };
+        if (i!=1) {
+            throw new AssertionError("Unexpected result: " + i);
+        }
+        i = switch (j) {
+            default -> {
+                int k = switch (j) {
+                    default -> {
+                        break 42;
+                    }
+                };
+                System.out.println("didn't break to the top level");
+                break 43;
+            }
+        };
+        if (i!=43) {
+            throw new AssertionError("Unexpected result: " + i);
+        }
+    }
+}