Søren Gjesse | 8e1222c | 2024-04-23 15:20:54 +0200 | [diff] [blame] | 1 | // Copyright (c) 2024, the R8 project authors. Please see the AUTHORS file |
| 2 | // for details. All rights reserved. Use of this source code is governed by a |
| 3 | // BSD-style license that can be found in the LICENSE file. |
| 4 | package switchpatternmatching; |
| 5 | |
Søren Gjesse | eb9ab6e | 2024-12-09 14:15:16 +0100 | [diff] [blame] | 6 | import static com.android.tools.r8.desugar.switchpatternmatching.SwitchTestHelper.hasJdk21TypeSwitch; |
Clément Béra | f48bfff | 2024-05-15 11:22:09 +0200 | [diff] [blame] | 7 | import static org.junit.Assert.assertTrue; |
Søren Gjesse | 8e1222c | 2024-04-23 15:20:54 +0200 | [diff] [blame] | 8 | import static org.junit.Assume.assumeTrue; |
Søren Gjesse | 8e1222c | 2024-04-23 15:20:54 +0200 | [diff] [blame] | 9 | |
Clément Béra | 6592f0e | 2024-05-13 15:07:46 +0200 | [diff] [blame] | 10 | import com.android.tools.r8.JdkClassFileProvider; |
Søren Gjesse | 8e1222c | 2024-04-23 15:20:54 +0200 | [diff] [blame] | 11 | import com.android.tools.r8.TestBase; |
| 12 | import com.android.tools.r8.TestParameters; |
| 13 | import com.android.tools.r8.TestParametersCollection; |
| 14 | import com.android.tools.r8.TestRuntime.CfVm; |
| 15 | import com.android.tools.r8.ToolHelper; |
| 16 | import com.android.tools.r8.utils.StringUtils; |
| 17 | import com.android.tools.r8.utils.codeinspector.CodeInspector; |
Clément Béra | b54fc3e | 2024-05-02 09:19:45 +0200 | [diff] [blame] | 18 | import org.junit.Assume; |
Søren Gjesse | 8e1222c | 2024-04-23 15:20:54 +0200 | [diff] [blame] | 19 | import org.junit.Test; |
| 20 | import org.junit.runner.RunWith; |
| 21 | import org.junit.runners.Parameterized; |
| 22 | import org.junit.runners.Parameterized.Parameter; |
| 23 | import org.junit.runners.Parameterized.Parameters; |
Søren Gjesse | 8e1222c | 2024-04-23 15:20:54 +0200 | [diff] [blame] | 24 | |
Søren Gjesse | eb9ab6e | 2024-12-09 14:15:16 +0100 | [diff] [blame] | 25 | // This test is copied into later JDK tests (currently JDK-23). The reason for the copy is that |
| 26 | // from JDK-23 the code generation changed. Please update the copy as well if updating this test. |
Søren Gjesse | 8e1222c | 2024-04-23 15:20:54 +0200 | [diff] [blame] | 27 | @RunWith(Parameterized.class) |
| 28 | public class StringSwitchTest extends TestBase { |
| 29 | |
| 30 | @Parameter public TestParameters parameters; |
| 31 | |
| 32 | @Parameters(name = "{0}") |
| 33 | public static TestParametersCollection data() { |
Clément Béra | 2ab2d97 | 2024-05-15 11:32:33 +0200 | [diff] [blame] | 34 | return getTestParameters().withAllRuntimes().withAllApiLevelsAlsoForCf().build(); |
Søren Gjesse | 8e1222c | 2024-04-23 15:20:54 +0200 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | public static String EXPECTED_OUTPUT = |
| 38 | StringUtils.lines( |
| 39 | "null", "y or Y", "y or Y", "n or N", "n or N", "yes", "yes", "no", "no", "unknown"); |
| 40 | |
| 41 | @Test |
| 42 | public void testJvm() throws Exception { |
| 43 | assumeTrue(parameters.isCfRuntime()); |
| 44 | CodeInspector inspector = new CodeInspector(ToolHelper.getClassFileForTestClass(Main.class)); |
Clément Béra | f48bfff | 2024-05-15 11:22:09 +0200 | [diff] [blame] | 45 | assertTrue( |
| 46 | hasJdk21TypeSwitch( |
| 47 | inspector.clazz(Main.class).uniqueMethodWithOriginalName("stringSwitch"))); |
Søren Gjesse | 8e1222c | 2024-04-23 15:20:54 +0200 | [diff] [blame] | 48 | |
| 49 | parameters.assumeJvmTestParameters(); |
| 50 | testForJvm(parameters) |
| 51 | .addInnerClassesAndStrippedOuter(getClass()) |
| 52 | .run(parameters.getRuntime(), Main.class) |
| 53 | .applyIf( |
| 54 | parameters.getCfRuntime().isNewerThanOrEqual(CfVm.JDK21), |
| 55 | r -> r.assertSuccessWithOutput(EXPECTED_OUTPUT), |
| 56 | r -> r.assertFailureWithErrorThatThrows(UnsupportedClassVersionError.class)); |
| 57 | } |
| 58 | |
| 59 | @Test |
| 60 | public void testD8() throws Exception { |
Clément Béra | 2ab2d97 | 2024-05-15 11:32:33 +0200 | [diff] [blame] | 61 | testForD8(parameters.getBackend()) |
Clément Béra | b54fc3e | 2024-05-02 09:19:45 +0200 | [diff] [blame] | 62 | .addInnerClassesAndStrippedOuter(getClass()) |
| 63 | .setMinApi(parameters) |
| 64 | .run(parameters.getRuntime(), Main.class) |
| 65 | .assertSuccessWithOutput(EXPECTED_OUTPUT); |
Søren Gjesse | 8e1222c | 2024-04-23 15:20:54 +0200 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | @Test |
| 69 | public void testR8() throws Exception { |
Clément Béra | 2ab2d97 | 2024-05-15 11:32:33 +0200 | [diff] [blame] | 70 | parameters.assumeR8TestParameters(); |
Clément Béra | 6592f0e | 2024-05-13 15:07:46 +0200 | [diff] [blame] | 71 | Assume.assumeTrue( |
| 72 | parameters.isDexRuntime() |
| 73 | || (parameters.isCfRuntime() |
| 74 | && parameters.getCfRuntime().isNewerThanOrEqual(CfVm.JDK21))); |
Clément Béra | b54fc3e | 2024-05-02 09:19:45 +0200 | [diff] [blame] | 75 | testForR8(parameters.getBackend()) |
| 76 | .addInnerClassesAndStrippedOuter(getClass()) |
Clément Béra | 6592f0e | 2024-05-13 15:07:46 +0200 | [diff] [blame] | 77 | .applyIf( |
| 78 | parameters.isCfRuntime(), |
| 79 | b -> b.addLibraryProvider(JdkClassFileProvider.fromSystemJdk())) |
Clément Béra | b54fc3e | 2024-05-02 09:19:45 +0200 | [diff] [blame] | 80 | .setMinApi(parameters) |
| 81 | .addKeepMainRule(Main.class) |
| 82 | .run(parameters.getRuntime(), Main.class) |
| 83 | .assertSuccessWithOutput(EXPECTED_OUTPUT); |
Søren Gjesse | 8e1222c | 2024-04-23 15:20:54 +0200 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | static class Main { |
| 87 | static void stringSwitch(String string) { |
| 88 | switch (string) { |
| 89 | case null -> { |
| 90 | System.out.println("null"); |
| 91 | } |
Søren Gjesse | 8e1222c | 2024-04-23 15:20:54 +0200 | [diff] [blame] | 92 | case String s when s.equalsIgnoreCase("YES") -> { |
| 93 | System.out.println("yes"); |
| 94 | } |
Clément Béra | e2438dd | 2024-04-30 12:38:46 +0200 | [diff] [blame] | 95 | case "y", "Y" -> { |
| 96 | System.out.println("y or Y"); |
| 97 | } |
Søren Gjesse | 8e1222c | 2024-04-23 15:20:54 +0200 | [diff] [blame] | 98 | case String s when s.equalsIgnoreCase("NO") -> { |
| 99 | System.out.println("no"); |
| 100 | } |
Clément Béra | e2438dd | 2024-04-30 12:38:46 +0200 | [diff] [blame] | 101 | case "n", "N" -> { |
| 102 | System.out.println("n or N"); |
| 103 | } |
Søren Gjesse | 8e1222c | 2024-04-23 15:20:54 +0200 | [diff] [blame] | 104 | case String s -> { |
| 105 | System.out.println("unknown"); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | public static void main(String[] args) { |
| 111 | stringSwitch(null); |
| 112 | stringSwitch("y"); |
| 113 | stringSwitch("Y"); |
| 114 | stringSwitch("n"); |
| 115 | stringSwitch("N"); |
| 116 | stringSwitch("yes"); |
| 117 | stringSwitch("YES"); |
| 118 | stringSwitch("no"); |
| 119 | stringSwitch("NO"); |
| 120 | stringSwitch("?"); |
| 121 | } |
| 122 | } |
| 123 | } |