Add new switch tests with old syntax
Change-Id: I60cffddeeff6ed0326a3eb8087ed2183f9314abf
diff --git a/src/test/java23/com/android/tools/r8/java23/switchpatternmatching/EnumSwitchOldSyntaxTest.java b/src/test/java23/com/android/tools/r8/java23/switchpatternmatching/EnumSwitchOldSyntaxTest.java
new file mode 100644
index 0000000..b07ac87
--- /dev/null
+++ b/src/test/java23/com/android/tools/r8/java23/switchpatternmatching/EnumSwitchOldSyntaxTest.java
@@ -0,0 +1,105 @@
+// Copyright (c) 2025 the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+package com.android.tools.r8.java23.switchpatternmatching;
+
+import com.android.tools.r8.JdkClassFileProvider;
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.TestRuntime.CfVm;
+import com.android.tools.r8.utils.StringUtils;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameter;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+public class EnumSwitchOldSyntaxTest extends TestBase {
+
+ @Parameter public TestParameters parameters;
+
+ @Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters()
+ .withCfRuntimesStartingFromIncluding(CfVm.JDK23)
+ .withDexRuntimes()
+ .withAllApiLevelsAlsoForCf()
+ // TODO(b/405902452(: Enable partial compilation.
+ // .withPartialCompilation()
+ .build();
+ }
+
+ public static String EXPECTED_OUTPUT = StringUtils.lines("null", "e11", "e22", "e33");
+
+ @Test
+ public void testJvm() throws Exception {
+ parameters.assumeJvmTestParameters();
+ testForJvm(parameters)
+ .addInnerClassesAndStrippedOuter(getClass())
+ .run(parameters.getRuntime(), Main.class)
+ .assertSuccessWithOutput(EXPECTED_OUTPUT);
+ }
+
+ @Test
+ public void testD8() throws Exception {
+ testForD8(parameters.getBackend())
+ .addInnerClassesAndStrippedOuter(getClass())
+ .setMinApi(parameters)
+ .run(parameters.getRuntime(), Main.class)
+ .assertSuccessWithOutput(EXPECTED_OUTPUT);
+ }
+
+ @Test
+ public void testR8() throws Exception {
+ parameters.assumeR8TestParameters();
+ testForR8(parameters)
+ .addInnerClassesAndStrippedOuter(getClass())
+ .applyIf(
+ parameters.isCfRuntime(),
+ b -> b.addLibraryProvider(JdkClassFileProvider.fromSystemJdk()))
+ .setMinApi(parameters)
+ .addKeepMainRule(Main.class)
+ .addKeepEnumsRule()
+ .run(parameters.getRuntime(), Main.class)
+ .assertSuccessWithOutput(EXPECTED_OUTPUT);
+ }
+
+ public enum E {
+ E1,
+ E2,
+ E3
+ }
+
+ static class Main {
+
+ static void enumSwitch(E e) {
+ switch (e) {
+ case E.E1:
+ System.out.println("e11");
+ break;
+ case E.E2:
+ System.out.println("e22");
+ break;
+ case E.E3:
+ System.out.println("e33");
+ break;
+ case null:
+ System.out.println("null");
+ break;
+ }
+ }
+
+ public static void main(String[] args) {
+ try {
+ enumSwitch(null);
+ } catch (NullPointerException e) {
+ System.out.println("caught npe");
+ }
+ enumSwitch(E.E1);
+ enumSwitch(E.E2);
+ enumSwitch(E.E3);
+ }
+ }
+}
diff --git a/src/test/java23/com/android/tools/r8/java23/switchpatternmatching/StringSwitchOldSyntaxTest.java b/src/test/java23/com/android/tools/r8/java23/switchpatternmatching/StringSwitchOldSyntaxTest.java
new file mode 100644
index 0000000..798d3d6
--- /dev/null
+++ b/src/test/java23/com/android/tools/r8/java23/switchpatternmatching/StringSwitchOldSyntaxTest.java
@@ -0,0 +1,101 @@
+// Copyright (c) 2025, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+package com.android.tools.r8.java23.switchpatternmatching;
+
+import com.android.tools.r8.JdkClassFileProvider;
+import com.android.tools.r8.TestBase;
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.TestParametersCollection;
+import com.android.tools.r8.TestRuntime.CfVm;
+import com.android.tools.r8.utils.StringUtils;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameter;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+public class StringSwitchOldSyntaxTest extends TestBase {
+
+ @Parameter public TestParameters parameters;
+
+ @Parameters(name = "{0}")
+ public static TestParametersCollection data() {
+ return getTestParameters()
+ .withCfRuntimesStartingFromIncluding(CfVm.JDK23)
+ .withDexRuntimes()
+ .withAllApiLevelsAlsoForCf()
+ .withPartialCompilation()
+ .build();
+ }
+
+ public static String EXPECTED_OUTPUT = StringUtils.lines("null", "e11", "e22", "e33", "def");
+
+ @Test
+ public void testJvm() throws Exception {
+ parameters.assumeJvmTestParameters();
+ testForJvm(parameters)
+ .addInnerClassesAndStrippedOuter(getClass())
+ .run(parameters.getRuntime(), Main.class)
+ .assertSuccessWithOutput(EXPECTED_OUTPUT);
+ }
+
+ @Test
+ public void testD8() throws Exception {
+ testForD8(parameters.getBackend())
+ .addInnerClassesAndStrippedOuter(getClass())
+ .setMinApi(parameters)
+ .run(parameters.getRuntime(), Main.class)
+ .assertSuccessWithOutput(EXPECTED_OUTPUT);
+ }
+
+ @Test
+ public void testR8() throws Exception {
+ parameters.assumeR8TestParameters();
+ testForR8(parameters)
+ .addInnerClassesAndStrippedOuter(getClass())
+ .applyIf(
+ parameters.isCfRuntime(),
+ b -> b.addLibraryProvider(JdkClassFileProvider.fromSystemJdk()))
+ .setMinApi(parameters)
+ .addKeepMainRule(Main.class)
+ .run(parameters.getRuntime(), Main.class)
+ .assertSuccessWithOutput(EXPECTED_OUTPUT);
+ }
+
+ static class Main {
+
+ static void stringSwitch(String s) {
+ switch (s) {
+ case "E1":
+ System.out.println("e11");
+ break;
+ case "E2":
+ System.out.println("e22");
+ break;
+ case "E3":
+ System.out.println("e33");
+ break;
+ case null:
+ System.out.println("null");
+ break;
+ default:
+ System.out.println("def");
+ break;
+ }
+ }
+
+ public static void main(String[] args) {
+ try {
+ stringSwitch(null);
+ } catch (NullPointerException e) {
+ System.out.println("caught npe");
+ }
+ stringSwitch("E1");
+ stringSwitch("E2");
+ stringSwitch("E3");
+ stringSwitch("Other");
+ }
+ }
+}