| // 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.jdk25.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.internal.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.JDK25) | |
| .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) | |
| .addInnerClassesAndStrippedOuter(getClass()) | |
| .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())) | |
| .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"); | |
| } | |
| } | |
| } |