changeset 13676:8719783940f1

8148838: Stream.flatMap(...).spliterator() cannot properly split after tryAdvance() Reviewed-by: psandoz
author tvaleev
date Mon, 08 Feb 2016 15:33:53 +0100
parents 4a497e746019
children 3e5970acb0a7
files src/java.base/share/classes/java/util/stream/StreamSpliterators.java test/java/util/stream/test/org/openjdk/tests/java/util/stream/StreamSpliteratorTest.java
diffstat 2 files changed, 25 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/util/stream/StreamSpliterators.java	Mon Feb 08 10:40:00 2016 +0100
+++ b/src/java.base/share/classes/java/util/stream/StreamSpliterators.java	Mon Feb 08 15:33:53 2016 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -186,7 +186,7 @@
 
         @Override
         public Spliterator<P_OUT> trySplit() {
-            if (isParallel && !finished) {
+            if (isParallel && buffer == null && !finished) {
                 init();
 
                 Spliterator<P_IN> split = spliterator.trySplit();
--- a/test/java/util/stream/test/org/openjdk/tests/java/util/stream/StreamSpliteratorTest.java	Mon Feb 08 10:40:00 2016 +0100
+++ b/test/java/util/stream/test/org/openjdk/tests/java/util/stream/StreamSpliteratorTest.java	Mon Feb 08 15:33:53 2016 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -20,6 +20,12 @@
  * or visit www.oracle.com if you need additional information or have any
  * questions.
  */
+
+/**
+ * @test
+ * @bug 8148838
+ */
+
 package org.openjdk.tests.java.util.stream;
 
 import java.util.Arrays;
@@ -282,7 +288,7 @@
         }
     }
 
-    @Test(dataProvider = "StreamTestData<Integer>",
+    @Test(dataProvider = "StreamTestData<Integer>.small",
           dataProviderClass = StreamTestDataProvider.class,
           groups = { "serialization-hostile" })
     public void testStreamSpliterators(String name, TestData.OfRef<Integer> data) {
@@ -303,14 +309,14 @@
         }
     }
 
-    @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
+    @Test(dataProvider = "StreamTestData<Integer>.small", dataProviderClass = StreamTestDataProvider.class)
     public void testSpliterators(String name, TestData.OfRef<Integer> data) {
         for (Function<Stream<Integer>, Stream<Integer>> f : streamFunctions()) {
             SpliteratorTestHelper.testSpliterator(() -> f.apply(data.stream()).spliterator());
         }
     }
 
-    @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
+    @Test(dataProvider = "StreamTestData<Integer>.small", dataProviderClass = StreamTestDataProvider.class)
     public void testParSpliterators(String name, TestData.OfRef<Integer> data) {
         for (Function<Stream<Integer>, Stream<Integer>> f : streamFunctions()) {
             SpliteratorTestHelper.testSpliterator(() -> f.apply(data.parallelStream()).spliterator());
@@ -323,7 +329,7 @@
         if (streamFunctions == null) {
             List<Function<Stream<Integer>, Stream<Integer>>> opFunctions = Arrays.asList(
                     s -> s.filter(pEven),
-                    s -> s.map(mDoubler),
+                    s -> s.flatMap(x -> Stream.of(x, x)),
                     // @@@ Add distinct once asserting results with or without order
                     //     is correctly supported
 //                    s -> s.distinct(),
@@ -378,7 +384,7 @@
         }
     }
 
-    @Test(dataProvider = "IntStreamTestData",
+    @Test(dataProvider = "IntStreamTestData.small",
           dataProviderClass = IntStreamTestDataProvider.class,
           groups = { "serialization-hostile" })
     public void testIntStreamSpliterators(String name, TestData.OfInt data) {
@@ -399,14 +405,14 @@
         }
     }
 
-    @Test(dataProvider = "IntStreamTestData", dataProviderClass = IntStreamTestDataProvider.class)
+    @Test(dataProvider = "IntStreamTestData.small", dataProviderClass = IntStreamTestDataProvider.class)
     public void testIntSpliterators(String name, TestData.OfInt data) {
         for (Function<IntStream, IntStream> f : intStreamFunctions()) {
             SpliteratorTestHelper.testIntSpliterator(() -> f.apply(data.stream()).spliterator());
         }
     }
 
-    @Test(dataProvider = "IntStreamTestData", dataProviderClass = IntStreamTestDataProvider.class)
+    @Test(dataProvider = "IntStreamTestData.small", dataProviderClass = IntStreamTestDataProvider.class)
     public void testIntParSpliterators(String name, TestData.OfInt data) {
         for (Function<IntStream, IntStream> f : intStreamFunctions()) {
             SpliteratorTestHelper.testIntSpliterator(() -> f.apply(data.parallelStream()).spliterator());
@@ -419,7 +425,7 @@
         if (intStreamFunctions == null) {
             List<Function<IntStream, IntStream>> opFunctions = Arrays.asList(
                     s -> s.filter(ipEven),
-                    s -> s.map(irDoubler),
+                    s -> s.flatMap(x -> IntStream.of(x, x)),
                     s -> s.sorted());
 
             intStreamFunctions = permuteStreamFunctions(opFunctions);
@@ -471,7 +477,7 @@
         }
     }
 
-    @Test(dataProvider = "LongStreamTestData",
+    @Test(dataProvider = "LongStreamTestData.small",
           dataProviderClass = LongStreamTestDataProvider.class,
           groups = { "serialization-hostile" })
     public void testLongStreamSpliterators(String name, TestData.OfLong data) {
@@ -492,14 +498,14 @@
         }
     }
 
-    @Test(dataProvider = "LongStreamTestData", dataProviderClass = LongStreamTestDataProvider.class)
+    @Test(dataProvider = "LongStreamTestData.small", dataProviderClass = LongStreamTestDataProvider.class)
     public void testLongSpliterators(String name, TestData.OfLong data) {
         for (Function<LongStream, LongStream> f : longStreamFunctions()) {
             SpliteratorTestHelper.testLongSpliterator(() -> f.apply(data.stream()).spliterator());
         }
     }
 
-    @Test(dataProvider = "LongStreamTestData", dataProviderClass = LongStreamTestDataProvider.class)
+    @Test(dataProvider = "LongStreamTestData.small", dataProviderClass = LongStreamTestDataProvider.class)
     public void testLongParSpliterators(String name, TestData.OfLong data) {
         for (Function<LongStream, LongStream> f : longStreamFunctions()) {
             SpliteratorTestHelper.testLongSpliterator(() -> f.apply(data.parallelStream()).spliterator());
@@ -512,7 +518,7 @@
         if (longStreamFunctions == null) {
             List<Function<LongStream, LongStream>> opFunctions = Arrays.asList(
                     s -> s.filter(lpEven),
-                    s -> s.map(x -> x * 2L),
+                    s -> s.flatMap(x -> LongStream.of(x, x)),
                     s -> s.sorted());
 
             longStreamFunctions = permuteStreamFunctions(opFunctions);
@@ -564,7 +570,7 @@
         }
     }
 
-    @Test(dataProvider = "DoubleStreamTestData",
+    @Test(dataProvider = "DoubleStreamTestData.small",
           dataProviderClass = DoubleStreamTestDataProvider.class,
           groups = { "serialization-hostile" })
     public void testDoubleStreamSpliterators(String name, TestData.OfDouble data) {
@@ -585,14 +591,14 @@
         }
     }
 
-    @Test(dataProvider = "DoubleStreamTestData", dataProviderClass = DoubleStreamTestDataProvider.class)
+    @Test(dataProvider = "DoubleStreamTestData.small", dataProviderClass = DoubleStreamTestDataProvider.class)
     public void testDoubleSpliterators(String name, TestData.OfDouble data) {
         for (Function<DoubleStream, DoubleStream> f : doubleStreamFunctions()) {
             SpliteratorTestHelper.testDoubleSpliterator(() -> f.apply(data.stream()).spliterator());
         }
     }
 
-    @Test(dataProvider = "DoubleStreamTestData", dataProviderClass = DoubleStreamTestDataProvider.class)
+    @Test(dataProvider = "DoubleStreamTestData.small", dataProviderClass = DoubleStreamTestDataProvider.class)
     public void testDoubleParSpliterators(String name, TestData.OfDouble data) {
         for (Function<DoubleStream, DoubleStream> f : doubleStreamFunctions()) {
             SpliteratorTestHelper.testDoubleSpliterator(() -> f.apply(data.parallelStream()).spliterator());
@@ -605,7 +611,7 @@
         if (doubleStreamFunctions == null) {
             List<Function<DoubleStream, DoubleStream>> opFunctions = Arrays.asList(
                     s -> s.filter(dpEven),
-                    s -> s.map(x -> x * 2.0),
+                    s -> s.flatMap(x -> DoubleStream.of(x, x)),
                     s -> s.sorted());
 
             doubleStreamFunctions = permuteStreamFunctions(opFunctions);