1 /* |
1 /* |
2 * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
52 import static java.util.stream.ThrowableHelper.checkNPE; |
52 import static java.util.stream.ThrowableHelper.checkNPE; |
53 |
53 |
54 @Test |
54 @Test |
55 public class FlatMapOpTest extends OpTestCase { |
55 public class FlatMapOpTest extends OpTestCase { |
56 |
56 |
|
57 @Test |
57 public void testNullMapper() { |
58 public void testNullMapper() { |
58 checkNPE(() -> Stream.of(1).flatMap(null)); |
59 checkNPE(() -> Stream.of(1).flatMap(null)); |
59 checkNPE(() -> IntStream.of(1).flatMap(null)); |
60 checkNPE(() -> IntStream.of(1).flatMap(null)); |
60 checkNPE(() -> LongStream.of(1).flatMap(null)); |
61 checkNPE(() -> LongStream.of(1).flatMap(null)); |
61 checkNPE(() -> DoubleStream.of(1).flatMap(null)); |
62 checkNPE(() -> DoubleStream.of(1).flatMap(null)); |
62 } |
63 } |
63 |
64 |
64 static final Function<Integer, Stream<Integer>> integerRangeMapper |
65 static final Function<Integer, Stream<Integer>> integerRangeMapper |
65 = e -> IntStream.range(0, e).boxed(); |
66 = e -> IntStream.range(0, e).boxed(); |
66 |
67 |
|
68 @Test |
67 public void testFlatMap() { |
69 public void testFlatMap() { |
68 String[] stringsArray = {"hello", "there", "", "yada"}; |
70 String[] stringsArray = {"hello", "there", "", "yada"}; |
69 Stream<String> strings = Arrays.asList(stringsArray).stream(); |
71 Stream<String> strings = Arrays.asList(stringsArray).stream(); |
70 assertConcat(strings.flatMap(flattenChars).iterator(), "hellothereyada"); |
72 assertConcat(strings.flatMap(flattenChars).iterator(), "hellothereyada"); |
71 |
73 |
147 exerciseOps(data, s -> s.flatMap(mfLt)); |
149 exerciseOps(data, s -> s.flatMap(mfLt)); |
148 exerciseOps(data, s -> s.flatMap(integerRangeMapper)); |
150 exerciseOps(data, s -> s.flatMap(integerRangeMapper)); |
149 exerciseOps(data, s -> s.flatMap((Integer e) -> IntStream.range(0, e).boxed().limit(10))); |
151 exerciseOps(data, s -> s.flatMap((Integer e) -> IntStream.range(0, e).boxed().limit(10))); |
150 } |
152 } |
151 |
153 |
|
154 @Test |
|
155 public void testOpsShortCircuit() { |
|
156 AtomicInteger count = new AtomicInteger(); |
|
157 Stream.of(0).flatMap(i -> IntStream.range(0, 100).boxed()). |
|
158 peek(i -> count.incrementAndGet()). |
|
159 limit(10).toArray(); |
|
160 assertEquals(count.get(), 10); |
|
161 } |
|
162 |
152 // |
163 // |
153 |
164 |
154 @Test(dataProvider = "IntStreamTestData", dataProviderClass = IntStreamTestDataProvider.class) |
165 @Test(dataProvider = "IntStreamTestData", dataProviderClass = IntStreamTestDataProvider.class) |
155 public void testIntOps(String name, TestData.OfInt data) { |
166 public void testIntOps(String name, TestData.OfInt data) { |
156 Collection<Integer> result = exerciseOps(data, s -> s.flatMap(i -> Collections.singleton(i).stream().mapToInt(j -> j))); |
167 Collection<Integer> result = exerciseOps(data, s -> s.flatMap(IntStream::of)); |
|
168 assertEquals(data.size(), result.size()); |
|
169 assertContents(data, result); |
|
170 |
|
171 result = exerciseOps(data, s -> s.boxed().flatMapToInt(IntStream::of)); |
157 assertEquals(data.size(), result.size()); |
172 assertEquals(data.size(), result.size()); |
158 assertContents(data, result); |
173 assertContents(data, result); |
159 |
174 |
160 result = exerciseOps(data, s -> s.flatMap(i -> IntStream.empty())); |
175 result = exerciseOps(data, s -> s.flatMap(i -> IntStream.empty())); |
161 assertEquals(0, result.size()); |
176 assertEquals(0, result.size()); |
163 |
178 |
164 @Test(dataProvider = "IntStreamTestData.small", dataProviderClass = IntStreamTestDataProvider.class) |
179 @Test(dataProvider = "IntStreamTestData.small", dataProviderClass = IntStreamTestDataProvider.class) |
165 public void testIntOpsX(String name, TestData.OfInt data) { |
180 public void testIntOpsX(String name, TestData.OfInt data) { |
166 exerciseOps(data, s -> s.flatMap(e -> IntStream.range(0, e))); |
181 exerciseOps(data, s -> s.flatMap(e -> IntStream.range(0, e))); |
167 exerciseOps(data, s -> s.flatMap(e -> IntStream.range(0, e).limit(10))); |
182 exerciseOps(data, s -> s.flatMap(e -> IntStream.range(0, e).limit(10))); |
|
183 |
|
184 exerciseOps(data, s -> s.boxed().flatMapToInt(e -> IntStream.range(0, e))); |
|
185 exerciseOps(data, s -> s.boxed().flatMapToInt(e -> IntStream.range(0, e).limit(10))); |
|
186 } |
|
187 |
|
188 @Test |
|
189 public void testIntOpsShortCircuit() { |
|
190 AtomicInteger count = new AtomicInteger(); |
|
191 IntStream.of(0).flatMap(i -> IntStream.range(0, 100)). |
|
192 peek(i -> count.incrementAndGet()). |
|
193 limit(10).toArray(); |
|
194 assertEquals(count.get(), 10); |
|
195 |
|
196 count.set(0); |
|
197 Stream.of(0).flatMapToInt(i -> IntStream.range(0, 100)). |
|
198 peek(i -> count.incrementAndGet()). |
|
199 limit(10).toArray(); |
|
200 assertEquals(count.get(), 10); |
168 } |
201 } |
169 |
202 |
170 // |
203 // |
171 |
204 |
172 @Test(dataProvider = "LongStreamTestData", dataProviderClass = LongStreamTestDataProvider.class) |
205 @Test(dataProvider = "LongStreamTestData", dataProviderClass = LongStreamTestDataProvider.class) |
173 public void testLongOps(String name, TestData.OfLong data) { |
206 public void testLongOps(String name, TestData.OfLong data) { |
174 Collection<Long> result = exerciseOps(data, s -> s.flatMap(i -> Collections.singleton(i).stream().mapToLong(j -> j))); |
207 Collection<Long> result = exerciseOps(data, s -> s.flatMap(LongStream::of)); |
|
208 assertEquals(data.size(), result.size()); |
|
209 assertContents(data, result); |
|
210 |
|
211 result = exerciseOps(data, s -> s.boxed().flatMapToLong(LongStream::of)); |
175 assertEquals(data.size(), result.size()); |
212 assertEquals(data.size(), result.size()); |
176 assertContents(data, result); |
213 assertContents(data, result); |
177 |
214 |
178 result = exerciseOps(data, s -> LongStream.empty()); |
215 result = exerciseOps(data, s -> LongStream.empty()); |
179 assertEquals(0, result.size()); |
216 assertEquals(0, result.size()); |
183 public void testLongOpsX(String name, TestData.OfLong data) { |
220 public void testLongOpsX(String name, TestData.OfLong data) { |
184 exerciseOps(data, s -> s.flatMap(e -> LongStream.range(0, e))); |
221 exerciseOps(data, s -> s.flatMap(e -> LongStream.range(0, e))); |
185 exerciseOps(data, s -> s.flatMap(e -> LongStream.range(0, e).limit(10))); |
222 exerciseOps(data, s -> s.flatMap(e -> LongStream.range(0, e).limit(10))); |
186 } |
223 } |
187 |
224 |
|
225 @Test |
|
226 public void testLongOpsShortCircuit() { |
|
227 AtomicInteger count = new AtomicInteger(); |
|
228 LongStream.of(0).flatMap(i -> LongStream.range(0, 100)). |
|
229 peek(i -> count.incrementAndGet()). |
|
230 limit(10).toArray(); |
|
231 assertEquals(count.get(), 10); |
|
232 |
|
233 count.set(0); |
|
234 Stream.of(0).flatMapToLong(i -> LongStream.range(0, 100)). |
|
235 peek(i -> count.incrementAndGet()). |
|
236 limit(10).toArray(); |
|
237 assertEquals(count.get(), 10); |
|
238 } |
|
239 |
188 // |
240 // |
189 |
241 |
190 @Test(dataProvider = "DoubleStreamTestData", dataProviderClass = DoubleStreamTestDataProvider.class) |
242 @Test(dataProvider = "DoubleStreamTestData", dataProviderClass = DoubleStreamTestDataProvider.class) |
191 public void testDoubleOps(String name, TestData.OfDouble data) { |
243 public void testDoubleOps(String name, TestData.OfDouble data) { |
192 Collection<Double> result = exerciseOps(data, s -> s.flatMap(i -> Collections.singleton(i).stream().mapToDouble(j -> j))); |
244 Collection<Double> result = exerciseOps(data, s -> s.flatMap(DoubleStream::of)); |
|
245 assertEquals(data.size(), result.size()); |
|
246 assertContents(data, result); |
|
247 |
|
248 result = exerciseOps(data, s -> s.boxed().flatMapToDouble(DoubleStream::of)); |
193 assertEquals(data.size(), result.size()); |
249 assertEquals(data.size(), result.size()); |
194 assertContents(data, result); |
250 assertContents(data, result); |
195 |
251 |
196 result = exerciseOps(data, s -> DoubleStream.empty()); |
252 result = exerciseOps(data, s -> DoubleStream.empty()); |
197 assertEquals(0, result.size()); |
253 assertEquals(0, result.size()); |
200 @Test(dataProvider = "DoubleStreamTestData.small", dataProviderClass = DoubleStreamTestDataProvider.class) |
256 @Test(dataProvider = "DoubleStreamTestData.small", dataProviderClass = DoubleStreamTestDataProvider.class) |
201 public void testDoubleOpsX(String name, TestData.OfDouble data) { |
257 public void testDoubleOpsX(String name, TestData.OfDouble data) { |
202 exerciseOps(data, s -> s.flatMap(e -> IntStream.range(0, (int) e).asDoubleStream())); |
258 exerciseOps(data, s -> s.flatMap(e -> IntStream.range(0, (int) e).asDoubleStream())); |
203 exerciseOps(data, s -> s.flatMap(e -> IntStream.range(0, (int) e).limit(10).asDoubleStream())); |
259 exerciseOps(data, s -> s.flatMap(e -> IntStream.range(0, (int) e).limit(10).asDoubleStream())); |
204 } |
260 } |
|
261 |
|
262 @Test |
|
263 public void testDoubleOpsShortCircuit() { |
|
264 AtomicInteger count = new AtomicInteger(); |
|
265 DoubleStream.of(0).flatMap(i -> IntStream.range(0, 100).asDoubleStream()). |
|
266 peek(i -> count.incrementAndGet()). |
|
267 limit(10).toArray(); |
|
268 assertEquals(count.get(), 10); |
|
269 |
|
270 count.set(0); |
|
271 Stream.of(0).flatMapToDouble(i -> IntStream.range(0, 100).asDoubleStream()). |
|
272 peek(i -> count.incrementAndGet()). |
|
273 limit(10).toArray(); |
|
274 assertEquals(count.get(), 10); |
|
275 } |
205 } |
276 } |