1 /* 2 * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @summary Test mapMulti(BiConsumer) and primitive stream operations 27 */ 28 29 package org.openjdk.tests.java.util.stream; 30 31 import org.testng.annotations.DataProvider; 32 import org.testng.annotations.Test; 33 34 import org.openjdk.testlib.java.util.stream.DefaultMethodStreams; 35 import org.openjdk.testlib.java.util.stream.OpTestCase; 36 import org.openjdk.testlib.java.util.stream.StreamTestDataProvider; 37 import org.openjdk.testlib.java.util.stream.TestData; 38 39 import java.util.Arrays; 40 import java.util.Collection; 41 import java.util.function.BiConsumer; 42 import java.util.function.Consumer; 43 import java.util.function.Function; 44 import java.util.stream.IntStream; 45 import java.util.stream.Stream; 46 47 import static org.openjdk.testlib.java.util.stream.DefaultMethodStreams.delegateTo; 48 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.LONG_STRING; 49 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.assertConcat; 50 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.assertContents; 51 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.assertCountSum; 52 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.countTo; 53 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.flattenChars; 54 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.mfId; 55 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.mfLt; 56 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.mfNull; 57 import static org.openjdk.testlib.java.util.stream.ThrowableHelper.checkNPE; 58 59 @Test 60 public class MapMultiOpTest extends OpTestCase { 61 62 BiConsumer<Integer, Consumer<Integer>> nullConsumer = 63 (e, sink) -> mfNull.apply(e).forEach(sink); 64 BiConsumer<Integer, Consumer<Integer>> idConsumer = 65 (e, sink) -> mfId.apply(e).forEach(sink); 66 BiConsumer<Integer, Consumer<Integer>> listConsumer = 67 (e, sink) -> mfLt.apply(e).forEach(sink); 68 BiConsumer<String, Consumer<Character>> charConsumer = 69 (e, sink) -> flattenChars.apply(e).forEach(sink); 70 BiConsumer<Integer, Consumer<Integer>> emptyStreamConsumer = 71 (e, sink) -> Stream.empty().forEach(i -> sink.accept((Integer) i)); 72 BiConsumer<Integer, Consumer<Integer>> intRangeConsumer = 73 (e, sink) -> IntStream.range(0, e).boxed().forEach(sink); 74 BiConsumer<Integer, Consumer<Integer>> rangeConsumerWithLimit = 75 (e, sink) -> IntStream.range(0, e).boxed().limit(10).forEach(sink); 76 77 @DataProvider(name = "Stream<Integer>") streamProvider()78 public Object[][] streamProvider() { 79 return new Object[][]{ 80 {Stream.of(0, 1, 2)}, 81 {DefaultMethodStreams.delegateTo(Stream.of(0, 1, 2))} 82 }; 83 } 84 85 @Test(dataProvider = "Stream<Integer>") testNullMapper(Stream<Integer> s)86 public void testNullMapper(Stream<Integer> s) { 87 checkNPE(() -> s.mapMulti(null)); 88 checkNPE(() -> s.mapMultiToInt(null)); 89 checkNPE(() -> s.mapMultiToDouble(null)); 90 checkNPE(() -> s.mapMultiToLong(null)); 91 } 92 93 @Test testMapMulti()94 public void testMapMulti() { 95 String[] stringsArray = {"hello", "there", "", "yada"}; 96 Stream<String> strings = Arrays.asList(stringsArray).stream(); 97 98 assertConcat(strings.mapMulti(charConsumer) 99 .iterator(), "hellothereyada"); 100 assertCountSum((countTo(10).stream().mapMulti(idConsumer)), 101 10, 55); 102 assertCountSum(countTo(10).stream().mapMulti(nullConsumer), 103 0, 0); 104 assertCountSum(countTo(3).stream().mapMulti(listConsumer), 105 6, 4); 106 107 exerciseOps(TestData.Factory.ofArray("stringsArray", 108 stringsArray), s -> s.mapMulti(charConsumer)); 109 exerciseOps(TestData.Factory.ofArray("LONG_STRING", 110 new String[]{LONG_STRING}), s -> s.mapMulti(charConsumer)); 111 } 112 113 @Test testDefaultMapMulti()114 public void testDefaultMapMulti() { 115 String[] stringsArray = {"hello", "there", "", "yada"}; 116 Stream<String> strings = Arrays.stream(stringsArray); 117 118 assertConcat(delegateTo(strings) 119 .mapMulti(charConsumer).iterator(), "hellothereyada"); 120 assertCountSum(delegateTo(countTo(10).stream()) 121 .mapMulti(idConsumer), 10, 55); 122 assertCountSum(delegateTo(countTo(10).stream()) 123 .mapMulti(nullConsumer), 0, 0); 124 assertCountSum(delegateTo(countTo(3).stream()) 125 .mapMulti(listConsumer), 6, 4); 126 127 exerciseOps(TestData.Factory.ofArray("stringsArray", 128 stringsArray), s -> delegateTo(s).mapMulti(charConsumer)); 129 exerciseOps(TestData.Factory.ofArray("LONG_STRING", 130 new String[]{LONG_STRING}), s -> delegateTo(s).mapMulti(charConsumer)); 131 } 132 133 @Test(dataProvider = "StreamTestData<Integer>", 134 dataProviderClass = StreamTestDataProvider.class) testOps(String name, TestData.OfRef<Integer> data)135 public void testOps(String name, TestData.OfRef<Integer> data) { 136 testOps(name, data, s -> s); 137 testOps(name, data, s -> delegateTo(s)); 138 } 139 testOps(String name, TestData.OfRef<Integer> data, Function<Stream<Integer>, Stream<Integer>> sf)140 private void testOps(String name, 141 TestData.OfRef<Integer> data, 142 Function<Stream<Integer>, Stream<Integer>> sf) { 143 Collection<Integer> result; 144 result = exerciseOps(data, s -> sf.apply(s).mapMulti(idConsumer)); 145 assertEquals(data.size(), result.size()); 146 147 result = exerciseOps(data, s -> sf.apply(s).mapMulti(nullConsumer)); 148 assertEquals(0, result.size()); 149 150 result = exerciseOps(data, s -> sf.apply(s).mapMulti(emptyStreamConsumer)); 151 assertEquals(0, result.size()); 152 } 153 154 @Test(dataProvider = "StreamTestData<Integer>.small", 155 dataProviderClass = StreamTestDataProvider.class) testOpsX(String name, TestData.OfRef<Integer> data)156 public void testOpsX(String name, TestData.OfRef<Integer> data) { 157 exerciseOps(data, s -> s.mapMulti(listConsumer)); 158 exerciseOps(data, s -> s.mapMulti(intRangeConsumer)); 159 exerciseOps(data, s -> s.mapMulti(rangeConsumerWithLimit)); 160 } 161 162 @Test(dataProvider = "StreamTestData<Integer>.small", 163 dataProviderClass = StreamTestDataProvider.class) testDefaultOpsX(String name, TestData.OfRef<Integer> data)164 public void testDefaultOpsX(String name, TestData.OfRef<Integer> data) { 165 exerciseOps(data, s -> delegateTo(s).mapMulti(listConsumer)); 166 exerciseOps(data, s -> delegateTo(s).mapMulti(intRangeConsumer)); 167 exerciseOps(data, s -> delegateTo(s).mapMulti(rangeConsumerWithLimit)); 168 } 169 170 // Android-removed: mapMulti is not yet available for primitive Streams. 171 /* 172 // Int 173 174 @DataProvider(name = "IntStream") 175 public Object[][] intStreamProvider() { 176 return new Object[][]{ 177 {IntStream.of(0, 1, 2)}, 178 {DefaultMethodStreams.delegateTo(IntStream.of(0, 1, 2))} 179 }; 180 } 181 182 @Test(dataProvider = "IntStream") 183 public void testIntNullMapper(IntStream s) { 184 checkNPE(() -> s.mapMulti(null)); 185 } 186 187 @Test(dataProvider = "IntStreamTestData", dataProviderClass = IntStreamTestDataProvider.class) 188 public void testIntOps(String name, TestData.OfInt data) { 189 testIntOps(name, data, s -> s); 190 testIntOps(name, data, s -> delegateTo(s)); 191 } 192 193 private void testIntOps(String name, 194 TestData.OfInt data, 195 Function<IntStream, IntStream> sf) { 196 Collection<Integer> result = exerciseOps(data, s -> sf.apply(s).mapMulti((i, sink) -> IntStream.of(i).forEach(sink))); 197 assertEquals(data.size(), result.size()); 198 assertContents(data, result); 199 200 result = exerciseOps(data, s -> sf.apply(s).boxed().mapMultiToInt((i, sink) -> IntStream.of(i).forEach(sink))); 201 assertEquals(data.size(), result.size()); 202 assertContents(data, result); 203 204 result = exerciseOps(data, s -> sf.apply(s).mapMulti((i, sink) -> IntStream.empty().forEach(sink))); 205 assertEquals(0, result.size()); 206 } 207 208 @Test(dataProvider = "IntStreamTestData.small", dataProviderClass = IntStreamTestDataProvider.class) 209 public void testIntOpsX(String name, TestData.OfInt data) { 210 exerciseOps(data, s -> s.mapMulti((e, sink) -> IntStream.range(0, e).forEach(sink))); 211 exerciseOps(data, s -> s.mapMulti((e, sink) -> IntStream.range(0, e).limit(10).forEach(sink))); 212 213 exerciseOps(data, s -> s.boxed().mapMultiToInt((e, sink) -> IntStream.range(0, e).forEach(sink))); 214 exerciseOps(data, s -> s.boxed().mapMultiToInt((e, sink) -> IntStream.range(0, e).limit(10).forEach(sink))); 215 } 216 217 @Test(dataProvider = "IntStreamTestData.small", dataProviderClass = IntStreamTestDataProvider.class) 218 public void testDefaultIntOpsX(String name, TestData.OfInt data) { 219 exerciseOps(data, s -> delegateTo(s).mapMulti((e, sink) -> IntStream.range(0, e).forEach(sink))); 220 exerciseOps(data, s -> delegateTo(s).mapMulti((e, sink) -> IntStream.range(0, e).limit(10).forEach(sink))); 221 222 exerciseOps(data, s -> delegateTo(s).boxed().mapMultiToInt((e, sink) -> IntStream.range(0, e).forEach(sink))); 223 exerciseOps(data, s -> delegateTo(s).boxed().mapMultiToInt((e, sink) -> IntStream.range(0, e).limit(10).forEach(sink))); 224 } 225 226 // Double 227 228 @DataProvider(name = "DoubleStream") 229 public Object[][] doubleStreamProvider() { 230 return new Object[][]{ 231 {DoubleStream.of(0, 1, 2)}, 232 {DefaultMethodStreams.delegateTo(DoubleStream.of(0, 1, 2))} 233 }; 234 } 235 236 @Test(dataProvider = "DoubleStream") 237 public void testDoubleNullMapper(DoubleStream s) { 238 checkNPE(() -> s.mapMulti(null)); 239 } 240 241 @Test(dataProvider = "DoubleStreamTestData", dataProviderClass = DoubleStreamTestDataProvider.class) 242 public void testDoubleOps(String name, TestData.OfDouble data) { 243 testDoubleOps(name, data, s -> s); 244 testDoubleOps(name, data, s -> delegateTo(s)); 245 } 246 247 private void testDoubleOps(String name, 248 TestData.OfDouble data, 249 Function<DoubleStream, DoubleStream> sf) { 250 Collection<Double> result = exerciseOps(data, s -> sf.apply(s).mapMulti((i, sink) -> DoubleStream.of(i).forEach(sink))); 251 assertEquals(data.size(), result.size()); 252 assertContents(data, result); 253 254 result = exerciseOps(data, s -> sf.apply(s).boxed().mapMultiToDouble((i, sink) -> DoubleStream.of(i).forEach(sink))); 255 assertEquals(data.size(), result.size()); 256 assertContents(data, result); 257 258 result = exerciseOps(data, s -> sf.apply(s).mapMulti((i, sink) -> DoubleStream.empty().forEach(sink))); 259 assertEquals(0, result.size()); 260 } 261 262 @Test(dataProvider = "DoubleStreamTestData.small", dataProviderClass = DoubleStreamTestDataProvider.class) 263 public void testDoubleOpsX(String name, TestData.OfDouble data) { 264 exerciseOps(data, s -> s.mapMulti((e, sink) -> IntStream.range(0, (int) e).asDoubleStream().forEach(sink))); 265 exerciseOps(data, s -> s.mapMulti((e, sink) -> IntStream.range(0, (int) e).limit(10).asDoubleStream().forEach(sink))); 266 } 267 268 @Test(dataProvider = "DoubleStreamTestData.small", dataProviderClass = DoubleStreamTestDataProvider.class) 269 public void testDefaultDoubleOpsX(String name, TestData.OfDouble data) { 270 exerciseOps(data, s -> delegateTo(s).mapMulti((e, sink) -> IntStream.range(0, (int) e).asDoubleStream().forEach(sink))); 271 exerciseOps(data, s -> delegateTo(s).mapMulti((e, sink) -> IntStream.range(0, (int) e).limit(10).asDoubleStream().forEach(sink))); 272 } 273 274 // Long 275 276 @DataProvider(name = "LongStream") 277 public Object[][] longStreamProvider() { 278 return new Object[][]{ 279 {LongStream.of(0, 1, 2)}, 280 {DefaultMethodStreams.delegateTo(LongStream.of(0, 1, 2))} 281 }; 282 } 283 284 @Test(dataProvider = "LongStream") 285 public void testLongNullMapper(LongStream s) { 286 checkNPE(() -> s.mapMulti(null)); 287 } 288 289 @Test(dataProvider = "LongStreamTestData", dataProviderClass = LongStreamTestDataProvider.class) 290 public void testLongOps(String name, TestData.OfLong data) { 291 testLongOps(name, data, s -> s); 292 testLongOps(name, data, s -> delegateTo(s)); 293 } 294 295 private void testLongOps(String name, 296 TestData.OfLong data, 297 Function<LongStream, LongStream> sf) { 298 Collection<Long> result = exerciseOps(data, s -> sf.apply(s).mapMulti((i, sink) -> LongStream.of(i).forEach(sink))); 299 assertEquals(data.size(), result.size()); 300 assertContents(data, result); 301 302 result = exerciseOps(data, s -> sf.apply(s).boxed().mapMultiToLong((i, sink) -> LongStream.of(i).forEach(sink))); 303 assertEquals(data.size(), result.size()); 304 assertContents(data, result); 305 306 result = exerciseOps(data, s -> sf.apply(s).mapMulti((i, sink) -> LongStream.empty().forEach(sink))); 307 assertEquals(0, result.size()); 308 } 309 310 @Test(dataProvider = "LongStreamTestData.small", dataProviderClass = LongStreamTestDataProvider.class) 311 public void testLongOpsX(String name, TestData.OfLong data) { 312 exerciseOps(data, s -> s.mapMulti((e, sink) -> LongStream.range(0, e).forEach(sink))); 313 exerciseOps(data, s -> s.mapMulti((e, sink) -> LongStream.range(0, e).limit(10).forEach(sink))); 314 } 315 316 @Test(dataProvider = "LongStreamTestData.small", dataProviderClass = LongStreamTestDataProvider.class) 317 public void testDefaultLongOpsX(String name, TestData.OfLong data) { 318 exerciseOps(data, s -> delegateTo(s).mapMulti((e, sink) -> LongStream.range(0, e).forEach(sink))); 319 exerciseOps(data, s -> delegateTo(s).mapMulti((e, sink) -> LongStream.range(0, e).limit(10).forEach(sink))); 320 } 321 */ 322 } 323