Exhaustive reproduction for flatMap conversion issue
Bug: b/243636261
Bug: b/238179854
Change-Id: Ia4220c65b436d31d5cd478e12a1323821b1b3655
diff --git a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/conversiontests/FlatMapConversionTest.java b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/conversiontests/FlatMapConversionTest.java
new file mode 100644
index 0000000..b9a3dba
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/conversiontests/FlatMapConversionTest.java
@@ -0,0 +1,131 @@
+// Copyright (c) 2022, 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 com.android.tools.r8.desugar.desugaredlibrary.test.CompilationSpecification.DEFAULT_SPECIFICATIONS;
+import static com.android.tools.r8.desugar.desugaredlibrary.test.LibraryDesugaringSpecification.getJdk8Jdk11;
+import static org.hamcrest.CoreMatchers.containsString;
+
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.desugar.desugaredlibrary.DesugaredLibraryTestBase;
+import com.android.tools.r8.desugar.desugaredlibrary.test.CompilationSpecification;
+import com.android.tools.r8.desugar.desugaredlibrary.test.CustomLibrarySpecification;
+import com.android.tools.r8.desugar.desugaredlibrary.test.LibraryDesugaringSpecification;
+import com.android.tools.r8.utils.AndroidApiLevel;
+import com.android.tools.r8.utils.StringUtils;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.DoubleStream;
+import java.util.stream.IntStream;
+import java.util.stream.LongStream;
+import java.util.stream.Stream;
+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 FlatMapConversionTest extends DesugaredLibraryTestBase {
+
+ private final TestParameters parameters;
+ private final LibraryDesugaringSpecification libraryDesugaringSpecification;
+ private final CompilationSpecification compilationSpecification;
+
+ private static final AndroidApiLevel MIN_SUPPORTED = AndroidApiLevel.N;
+ private static final String EXPECTED_RESULT =
+ StringUtils.lines(
+ "[1, 2, 3]",
+ "[1, 2, 3]",
+ "[1.0, 2.0, 3.0]",
+ "[1, 2, 3]",
+ "[1, 2, 3]",
+ "[1.0, 2.0, 3.0]",
+ "[1, 2, 3]");
+
+ @Parameters(name = "{0}, spec: {1}, {2}")
+ public static List<Object[]> data() {
+ return buildParameters(
+ getConversionParametersUpToExcluding(MIN_SUPPORTED),
+ getJdk8Jdk11(),
+ DEFAULT_SPECIFICATIONS);
+ }
+
+ public FlatMapConversionTest(
+ TestParameters parameters,
+ LibraryDesugaringSpecification libraryDesugaringSpecification,
+ CompilationSpecification compilationSpecification) {
+ this.parameters = parameters;
+ this.libraryDesugaringSpecification = libraryDesugaringSpecification;
+ this.compilationSpecification = compilationSpecification;
+ }
+
+ @Test
+ public void testConvert() throws Throwable {
+ testForDesugaredLibrary(parameters, libraryDesugaringSpecification, compilationSpecification)
+ .addProgramClasses(Executor.class)
+ .setCustomLibrarySpecification(
+ new CustomLibrarySpecification(CustomLibClass.class, MIN_SUPPORTED))
+ .addKeepMainRule(Executor.class)
+ .run(parameters.getRuntime(), Executor.class)
+ .assertFailureWithErrorThatMatches(containsString("java.lang.ClassCastException"));
+ }
+
+ static class Executor {
+
+ public static void main(String[] args) {
+ System.out.println(
+ Arrays.toString(CustomLibClass.getIntStreamAsStream().flatMap(Stream::of).toArray()));
+
+ System.out.println(
+ Arrays.toString(
+ CustomLibClass.getIntStreamAsStream().flatMapToInt(IntStream::of).toArray()));
+ System.out.println(
+ Arrays.toString(
+ CustomLibClass.getDoubleStreamAsStream()
+ .flatMapToDouble(DoubleStream::of)
+ .toArray()));
+ System.out.println(
+ Arrays.toString(
+ CustomLibClass.getLongStreamAsStream().flatMapToLong(LongStream::of).toArray()));
+
+ System.out.println(
+ Arrays.toString(CustomLibClass.getIntStream().flatMap(IntStream::of).toArray()));
+ System.out.println(
+ Arrays.toString(CustomLibClass.getDoubleStream().flatMap(DoubleStream::of).toArray()));
+ System.out.println(
+ Arrays.toString(CustomLibClass.getLongStream().flatMap(LongStream::of).toArray()));
+ }
+ }
+
+ // 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 {
+
+ public static Stream<Integer> getIntStreamAsStream() {
+ return Arrays.stream(new Integer[] {1, 2, 3});
+ }
+
+ public static Stream<Double> getDoubleStreamAsStream() {
+ return Arrays.stream(new Double[] {1.0, 2.0, 3.0});
+ }
+
+ public static Stream<Long> getLongStreamAsStream() {
+ return Arrays.stream(new Long[] {1L, 2L, 3L});
+ }
+
+ public static IntStream getIntStream() {
+ return Arrays.stream(new int[] {1, 2, 3});
+ }
+
+ public static DoubleStream getDoubleStream() {
+ return Arrays.stream(new double[] {1.0, 2.0, 3.0});
+ }
+
+ public static LongStream getLongStream() {
+ return Arrays.stream(new long[] {1L, 2L, 3L});
+ }
+ }
+}