# HG changeset patch # User henryjen # Date 1381170356 25200 # Node ID ab8ccd264bf3d049ddcebb05a65f3ef950b414bb # Parent 2e22d22c52ed12f0114add86e2eb2b688483fb0f 8025968: Integrate test improvements made in lambda repo Reviewed-by: psandoz diff -r 2e22d22c52ed -r ab8ccd264bf3 jdk/test/java/util/stream/bootlib/java/util/stream/OpTestCase.java --- a/jdk/test/java/util/stream/bootlib/java/util/stream/OpTestCase.java Mon Oct 07 11:48:55 2013 +0100 +++ b/jdk/test/java/util/stream/bootlib/java/util/stream/OpTestCase.java Mon Oct 07 11:25:56 2013 -0700 @@ -30,7 +30,6 @@ import java.util.Collections; import java.util.EnumMap; import java.util.EnumSet; -import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -41,7 +40,6 @@ import java.util.function.Consumer; import java.util.function.Function; -import org.testng.Assert; import org.testng.annotations.Test; /** @@ -397,14 +395,68 @@ // Exercise terminal operations - static enum TerminalTestScenario { - SINGLE_SEQUENTIAL, - SINGLE_SEQUENTIAL_SHORT_CIRCUIT, - SINGLE_PARALLEL, - ALL_SEQUENTIAL, - ALL_SEQUENTIAL_SHORT_CIRCUIT, - ALL_PARALLEL, - ALL_PARALLEL_SEQUENTIAL, + interface BaseTerminalTestScenario> { + boolean requiresSingleStageSource(); + + boolean requiresParallelSource(); + + default R run(Function terminalF, S_OUT source, StreamShape shape) { + return terminalF.apply(source); + } + } + + @SuppressWarnings({"rawtypes", "unchecked"}) + static enum TerminalTestScenario implements BaseTerminalTestScenario { + SINGLE_SEQUENTIAL(true, false), + + SINGLE_SEQUENTIAL_SHORT_CIRCUIT(true, false) { + @Override + public Object run(Function terminalF, BaseStream source, StreamShape shape) { + source = (BaseStream) chain(source, new ShortCircuitOp(shape)); + return terminalF.apply(source); + } + }, + + SINGLE_PARALLEL(true, true), + + ALL_SEQUENTIAL(false, false), + + ALL_SEQUENTIAL_SHORT_CIRCUIT(false, false) { + @Override + public Object run(Function terminalF, BaseStream source, StreamShape shape) { + source = (BaseStream) chain(source, new ShortCircuitOp(shape)); + return terminalF.apply(source); + } + }, + + ALL_PARALLEL(false, true), + + ALL_PARALLEL_SEQUENTIAL(false, false) { + @Override + public Object run(Function terminalF, BaseStream source, StreamShape shape) { + return terminalF.apply(source.sequential()); + } + }, + ; + + private final boolean requiresSingleStageSource; + private final boolean isParallel; + + TerminalTestScenario(boolean requiresSingleStageSource, boolean isParallel) { + this.requiresSingleStageSource = requiresSingleStageSource; + this.isParallel = isParallel; + } + + @Override + public boolean requiresSingleStageSource() { + return requiresSingleStageSource; + } + + @Override + public boolean requiresParallelSource() { + return isParallel; + } + } @SuppressWarnings({"rawtypes", "unchecked"}) @@ -415,8 +467,6 @@ R refResult; - Set testSet = EnumSet.allOf(TerminalTestScenario.class); - ResultAsserter resultAsserter = (act, exp, ord, par) -> LambdaTestHelpers.assertContentsEqual(act, exp); private ExerciseDataTerminalBuilder(TestData data, Function streamF, Function terminalF) { @@ -442,27 +492,6 @@ return this; } - public ExerciseDataTerminalBuilder without(TerminalTestScenario... tests) { - return without(Arrays.asList(tests)); - } - - public ExerciseDataTerminalBuilder without(Collection tests) { - testSet.removeAll(tests); - if (testSet.isEmpty()) { - throw new IllegalStateException("Terminal test scenario set is empty"); - } - return this; - } - - public ExerciseDataTerminalBuilder with(TerminalTestScenario... tests) { - return with(Arrays.asList(tests)); - } - - public ExerciseDataTerminalBuilder with(Collection tests) { - testSet.addAll(tests); - return this; - } - // Build method public R exercise() { @@ -471,70 +500,36 @@ boolean isOrdered = StreamOpFlag.ORDERED.isKnown(ap.getStreamFlags()); StreamShape shape = ap.getOutputShape(); + EnumSet tests = EnumSet.allOf(TerminalTestScenario.class); + // Sequentially collect the output that will be input to the terminal op Node node = ap.evaluateToArrayNode(size -> (U[]) new Object[size]); if (refResult == null) { - // Sequentially collect the output that will be input to the terminal op - refResult = terminalF.apply((S_OUT) createPipeline(shape, node.spliterator(), - StreamOpFlag.IS_ORDERED | StreamOpFlag.IS_SIZED, - false)); - } else if (testSet.contains(TerminalTestScenario.SINGLE_SEQUENTIAL)) { - S_OUT source = (S_OUT) createPipeline(shape, node.spliterator(), - StreamOpFlag.IS_ORDERED | StreamOpFlag.IS_SIZED, - false); - R result = terminalF.apply(source); - LambdaTestHelpers.launderAssertion(() -> resultAsserter.assertResult(result, refResult, isOrdered, false), - () -> String.format("Single sequential: %s != %s", refResult, result)); - } - - if (testSet.contains(TerminalTestScenario.SINGLE_SEQUENTIAL_SHORT_CIRCUIT)) { + // Induce the reference result S_OUT source = (S_OUT) createPipeline(shape, node.spliterator(), StreamOpFlag.IS_ORDERED | StreamOpFlag.IS_SIZED, false); - // Force short-circuit - source = (S_OUT) chain(source, new ShortCircuitOp(shape)); - R result = terminalF.apply(source); - LambdaTestHelpers.launderAssertion(() -> resultAsserter.assertResult(result, refResult, isOrdered, false), - () -> String.format("Single sequential pull: %s != %s", refResult, result)); - } - if (testSet.contains(TerminalTestScenario.SINGLE_PARALLEL)) { - S_OUT source = (S_OUT) createPipeline(shape, node.spliterator(), - StreamOpFlag.IS_ORDERED | StreamOpFlag.IS_SIZED, - true); - R result = terminalF.apply(source); - LambdaTestHelpers.launderAssertion(() -> resultAsserter.assertResult(result, refResult, isOrdered, true), - () -> String.format("Single parallel: %s != %s", refResult, result)); + refResult = (R) TerminalTestScenario.SINGLE_SEQUENTIAL.run(terminalF, source, shape); + tests.remove(TerminalTestScenario.SINGLE_SEQUENTIAL); } - if (testSet.contains(TerminalTestScenario.ALL_SEQUENTIAL)) { - // This may forEach or tryAdvance depending on the terminal op implementation - S_OUT source = streamF.apply(data.stream()); - R result = terminalF.apply(source); - LambdaTestHelpers.launderAssertion(() -> resultAsserter.assertResult(result, refResult, isOrdered, false), - () -> String.format("All sequential: %s != %s", refResult, result)); - } + for (BaseTerminalTestScenario test : tests) { + S_OUT source; + if (test.requiresSingleStageSource()) { + source = (S_OUT) createPipeline(shape, node.spliterator(), + StreamOpFlag.IS_ORDERED | StreamOpFlag.IS_SIZED, + test.requiresParallelSource()); + } + else { + source = streamF.apply(test.requiresParallelSource() + ? data.parallelStream() : data.stream()); + } - if (testSet.contains(TerminalTestScenario.ALL_SEQUENTIAL_SHORT_CIRCUIT)) { - S_OUT source = streamF.apply(data.stream()); - // Force short-circuit - source = (S_OUT) chain(source, new ShortCircuitOp(shape)); - R result = terminalF.apply(source); - LambdaTestHelpers.launderAssertion(() -> resultAsserter.assertResult(result, refResult, isOrdered, false), - () -> String.format("All sequential pull: %s != %s", refResult, result)); - } + R result = (R) test.run(terminalF, source, shape); - if (testSet.contains(TerminalTestScenario.ALL_PARALLEL)) { - S_OUT source = streamF.apply(data.parallelStream()); - R result = terminalF.apply(source); - LambdaTestHelpers.launderAssertion(() -> resultAsserter.assertResult(result, refResult, isOrdered, true), - () -> String.format("All parallel: %s != %s", refResult, result)); - } - - if (testSet.contains(TerminalTestScenario.ALL_PARALLEL_SEQUENTIAL)) { - S_OUT source = streamF.apply(data.parallelStream()); - R result = terminalF.apply(source.sequential()); - LambdaTestHelpers.launderAssertion(() -> resultAsserter.assertResult(result, refResult, isOrdered, false), - () -> String.format("All parallel then sequential: %s != %s", refResult, result)); + LambdaTestHelpers.launderAssertion( + () -> resultAsserter.assertResult(result, refResult, isOrdered, test.requiresParallelSource()), + () -> String.format("%s: %s != %s", test, refResult, result)); } return refResult; diff -r 2e22d22c52ed -r ab8ccd264bf3 jdk/test/java/util/stream/test/org/openjdk/tests/java/util/stream/ExplodeOpTest.java --- a/jdk/test/java/util/stream/test/org/openjdk/tests/java/util/stream/ExplodeOpTest.java Mon Oct 07 11:48:55 2013 +0100 +++ b/jdk/test/java/util/stream/test/org/openjdk/tests/java/util/stream/ExplodeOpTest.java Mon Oct 07 11:25:56 2013 -0700 @@ -64,6 +64,9 @@ result = exerciseOps(data, s -> s.flatMap(mfNull)); assertEquals(0, result.size()); + result = exerciseOps(data, s-> s.flatMap(e -> Stream.empty())); + assertEquals(0, result.size()); + exerciseOps(data, s -> s.flatMap(mfLt)); exerciseOps(data, s -> s.flatMap(integerRangeMapper)); exerciseOps(data, s -> s.flatMap((Integer e) -> IntStream.range(0, e).boxed().limit(10))); diff -r 2e22d22c52ed -r ab8ccd264bf3 jdk/test/java/util/stream/test/org/openjdk/tests/java/util/stream/TabulatorsTest.java --- a/jdk/test/java/util/stream/test/org/openjdk/tests/java/util/stream/TabulatorsTest.java Mon Oct 07 11:48:55 2013 +0100 +++ b/jdk/test/java/util/stream/test/org/openjdk/tests/java/util/stream/TabulatorsTest.java Mon Oct 07 11:25:56 2013 -0700 @@ -609,7 +609,7 @@ @Test(dataProvider = "StreamTestData", dataProviderClass = StreamTestDataProvider.class) public void testComposeFinisher(String name, TestData.OfRef data) throws ReflectiveOperationException { List asList = exerciseTerminalOps(data, s -> s.collect(toList())); - List asImmutableList = exerciseTerminalOps(data, s -> s.collect(collectingAndThen(toList(), Collections::unmodifiableList))); + List asImmutableList = exerciseTerminalOps(data, s -> s.collect(collectingAndThen(toList(), Collections::unmodifiableList))); assertEquals(asList, asImmutableList); try { asImmutableList.add(0);