Add Collectors.mapping tests.
Cover Collectors.mapping with unit tests.
Bug: 182165513
Test: atest libcore.java.util.stream.CollectorsTest
Change-Id: I17439ccf648b5db8d30715d46e41e451b931b78c
(cherry picked from commit bd2717711271290403670a32de295b446875d5ac)
Merged-In: I17439ccf648b5db8d30715d46e41e451b931b78c
diff --git a/luni/src/test/java/libcore/java/util/stream/CollectorsTest.java b/luni/src/test/java/libcore/java/util/stream/CollectorsTest.java
index afe097c..ecec61b 100644
--- a/luni/src/test/java/libcore/java/util/stream/CollectorsTest.java
+++ b/luni/src/test/java/libcore/java/util/stream/CollectorsTest.java
@@ -22,13 +22,19 @@
import static org.junit.Assert.assertTrue;
import static java.util.stream.Collectors.counting;
+import static java.util.stream.Collectors.groupingBy;
+import static java.util.stream.Collectors.toList;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Function;
import java.util.stream.Collector;
+import java.util.stream.Collectors;
import java.util.stream.Stream;
@RunWith(JUnit4.class)
@@ -85,4 +91,26 @@
assertArrayEquals(new int[] {30}, Stream.of(1, 2, 3, 4).collect(sqSumCollector));
}
+
+ @Test
+ public void mapping_asArgumentToCollect() {
+ Collector<Integer, ?, List<Integer>> plusOneCollector =
+ Collectors.mapping((Integer x) -> x + 1, toList());
+
+ List<Integer> actual = Stream.of(1, 2, 10).collect(plusOneCollector);
+
+ assertEquals(List.of(2, 3, 11), actual);
+ }
+
+ @Test
+ public void mapping_asArgumentInGroupingBy() {
+ Collector<Integer, ?, List<Integer>> plusOneCollector =
+ Collectors.mapping((Integer x) -> x + 1, toList());
+
+ Map<Boolean, List<Integer>> actual = Stream.of(1, 2, 3, 4)
+ .collect(groupingBy(x -> x % 2 == 0, plusOneCollector));
+ Map<Boolean, List<Integer>> expected = Map.of(true, List.of(3, 5), false, List.of(2, 4));
+
+ assertEquals(expected, actual);
+ }
}