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.DoubleStreamTestDataProvider; 36 import org.openjdk.testlib.java.util.stream.IntStreamTestDataProvider; 37 import org.openjdk.testlib.java.util.stream.LongStreamTestDataProvider; 38 import org.openjdk.testlib.java.util.stream.OpTestCase; 39 import org.openjdk.testlib.java.util.stream.StreamTestDataProvider; 40 import org.openjdk.testlib.java.util.stream.TestData; 41 42 import java.util.Arrays; 43 import java.util.Collection; 44 import java.util.function.BiConsumer; 45 import java.util.function.Consumer; 46 import java.util.function.Function; 47 import java.util.stream.DoubleStream; 48 import java.util.stream.IntStream; 49 import java.util.stream.LongStream; 50 import java.util.stream.Stream; 51 52 import static org.openjdk.testlib.java.util.stream.DefaultMethodStreams.delegateTo; 53 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.LONG_STRING; 54 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.assertConcat; 55 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.assertContents; 56 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.assertCountSum; 57 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.countTo; 58 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.flattenChars; 59 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.mfId; 60 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.mfLt; 61 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.mfNull; 62 import static org.openjdk.testlib.java.util.stream.ThrowableHelper.checkNPE; 63 64 @Test 65 public class MapMultiOpTest extends OpTestCase { 66 67 BiConsumer<Integer, Consumer<Integer>> nullConsumer = 68 (e, sink) -> mfNull.apply(e).forEach(sink); 69 BiConsumer<Integer, Consumer<Integer>> idConsumer = 70 (e, sink) -> mfId.apply(e).forEach(sink); 71 BiConsumer<Integer, Consumer<Integer>> listConsumer = 72 (e, sink) -> mfLt.apply(e).forEach(sink); 73 BiConsumer<String, Consumer<Character>> charConsumer = 74 (e, sink) -> flattenChars.apply(e).forEach(sink); 75 BiConsumer<Integer, Consumer<Integer>> emptyStreamConsumer = 76 (e, sink) -> Stream.empty().forEach(i -> sink.accept((Integer) i)); 77 BiConsumer<Integer, Consumer<Integer>> intRangeConsumer = 78 (e, sink) -> IntStream.range(0, e).boxed().forEach(sink); 79 BiConsumer<Integer, Consumer<Integer>> rangeConsumerWithLimit = 80 (e, sink) -> IntStream.range(0, e).boxed().limit(10).forEach(sink); 81 82 @DataProvider(name = "Stream<Integer>") streamProvider()83 public Object[][] streamProvider() { 84 return new Object[][]{ 85 {Stream.of(0, 1, 2)}, 86 {DefaultMethodStreams.delegateTo(Stream.of(0, 1, 2))} 87 }; 88 } 89 90 @Test(dataProvider = "Stream<Integer>") testNullMapper(Stream<Integer> s)91 public void testNullMapper(Stream<Integer> s) { 92 checkNPE(() -> s.mapMulti(null)); 93 checkNPE(() -> s.mapMultiToInt(null)); 94 checkNPE(() -> s.mapMultiToDouble(null)); 95 checkNPE(() -> s.mapMultiToLong(null)); 96 } 97 98 @Test testMapMulti()99 public void testMapMulti() { 100 String[] stringsArray = {"hello", "there", "", "yada"}; 101 Stream<String> strings = Arrays.asList(stringsArray).stream(); 102 103 assertConcat(strings.mapMulti(charConsumer) 104 .iterator(), "hellothereyada"); 105 assertCountSum((countTo(10).stream().mapMulti(idConsumer)), 106 10, 55); 107 assertCountSum(countTo(10).stream().mapMulti(nullConsumer), 108 0, 0); 109 assertCountSum(countTo(3).stream().mapMulti(listConsumer), 110 6, 4); 111 112 exerciseOps(TestData.Factory.ofArray("stringsArray", 113 stringsArray), s -> s.mapMulti(charConsumer)); 114 exerciseOps(TestData.Factory.ofArray("LONG_STRING", 115 new String[]{LONG_STRING}), s -> s.mapMulti(charConsumer)); 116 } 117 118 @Test testDefaultMapMulti()119 public void testDefaultMapMulti() { 120 String[] stringsArray = {"hello", "there", "", "yada"}; 121 Stream<String> strings = Arrays.stream(stringsArray); 122 123 assertConcat(delegateTo(strings) 124 .mapMulti(charConsumer).iterator(), "hellothereyada"); 125 assertCountSum(delegateTo(countTo(10).stream()) 126 .mapMulti(idConsumer), 10, 55); 127 assertCountSum(delegateTo(countTo(10).stream()) 128 .mapMulti(nullConsumer), 0, 0); 129 assertCountSum(delegateTo(countTo(3).stream()) 130 .mapMulti(listConsumer), 6, 4); 131 132 exerciseOps(TestData.Factory.ofArray("stringsArray", 133 stringsArray), s -> delegateTo(s).mapMulti(charConsumer)); 134 exerciseOps(TestData.Factory.ofArray("LONG_STRING", 135 new String[]{LONG_STRING}), s -> delegateTo(s).mapMulti(charConsumer)); 136 } 137 138 @Test(dataProvider = "StreamTestData<Integer>", 139 dataProviderClass = StreamTestDataProvider.class) testOps(String name, TestData.OfRef<Integer> data)140 public void testOps(String name, TestData.OfRef<Integer> data) { 141 testOps(name, data, s -> s); 142 testOps(name, data, s -> delegateTo(s)); 143 } 144 testOps(String name, TestData.OfRef<Integer> data, Function<Stream<Integer>, Stream<Integer>> sf)145 private void testOps(String name, 146 TestData.OfRef<Integer> data, 147 Function<Stream<Integer>, Stream<Integer>> sf) { 148 Collection<Integer> result; 149 result = exerciseOps(data, s -> sf.apply(s).mapMulti(idConsumer)); 150 assertEquals(data.size(), result.size()); 151 152 result = exerciseOps(data, s -> sf.apply(s).mapMulti(nullConsumer)); 153 assertEquals(0, result.size()); 154 155 result = exerciseOps(data, s -> sf.apply(s).mapMulti(emptyStreamConsumer)); 156 assertEquals(0, result.size()); 157 } 158 159 @Test(dataProvider = "StreamTestData<Integer>.small", 160 dataProviderClass = StreamTestDataProvider.class) testOpsX(String name, TestData.OfRef<Integer> data)161 public void testOpsX(String name, TestData.OfRef<Integer> data) { 162 exerciseOps(data, s -> s.mapMulti(listConsumer)); 163 exerciseOps(data, s -> s.mapMulti(intRangeConsumer)); 164 exerciseOps(data, s -> s.mapMulti(rangeConsumerWithLimit)); 165 } 166 167 @Test(dataProvider = "StreamTestData<Integer>.small", 168 dataProviderClass = StreamTestDataProvider.class) testDefaultOpsX(String name, TestData.OfRef<Integer> data)169 public void testDefaultOpsX(String name, TestData.OfRef<Integer> data) { 170 exerciseOps(data, s -> delegateTo(s).mapMulti(listConsumer)); 171 exerciseOps(data, s -> delegateTo(s).mapMulti(intRangeConsumer)); 172 exerciseOps(data, s -> delegateTo(s).mapMulti(rangeConsumerWithLimit)); 173 } 174 175 // Int 176 177 @DataProvider(name = "IntStream") intStreamProvider()178 public Object[][] intStreamProvider() { 179 return new Object[][]{ 180 {IntStream.of(0, 1, 2)}, 181 {DefaultMethodStreams.delegateTo(IntStream.of(0, 1, 2))} 182 }; 183 } 184 185 @Test(dataProvider = "IntStream") testIntNullMapper(IntStream s)186 public void testIntNullMapper(IntStream s) { 187 checkNPE(() -> s.mapMulti(null)); 188 } 189 190 @Test(dataProvider = "IntStreamTestData", dataProviderClass = IntStreamTestDataProvider.class) testIntOps(String name, TestData.OfInt data)191 public void testIntOps(String name, TestData.OfInt data) { 192 testIntOps(name, data, s -> s); 193 testIntOps(name, data, s -> delegateTo(s)); 194 } 195 testIntOps(String name, TestData.OfInt data, Function<IntStream, IntStream> sf)196 private void testIntOps(String name, 197 TestData.OfInt data, 198 Function<IntStream, IntStream> sf) { 199 Collection<Integer> result = exerciseOps(data, s -> sf.apply(s).mapMulti((i, sink) -> IntStream.of(i).forEach(sink))); 200 assertEquals(data.size(), result.size()); 201 assertContents(data, result); 202 203 result = exerciseOps(data, s -> sf.apply(s).boxed().mapMultiToInt((i, sink) -> IntStream.of(i).forEach(sink))); 204 assertEquals(data.size(), result.size()); 205 assertContents(data, result); 206 207 result = exerciseOps(data, s -> sf.apply(s).mapMulti((i, sink) -> IntStream.empty().forEach(sink))); 208 assertEquals(0, result.size()); 209 } 210 211 @Test(dataProvider = "IntStreamTestData.small", dataProviderClass = IntStreamTestDataProvider.class) testIntOpsX(String name, TestData.OfInt data)212 public void testIntOpsX(String name, TestData.OfInt data) { 213 exerciseOps(data, s -> s.mapMulti((e, sink) -> IntStream.range(0, e).forEach(sink))); 214 exerciseOps(data, s -> s.mapMulti((e, sink) -> IntStream.range(0, e).limit(10).forEach(sink))); 215 216 exerciseOps(data, s -> s.boxed().mapMultiToInt((e, sink) -> IntStream.range(0, e).forEach(sink))); 217 exerciseOps(data, s -> s.boxed().mapMultiToInt((e, sink) -> IntStream.range(0, e).limit(10).forEach(sink))); 218 } 219 220 @Test(dataProvider = "IntStreamTestData.small", dataProviderClass = IntStreamTestDataProvider.class) testDefaultIntOpsX(String name, TestData.OfInt data)221 public void testDefaultIntOpsX(String name, TestData.OfInt data) { 222 exerciseOps(data, s -> delegateTo(s).mapMulti((e, sink) -> IntStream.range(0, e).forEach(sink))); 223 exerciseOps(data, s -> delegateTo(s).mapMulti((e, sink) -> IntStream.range(0, e).limit(10).forEach(sink))); 224 225 exerciseOps(data, s -> delegateTo(s).boxed().mapMultiToInt((e, sink) -> IntStream.range(0, e).forEach(sink))); 226 exerciseOps(data, s -> delegateTo(s).boxed().mapMultiToInt((e, sink) -> IntStream.range(0, e).limit(10).forEach(sink))); 227 } 228 229 // Double 230 231 @DataProvider(name = "DoubleStream") doubleStreamProvider()232 public Object[][] doubleStreamProvider() { 233 return new Object[][]{ 234 {DoubleStream.of(0, 1, 2)}, 235 {DefaultMethodStreams.delegateTo(DoubleStream.of(0, 1, 2))} 236 }; 237 } 238 239 @Test(dataProvider = "DoubleStream") testDoubleNullMapper(DoubleStream s)240 public void testDoubleNullMapper(DoubleStream s) { 241 checkNPE(() -> s.mapMulti(null)); 242 } 243 244 @Test(dataProvider = "DoubleStreamTestData", dataProviderClass = DoubleStreamTestDataProvider.class) testDoubleOps(String name, TestData.OfDouble data)245 public void testDoubleOps(String name, TestData.OfDouble data) { 246 testDoubleOps(name, data, s -> s); 247 testDoubleOps(name, data, s -> delegateTo(s)); 248 } 249 testDoubleOps(String name, TestData.OfDouble data, Function<DoubleStream, DoubleStream> sf)250 private void testDoubleOps(String name, 251 TestData.OfDouble data, 252 Function<DoubleStream, DoubleStream> sf) { 253 Collection<Double> result = exerciseOps(data, s -> sf.apply(s).mapMulti((i, sink) -> DoubleStream.of(i).forEach(sink))); 254 assertEquals(data.size(), result.size()); 255 assertContents(data, result); 256 257 result = exerciseOps(data, s -> sf.apply(s).boxed().mapMultiToDouble((i, sink) -> DoubleStream.of(i).forEach(sink))); 258 assertEquals(data.size(), result.size()); 259 assertContents(data, result); 260 261 result = exerciseOps(data, s -> sf.apply(s).mapMulti((i, sink) -> DoubleStream.empty().forEach(sink))); 262 assertEquals(0, result.size()); 263 } 264 265 @Test(dataProvider = "DoubleStreamTestData.small", dataProviderClass = DoubleStreamTestDataProvider.class) testDoubleOpsX(String name, TestData.OfDouble data)266 public void testDoubleOpsX(String name, TestData.OfDouble data) { 267 exerciseOps(data, s -> s.mapMulti((e, sink) -> IntStream.range(0, (int) e).asDoubleStream().forEach(sink))); 268 exerciseOps(data, s -> s.mapMulti((e, sink) -> IntStream.range(0, (int) e).limit(10).asDoubleStream().forEach(sink))); 269 } 270 271 @Test(dataProvider = "DoubleStreamTestData.small", dataProviderClass = DoubleStreamTestDataProvider.class) testDefaultDoubleOpsX(String name, TestData.OfDouble data)272 public void testDefaultDoubleOpsX(String name, TestData.OfDouble data) { 273 exerciseOps(data, s -> delegateTo(s).mapMulti((e, sink) -> IntStream.range(0, (int) e).asDoubleStream().forEach(sink))); 274 exerciseOps(data, s -> delegateTo(s).mapMulti((e, sink) -> IntStream.range(0, (int) e).limit(10).asDoubleStream().forEach(sink))); 275 } 276 277 // Long 278 279 @DataProvider(name = "LongStream") longStreamProvider()280 public Object[][] longStreamProvider() { 281 return new Object[][]{ 282 {LongStream.of(0, 1, 2)}, 283 {DefaultMethodStreams.delegateTo(LongStream.of(0, 1, 2))} 284 }; 285 } 286 287 @Test(dataProvider = "LongStream") testLongNullMapper(LongStream s)288 public void testLongNullMapper(LongStream s) { 289 checkNPE(() -> s.mapMulti(null)); 290 } 291 292 @Test(dataProvider = "LongStreamTestData", dataProviderClass = LongStreamTestDataProvider.class) testLongOps(String name, TestData.OfLong data)293 public void testLongOps(String name, TestData.OfLong data) { 294 testLongOps(name, data, s -> s); 295 testLongOps(name, data, s -> delegateTo(s)); 296 } 297 testLongOps(String name, TestData.OfLong data, Function<LongStream, LongStream> sf)298 private void testLongOps(String name, 299 TestData.OfLong data, 300 Function<LongStream, LongStream> sf) { 301 Collection<Long> result = exerciseOps(data, s -> sf.apply(s).mapMulti((i, sink) -> LongStream.of(i).forEach(sink))); 302 assertEquals(data.size(), result.size()); 303 assertContents(data, result); 304 305 result = exerciseOps(data, s -> sf.apply(s).boxed().mapMultiToLong((i, sink) -> LongStream.of(i).forEach(sink))); 306 assertEquals(data.size(), result.size()); 307 assertContents(data, result); 308 309 result = exerciseOps(data, s -> sf.apply(s).mapMulti((i, sink) -> LongStream.empty().forEach(sink))); 310 assertEquals(0, result.size()); 311 } 312 313 @Test(dataProvider = "LongStreamTestData.small", dataProviderClass = LongStreamTestDataProvider.class) testLongOpsX(String name, TestData.OfLong data)314 public void testLongOpsX(String name, TestData.OfLong data) { 315 exerciseOps(data, s -> s.mapMulti((e, sink) -> LongStream.range(0, e).forEach(sink))); 316 exerciseOps(data, s -> s.mapMulti((e, sink) -> LongStream.range(0, e).limit(10).forEach(sink))); 317 } 318 319 @Test(dataProvider = "LongStreamTestData.small", dataProviderClass = LongStreamTestDataProvider.class) testDefaultLongOpsX(String name, TestData.OfLong data)320 public void testDefaultLongOpsX(String name, TestData.OfLong data) { 321 exerciseOps(data, s -> delegateTo(s).mapMulti((e, sink) -> LongStream.range(0, e).forEach(sink))); 322 exerciseOps(data, s -> delegateTo(s).mapMulti((e, sink) -> LongStream.range(0, e).limit(10).forEach(sink))); 323 } 324 } 325