1 /*
2 * Copyright (c) 2012, 2013, 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 package org.openjdk.tests.java.util.stream;
24
25 import java.util.Arrays;
26 import java.util.Comparator;
27 import java.util.List;
28 import java.util.Spliterator;
29 import java.util.function.Consumer;
30 import java.util.function.DoubleConsumer;
31 import java.util.function.Function;
32 import java.util.function.IntConsumer;
33 import java.util.function.LongConsumer;
34 import java.util.function.UnaryOperator;
35 import java.util.stream.DoubleStream;
36 import java.util.stream.DoubleStreamTestDataProvider;
37 import java.util.stream.IntStream;
38 import java.util.stream.IntStreamTestDataProvider;
39 import java.util.stream.LambdaTestHelpers;
40 import java.util.stream.LongStream;
41 import java.util.stream.LongStreamTestDataProvider;
42 import java.util.stream.OpTestCase;
265 for (boolean proxyEstimateSize : new boolean[] {false, true}) {
266 setContext("proxyEstimateSize", proxyEstimateSize);
267 Spliterator<Integer> sp = intermediateOp.apply(l.stream()).spliterator();
268 ProxyNoExactSizeSpliterator<Integer> psp = new ProxyNoExactSizeSpliterator<>(sp, proxyEstimateSize);
269 Stream<Integer> s = StreamSupport.stream(psp, true);
270 terminalOp.accept(s);
271 Assert.assertTrue(psp.splits > 0,
272 String.format("Number of splits should be greater that zero when proxyEstimateSize is %s",
273 proxyEstimateSize));
274 Assert.assertTrue(psp.prefixSplits > 0,
275 String.format("Number of non-null prefix splits should be greater that zero when proxyEstimateSize is %s",
276 proxyEstimateSize));
277 Assert.assertTrue(psp.sizeOnTraversal < l.size(),
278 String.format("Size on traversal of last split should be less than the size of the list, %d, when proxyEstimateSize is %s",
279 l.size(), proxyEstimateSize));
280 }
281 }
282 }
283 }
284
285 @Test(dataProvider = "StreamTestData<Integer>",
286 dataProviderClass = StreamTestDataProvider.class,
287 groups = { "serialization-hostile" })
288 public void testStreamSpliterators(String name, TestData.OfRef<Integer> data) {
289 for (Function<Stream<Integer>, Stream<Integer>> f : streamFunctions()) {
290 withData(data).
291 stream((Stream<Integer> in) -> {
292 Stream<Integer> out = f.apply(in);
293 return StreamSupport.stream(() -> out.spliterator(), OpTestCase.getStreamFlags(out), false);
294 }).
295 exercise();
296
297 withData(data).
298 stream((Stream<Integer> in) -> {
299 Stream<Integer> out = f.apply(in);
300 return StreamSupport.stream(() -> out.spliterator(), OpTestCase.getStreamFlags(out), true);
301 }).
302 exercise();
303 }
304 }
305
306 @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
307 public void testSpliterators(String name, TestData.OfRef<Integer> data) {
308 for (Function<Stream<Integer>, Stream<Integer>> f : streamFunctions()) {
309 SpliteratorTestHelper.testSpliterator(() -> f.apply(data.stream()).spliterator());
310 }
311 }
312
313 @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
314 public void testParSpliterators(String name, TestData.OfRef<Integer> data) {
315 for (Function<Stream<Integer>, Stream<Integer>> f : streamFunctions()) {
316 SpliteratorTestHelper.testSpliterator(() -> f.apply(data.parallelStream()).spliterator());
317 }
318 }
319
320 private List<Function<Stream<Integer>, Stream<Integer>>> streamFunctions;
321
322 List<Function<Stream<Integer>, Stream<Integer>>> streamFunctions() {
323 if (streamFunctions == null) {
324 List<Function<Stream<Integer>, Stream<Integer>>> opFunctions = Arrays.asList(
325 s -> s.filter(pEven),
326 s -> s.map(mDoubler),
327 // @@@ Add distinct once asserting results with or without order
328 // is correctly supported
329 // s -> s.distinct(),
330 s -> s.sorted());
331
332 streamFunctions = permuteStreamFunctions(opFunctions);
333 }
334
335 return streamFunctions;
336 }
337
338 //
339
340 public void testIntSplitting() {
341 List<Consumer<IntStream>> terminalOps = Arrays.asList(
342 s -> s.toArray(),
343 s -> s.forEach(e -> {}),
344 s -> s.reduce(Integer::sum)
345 );
346
361 // Size is assumed to be larger than the target size for no splitting
362 // @@@ Need way to obtain the target size
363 Spliterator.OfInt sp = intermediateOp.apply(IntStream.range(0, 1000)).spliterator();
364 ProxyNoExactSizeSpliterator.OfInt psp = new ProxyNoExactSizeSpliterator.OfInt(sp, proxyEstimateSize);
365 IntStream s = StreamSupport.intStream(psp, true);
366 terminalOp.accept(s);
367 Assert.assertTrue(psp.splits > 0,
368 String.format("Number of splits should be greater that zero when proxyEstimateSize is %s",
369 proxyEstimateSize));
370 Assert.assertTrue(psp.prefixSplits > 0,
371 String.format("Number of non-null prefix splits should be greater that zero when proxyEstimateSize is %s",
372 proxyEstimateSize));
373 Assert.assertTrue(psp.sizeOnTraversal < 1000,
374 String.format("Size on traversal of last split should be less than the size of the list, %d, when proxyEstimateSize is %s",
375 1000, proxyEstimateSize));
376 }
377 }
378 }
379 }
380
381 @Test(dataProvider = "IntStreamTestData",
382 dataProviderClass = IntStreamTestDataProvider.class,
383 groups = { "serialization-hostile" })
384 public void testIntStreamSpliterators(String name, TestData.OfInt data) {
385 for (Function<IntStream, IntStream> f : intStreamFunctions()) {
386 withData(data).
387 stream(in -> {
388 IntStream out = f.apply(in);
389 return StreamSupport.intStream(() -> out.spliterator(), OpTestCase.getStreamFlags(out), false);
390 }).
391 exercise();
392
393 withData(data).
394 stream((in) -> {
395 IntStream out = f.apply(in);
396 return StreamSupport.intStream(() -> out.spliterator(), OpTestCase.getStreamFlags(out), true);
397 }).
398 exercise();
399 }
400 }
401
402 @Test(dataProvider = "IntStreamTestData", dataProviderClass = IntStreamTestDataProvider.class)
403 public void testIntSpliterators(String name, TestData.OfInt data) {
404 for (Function<IntStream, IntStream> f : intStreamFunctions()) {
405 SpliteratorTestHelper.testIntSpliterator(() -> f.apply(data.stream()).spliterator());
406 }
407 }
408
409 @Test(dataProvider = "IntStreamTestData", dataProviderClass = IntStreamTestDataProvider.class)
410 public void testIntParSpliterators(String name, TestData.OfInt data) {
411 for (Function<IntStream, IntStream> f : intStreamFunctions()) {
412 SpliteratorTestHelper.testIntSpliterator(() -> f.apply(data.parallelStream()).spliterator());
413 }
414 }
415
416 private List<Function<IntStream, IntStream>> intStreamFunctions;
417
418 List<Function<IntStream, IntStream>> intStreamFunctions() {
419 if (intStreamFunctions == null) {
420 List<Function<IntStream, IntStream>> opFunctions = Arrays.asList(
421 s -> s.filter(ipEven),
422 s -> s.map(irDoubler),
423 s -> s.sorted());
424
425 intStreamFunctions = permuteStreamFunctions(opFunctions);
426 }
427
428 return intStreamFunctions;
429 }
430
431 //
432
433 public void testLongSplitting() {
434 List<Consumer<LongStream>> terminalOps = Arrays.asList(
435 s -> s.toArray(),
436 s -> s.forEach(e -> {}),
437 s -> s.reduce(Long::sum)
438 );
439
440 List<UnaryOperator<LongStream>> intermediateOps = Arrays.asList(
441 s -> s.parallel(),
442 // The following ensures the wrapping spliterator is tested
454 // Size is assumed to be larger than the target size for no splitting
455 // @@@ Need way to obtain the target size
456 Spliterator.OfLong sp = intermediateOp.apply(LongStream.range(0, 1000)).spliterator();
457 ProxyNoExactSizeSpliterator.OfLong psp = new ProxyNoExactSizeSpliterator.OfLong(sp, proxyEstimateSize);
458 LongStream s = StreamSupport.longStream(psp, true);
459 terminalOp.accept(s);
460 Assert.assertTrue(psp.splits > 0,
461 String.format("Number of splits should be greater that zero when proxyEstimateSize is %s",
462 proxyEstimateSize));
463 Assert.assertTrue(psp.prefixSplits > 0,
464 String.format("Number of non-null prefix splits should be greater that zero when proxyEstimateSize is %s",
465 proxyEstimateSize));
466 Assert.assertTrue(psp.sizeOnTraversal < 1000,
467 String.format("Size on traversal of last split should be less than the size of the list, %d, when proxyEstimateSize is %s",
468 1000, proxyEstimateSize));
469 }
470 }
471 }
472 }
473
474 @Test(dataProvider = "LongStreamTestData",
475 dataProviderClass = LongStreamTestDataProvider.class,
476 groups = { "serialization-hostile" })
477 public void testLongStreamSpliterators(String name, TestData.OfLong data) {
478 for (Function<LongStream, LongStream> f : longStreamFunctions()) {
479 withData(data).
480 stream(in -> {
481 LongStream out = f.apply(in);
482 return StreamSupport.longStream(() -> out.spliterator(), OpTestCase.getStreamFlags(out), false);
483 }).
484 exercise();
485
486 withData(data).
487 stream((in) -> {
488 LongStream out = f.apply(in);
489 return StreamSupport.longStream(() -> out.spliterator(), OpTestCase.getStreamFlags(out), true);
490 }).
491 exercise();
492 }
493 }
494
495 @Test(dataProvider = "LongStreamTestData", dataProviderClass = LongStreamTestDataProvider.class)
496 public void testLongSpliterators(String name, TestData.OfLong data) {
497 for (Function<LongStream, LongStream> f : longStreamFunctions()) {
498 SpliteratorTestHelper.testLongSpliterator(() -> f.apply(data.stream()).spliterator());
499 }
500 }
501
502 @Test(dataProvider = "LongStreamTestData", dataProviderClass = LongStreamTestDataProvider.class)
503 public void testLongParSpliterators(String name, TestData.OfLong data) {
504 for (Function<LongStream, LongStream> f : longStreamFunctions()) {
505 SpliteratorTestHelper.testLongSpliterator(() -> f.apply(data.parallelStream()).spliterator());
506 }
507 }
508
509 private List<Function<LongStream, LongStream>> longStreamFunctions;
510
511 List<Function<LongStream, LongStream>> longStreamFunctions() {
512 if (longStreamFunctions == null) {
513 List<Function<LongStream, LongStream>> opFunctions = Arrays.asList(
514 s -> s.filter(lpEven),
515 s -> s.map(x -> x * 2L),
516 s -> s.sorted());
517
518 longStreamFunctions = permuteStreamFunctions(opFunctions);
519 }
520
521 return longStreamFunctions;
522 }
523
524 //
525
526 public void testDoubleSplitting() {
527 List<Consumer<DoubleStream>> terminalOps = Arrays.asList(
528 s -> s.toArray(),
529 s -> s.forEach(e -> {}),
530 s -> s.reduce(Double::sum)
531 );
532
533 List<UnaryOperator<DoubleStream>> intermediateOps = Arrays.asList(
534 s -> s.parallel(),
535 // The following ensures the wrapping spliterator is tested
547 // Size is assumed to be larger than the target size for no splitting
548 // @@@ Need way to obtain the target size
549 Spliterator.OfDouble sp = intermediateOp.apply(IntStream.range(0, 1000).asDoubleStream()).spliterator();
550 ProxyNoExactSizeSpliterator.OfDouble psp = new ProxyNoExactSizeSpliterator.OfDouble(sp, proxyEstimateSize);
551 DoubleStream s = StreamSupport.doubleStream(psp, true);
552 terminalOp.accept(s);
553 Assert.assertTrue(psp.splits > 0,
554 String.format("Number of splits should be greater that zero when proxyEstimateSize is %s",
555 proxyEstimateSize));
556 Assert.assertTrue(psp.prefixSplits > 0,
557 String.format("Number of non-null prefix splits should be greater that zero when proxyEstimateSize is %s",
558 proxyEstimateSize));
559 Assert.assertTrue(psp.sizeOnTraversal < 1000,
560 String.format("Size on traversal of last split should be less than the size of the list, %d, when proxyEstimateSize is %s",
561 1000, proxyEstimateSize));
562 }
563 }
564 }
565 }
566
567 @Test(dataProvider = "DoubleStreamTestData",
568 dataProviderClass = DoubleStreamTestDataProvider.class,
569 groups = { "serialization-hostile" })
570 public void testDoubleStreamSpliterators(String name, TestData.OfDouble data) {
571 for (Function<DoubleStream, DoubleStream> f : doubleStreamFunctions()) {
572 withData(data).
573 stream(in -> {
574 DoubleStream out = f.apply(in);
575 return StreamSupport.doubleStream(() -> out.spliterator(), OpTestCase.getStreamFlags(out), false);
576 }).
577 exercise();
578
579 withData(data).
580 stream((in) -> {
581 DoubleStream out = f.apply(in);
582 return StreamSupport.doubleStream(() -> out.spliterator(), OpTestCase.getStreamFlags(out), true);
583 }).
584 exercise();
585 }
586 }
587
588 @Test(dataProvider = "DoubleStreamTestData", dataProviderClass = DoubleStreamTestDataProvider.class)
589 public void testDoubleSpliterators(String name, TestData.OfDouble data) {
590 for (Function<DoubleStream, DoubleStream> f : doubleStreamFunctions()) {
591 SpliteratorTestHelper.testDoubleSpliterator(() -> f.apply(data.stream()).spliterator());
592 }
593 }
594
595 @Test(dataProvider = "DoubleStreamTestData", dataProviderClass = DoubleStreamTestDataProvider.class)
596 public void testDoubleParSpliterators(String name, TestData.OfDouble data) {
597 for (Function<DoubleStream, DoubleStream> f : doubleStreamFunctions()) {
598 SpliteratorTestHelper.testDoubleSpliterator(() -> f.apply(data.parallelStream()).spliterator());
599 }
600 }
601
602 private List<Function<DoubleStream, DoubleStream>> doubleStreamFunctions;
603
604 List<Function<DoubleStream, DoubleStream>> doubleStreamFunctions() {
605 if (doubleStreamFunctions == null) {
606 List<Function<DoubleStream, DoubleStream>> opFunctions = Arrays.asList(
607 s -> s.filter(dpEven),
608 s -> s.map(x -> x * 2.0),
609 s -> s.sorted());
610
611 doubleStreamFunctions = permuteStreamFunctions(opFunctions);
612 }
613
614 return doubleStreamFunctions;
615 }
616 }
|
1 /*
2 * Copyright (c) 2012, 2016, 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 * @bug 8148838
27 */
28
29 package org.openjdk.tests.java.util.stream;
30
31 import java.util.Arrays;
32 import java.util.Comparator;
33 import java.util.List;
34 import java.util.Spliterator;
35 import java.util.function.Consumer;
36 import java.util.function.DoubleConsumer;
37 import java.util.function.Function;
38 import java.util.function.IntConsumer;
39 import java.util.function.LongConsumer;
40 import java.util.function.UnaryOperator;
41 import java.util.stream.DoubleStream;
42 import java.util.stream.DoubleStreamTestDataProvider;
43 import java.util.stream.IntStream;
44 import java.util.stream.IntStreamTestDataProvider;
45 import java.util.stream.LambdaTestHelpers;
46 import java.util.stream.LongStream;
47 import java.util.stream.LongStreamTestDataProvider;
48 import java.util.stream.OpTestCase;
271 for (boolean proxyEstimateSize : new boolean[] {false, true}) {
272 setContext("proxyEstimateSize", proxyEstimateSize);
273 Spliterator<Integer> sp = intermediateOp.apply(l.stream()).spliterator();
274 ProxyNoExactSizeSpliterator<Integer> psp = new ProxyNoExactSizeSpliterator<>(sp, proxyEstimateSize);
275 Stream<Integer> s = StreamSupport.stream(psp, true);
276 terminalOp.accept(s);
277 Assert.assertTrue(psp.splits > 0,
278 String.format("Number of splits should be greater that zero when proxyEstimateSize is %s",
279 proxyEstimateSize));
280 Assert.assertTrue(psp.prefixSplits > 0,
281 String.format("Number of non-null prefix splits should be greater that zero when proxyEstimateSize is %s",
282 proxyEstimateSize));
283 Assert.assertTrue(psp.sizeOnTraversal < l.size(),
284 String.format("Size on traversal of last split should be less than the size of the list, %d, when proxyEstimateSize is %s",
285 l.size(), proxyEstimateSize));
286 }
287 }
288 }
289 }
290
291 @Test(dataProvider = "StreamTestData<Integer>.small",
292 dataProviderClass = StreamTestDataProvider.class,
293 groups = { "serialization-hostile" })
294 public void testStreamSpliterators(String name, TestData.OfRef<Integer> data) {
295 for (Function<Stream<Integer>, Stream<Integer>> f : streamFunctions()) {
296 withData(data).
297 stream((Stream<Integer> in) -> {
298 Stream<Integer> out = f.apply(in);
299 return StreamSupport.stream(() -> out.spliterator(), OpTestCase.getStreamFlags(out), false);
300 }).
301 exercise();
302
303 withData(data).
304 stream((Stream<Integer> in) -> {
305 Stream<Integer> out = f.apply(in);
306 return StreamSupport.stream(() -> out.spliterator(), OpTestCase.getStreamFlags(out), true);
307 }).
308 exercise();
309 }
310 }
311
312 @Test(dataProvider = "StreamTestData<Integer>.small", dataProviderClass = StreamTestDataProvider.class)
313 public void testSpliterators(String name, TestData.OfRef<Integer> data) {
314 for (Function<Stream<Integer>, Stream<Integer>> f : streamFunctions()) {
315 SpliteratorTestHelper.testSpliterator(() -> f.apply(data.stream()).spliterator());
316 }
317 }
318
319 @Test(dataProvider = "StreamTestData<Integer>.small", dataProviderClass = StreamTestDataProvider.class)
320 public void testParSpliterators(String name, TestData.OfRef<Integer> data) {
321 for (Function<Stream<Integer>, Stream<Integer>> f : streamFunctions()) {
322 SpliteratorTestHelper.testSpliterator(() -> f.apply(data.parallelStream()).spliterator());
323 }
324 }
325
326 private List<Function<Stream<Integer>, Stream<Integer>>> streamFunctions;
327
328 List<Function<Stream<Integer>, Stream<Integer>>> streamFunctions() {
329 if (streamFunctions == null) {
330 List<Function<Stream<Integer>, Stream<Integer>>> opFunctions = Arrays.asList(
331 s -> s.filter(pEven),
332 s -> s.flatMap(x -> Stream.of(x, x)),
333 // @@@ Add distinct once asserting results with or without order
334 // is correctly supported
335 // s -> s.distinct(),
336 s -> s.sorted());
337
338 streamFunctions = permuteStreamFunctions(opFunctions);
339 }
340
341 return streamFunctions;
342 }
343
344 //
345
346 public void testIntSplitting() {
347 List<Consumer<IntStream>> terminalOps = Arrays.asList(
348 s -> s.toArray(),
349 s -> s.forEach(e -> {}),
350 s -> s.reduce(Integer::sum)
351 );
352
367 // Size is assumed to be larger than the target size for no splitting
368 // @@@ Need way to obtain the target size
369 Spliterator.OfInt sp = intermediateOp.apply(IntStream.range(0, 1000)).spliterator();
370 ProxyNoExactSizeSpliterator.OfInt psp = new ProxyNoExactSizeSpliterator.OfInt(sp, proxyEstimateSize);
371 IntStream s = StreamSupport.intStream(psp, true);
372 terminalOp.accept(s);
373 Assert.assertTrue(psp.splits > 0,
374 String.format("Number of splits should be greater that zero when proxyEstimateSize is %s",
375 proxyEstimateSize));
376 Assert.assertTrue(psp.prefixSplits > 0,
377 String.format("Number of non-null prefix splits should be greater that zero when proxyEstimateSize is %s",
378 proxyEstimateSize));
379 Assert.assertTrue(psp.sizeOnTraversal < 1000,
380 String.format("Size on traversal of last split should be less than the size of the list, %d, when proxyEstimateSize is %s",
381 1000, proxyEstimateSize));
382 }
383 }
384 }
385 }
386
387 @Test(dataProvider = "IntStreamTestData.small",
388 dataProviderClass = IntStreamTestDataProvider.class,
389 groups = { "serialization-hostile" })
390 public void testIntStreamSpliterators(String name, TestData.OfInt data) {
391 for (Function<IntStream, IntStream> f : intStreamFunctions()) {
392 withData(data).
393 stream(in -> {
394 IntStream out = f.apply(in);
395 return StreamSupport.intStream(() -> out.spliterator(), OpTestCase.getStreamFlags(out), false);
396 }).
397 exercise();
398
399 withData(data).
400 stream((in) -> {
401 IntStream out = f.apply(in);
402 return StreamSupport.intStream(() -> out.spliterator(), OpTestCase.getStreamFlags(out), true);
403 }).
404 exercise();
405 }
406 }
407
408 @Test(dataProvider = "IntStreamTestData.small", dataProviderClass = IntStreamTestDataProvider.class)
409 public void testIntSpliterators(String name, TestData.OfInt data) {
410 for (Function<IntStream, IntStream> f : intStreamFunctions()) {
411 SpliteratorTestHelper.testIntSpliterator(() -> f.apply(data.stream()).spliterator());
412 }
413 }
414
415 @Test(dataProvider = "IntStreamTestData.small", dataProviderClass = IntStreamTestDataProvider.class)
416 public void testIntParSpliterators(String name, TestData.OfInt data) {
417 for (Function<IntStream, IntStream> f : intStreamFunctions()) {
418 SpliteratorTestHelper.testIntSpliterator(() -> f.apply(data.parallelStream()).spliterator());
419 }
420 }
421
422 private List<Function<IntStream, IntStream>> intStreamFunctions;
423
424 List<Function<IntStream, IntStream>> intStreamFunctions() {
425 if (intStreamFunctions == null) {
426 List<Function<IntStream, IntStream>> opFunctions = Arrays.asList(
427 s -> s.filter(ipEven),
428 s -> s.flatMap(x -> IntStream.of(x, x)),
429 s -> s.sorted());
430
431 intStreamFunctions = permuteStreamFunctions(opFunctions);
432 }
433
434 return intStreamFunctions;
435 }
436
437 //
438
439 public void testLongSplitting() {
440 List<Consumer<LongStream>> terminalOps = Arrays.asList(
441 s -> s.toArray(),
442 s -> s.forEach(e -> {}),
443 s -> s.reduce(Long::sum)
444 );
445
446 List<UnaryOperator<LongStream>> intermediateOps = Arrays.asList(
447 s -> s.parallel(),
448 // The following ensures the wrapping spliterator is tested
460 // Size is assumed to be larger than the target size for no splitting
461 // @@@ Need way to obtain the target size
462 Spliterator.OfLong sp = intermediateOp.apply(LongStream.range(0, 1000)).spliterator();
463 ProxyNoExactSizeSpliterator.OfLong psp = new ProxyNoExactSizeSpliterator.OfLong(sp, proxyEstimateSize);
464 LongStream s = StreamSupport.longStream(psp, true);
465 terminalOp.accept(s);
466 Assert.assertTrue(psp.splits > 0,
467 String.format("Number of splits should be greater that zero when proxyEstimateSize is %s",
468 proxyEstimateSize));
469 Assert.assertTrue(psp.prefixSplits > 0,
470 String.format("Number of non-null prefix splits should be greater that zero when proxyEstimateSize is %s",
471 proxyEstimateSize));
472 Assert.assertTrue(psp.sizeOnTraversal < 1000,
473 String.format("Size on traversal of last split should be less than the size of the list, %d, when proxyEstimateSize is %s",
474 1000, proxyEstimateSize));
475 }
476 }
477 }
478 }
479
480 @Test(dataProvider = "LongStreamTestData.small",
481 dataProviderClass = LongStreamTestDataProvider.class,
482 groups = { "serialization-hostile" })
483 public void testLongStreamSpliterators(String name, TestData.OfLong data) {
484 for (Function<LongStream, LongStream> f : longStreamFunctions()) {
485 withData(data).
486 stream(in -> {
487 LongStream out = f.apply(in);
488 return StreamSupport.longStream(() -> out.spliterator(), OpTestCase.getStreamFlags(out), false);
489 }).
490 exercise();
491
492 withData(data).
493 stream((in) -> {
494 LongStream out = f.apply(in);
495 return StreamSupport.longStream(() -> out.spliterator(), OpTestCase.getStreamFlags(out), true);
496 }).
497 exercise();
498 }
499 }
500
501 @Test(dataProvider = "LongStreamTestData.small", dataProviderClass = LongStreamTestDataProvider.class)
502 public void testLongSpliterators(String name, TestData.OfLong data) {
503 for (Function<LongStream, LongStream> f : longStreamFunctions()) {
504 SpliteratorTestHelper.testLongSpliterator(() -> f.apply(data.stream()).spliterator());
505 }
506 }
507
508 @Test(dataProvider = "LongStreamTestData.small", dataProviderClass = LongStreamTestDataProvider.class)
509 public void testLongParSpliterators(String name, TestData.OfLong data) {
510 for (Function<LongStream, LongStream> f : longStreamFunctions()) {
511 SpliteratorTestHelper.testLongSpliterator(() -> f.apply(data.parallelStream()).spliterator());
512 }
513 }
514
515 private List<Function<LongStream, LongStream>> longStreamFunctions;
516
517 List<Function<LongStream, LongStream>> longStreamFunctions() {
518 if (longStreamFunctions == null) {
519 List<Function<LongStream, LongStream>> opFunctions = Arrays.asList(
520 s -> s.filter(lpEven),
521 s -> s.flatMap(x -> LongStream.of(x, x)),
522 s -> s.sorted());
523
524 longStreamFunctions = permuteStreamFunctions(opFunctions);
525 }
526
527 return longStreamFunctions;
528 }
529
530 //
531
532 public void testDoubleSplitting() {
533 List<Consumer<DoubleStream>> terminalOps = Arrays.asList(
534 s -> s.toArray(),
535 s -> s.forEach(e -> {}),
536 s -> s.reduce(Double::sum)
537 );
538
539 List<UnaryOperator<DoubleStream>> intermediateOps = Arrays.asList(
540 s -> s.parallel(),
541 // The following ensures the wrapping spliterator is tested
553 // Size is assumed to be larger than the target size for no splitting
554 // @@@ Need way to obtain the target size
555 Spliterator.OfDouble sp = intermediateOp.apply(IntStream.range(0, 1000).asDoubleStream()).spliterator();
556 ProxyNoExactSizeSpliterator.OfDouble psp = new ProxyNoExactSizeSpliterator.OfDouble(sp, proxyEstimateSize);
557 DoubleStream s = StreamSupport.doubleStream(psp, true);
558 terminalOp.accept(s);
559 Assert.assertTrue(psp.splits > 0,
560 String.format("Number of splits should be greater that zero when proxyEstimateSize is %s",
561 proxyEstimateSize));
562 Assert.assertTrue(psp.prefixSplits > 0,
563 String.format("Number of non-null prefix splits should be greater that zero when proxyEstimateSize is %s",
564 proxyEstimateSize));
565 Assert.assertTrue(psp.sizeOnTraversal < 1000,
566 String.format("Size on traversal of last split should be less than the size of the list, %d, when proxyEstimateSize is %s",
567 1000, proxyEstimateSize));
568 }
569 }
570 }
571 }
572
573 @Test(dataProvider = "DoubleStreamTestData.small",
574 dataProviderClass = DoubleStreamTestDataProvider.class,
575 groups = { "serialization-hostile" })
576 public void testDoubleStreamSpliterators(String name, TestData.OfDouble data) {
577 for (Function<DoubleStream, DoubleStream> f : doubleStreamFunctions()) {
578 withData(data).
579 stream(in -> {
580 DoubleStream out = f.apply(in);
581 return StreamSupport.doubleStream(() -> out.spliterator(), OpTestCase.getStreamFlags(out), false);
582 }).
583 exercise();
584
585 withData(data).
586 stream((in) -> {
587 DoubleStream out = f.apply(in);
588 return StreamSupport.doubleStream(() -> out.spliterator(), OpTestCase.getStreamFlags(out), true);
589 }).
590 exercise();
591 }
592 }
593
594 @Test(dataProvider = "DoubleStreamTestData.small", dataProviderClass = DoubleStreamTestDataProvider.class)
595 public void testDoubleSpliterators(String name, TestData.OfDouble data) {
596 for (Function<DoubleStream, DoubleStream> f : doubleStreamFunctions()) {
597 SpliteratorTestHelper.testDoubleSpliterator(() -> f.apply(data.stream()).spliterator());
598 }
599 }
600
601 @Test(dataProvider = "DoubleStreamTestData.small", dataProviderClass = DoubleStreamTestDataProvider.class)
602 public void testDoubleParSpliterators(String name, TestData.OfDouble data) {
603 for (Function<DoubleStream, DoubleStream> f : doubleStreamFunctions()) {
604 SpliteratorTestHelper.testDoubleSpliterator(() -> f.apply(data.parallelStream()).spliterator());
605 }
606 }
607
608 private List<Function<DoubleStream, DoubleStream>> doubleStreamFunctions;
609
610 List<Function<DoubleStream, DoubleStream>> doubleStreamFunctions() {
611 if (doubleStreamFunctions == null) {
612 List<Function<DoubleStream, DoubleStream>> opFunctions = Arrays.asList(
613 s -> s.filter(dpEven),
614 s -> s.flatMap(x -> DoubleStream.of(x, x)),
615 s -> s.sorted());
616
617 doubleStreamFunctions = permuteStreamFunctions(opFunctions);
618 }
619
620 return doubleStreamFunctions;
621 }
622 }
|