test/jdk/lib/testlibrary/bootlib/java.base/java/util/stream/OpTestCase.java
author herrick
Thu, 01 Nov 2018 08:52:50 -0400
branchJDK-8200758-branch
changeset 57013 0ece1b6f0958
parent 47216 71c04702a3d5
permissions -rw-r--r--
8213163: --app-image arg does not work creating exe installers Reviewed-by: almatvee
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17442
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
     1
/*
31248
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
     2
 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
17442
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
     4
 *
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
     7
 * published by the Free Software Foundation.
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
     8
 *
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    13
 * accompanied this code).
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    14
 *
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    18
 *
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    21
 * questions.
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    22
 */
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    23
package java.util.stream;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    24
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    25
import java.io.PrintWriter;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    26
import java.io.StringWriter;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    27
import java.util.ArrayList;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    28
import java.util.Arrays;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    29
import java.util.Collection;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    30
import java.util.Collections;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    31
import java.util.EnumMap;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    32
import java.util.EnumSet;
31248
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
    33
import java.util.HashMap;
17442
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    34
import java.util.HashSet;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    35
import java.util.List;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    36
import java.util.Map;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    37
import java.util.Objects;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    38
import java.util.Set;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    39
import java.util.Spliterator;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    40
import java.util.function.BiConsumer;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    41
import java.util.function.Consumer;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    42
import java.util.function.Function;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    43
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    44
import org.testng.annotations.Test;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    45
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    46
/**
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    47
 * Base class for streams test cases.  Provides 'exercise' methods for taking
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    48
 * lambdas that construct and modify streams, and evaluates them in different
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    49
 * ways and asserts that they produce equivalent results.
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    50
 */
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    51
@Test
18529
930f51c90ea7 8016455: Sync stream tests from lambda to tl
psandoz
parents: 17442
diff changeset
    52
public abstract class OpTestCase extends LoggingTestCase {
17442
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    53
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    54
    private final Map<StreamShape, Set<? extends BaseStreamTestScenario>> testScenarios;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    55
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    56
    protected OpTestCase() {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    57
        testScenarios = new EnumMap<>(StreamShape.class);
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    58
        testScenarios.put(StreamShape.REFERENCE, Collections.unmodifiableSet(EnumSet.allOf(StreamTestScenario.class)));
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    59
        testScenarios.put(StreamShape.INT_VALUE, Collections.unmodifiableSet(EnumSet.allOf(IntStreamTestScenario.class)));
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    60
        testScenarios.put(StreamShape.LONG_VALUE, Collections.unmodifiableSet(EnumSet.allOf(LongStreamTestScenario.class)));
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    61
        testScenarios.put(StreamShape.DOUBLE_VALUE, Collections.unmodifiableSet(EnumSet.allOf(DoubleStreamTestScenario.class)));
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    62
    }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    63
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    64
    @SuppressWarnings("rawtypes")
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    65
    public static int getStreamFlags(BaseStream s) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    66
        return ((AbstractPipeline) s).getStreamFlags();
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    67
    }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    68
18529
930f51c90ea7 8016455: Sync stream tests from lambda to tl
psandoz
parents: 17442
diff changeset
    69
    /**
930f51c90ea7 8016455: Sync stream tests from lambda to tl
psandoz
parents: 17442
diff changeset
    70
     * An asserter for results produced when exercising of stream or terminal
930f51c90ea7 8016455: Sync stream tests from lambda to tl
psandoz
parents: 17442
diff changeset
    71
     * tests.
930f51c90ea7 8016455: Sync stream tests from lambda to tl
psandoz
parents: 17442
diff changeset
    72
     *
930f51c90ea7 8016455: Sync stream tests from lambda to tl
psandoz
parents: 17442
diff changeset
    73
     * @param <R> the type of result to assert on
930f51c90ea7 8016455: Sync stream tests from lambda to tl
psandoz
parents: 17442
diff changeset
    74
     */
930f51c90ea7 8016455: Sync stream tests from lambda to tl
psandoz
parents: 17442
diff changeset
    75
    public interface ResultAsserter<R> {
930f51c90ea7 8016455: Sync stream tests from lambda to tl
psandoz
parents: 17442
diff changeset
    76
        /**
930f51c90ea7 8016455: Sync stream tests from lambda to tl
psandoz
parents: 17442
diff changeset
    77
         * Assert a result produced when exercising of stream or terminal
930f51c90ea7 8016455: Sync stream tests from lambda to tl
psandoz
parents: 17442
diff changeset
    78
         * test.
930f51c90ea7 8016455: Sync stream tests from lambda to tl
psandoz
parents: 17442
diff changeset
    79
         *
930f51c90ea7 8016455: Sync stream tests from lambda to tl
psandoz
parents: 17442
diff changeset
    80
         * @param actual the actual result
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 18529
diff changeset
    81
         * @param expected the expected result
18529
930f51c90ea7 8016455: Sync stream tests from lambda to tl
psandoz
parents: 17442
diff changeset
    82
         * @param isOrdered true if the pipeline is ordered
930f51c90ea7 8016455: Sync stream tests from lambda to tl
psandoz
parents: 17442
diff changeset
    83
         * @param isParallel true if the pipeline is parallel
930f51c90ea7 8016455: Sync stream tests from lambda to tl
psandoz
parents: 17442
diff changeset
    84
         */
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 18529
diff changeset
    85
        void assertResult(R actual, R expected, boolean isOrdered, boolean isParallel);
18529
930f51c90ea7 8016455: Sync stream tests from lambda to tl
psandoz
parents: 17442
diff changeset
    86
    }
930f51c90ea7 8016455: Sync stream tests from lambda to tl
psandoz
parents: 17442
diff changeset
    87
17442
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    88
    // Exercise stream operations
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    89
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    90
    public interface BaseStreamTestScenario {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    91
        StreamShape getShape();
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    92
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    93
        boolean isParallel();
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
    94
31248
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
    95
        boolean isOrdered();
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
    96
31258
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 31248
diff changeset
    97
        default <T, S_IN extends BaseStream<T, S_IN>>
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 31248
diff changeset
    98
        S_IN getStream(TestData<T, S_IN> data) {
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 31248
diff changeset
    99
            return isParallel()
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 31248
diff changeset
   100
                   ? data.parallelStream()
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 31248
diff changeset
   101
                   : data.stream();
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 31248
diff changeset
   102
        }
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 31248
diff changeset
   103
31248
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   104
        <T, U, S_IN extends BaseStream<T, S_IN>, S_OUT extends BaseStream<U, S_OUT>>
17442
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   105
        void run(TestData<T, S_IN> data, Consumer<U> b, Function<S_IN, S_OUT> m);
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   106
    }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   107
31248
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   108
    protected <T, U, S_IN extends BaseStream<T, S_IN>, S_OUT extends BaseStream<U, S_OUT>>
17442
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   109
    Collection<U> exerciseOps(TestData<T, S_IN> data, Function<S_IN, S_OUT> m) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   110
        return withData(data).stream(m).exercise();
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   111
    }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   112
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   113
    // Run multiple versions of exercise(), returning the result of the first, and asserting that others return the same result
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   114
    // If the first version is s -> s.foo(), can be used with s -> s.mapToInt(i -> i).foo().mapToObj(i -> i) to test all shape variants
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   115
    @SafeVarargs
31248
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   116
    protected final<T, U, S_IN extends BaseStream<T, S_IN>, S_OUT extends BaseStream<U, S_OUT>>
17442
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   117
    Collection<U> exerciseOpsMulti(TestData<T, S_IN> data,
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   118
                                   Function<S_IN, S_OUT>... ms) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   119
        Collection<U> result = null;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   120
        for (Function<S_IN, S_OUT> m : ms) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   121
            if (result == null)
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   122
                result = withData(data).stream(m).exercise();
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   123
            else {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   124
                Collection<U> r2 = withData(data).stream(m).exercise();
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   125
                assertEquals(result, r2);
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   126
            }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   127
        }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   128
        return result;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   129
    }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   130
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   131
    // Run multiple versions of exercise() for an Integer stream, returning the result of the first, and asserting that others return the same result
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   132
    // Automates the conversion between Stream<Integer> and {Int,Long,Double}Stream and back, so client sites look like you are passing the same
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   133
    // lambda four times, but in fact they are four different lambdas since they are transforming four different kinds of streams
31248
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   134
    protected final
17442
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   135
    Collection<Integer> exerciseOpsInt(TestData.OfRef<Integer> data,
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   136
                                       Function<Stream<Integer>, Stream<Integer>> mRef,
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   137
                                       Function<IntStream, IntStream> mInt,
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   138
                                       Function<LongStream, LongStream> mLong,
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   139
                                       Function<DoubleStream, DoubleStream> mDouble) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   140
        @SuppressWarnings({ "rawtypes", "unchecked" })
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   141
        Function<Stream<Integer>, Stream<Integer>>[] ms = new Function[4];
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   142
        ms[0] = mRef;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   143
        ms[1] = s -> mInt.apply(s.mapToInt(e -> e)).mapToObj(e -> e);
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   144
        ms[2] = s -> mLong.apply(s.mapToLong(e -> e)).mapToObj(e -> (int) e);
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   145
        ms[3] = s -> mDouble.apply(s.mapToDouble(e -> e)).mapToObj(e -> (int) e);
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   146
        return exerciseOpsMulti(data, ms);
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   147
    }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   148
31248
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   149
    // Run multiple versions of exercise() with multiple terminal operations for all kinds of stream, , and asserting against the expected result
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   150
    // If the first version is s -> s.foo(), can be used with s -> s.mapToInt(i -> i).foo().mapToObj(i -> i) to test all shape variants
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   151
    protected final<T, U, R, S_IN extends BaseStream<T, S_IN>, S_OUT extends BaseStream<U, S_OUT>>
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   152
    void exerciseTerminalOpsMulti(TestData<T, S_IN> data,
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   153
                                  R expected,
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   154
                                  Map<String, Function<S_IN, S_OUT>> streams,
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   155
                                  Map<String, Function<S_OUT, R>> terminals) {
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   156
        for (Map.Entry<String, Function<S_IN, S_OUT>> se : streams.entrySet()) {
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   157
            setContext("Intermediate stream", se.getKey());
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   158
            for (Map.Entry<String, Function<S_OUT, R>> te : terminals.entrySet()) {
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   159
                setContext("Terminal stream", te.getKey());
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   160
                withData(data)
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   161
                        .terminal(se.getValue(), te.getValue())
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   162
                        .expectedResult(expected)
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   163
                        .exercise();
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   164
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   165
            }
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   166
        }
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   167
    }
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   168
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   169
    // Run multiple versions of exercise() with multiple terminal operation for all kinds of stream, and asserting against the expected result
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   170
    // Automates the conversion between Stream<Integer> and {Int,Long,Double}Stream and back, so client sites look like you are passing the same
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   171
    // lambda four times, but in fact they are four different lambdas since they are transforming four different kinds of streams
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   172
    protected final
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   173
    void exerciseTerminalOpsInt(TestData<Integer, Stream<Integer>> data,
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   174
                                Collection<Integer> expected,
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   175
                                String desc,
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   176
                                Function<Stream<Integer>, Stream<Integer>> mRef,
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   177
                                Function<IntStream, IntStream> mInt,
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   178
                                Function<LongStream, LongStream> mLong,
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   179
                                Function<DoubleStream, DoubleStream> mDouble,
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   180
                                Map<String, Function<Stream<Integer>, Collection<Integer>>> terminals) {
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   181
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   182
        Map<String, Function<Stream<Integer>, Stream<Integer>>> m = new HashMap<>();
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   183
        m.put("Ref " + desc, mRef);
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   184
        m.put("Int " + desc, s -> mInt.apply(s.mapToInt(e -> e)).mapToObj(e -> e));
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   185
        m.put("Long " + desc, s -> mLong.apply(s.mapToLong(e -> e)).mapToObj(e -> (int) e));
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   186
        m.put("Double " + desc, s -> mDouble.apply(s.mapToDouble(e -> e)).mapToObj(e -> (int) e));
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   187
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   188
        exerciseTerminalOpsMulti(data, expected, m, terminals);
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   189
    }
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   190
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   191
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   192
    protected <T, U, S_OUT extends BaseStream<U, S_OUT>>
17442
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   193
    Collection<U> exerciseOps(Collection<T> data, Function<Stream<T>, S_OUT> m) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   194
        TestData.OfRef<T> data1 = TestData.Factory.ofCollection("Collection of type " + data.getClass().getName(), data);
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   195
        return withData(data1).stream(m).exercise();
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   196
    }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   197
31248
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   198
    protected <T, U, S_OUT extends BaseStream<U, S_OUT>, I extends Iterable<U>>
17442
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   199
    Collection<U> exerciseOps(Collection<T> data, Function<Stream<T>, S_OUT> m, I expected) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   200
        TestData.OfRef<T> data1 = TestData.Factory.ofCollection("Collection of type " + data.getClass().getName(), data);
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   201
        return withData(data1).stream(m).expectedResult(expected).exercise();
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   202
    }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   203
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   204
    @SuppressWarnings("unchecked")
31248
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   205
    protected <U, S_OUT extends BaseStream<U, S_OUT>>
17442
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   206
    Collection<U> exerciseOps(int[] data, Function<IntStream, S_OUT> m) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   207
        return withData(TestData.Factory.ofArray("int array", data)).stream(m).exercise();
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   208
    }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   209
31248
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   210
    protected Collection<Integer> exerciseOps(int[] data, Function<IntStream, IntStream> m, int[] expected) {
17442
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   211
        TestData.OfInt data1 = TestData.Factory.ofArray("int array", data);
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   212
        return withData(data1).stream(m).expectedResult(expected).exercise();
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   213
    }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   214
31248
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   215
    protected <T, S_IN extends BaseStream<T, S_IN>> DataStreamBuilder<T, S_IN> withData(TestData<T, S_IN> data) {
17442
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   216
        Objects.requireNonNull(data);
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   217
        return new DataStreamBuilder<>(data);
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   218
    }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   219
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   220
    @SuppressWarnings({"rawtypes", "unchecked"})
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   221
    public class DataStreamBuilder<T, S_IN extends BaseStream<T, S_IN>> {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   222
        final TestData<T, S_IN> data;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   223
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   224
        private DataStreamBuilder(TestData<T, S_IN> data) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   225
            this.data = Objects.requireNonNull(data);
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   226
        }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   227
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   228
        public <U, S_OUT extends BaseStream<U, S_OUT>>
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   229
        ExerciseDataStreamBuilder<T, U, S_IN, S_OUT> ops(IntermediateTestOp... ops) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   230
            return new ExerciseDataStreamBuilder<>(data, (S_IN s) -> (S_OUT) chain(s, ops));
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   231
        }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   232
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   233
        public <U, S_OUT extends BaseStream<U, S_OUT>> ExerciseDataStreamBuilder<T, U, S_IN, S_OUT>
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   234
        stream(Function<S_IN, S_OUT> m) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   235
            return new ExerciseDataStreamBuilder<>(data, m);
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   236
        }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   237
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   238
        public <U, S_OUT extends BaseStream<U, S_OUT>> ExerciseDataStreamBuilder<T, U, S_IN, S_OUT>
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   239
        stream(Function<S_IN, S_OUT> m, IntermediateTestOp<U, U> additionalOp) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   240
            return new ExerciseDataStreamBuilder<>(data, s -> (S_OUT) chain(m.apply(s), additionalOp));
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   241
        }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   242
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   243
        public <R> ExerciseDataTerminalBuilder<T, T, R, S_IN, S_IN>
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   244
        terminal(Function<S_IN, R> terminalF) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   245
            return new ExerciseDataTerminalBuilder<>(data, s -> s, terminalF);
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   246
        }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   247
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   248
        public <U, R, S_OUT extends BaseStream<U, S_OUT>> ExerciseDataTerminalBuilder<T, U, R, S_IN, S_OUT>
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   249
        terminal(Function<S_IN, S_OUT> streamF, Function<S_OUT, R> terminalF) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   250
            return new ExerciseDataTerminalBuilder<>(data, streamF, terminalF);
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   251
        }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   252
    }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   253
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   254
    @SuppressWarnings({"rawtypes", "unchecked"})
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   255
    public class ExerciseDataStreamBuilder<T, U, S_IN extends BaseStream<T, S_IN>, S_OUT extends BaseStream<U, S_OUT>> {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   256
        final TestData<T, S_IN> data;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   257
        final Function<S_IN, S_OUT> m;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   258
        final StreamShape shape;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   259
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   260
        Set<BaseStreamTestScenario> testSet = new HashSet<>();
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   261
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   262
        Collection<U> refResult;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   263
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   264
        Consumer<TestData<T, S_IN>> before = LambdaTestHelpers.bEmpty;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   265
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   266
        Consumer<TestData<T, S_IN>> after = LambdaTestHelpers.bEmpty;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   267
18529
930f51c90ea7 8016455: Sync stream tests from lambda to tl
psandoz
parents: 17442
diff changeset
   268
        ResultAsserter<Iterable<U>> resultAsserter = (act, exp, ord, par) -> {
930f51c90ea7 8016455: Sync stream tests from lambda to tl
psandoz
parents: 17442
diff changeset
   269
            if (par & !ord) {
930f51c90ea7 8016455: Sync stream tests from lambda to tl
psandoz
parents: 17442
diff changeset
   270
                LambdaTestHelpers.assertContentsUnordered(act, exp);
930f51c90ea7 8016455: Sync stream tests from lambda to tl
psandoz
parents: 17442
diff changeset
   271
            }
930f51c90ea7 8016455: Sync stream tests from lambda to tl
psandoz
parents: 17442
diff changeset
   272
            else {
930f51c90ea7 8016455: Sync stream tests from lambda to tl
psandoz
parents: 17442
diff changeset
   273
                LambdaTestHelpers.assertContentsEqual(act, exp);
930f51c90ea7 8016455: Sync stream tests from lambda to tl
psandoz
parents: 17442
diff changeset
   274
            }
930f51c90ea7 8016455: Sync stream tests from lambda to tl
psandoz
parents: 17442
diff changeset
   275
        };
17442
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   276
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   277
        private ExerciseDataStreamBuilder(TestData<T, S_IN> data, Function<S_IN, S_OUT> m) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   278
            this.data = data;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   279
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   280
            this.m = Objects.requireNonNull(m);
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   281
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   282
            this.shape = ((AbstractPipeline<?, U, ?>) m.apply(data.stream())).getOutputShape();
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   283
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   284
            // Have to initiate from the output shape of the last stream
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   285
            // This means the stream mapper is required first rather than last
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   286
            testSet.addAll(testScenarios.get(shape));
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   287
        }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   288
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   289
        //
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   290
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   291
        public <I extends Iterable<U>> ExerciseDataStreamBuilder<T, U, S_IN, S_OUT> expectedResult(I expectedResult) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   292
            List<U> l = new ArrayList<>();
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   293
            expectedResult.forEach(l::add);
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   294
            refResult = l;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   295
            return this;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   296
        }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   297
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   298
        public ExerciseDataStreamBuilder<T, U, S_IN, S_OUT> expectedResult(int[] expectedResult) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   299
            List l = new ArrayList();
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   300
            for (int anExpectedResult : expectedResult) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   301
                l.add(anExpectedResult);
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   302
            }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   303
            refResult = l;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   304
            return this;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   305
        }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   306
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   307
        public ExerciseDataStreamBuilder<T, U, S_IN, S_OUT> expectedResult(long[] expectedResult) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   308
            List l = new ArrayList();
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   309
            for (long anExpectedResult : expectedResult) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   310
                l.add(anExpectedResult);
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   311
            }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   312
            refResult = l;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   313
            return this;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   314
        }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   315
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   316
        public ExerciseDataStreamBuilder<T, U, S_IN, S_OUT> expectedResult(double[] expectedResult) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   317
            List l = new ArrayList();
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   318
            for (double anExpectedResult : expectedResult) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   319
                l.add(anExpectedResult);
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   320
            }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   321
            refResult = l;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   322
            return this;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   323
        }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   324
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   325
        public ExerciseDataStreamBuilder<T, U, S_IN, S_OUT> before(Consumer<TestData<T, S_IN>> before) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   326
            this.before = Objects.requireNonNull(before);
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   327
            return this;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   328
        }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   329
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   330
        public ExerciseDataStreamBuilder<T, U, S_IN, S_OUT> after(Consumer<TestData<T, S_IN>> after) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   331
            this.after = Objects.requireNonNull(after);
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   332
            return this;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   333
        }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   334
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   335
        public ExerciseDataStreamBuilder<T, U, S_IN, S_OUT> without(BaseStreamTestScenario... tests) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   336
            return without(Arrays.asList(tests));
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   337
        }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   338
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   339
        public ExerciseDataStreamBuilder<T, U, S_IN, S_OUT> without(Collection<? extends BaseStreamTestScenario> tests) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   340
            for (BaseStreamTestScenario ts : tests) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   341
                if (ts.getShape() == shape) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   342
                    testSet.remove(ts);
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   343
                }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   344
            }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   345
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   346
            if (testSet.isEmpty()) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   347
                throw new IllegalStateException("Test scenario set is empty");
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   348
            }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   349
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   350
            return this;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   351
        }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   352
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   353
        public ExerciseDataStreamBuilder<T, U, S_IN, S_OUT> with(BaseStreamTestScenario... tests) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   354
            return with(Arrays.asList(tests));
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   355
        }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   356
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   357
        public ExerciseDataStreamBuilder<T, U, S_IN, S_OUT> with(Collection<? extends BaseStreamTestScenario> tests) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   358
            testSet = new HashSet<>();
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   359
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   360
            for (BaseStreamTestScenario ts : tests) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   361
                if (ts.getShape() == shape) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   362
                    testSet.add(ts);
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   363
                }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   364
            }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   365
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   366
            if (testSet.isEmpty()) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   367
                throw new IllegalStateException("Test scenario set is empty");
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   368
            }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   369
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   370
            return this;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   371
        }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   372
18529
930f51c90ea7 8016455: Sync stream tests from lambda to tl
psandoz
parents: 17442
diff changeset
   373
        public ExerciseDataStreamBuilder<T, U, S_IN, S_OUT> resultAsserter(ResultAsserter<Iterable<U>> resultAsserter) {
930f51c90ea7 8016455: Sync stream tests from lambda to tl
psandoz
parents: 17442
diff changeset
   374
            this.resultAsserter = resultAsserter;
17442
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   375
            return this;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   376
        }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   377
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   378
        // Build method
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   379
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   380
        public Collection<U> exercise() {
31248
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   381
            final boolean isStreamOrdered;
17442
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   382
            if (refResult == null) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   383
                // Induce the reference result
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   384
                before.accept(data);
31258
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 31248
diff changeset
   385
                try (S_OUT sOut = m.apply(data.stream())) {
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 31248
diff changeset
   386
                    isStreamOrdered = StreamOpFlag.ORDERED.isKnown(((AbstractPipeline) sOut).getStreamFlags());
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 31248
diff changeset
   387
                    Node<U> refNodeResult = ((AbstractPipeline<?, U, ?>) sOut).evaluateToArrayNode(size -> (U[]) new Object[size]);
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 31248
diff changeset
   388
                    refResult = LambdaTestHelpers.toBoxedList(refNodeResult.spliterator());
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 31248
diff changeset
   389
                }
17442
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   390
                after.accept(data);
18529
930f51c90ea7 8016455: Sync stream tests from lambda to tl
psandoz
parents: 17442
diff changeset
   391
            }
930f51c90ea7 8016455: Sync stream tests from lambda to tl
psandoz
parents: 17442
diff changeset
   392
            else {
31258
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 31248
diff changeset
   393
                try (S_OUT sOut = m.apply(data.stream())) {
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 31248
diff changeset
   394
                    isStreamOrdered = StreamOpFlag.ORDERED.isKnown(((AbstractPipeline) sOut).getStreamFlags());
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 31248
diff changeset
   395
                }
17442
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   396
            }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   397
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   398
            List<Error> errors = new ArrayList<>();
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   399
            for (BaseStreamTestScenario test : testSet) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   400
                try {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   401
                    before.accept(data);
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   402
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   403
                    List<U> result = new ArrayList<>();
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   404
                    test.run(data, LambdaTestHelpers.<U>toBoxingConsumer(result::add), m);
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   405
31248
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   406
                    Runnable asserter = () -> resultAsserter.assertResult(result, refResult, isStreamOrdered && test.isOrdered(), test.isParallel());
18529
930f51c90ea7 8016455: Sync stream tests from lambda to tl
psandoz
parents: 17442
diff changeset
   407
930f51c90ea7 8016455: Sync stream tests from lambda to tl
psandoz
parents: 17442
diff changeset
   408
                    if (refResult.size() > 1000) {
930f51c90ea7 8016455: Sync stream tests from lambda to tl
psandoz
parents: 17442
diff changeset
   409
                        LambdaTestHelpers.launderAssertion(
930f51c90ea7 8016455: Sync stream tests from lambda to tl
psandoz
parents: 17442
diff changeset
   410
                                asserter,
930f51c90ea7 8016455: Sync stream tests from lambda to tl
psandoz
parents: 17442
diff changeset
   411
                                () -> String.format("%n%s: [actual size=%d] != [expected size=%d]", test, result.size(), refResult.size()));
930f51c90ea7 8016455: Sync stream tests from lambda to tl
psandoz
parents: 17442
diff changeset
   412
                    }
930f51c90ea7 8016455: Sync stream tests from lambda to tl
psandoz
parents: 17442
diff changeset
   413
                    else {
930f51c90ea7 8016455: Sync stream tests from lambda to tl
psandoz
parents: 17442
diff changeset
   414
                        LambdaTestHelpers.launderAssertion(
930f51c90ea7 8016455: Sync stream tests from lambda to tl
psandoz
parents: 17442
diff changeset
   415
                                asserter,
930f51c90ea7 8016455: Sync stream tests from lambda to tl
psandoz
parents: 17442
diff changeset
   416
                                () -> String.format("%n%s: [actual] %s != [expected] %s", test, result, refResult));
930f51c90ea7 8016455: Sync stream tests from lambda to tl
psandoz
parents: 17442
diff changeset
   417
                    }
17442
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   418
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   419
                    after.accept(data);
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   420
                } catch (Throwable t) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   421
                    errors.add(new Error(String.format("%s: %s", test, t), t));
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   422
                }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   423
            }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   424
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   425
            if (!errors.isEmpty()) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   426
                StringBuilder sb = new StringBuilder();
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   427
                int i = 1;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   428
                for (Error t : errors) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   429
                    sb.append(i++).append(": ");
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   430
                    if (t instanceof AssertionError) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   431
                        sb.append(t).append("\n");
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   432
                    }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   433
                    else {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   434
                        StringWriter sw = new StringWriter();
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   435
                        PrintWriter pw = new PrintWriter(sw);
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   436
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   437
                        t.getCause().printStackTrace(pw);
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   438
                        pw.flush();
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   439
                        sb.append(t).append("\n").append(sw);
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   440
                    }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   441
                }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   442
                sb.append("--");
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   443
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   444
                fail(String.format("%d failure(s) for test data: %s\n%s", i - 1, data.toString(), sb));
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   445
            }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   446
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   447
            return refResult;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   448
        }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   449
    }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   450
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   451
    // Exercise terminal operations
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   452
20537
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   453
    interface BaseTerminalTestScenario<U, R, S_OUT extends BaseStream<U, S_OUT>> {
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   454
        boolean requiresSingleStageSource();
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   455
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   456
        boolean requiresParallelSource();
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   457
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   458
        default R run(Function<S_OUT, R> terminalF, S_OUT source, StreamShape shape) {
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   459
            return terminalF.apply(source);
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   460
        }
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   461
    }
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   462
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   463
    @SuppressWarnings({"rawtypes", "unchecked"})
31248
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   464
    enum TerminalTestScenario implements BaseTerminalTestScenario {
20537
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   465
        SINGLE_SEQUENTIAL(true, false),
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   466
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   467
        SINGLE_SEQUENTIAL_SHORT_CIRCUIT(true, false) {
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   468
            @Override
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   469
            public Object run(Function terminalF, BaseStream source, StreamShape shape) {
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   470
                source = (BaseStream) chain(source, new ShortCircuitOp(shape));
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   471
                return terminalF.apply(source);
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   472
            }
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   473
        },
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   474
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   475
        SINGLE_PARALLEL(true, true),
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   476
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   477
        ALL_SEQUENTIAL(false, false),
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   478
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   479
        ALL_SEQUENTIAL_SHORT_CIRCUIT(false, false) {
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   480
            @Override
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   481
            public Object run(Function terminalF, BaseStream source, StreamShape shape) {
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   482
                source = (BaseStream) chain(source, new ShortCircuitOp(shape));
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   483
                return terminalF.apply(source);
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   484
            }
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   485
        },
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   486
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   487
        ALL_PARALLEL(false, true),
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   488
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   489
        ALL_PARALLEL_SEQUENTIAL(false, false) {
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   490
            @Override
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   491
            public Object run(Function terminalF, BaseStream source, StreamShape shape) {
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   492
                return terminalF.apply(source.sequential());
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   493
            }
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   494
        },
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   495
        ;
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   496
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   497
        private final boolean requiresSingleStageSource;
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   498
        private final boolean isParallel;
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   499
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   500
        TerminalTestScenario(boolean requiresSingleStageSource, boolean isParallel) {
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   501
            this.requiresSingleStageSource = requiresSingleStageSource;
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   502
            this.isParallel = isParallel;
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   503
        }
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   504
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   505
        @Override
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   506
        public boolean requiresSingleStageSource() {
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   507
            return requiresSingleStageSource;
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   508
        }
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   509
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   510
        @Override
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   511
        public boolean requiresParallelSource() {
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   512
            return isParallel;
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   513
        }
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   514
17442
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   515
    }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   516
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   517
    @SuppressWarnings({"rawtypes", "unchecked"})
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   518
    public class ExerciseDataTerminalBuilder<T, U, R, S_IN extends BaseStream<T, S_IN>, S_OUT extends BaseStream<U, S_OUT>> {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   519
        final TestData<T, S_IN> data;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   520
        final Function<S_IN, S_OUT> streamF;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   521
        final Function<S_OUT, R> terminalF;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   522
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   523
        R refResult;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   524
18529
930f51c90ea7 8016455: Sync stream tests from lambda to tl
psandoz
parents: 17442
diff changeset
   525
        ResultAsserter<R> resultAsserter = (act, exp, ord, par) -> LambdaTestHelpers.assertContentsEqual(act, exp);
17442
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   526
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   527
        private ExerciseDataTerminalBuilder(TestData<T, S_IN> data, Function<S_IN, S_OUT> streamF, Function<S_OUT, R> terminalF) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   528
            this.data = data;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   529
            this.streamF = Objects.requireNonNull(streamF);
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   530
            this.terminalF = Objects.requireNonNull(terminalF);
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   531
        }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   532
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   533
        //
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   534
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   535
        public ExerciseDataTerminalBuilder<T, U, R, S_IN, S_OUT> expectedResult(R expectedResult) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   536
            this.refResult = expectedResult;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   537
            return this;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   538
        }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   539
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   540
        public ExerciseDataTerminalBuilder<T, U, R, S_IN, S_OUT> equalator(BiConsumer<R, R> equalityAsserter) {
18529
930f51c90ea7 8016455: Sync stream tests from lambda to tl
psandoz
parents: 17442
diff changeset
   541
            resultAsserter = (act, exp, ord, par) -> equalityAsserter.accept(act, exp);
17442
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   542
            return this;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   543
        }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   544
18529
930f51c90ea7 8016455: Sync stream tests from lambda to tl
psandoz
parents: 17442
diff changeset
   545
        public ExerciseDataTerminalBuilder<T, U, R, S_IN, S_OUT> resultAsserter(ResultAsserter<R> resultAsserter) {
930f51c90ea7 8016455: Sync stream tests from lambda to tl
psandoz
parents: 17442
diff changeset
   546
            this.resultAsserter = resultAsserter;
17442
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   547
            return this;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   548
        }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   549
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   550
        // Build method
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   551
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   552
        public R exercise() {
31258
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 31248
diff changeset
   553
            boolean isOrdered;
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 31248
diff changeset
   554
            StreamShape shape;
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 31248
diff changeset
   555
            Node<U> node;
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 31248
diff changeset
   556
            try (S_OUT out = streamF.apply(data.stream()).sequential()) {
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 31248
diff changeset
   557
                AbstractPipeline ap = (AbstractPipeline) out;
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 31248
diff changeset
   558
                isOrdered = StreamOpFlag.ORDERED.isKnown(ap.getStreamFlags());
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 31248
diff changeset
   559
                shape = ap.getOutputShape();
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 31248
diff changeset
   560
                // Sequentially collect the output that will be input to the terminal op
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 31248
diff changeset
   561
                node = ap.evaluateToArrayNode(size -> (U[]) new Object[size]);
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 31248
diff changeset
   562
            }
17442
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   563
20537
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   564
            EnumSet<TerminalTestScenario> tests = EnumSet.allOf(TerminalTestScenario.class);
17442
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   565
            if (refResult == null) {
20537
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   566
                // Induce the reference result
17442
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   567
                S_OUT source = (S_OUT) createPipeline(shape, node.spliterator(),
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   568
                                                      StreamOpFlag.IS_ORDERED | StreamOpFlag.IS_SIZED,
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   569
                                                      false);
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   570
20537
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   571
                refResult = (R) TerminalTestScenario.SINGLE_SEQUENTIAL.run(terminalF, source, shape);
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   572
                tests.remove(TerminalTestScenario.SINGLE_SEQUENTIAL);
17442
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   573
            }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   574
20537
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   575
            for (BaseTerminalTestScenario test : tests) {
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   576
                S_OUT source;
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   577
                if (test.requiresSingleStageSource()) {
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   578
                    source = (S_OUT) createPipeline(shape, node.spliterator(),
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   579
                                                    StreamOpFlag.IS_ORDERED | StreamOpFlag.IS_SIZED,
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   580
                                                    test.requiresParallelSource());
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   581
                }
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   582
                else {
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   583
                    source = streamF.apply(test.requiresParallelSource()
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   584
                                           ? data.parallelStream() : data.stream());
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   585
                }
17442
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   586
31258
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 31248
diff changeset
   587
                R result;
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 31248
diff changeset
   588
                try (source) {
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 31248
diff changeset
   589
                    result = (R) test.run(terminalF, source, shape);
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 31248
diff changeset
   590
                }
20537
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   591
                LambdaTestHelpers.launderAssertion(
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   592
                        () -> resultAsserter.assertResult(result, refResult, isOrdered, test.requiresParallelSource()),
ab8ccd264bf3 8025968: Integrate test improvements made in lambda repo
henryjen
parents: 18572
diff changeset
   593
                        () -> String.format("%s: %s != %s", test, refResult, result));
17442
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   594
            }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   595
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   596
            return refResult;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   597
        }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   598
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   599
        AbstractPipeline createPipeline(StreamShape shape, Spliterator s, int flags, boolean parallel) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   600
            switch (shape) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   601
                case REFERENCE:    return new ReferencePipeline.Head<>(s, flags, parallel);
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   602
                case INT_VALUE:    return new IntPipeline.Head(s, flags, parallel);
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   603
                case LONG_VALUE:   return new LongPipeline.Head(s, flags, parallel);
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   604
                case DOUBLE_VALUE: return new DoublePipeline.Head(s, flags, parallel);
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   605
                default: throw new IllegalStateException("Unknown shape: " + shape);
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   606
            }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   607
        }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   608
    }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   609
31248
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   610
    protected <T, R> R exerciseTerminalOps(Collection<T> data, Function<Stream<T>, R> m, R expected) {
17442
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   611
        TestData.OfRef<T> data1
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   612
                = TestData.Factory.ofCollection("Collection of type " + data.getClass().getName(), data);
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   613
        return withData(data1).terminal(m).expectedResult(expected).exercise();
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   614
    }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   615
31248
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   616
    protected <T, R, S_IN extends BaseStream<T, S_IN>> R
17442
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   617
    exerciseTerminalOps(TestData<T, S_IN> data,
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   618
                        Function<S_IN, R> terminalF) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   619
        return withData(data).terminal(terminalF).exercise();
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   620
    }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   621
31248
0006a0e47a04 8129120: Terminal operation properties should not be back-propagated to upstream operations
psandoz
parents: 20538
diff changeset
   622
    protected <T, U, R, S_IN extends BaseStream<T, S_IN>, S_OUT extends BaseStream<U, S_OUT>> R
17442
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   623
    exerciseTerminalOps(TestData<T, S_IN> data,
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   624
                        Function<S_IN, S_OUT> streamF,
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   625
                        Function<S_OUT, R> terminalF) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   626
        return withData(data).terminal(streamF, terminalF).exercise();
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   627
    }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   628
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   629
    //
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   630
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   631
    @SuppressWarnings({"rawtypes", "unchecked"})
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   632
    private static <T> AbstractPipeline<?, T, ?> chain(AbstractPipeline upstream, IntermediateTestOp<?, T> op) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   633
        return (AbstractPipeline<?, T, ?>) IntermediateTestOp.chain(upstream, op);
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   634
    }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   635
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   636
    @SuppressWarnings({"rawtypes", "unchecked"})
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   637
    private static AbstractPipeline<?, ?, ?> chain(AbstractPipeline pipe, IntermediateTestOp... ops) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   638
        for (IntermediateTestOp op : ops)
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   639
            pipe = chain(pipe, op);
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   640
        return pipe;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   641
    }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   642
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   643
    @SuppressWarnings("rawtypes")
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   644
    private static <T> AbstractPipeline<?, T, ?> chain(BaseStream pipe, IntermediateTestOp<?, T> op) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   645
        return chain((AbstractPipeline) pipe, op);
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   646
    }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   647
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   648
    @SuppressWarnings("rawtypes")
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   649
    public static AbstractPipeline<?, ?, ?> chain(BaseStream pipe, IntermediateTestOp... ops) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   650
        return chain((AbstractPipeline) pipe, ops);
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   651
    }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   652
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   653
    // Test data
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   654
20538
4f7f94090a46 8026009: Changes for 8025968 break all stream tests
henryjen
parents: 20537
diff changeset
   655
    static class ShortCircuitOp<T> implements StatelessTestOp<T,T> {
17442
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   656
        private final StreamShape shape;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   657
20538
4f7f94090a46 8026009: Changes for 8025968 break all stream tests
henryjen
parents: 20537
diff changeset
   658
        ShortCircuitOp(StreamShape shape) {
17442
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   659
            this.shape = shape;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   660
        }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   661
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   662
        @Override
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   663
        public Sink<T> opWrapSink(int flags, boolean parallel, Sink<T> sink) {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   664
            return sink;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   665
        }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   666
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   667
        @Override
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   668
        public int opGetFlags() {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   669
            return StreamOpFlag.IS_SHORT_CIRCUIT;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   670
        }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   671
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   672
        @Override
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   673
        public StreamShape outputShape() {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   674
            return shape;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   675
        }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   676
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   677
        @Override
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   678
        public StreamShape inputShape() {
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   679
            return shape;
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   680
        }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   681
    }
1444a769a68a 8012664: Add tests for java.util.stream and lambda translation
briangoetz
parents:
diff changeset
   682
}