blob: a96a874e9e75f446dee9829d73881f3466547997 [file] [log] [blame]
// Copyright (c) 2019, 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.desugar.desugaredlibrary.conversiontests;
import static junit.framework.TestCase.assertEquals;
import com.android.tools.r8.TestParameters;
import com.android.tools.r8.desugar.desugaredlibrary.DesugaredLibraryTestBase;
import com.android.tools.r8.utils.AndroidApiLevel;
import com.android.tools.r8.utils.BooleanUtils;
import com.android.tools.r8.utils.codeinspector.CodeInspector;
import java.nio.file.Path;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class DuplicateAPIProgramTest extends DesugaredLibraryTestBase {
private final TestParameters parameters;
private final boolean shrinkDesugaredLibrary;
private static final AndroidApiLevel MIN_SUPPORTED = AndroidApiLevel.N;
private static Path CUSTOM_LIB;
@Parameters(name = "{0}, shrinkDesugaredLibrary: {1}")
public static List<Object[]> data() {
return buildParameters(
getConversionParametersUpToExcluding(MIN_SUPPORTED), BooleanUtils.values());
}
public DuplicateAPIProgramTest(TestParameters parameters, boolean shrinkDesugaredLibrary) {
this.shrinkDesugaredLibrary = shrinkDesugaredLibrary;
this.parameters = parameters;
}
@BeforeClass
public static void compileCustomLib() throws Exception {
CUSTOM_LIB =
testForD8(getStaticTemp())
.addProgramClasses(CustomLibClass.class)
.setMinApi(MIN_SUPPORTED)
.compile()
.writeToZip();
}
@Test
public void testMapD8() throws Exception {
KeepRuleConsumer keepRuleConsumer = createKeepRuleConsumer(parameters);
String stdOut =
testForD8()
.setMinApi(parameters.getApiLevel())
.addProgramClasses(Executor.class, MyMap.class)
.addLibraryClasses(CustomLibClass.class)
.enableCoreLibraryDesugaring(parameters.getApiLevel(), keepRuleConsumer)
.compile()
.inspect(this::assertDupMethod)
.addDesugaredCoreLibraryRunClassPath(
this::buildDesugaredLibrary,
parameters.getApiLevel(),
keepRuleConsumer.get(),
shrinkDesugaredLibrary)
.addRunClasspathFiles(CUSTOM_LIB)
.run(parameters.getRuntime(), Executor.class)
.assertSuccess()
.getStdOut();
assertLines2By2Correct(stdOut);
}
@Test
public void testMapR8() throws Exception {
KeepRuleConsumer keepRuleConsumer = createKeepRuleConsumer(parameters);
String stdOut =
testForR8(parameters.getBackend())
.setMinApi(parameters.getApiLevel())
.addKeepMainRule(Executor.class)
.addProgramClasses(Executor.class, MyMap.class)
.addLibraryClasses(CustomLibClass.class)
.enableCoreLibraryDesugaring(parameters.getApiLevel(), keepRuleConsumer)
.compile()
.inspect(this::assertDupMethod)
.addDesugaredCoreLibraryRunClassPath(
this::buildDesugaredLibrary,
parameters.getApiLevel(),
keepRuleConsumer.get(),
shrinkDesugaredLibrary)
.addRunClasspathFiles(CUSTOM_LIB)
.run(parameters.getRuntime(), Executor.class)
.assertSuccess()
.getStdOut();
assertLines2By2Correct(stdOut);
}
private void assertDupMethod(CodeInspector i) {
assertEquals(
2,
i.clazz(MyMap.class).virtualMethods().stream()
.filter(m -> m.getOriginalName().equals("forEach"))
.count());
}
static class Executor {
public static void main(String[] args) {
IdentityHashMap<Integer, Double> map = new MyMap<>();
map.put(1, 1.1);
map.put(2, 2.2);
BiConsumer<Integer, Double> biConsumer = (x, y) -> System.out.print(" " + x + " " + y);
map.forEach(biConsumer);
System.out.println();
CustomLibClass.javaForEach(map, biConsumer);
}
}
public static class MyMap<K, V> extends IdentityHashMap<K, V> {
@Override
public void forEach(BiConsumer<? super K, ? super V> action) {
System.out.print("ForEach");
super.forEach(action);
}
}
// This class will be put at compilation time as library and on the runtime class path.
// This class is convenient for easy testing. Each method plays the role of methods in the
// platform APIs for which argument/return values need conversion.
static class CustomLibClass {
@SuppressWarnings("WeakerAccess")
public static <K, V> void javaForEach(Map<K, V> map, BiConsumer<K, V> consumer) {
map.forEach(consumer);
}
}
}