8148838: Stream.flatMap(...).spliterator() cannot properly split after tryAdvance()

Reviewed-by: psandoz
This commit is contained in:
Tagir F. Valeev 2016-02-08 15:33:53 +01:00
parent e41fea88e7
commit 0e250ec504
2 changed files with 25 additions and 19 deletions

View File

@ -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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -186,7 +186,7 @@ class StreamSpliterators {
@Override @Override
public Spliterator<P_OUT> trySplit() { public Spliterator<P_OUT> trySplit() {
if (isParallel && !finished) { if (isParallel && buffer == null && !finished) {
init(); init();
Spliterator<P_IN> split = spliterator.trySplit(); Spliterator<P_IN> split = spliterator.trySplit();

View File

@ -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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * 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 * or visit www.oracle.com if you need additional information or have any
* questions. * questions.
*/ */
/**
* @test
* @bug 8148838
*/
package org.openjdk.tests.java.util.stream; package org.openjdk.tests.java.util.stream;
import java.util.Arrays; import java.util.Arrays;
@ -282,7 +288,7 @@ public class StreamSpliteratorTest extends OpTestCase {
} }
} }
@Test(dataProvider = "StreamTestData<Integer>", @Test(dataProvider = "StreamTestData<Integer>.small",
dataProviderClass = StreamTestDataProvider.class, dataProviderClass = StreamTestDataProvider.class,
groups = { "serialization-hostile" }) groups = { "serialization-hostile" })
public void testStreamSpliterators(String name, TestData.OfRef<Integer> data) { public void testStreamSpliterators(String name, TestData.OfRef<Integer> data) {
@ -303,14 +309,14 @@ public class StreamSpliteratorTest extends OpTestCase {
} }
} }
@Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class) @Test(dataProvider = "StreamTestData<Integer>.small", dataProviderClass = StreamTestDataProvider.class)
public void testSpliterators(String name, TestData.OfRef<Integer> data) { public void testSpliterators(String name, TestData.OfRef<Integer> data) {
for (Function<Stream<Integer>, Stream<Integer>> f : streamFunctions()) { for (Function<Stream<Integer>, Stream<Integer>> f : streamFunctions()) {
SpliteratorTestHelper.testSpliterator(() -> f.apply(data.stream()).spliterator()); 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) { public void testParSpliterators(String name, TestData.OfRef<Integer> data) {
for (Function<Stream<Integer>, Stream<Integer>> f : streamFunctions()) { for (Function<Stream<Integer>, Stream<Integer>> f : streamFunctions()) {
SpliteratorTestHelper.testSpliterator(() -> f.apply(data.parallelStream()).spliterator()); SpliteratorTestHelper.testSpliterator(() -> f.apply(data.parallelStream()).spliterator());
@ -323,7 +329,7 @@ public class StreamSpliteratorTest extends OpTestCase {
if (streamFunctions == null) { if (streamFunctions == null) {
List<Function<Stream<Integer>, Stream<Integer>>> opFunctions = Arrays.asList( List<Function<Stream<Integer>, Stream<Integer>>> opFunctions = Arrays.asList(
s -> s.filter(pEven), 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 // @@@ Add distinct once asserting results with or without order
// is correctly supported // is correctly supported
// s -> s.distinct(), // s -> s.distinct(),
@ -378,7 +384,7 @@ public class StreamSpliteratorTest extends OpTestCase {
} }
} }
@Test(dataProvider = "IntStreamTestData", @Test(dataProvider = "IntStreamTestData.small",
dataProviderClass = IntStreamTestDataProvider.class, dataProviderClass = IntStreamTestDataProvider.class,
groups = { "serialization-hostile" }) groups = { "serialization-hostile" })
public void testIntStreamSpliterators(String name, TestData.OfInt data) { public void testIntStreamSpliterators(String name, TestData.OfInt data) {
@ -399,14 +405,14 @@ public class StreamSpliteratorTest extends OpTestCase {
} }
} }
@Test(dataProvider = "IntStreamTestData", dataProviderClass = IntStreamTestDataProvider.class) @Test(dataProvider = "IntStreamTestData.small", dataProviderClass = IntStreamTestDataProvider.class)
public void testIntSpliterators(String name, TestData.OfInt data) { public void testIntSpliterators(String name, TestData.OfInt data) {
for (Function<IntStream, IntStream> f : intStreamFunctions()) { for (Function<IntStream, IntStream> f : intStreamFunctions()) {
SpliteratorTestHelper.testIntSpliterator(() -> f.apply(data.stream()).spliterator()); 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) { public void testIntParSpliterators(String name, TestData.OfInt data) {
for (Function<IntStream, IntStream> f : intStreamFunctions()) { for (Function<IntStream, IntStream> f : intStreamFunctions()) {
SpliteratorTestHelper.testIntSpliterator(() -> f.apply(data.parallelStream()).spliterator()); SpliteratorTestHelper.testIntSpliterator(() -> f.apply(data.parallelStream()).spliterator());
@ -419,7 +425,7 @@ public class StreamSpliteratorTest extends OpTestCase {
if (intStreamFunctions == null) { if (intStreamFunctions == null) {
List<Function<IntStream, IntStream>> opFunctions = Arrays.asList( List<Function<IntStream, IntStream>> opFunctions = Arrays.asList(
s -> s.filter(ipEven), s -> s.filter(ipEven),
s -> s.map(irDoubler), s -> s.flatMap(x -> IntStream.of(x, x)),
s -> s.sorted()); s -> s.sorted());
intStreamFunctions = permuteStreamFunctions(opFunctions); intStreamFunctions = permuteStreamFunctions(opFunctions);
@ -471,7 +477,7 @@ public class StreamSpliteratorTest extends OpTestCase {
} }
} }
@Test(dataProvider = "LongStreamTestData", @Test(dataProvider = "LongStreamTestData.small",
dataProviderClass = LongStreamTestDataProvider.class, dataProviderClass = LongStreamTestDataProvider.class,
groups = { "serialization-hostile" }) groups = { "serialization-hostile" })
public void testLongStreamSpliterators(String name, TestData.OfLong data) { public void testLongStreamSpliterators(String name, TestData.OfLong data) {
@ -492,14 +498,14 @@ public class StreamSpliteratorTest extends OpTestCase {
} }
} }
@Test(dataProvider = "LongStreamTestData", dataProviderClass = LongStreamTestDataProvider.class) @Test(dataProvider = "LongStreamTestData.small", dataProviderClass = LongStreamTestDataProvider.class)
public void testLongSpliterators(String name, TestData.OfLong data) { public void testLongSpliterators(String name, TestData.OfLong data) {
for (Function<LongStream, LongStream> f : longStreamFunctions()) { for (Function<LongStream, LongStream> f : longStreamFunctions()) {
SpliteratorTestHelper.testLongSpliterator(() -> f.apply(data.stream()).spliterator()); 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) { public void testLongParSpliterators(String name, TestData.OfLong data) {
for (Function<LongStream, LongStream> f : longStreamFunctions()) { for (Function<LongStream, LongStream> f : longStreamFunctions()) {
SpliteratorTestHelper.testLongSpliterator(() -> f.apply(data.parallelStream()).spliterator()); SpliteratorTestHelper.testLongSpliterator(() -> f.apply(data.parallelStream()).spliterator());
@ -512,7 +518,7 @@ public class StreamSpliteratorTest extends OpTestCase {
if (longStreamFunctions == null) { if (longStreamFunctions == null) {
List<Function<LongStream, LongStream>> opFunctions = Arrays.asList( List<Function<LongStream, LongStream>> opFunctions = Arrays.asList(
s -> s.filter(lpEven), s -> s.filter(lpEven),
s -> s.map(x -> x * 2L), s -> s.flatMap(x -> LongStream.of(x, x)),
s -> s.sorted()); s -> s.sorted());
longStreamFunctions = permuteStreamFunctions(opFunctions); longStreamFunctions = permuteStreamFunctions(opFunctions);
@ -564,7 +570,7 @@ public class StreamSpliteratorTest extends OpTestCase {
} }
} }
@Test(dataProvider = "DoubleStreamTestData", @Test(dataProvider = "DoubleStreamTestData.small",
dataProviderClass = DoubleStreamTestDataProvider.class, dataProviderClass = DoubleStreamTestDataProvider.class,
groups = { "serialization-hostile" }) groups = { "serialization-hostile" })
public void testDoubleStreamSpliterators(String name, TestData.OfDouble data) { public void testDoubleStreamSpliterators(String name, TestData.OfDouble data) {
@ -585,14 +591,14 @@ public class StreamSpliteratorTest extends OpTestCase {
} }
} }
@Test(dataProvider = "DoubleStreamTestData", dataProviderClass = DoubleStreamTestDataProvider.class) @Test(dataProvider = "DoubleStreamTestData.small", dataProviderClass = DoubleStreamTestDataProvider.class)
public void testDoubleSpliterators(String name, TestData.OfDouble data) { public void testDoubleSpliterators(String name, TestData.OfDouble data) {
for (Function<DoubleStream, DoubleStream> f : doubleStreamFunctions()) { for (Function<DoubleStream, DoubleStream> f : doubleStreamFunctions()) {
SpliteratorTestHelper.testDoubleSpliterator(() -> f.apply(data.stream()).spliterator()); 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) { public void testDoubleParSpliterators(String name, TestData.OfDouble data) {
for (Function<DoubleStream, DoubleStream> f : doubleStreamFunctions()) { for (Function<DoubleStream, DoubleStream> f : doubleStreamFunctions()) {
SpliteratorTestHelper.testDoubleSpliterator(() -> f.apply(data.parallelStream()).spliterator()); SpliteratorTestHelper.testDoubleSpliterator(() -> f.apply(data.parallelStream()).spliterator());
@ -605,7 +611,7 @@ public class StreamSpliteratorTest extends OpTestCase {
if (doubleStreamFunctions == null) { if (doubleStreamFunctions == null) {
List<Function<DoubleStream, DoubleStream>> opFunctions = Arrays.asList( List<Function<DoubleStream, DoubleStream>> opFunctions = Arrays.asList(
s -> s.filter(dpEven), s -> s.filter(dpEven),
s -> s.map(x -> x * 2.0), s -> s.flatMap(x -> DoubleStream.of(x, x)),
s -> s.sorted()); s -> s.sorted());
doubleStreamFunctions = permuteStreamFunctions(opFunctions); doubleStreamFunctions = permuteStreamFunctions(opFunctions);