1 /*
   2  * Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8206986
  27  * @summary Verify behavior of various kinds of breaks.
  28  * @compile --enable-preview -source ${jdk.version} ExpressionSwitchBreaks1.java
  29  * @run main/othervm --enable-preview ExpressionSwitchBreaks1
  30  */
  31 
  32 import java.util.Objects;
  33 import java.util.function.Supplier;
  34 
  35 public class ExpressionSwitchBreaks1 {
  36     public static void main(String... args) {
  37         new ExpressionSwitchBreaks1().run();
  38     }
  39 
  40     private void run() {
  41         check(print1(0, 0), "0-0");
  42         check(print1(0, 1), "0-1");
  43         check(print1(0, -1), "0-X");
  44         check(print1(-1, -1), "X");
  45         check(print2(0, 0, 0), "0-0-0");
  46         check(print2(0, 0, 1), "0-0-1");
  47         check(print2(0, 0, 2), "0-0-2");
  48         check(print2(0, 0, -1), "0-0-X");
  49         check(print2(0, 1, -1), "0-1");
  50         check(print2(0, -1, -1), "0-X");
  51         check(print2(1, -1, -1), "1");
  52         check(print2(2, 5, 5), "2-X-5");
  53         check(print2(-11, -1, -1), "X");
  54     }
  55 
  56     private String print1(int i, int j) {
  57         switch (i) {
  58             case 0:
  59                 return switch (j) {
  60                     case 0:
  61                         if (true) yield "0-0";
  62                     case 1:
  63                         yield "0-1";
  64                     default:
  65                         yield "0-X";
  66                 };
  67             default: return "X";
  68         }
  69     }
  70 
  71     private String print2(int i, int j, int k) {
  72         return switch (i) {
  73             case 0:
  74                 String r;
  75                 OUTER: switch (j) {
  76                     case 0:
  77                         String res;
  78                         INNER: switch (k) {
  79                             case 0: res = "0-0-0"; break;
  80                             case 1: res = "0-0-1"; break;
  81                             case 2: res = "0-0-2"; break INNER;
  82                             default: r = "0-0-X"; break OUTER;
  83                         }
  84                         r = res;
  85                         break;
  86                     case 1:
  87                         r = "0-1";
  88                         break;
  89                     default:
  90                         r = "0-X";
  91                         break;
  92                 }
  93                 yield r;
  94             case 1:
  95                 yield "1";
  96             case 2:
  97                 LOP: while (j-- > 0) {
  98                     if (k == 5) {
  99                         k--;
 100                         continue;
 101                     }
 102                     break LOP;
 103                 }
 104                 Supplier<String> getter = () -> { return "2-X-5"; };
 105                 yield getter.get();
 106             default:
 107                 yield "X";
 108         };
 109     }
 110 
 111     private void check(String result, String expected) {
 112         if (!Objects.equals(result, expected)) {
 113             throw new AssertionError("Unexpected result: " + result);
 114         }
 115     }
 116 
 117     enum T {
 118         A, B;
 119     }
 120 }