8193856: takeWhile produces incorrect result with elements produced by flatMap

Reviewed-by: smarks
This commit is contained in:
Paul Sandoz 2017-12-20 11:40:45 -08:00
parent 388f422efd
commit e6680338c5
2 changed files with 36 additions and 7 deletions

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2015, 2017, 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
@ -95,7 +95,7 @@ final class WhileOps {
@Override @Override
public void accept(T t) { public void accept(T t) {
if (take = predicate.test(t)) { if (take && (take = predicate.test(t))) {
downstream.accept(t); downstream.accept(t);
} }
} }
@ -152,7 +152,7 @@ final class WhileOps {
@Override @Override
public void accept(int t) { public void accept(int t) {
if (take = predicate.test(t)) { if (take && (take = predicate.test(t))) {
downstream.accept(t); downstream.accept(t);
} }
} }
@ -209,7 +209,7 @@ final class WhileOps {
@Override @Override
public void accept(long t) { public void accept(long t) {
if (take = predicate.test(t)) { if (take && (take = predicate.test(t))) {
downstream.accept(t); downstream.accept(t);
} }
} }
@ -266,7 +266,7 @@ final class WhileOps {
@Override @Override
public void accept(double t) { public void accept(double t) {
if (take = predicate.test(t)) { if (take && (take = predicate.test(t))) {
downstream.accept(t); downstream.accept(t);
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2015, 2017, 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
@ -46,7 +46,7 @@ import java.util.stream.TestData;
/* /*
* @test * @test
* @bug 8071597 * @bug 8071597 8193856
*/ */
@Test @Test
public class WhileOpTest extends OpTestCase { public class WhileOpTest extends OpTestCase {
@ -361,4 +361,33 @@ public class WhileOpTest extends OpTestCase {
} }
assertTrue(isClosed.get()); assertTrue(isClosed.get());
} }
@Test(groups = { "serialization-hostile" })
public void testFlatMapThenTake() {
TestData.OfRef<Integer> range = TestData.Factory.ofSupplier(
"range", () -> IntStream.range(0, 100).boxed());
exerciseOpsMulti(range,
// Reference result
s -> s.takeWhile(e -> e != 50),
// For other results collect into array,
// stream the single array (not the elements),
// then flat map to stream the array elements
s -> Stream.<Integer[]>of(s.toArray(Integer[]::new)).
flatMap(Stream::of).
takeWhile(e -> e != 50),
s -> Stream.of(s.mapToInt(e -> e).toArray()).
flatMapToInt(IntStream::of).
takeWhile(e -> e != 50).
mapToObj(e -> e),
s -> Stream.of(s.mapToLong(e -> e).toArray()).
flatMapToLong(LongStream::of).
takeWhile(e -> e != 50L).
mapToObj(e -> (int) e),
s -> Stream.of(s.mapToDouble(e -> e).toArray()).
flatMapToDouble(DoubleStream::of).
takeWhile(e -> e != 50.0).
mapToObj(e -> (int) e)
);
}
} }