blob: 150951ac66d1ad9d371a3927f136fd3baf129872 [file] [log] [blame]
Søren Gjesse8e1222c2024-04-23 15:20:54 +02001// 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.
4package switchpatternmatching;
5
Søren Gjesseeb9ab6e2024-12-09 14:15:16 +01006import static com.android.tools.r8.desugar.switchpatternmatching.SwitchTestHelper.hasJdk21TypeSwitch;
Clément Béraf48bfff2024-05-15 11:22:09 +02007import static org.junit.Assert.assertTrue;
Søren Gjesse8e1222c2024-04-23 15:20:54 +02008import static org.junit.Assume.assumeTrue;
Søren Gjesse8e1222c2024-04-23 15:20:54 +02009
Clément Béra6592f0e2024-05-13 15:07:46 +020010import com.android.tools.r8.JdkClassFileProvider;
Søren Gjesse8e1222c2024-04-23 15:20:54 +020011import com.android.tools.r8.TestBase;
12import com.android.tools.r8.TestParameters;
13import com.android.tools.r8.TestParametersCollection;
14import com.android.tools.r8.TestRuntime.CfVm;
15import com.android.tools.r8.ToolHelper;
16import com.android.tools.r8.utils.StringUtils;
17import com.android.tools.r8.utils.codeinspector.CodeInspector;
Clément Bérab54fc3e2024-05-02 09:19:45 +020018import org.junit.Assume;
Søren Gjesse8e1222c2024-04-23 15:20:54 +020019import org.junit.Test;
20import org.junit.runner.RunWith;
21import org.junit.runners.Parameterized;
22import org.junit.runners.Parameterized.Parameter;
23import org.junit.runners.Parameterized.Parameters;
Søren Gjesse8e1222c2024-04-23 15:20:54 +020024
Søren Gjesseeb9ab6e2024-12-09 14:15:16 +010025// 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 Gjesse8e1222c2024-04-23 15:20:54 +020027@RunWith(Parameterized.class)
28public class StringSwitchTest extends TestBase {
29
30 @Parameter public TestParameters parameters;
31
32 @Parameters(name = "{0}")
33 public static TestParametersCollection data() {
Clément Béra2ab2d972024-05-15 11:32:33 +020034 return getTestParameters().withAllRuntimes().withAllApiLevelsAlsoForCf().build();
Søren Gjesse8e1222c2024-04-23 15:20:54 +020035 }
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éraf48bfff2024-05-15 11:22:09 +020045 assertTrue(
46 hasJdk21TypeSwitch(
47 inspector.clazz(Main.class).uniqueMethodWithOriginalName("stringSwitch")));
Søren Gjesse8e1222c2024-04-23 15:20:54 +020048
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éra2ab2d972024-05-15 11:32:33 +020061 testForD8(parameters.getBackend())
Clément Bérab54fc3e2024-05-02 09:19:45 +020062 .addInnerClassesAndStrippedOuter(getClass())
63 .setMinApi(parameters)
64 .run(parameters.getRuntime(), Main.class)
65 .assertSuccessWithOutput(EXPECTED_OUTPUT);
Søren Gjesse8e1222c2024-04-23 15:20:54 +020066 }
67
68 @Test
69 public void testR8() throws Exception {
Clément Béra2ab2d972024-05-15 11:32:33 +020070 parameters.assumeR8TestParameters();
Clément Béra6592f0e2024-05-13 15:07:46 +020071 Assume.assumeTrue(
72 parameters.isDexRuntime()
73 || (parameters.isCfRuntime()
74 && parameters.getCfRuntime().isNewerThanOrEqual(CfVm.JDK21)));
Clément Bérab54fc3e2024-05-02 09:19:45 +020075 testForR8(parameters.getBackend())
76 .addInnerClassesAndStrippedOuter(getClass())
Clément Béra6592f0e2024-05-13 15:07:46 +020077 .applyIf(
78 parameters.isCfRuntime(),
79 b -> b.addLibraryProvider(JdkClassFileProvider.fromSystemJdk()))
Clément Bérab54fc3e2024-05-02 09:19:45 +020080 .setMinApi(parameters)
81 .addKeepMainRule(Main.class)
82 .run(parameters.getRuntime(), Main.class)
83 .assertSuccessWithOutput(EXPECTED_OUTPUT);
Søren Gjesse8e1222c2024-04-23 15:20:54 +020084 }
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 Gjesse8e1222c2024-04-23 15:20:54 +020092 case String s when s.equalsIgnoreCase("YES") -> {
93 System.out.println("yes");
94 }
Clément Bérae2438dd2024-04-30 12:38:46 +020095 case "y", "Y" -> {
96 System.out.println("y or Y");
97 }
Søren Gjesse8e1222c2024-04-23 15:20:54 +020098 case String s when s.equalsIgnoreCase("NO") -> {
99 System.out.println("no");
100 }
Clément Bérae2438dd2024-04-30 12:38:46 +0200101 case "n", "N" -> {
102 System.out.println("n or N");
103 }
Søren Gjesse8e1222c2024-04-23 15:20:54 +0200104 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}