| // Copyright (c) 2023, 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.rewrite.arrays; |
| |
| import static org.junit.Assert.assertEquals; |
| |
| import com.android.tools.r8.CompilationMode; |
| import com.android.tools.r8.NeverInline; |
| import com.android.tools.r8.TestBase; |
| import com.android.tools.r8.TestCompilerBuilder; |
| import com.android.tools.r8.TestParameters; |
| import com.android.tools.r8.dex.Constants; |
| import com.android.tools.r8.utils.StringUtils; |
| import com.android.tools.r8.utils.codeinspector.CodeInspector; |
| import com.android.tools.r8.utils.codeinspector.InstructionSubject; |
| import com.android.tools.r8.utils.codeinspector.MethodSubject; |
| import com.google.common.collect.ImmutableList; |
| import java.util.Arrays; |
| 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 StringArrayWithNonUniqueValuesTest extends TestBase { |
| |
| @Parameter(0) |
| public TestParameters parameters; |
| |
| @Parameter(1) |
| public CompilationMode compilationMode; |
| |
| @Parameter(2) |
| public Integer maxMaterializingConstants; |
| |
| @Parameters(name = "{0}, mode = {1}, maxMaterializingConstants = {2}") |
| public static Iterable<?> data() { |
| return buildParameters( |
| getTestParameters().withDefaultCfRuntime().withDexRuntimesAndAllApiLevels().build(), |
| CompilationMode.values(), |
| ImmutableList.of(Constants.U8BIT_MAX - 16, 2)); |
| } |
| |
| private static final String EXPECTED_OUTPUT = StringUtils.lines("100", "104"); |
| |
| private void inspect(MethodSubject method, int constStrings, int puts) { |
| boolean expectingFilledNewArray = canUseFilledNewArrayOfStringObjects(parameters); |
| assertEquals( |
| expectingFilledNewArray ? 0 : puts, |
| method.streamInstructions().filter(InstructionSubject::isArrayPut).count()); |
| assertEquals( |
| expectingFilledNewArray ? 1 : 0, |
| method.streamInstructions().filter(InstructionSubject::isFilledNewArray).count()); |
| assertEquals( |
| expectingFilledNewArray || parameters.isCfRuntime() ? puts : constStrings, |
| method.streamInstructions().filter(InstructionSubject::isConstString).count()); |
| } |
| |
| private void configure(TestCompilerBuilder<?, ?, ?, ?, ?> builder) { |
| builder.addOptionsModification( |
| options -> |
| options.rewriteArrayOptions().maxMaterializingConstants = maxMaterializingConstants); |
| } |
| |
| private void inspect(CodeInspector inspector) { |
| inspect(inspector.clazz(TestClass.class).uniqueMethodWithOriginalName("m1"), 1, 100); |
| inspect( |
| inspector.clazz(TestClass.class).uniqueMethodWithOriginalName("m2"), |
| maxMaterializingConstants == 2 ? 32 : 26, |
| 104); |
| } |
| |
| @Test |
| public void testD8() throws Exception { |
| parameters.assumeDexRuntime(); |
| testForD8(parameters.getBackend()) |
| .addInnerClasses(getClass()) |
| .setMinApi(parameters) |
| .apply(this::configure) |
| .run(parameters.getRuntime(), TestClass.class) |
| .inspect(this::inspect) |
| .assertSuccessWithOutput(EXPECTED_OUTPUT); |
| } |
| |
| @Test |
| public void testR8() throws Exception { |
| testForR8(parameters.getBackend()) |
| .addInnerClasses(getClass()) |
| .addKeepMainRule(TestClass.class) |
| .setMinApi(parameters) |
| .enableInliningAnnotations() |
| .addDontObfuscate() |
| .apply(this::configure) |
| .run(parameters.getRuntime(), TestClass.class) |
| .inspect(this::inspect) |
| .assertSuccessWithOutput(EXPECTED_OUTPUT); |
| } |
| |
| public static final class TestClass { |
| |
| @NeverInline |
| public static void m1() { |
| String[] array = |
| new String[] { |
| "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", |
| "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", |
| "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", |
| "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", |
| "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", |
| "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", |
| "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", |
| "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", |
| "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", |
| "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", |
| }; |
| System.out.println(Arrays.asList(array).size()); |
| } |
| |
| @NeverInline |
| public static void m2() { |
| String[] array = |
| new String[] { |
| "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", |
| "R", "S", "T", "U", "V", "W", "X", "Y", "Z", |
| "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", |
| "R", "S", "T", "U", "V", "W", "X", "Y", "Z", |
| "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", |
| "R", "S", "T", "U", "V", "W", "X", "Y", "Z", |
| "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", |
| "R", "S", "T", "U", "V", "W", "X", "Y", "Z", |
| }; |
| System.out.println(Arrays.asList(array).size()); |
| } |
| |
| public static void main(String[] args) { |
| m1(); |
| m2(); |
| } |
| } |
| } |