test/jdk/java/util/stream/test/org/openjdk/tests/java/util/stream/WhileOpTest.java
author psandoz
Wed, 20 Dec 2017 11:40:45 -0800
changeset 48410 8aab0cea56bf
parent 47216 71c04702a3d5
child 48453 37d2147852fc
permissions -rw-r--r--
8193856: takeWhile produces incorrect result with elements produced by flatMap Reviewed-by: smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31644
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
     1
/*
48410
8aab0cea56bf 8193856: takeWhile produces incorrect result with elements produced by flatMap
psandoz
parents: 47216
diff changeset
     2
 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
31644
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
     4
 *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
     7
 * published by the Free Software Foundation.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
     8
 *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    13
 * accompanied this code).
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    14
 *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    18
 *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    21
 * questions.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    22
 */
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    23
package org.openjdk.tests.java.util.stream;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    24
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    25
import org.testng.annotations.Test;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    26
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    27
import java.util.ArrayList;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    28
import java.util.Collection;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    29
import java.util.HashMap;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    30
import java.util.LinkedHashSet;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    31
import java.util.List;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    32
import java.util.Map;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    33
import java.util.Set;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    34
import java.util.concurrent.atomic.AtomicBoolean;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    35
import java.util.function.Function;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    36
import java.util.function.Predicate;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    37
import java.util.stream.DefaultMethodStreams;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    38
import java.util.stream.DoubleStream;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    39
import java.util.stream.IntStream;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    40
import java.util.stream.LambdaTestHelpers;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    41
import java.util.stream.LongStream;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    42
import java.util.stream.OpTestCase;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    43
import java.util.stream.Stream;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    44
import java.util.stream.StreamTestDataProvider;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    45
import java.util.stream.TestData;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    46
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    47
/*
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    48
 * @test
48410
8aab0cea56bf 8193856: takeWhile produces incorrect result with elements produced by flatMap
psandoz
parents: 47216
diff changeset
    49
 * @bug 8071597 8193856
31644
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    50
 */
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    51
@Test
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    52
public class WhileOpTest extends OpTestCase {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    53
31726
50b5db23b528 8131140: Mark some tests from WhileOpStatefulTest.java and WhileOpTest.java as serialization hostile
amlu
parents: 31644
diff changeset
    54
    @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class,
50b5db23b528 8131140: Mark some tests from WhileOpStatefulTest.java and WhileOpTest.java as serialization hostile
amlu
parents: 31644
diff changeset
    55
          groups = { "serialization-hostile" })
31644
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    56
    public void testTakeWhileOps(String name, TestData.OfRef<Integer> data) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    57
        for (int size : sizes(data.size())) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    58
            setContext("takeWhile", size);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    59
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    60
            testWhileMulti(data,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    61
                           whileResultAsserter(data, WhileOp.Take, e -> e < size),
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    62
                           s -> s.takeWhile(e -> e < size),
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    63
                           s -> s.takeWhile(e -> e < size),
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    64
                           s -> s.takeWhile(e -> e < size),
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    65
                           s -> s.takeWhile(e -> e < size));
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    66
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    67
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    68
            testWhileMulti(data,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    69
                           whileResultAsserter(data, WhileOp.Take, e -> e < size / 2),
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    70
                           s -> s.takeWhile(e -> e < size).takeWhile(e -> e < size / 2),
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    71
                           s -> s.takeWhile(e -> e < size).takeWhile(e -> e < size / 2),
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    72
                           s -> s.takeWhile(e -> e < size).takeWhile(e -> e < size / 2),
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    73
                           s -> s.takeWhile(e -> e < size).takeWhile(e -> e < size / 2));
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    74
        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    75
    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    76
31726
50b5db23b528 8131140: Mark some tests from WhileOpStatefulTest.java and WhileOpTest.java as serialization hostile
amlu
parents: 31644
diff changeset
    77
    @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class,
50b5db23b528 8131140: Mark some tests from WhileOpStatefulTest.java and WhileOpTest.java as serialization hostile
amlu
parents: 31644
diff changeset
    78
          groups = { "serialization-hostile" })
31644
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    79
    public void testDropWhileOps(String name, TestData.OfRef<Integer> data) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    80
        for (int size : sizes(data.size())) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    81
            setContext("dropWhile", size);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    82
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    83
            testWhileMulti(data,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    84
                           whileResultAsserter(data, WhileOp.Drop, e -> e < size),
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    85
                           s -> s.dropWhile(e -> e < size),
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    86
                           s -> s.dropWhile(e -> e < size),
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    87
                           s -> s.dropWhile(e -> e < size),
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    88
                           s -> s.dropWhile(e -> e < size));
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    89
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    90
            testWhileMulti(data,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    91
                           whileResultAsserter(data, WhileOp.Drop, e -> e < size),
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    92
                           s -> s.dropWhile(e -> e < size / 2).dropWhile(e -> e < size),
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    93
                           s -> s.dropWhile(e -> e < size / 2).dropWhile(e -> e < size),
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    94
                           s -> s.dropWhile(e -> e < size / 2).dropWhile(e -> e < size),
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    95
                           s -> s.dropWhile(e -> e < size / 2).dropWhile(e -> e < size));
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    96
        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    97
    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
    98
31726
50b5db23b528 8131140: Mark some tests from WhileOpStatefulTest.java and WhileOpTest.java as serialization hostile
amlu
parents: 31644
diff changeset
    99
    @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class,
50b5db23b528 8131140: Mark some tests from WhileOpStatefulTest.java and WhileOpTest.java as serialization hostile
amlu
parents: 31644
diff changeset
   100
          groups = { "serialization-hostile" })
31644
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   101
    public void testDropTakeWhileOps(String name, TestData.OfRef<Integer> data) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   102
        for (int size : sizes(data.size())) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   103
            setContext("dropWhile", size);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   104
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   105
            testWhileMulti(data,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   106
                           whileResultAsserter(data, WhileOp.Undefined, null),
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   107
                           s -> s.dropWhile(e -> e < size / 2).takeWhile(e -> e < size),
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   108
                           s -> s.dropWhile(e -> e < size / 2).takeWhile(e -> e < size),
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   109
                           s -> s.dropWhile(e -> e < size / 2).takeWhile(e -> e < size),
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   110
                           s -> s.dropWhile(e -> e < size / 2).takeWhile(e -> e < size));
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   111
        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   112
    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   113
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   114
    /**
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   115
     * While operation type to be asserted on
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   116
     */
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   117
    enum WhileOp {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   118
        /**
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   119
         * The takeWhile operation
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   120
         */
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   121
        Take,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   122
        /**
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   123
         * The dropWhile operation
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   124
         */
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   125
        Drop,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   126
        /**
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   127
         * The operation(s) are undefined
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   128
         */
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   129
        Undefined
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   130
    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   131
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   132
    /**
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   133
     * Create a result asserter for takeWhile or dropWhile operations.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   134
     * <p>
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   135
     * If the stream pipeline consists of the takeWhile operation
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   136
     * ({@link WhileOp#Take}) or the dropWhile operation ({@link WhileOp#Drop})
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   137
     * then specific assertions can be made on the actual result based on the
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   138
     * input elements, {@code inputData}, and whether those elements match the
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   139
     * predicate, {@code p}, of the operation.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   140
     * <p>
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   141
     * If the input elements have an encounter order then the actual result
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   142
     * is asserted against the result of operating sequentially on input
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   143
     * elements given the predicate and in accordance with the operation
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   144
     * semantics. (The actual result whether produced sequentially or in
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   145
     * parallel should the same.)
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   146
     * <p>
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   147
     * If the input elements have no encounter order then an actual result
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   148
     * is, for practical purposes, considered non-deterministic.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   149
     * Consider an input list of lists that contains all possible permutations
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   150
     * of the input elements, and a output list of lists that is the result of
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   151
     * applying the pipeline with the operation sequentially to each input
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   152
     * list.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   153
     * Any list in the output lists is a valid result. It's not practical to
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   154
     * test in such a manner.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   155
     * For a takeWhile operation the following assertions can be made if
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   156
     * only some of the input elements match the predicate (i.e. taking will
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   157
     * short-circuit the pipeline):
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   158
     * <ol>
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   159
     * <li>The set of output elements is a subset of the set of matching
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   160
     * input elements</li>
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   161
     * <li>The set of output elements and the set of non-matching input
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   162
     * element are disjoint</li>
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   163
     * </ol>
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   164
     * For a dropWhile operation the following assertions can be made:
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   165
     * <ol>
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   166
     * <li>The set of non-matching input elements is a subset of the set of
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   167
     * output elements</li>
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   168
     * <li>The set of matching output elements is a subset of the set of
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   169
     * matching input elements</li>
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   170
     * </ol>
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   171
     *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   172
     * @param inputData the elements input into the stream pipeline
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   173
     * @param op the operation of the stream pipeline, one of takeWhile,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   174
     * dropWhile, or an undefined set of operations (possibly including
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   175
     * two or more takeWhile and/or dropWhile operations, or because
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   176
     * the predicate is not stateless).
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   177
     * @param p the stateless predicate applied to the operation, ignored if
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   178
     * the
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   179
     * operation is {@link WhileOp#Undefined}.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   180
     * @param <T> the type of elements
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   181
     * @return a result asserter
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   182
     */
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   183
    private <T> ResultAsserter<Iterable<T>> whileResultAsserter(Iterable<T> inputData,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   184
                                                                WhileOp op,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   185
                                                                Predicate<? super T> p) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   186
        return (act, exp, ord, par) -> {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   187
            if (par & !ord) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   188
                List<T> input = new ArrayList<>();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   189
                inputData.forEach(input::add);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   190
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   191
                List<T> output = new ArrayList<>();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   192
                act.forEach(output::add);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   193
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   194
                if (op == WhileOp.Take) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   195
                    List<T> matchingInput = new ArrayList<>();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   196
                    List<T> nonMatchingInput = new ArrayList<>();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   197
                    input.forEach(t -> {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   198
                        if (p.test(t))
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   199
                            matchingInput.add(t);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   200
                        else
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   201
                            nonMatchingInput.add(t);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   202
                    });
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   203
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   204
                    // If some, not all, elements are taken
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   205
                    if (matchingInput.size() < input.size()) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   206
                        assertTrue(output.size() <= matchingInput.size(),
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   207
                                   "Output is larger than the matching input");
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   208
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   209
                        // The output must be a subset of the matching input
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   210
                        assertTrue(matchingInput.containsAll(output),
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   211
                                   "Output is not a subset of the matching input");
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   212
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   213
                        // The output must not contain any non matching elements
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   214
                        for (T nonMatching : nonMatchingInput) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   215
                            assertFalse(output.contains(nonMatching),
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   216
                                        "Output and non-matching input are not disjoint");
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   217
                        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   218
                    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   219
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   220
                else if (op == WhileOp.Drop) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   221
                    List<T> matchingInput = new ArrayList<>();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   222
                    List<T> nonMatchingInput = new ArrayList<>();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   223
                    input.forEach(t -> {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   224
                        if (p.test(t))
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   225
                            matchingInput.add(t);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   226
                        else
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   227
                            nonMatchingInput.add(t);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   228
                    });
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   229
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   230
                    // The non matching input must be a subset of output
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   231
                    assertTrue(output.containsAll(nonMatchingInput),
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   232
                               "Non-matching input is not a subset of the output");
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   233
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   234
                    // The matching output must be a subset of the matching input
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   235
                    List<T> matchingOutput = new ArrayList<>();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   236
                    output.forEach(i -> {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   237
                        if (p.test(i))
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   238
                            matchingOutput.add(i);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   239
                    });
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   240
                    assertTrue(matchingInput.containsAll(matchingOutput),
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   241
                               "Matching output is not a subset of matching input");
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   242
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   243
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   244
                // Note: if there is a combination of takeWhile and dropWhile then specific
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   245
                // assertions cannot be performed.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   246
                // All that can be reliably asserted is the output is a subset of the input
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   247
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   248
                assertTrue(input.containsAll(output));
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   249
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   250
            else {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   251
                // For specific operations derive expected result from the input
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   252
                if (op == WhileOp.Take) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   253
                    List<T> takeInput = new ArrayList<>();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   254
                    for (T t : inputData) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   255
                        if (p.test(t))
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   256
                            takeInput.add(t);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   257
                        else
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   258
                            break;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   259
                    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   260
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   261
                    LambdaTestHelpers.assertContents(act, takeInput);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   262
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   263
                else if (op == WhileOp.Drop) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   264
                    List<T> dropInput = new ArrayList<>();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   265
                    for (T t : inputData) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   266
                        if (dropInput.size() > 0 || !p.test(t))
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   267
                            dropInput.add(t);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   268
                    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   269
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   270
                    LambdaTestHelpers.assertContents(act, dropInput);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   271
                }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   272
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   273
                LambdaTestHelpers.assertContents(act, exp);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   274
            }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   275
        };
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   276
    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   277
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   278
    private Collection<Integer> sizes(int s) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   279
        Set<Integer> sizes = new LinkedHashSet<>();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   280
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   281
        sizes.add(0);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   282
        sizes.add(1);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   283
        sizes.add(s / 4);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   284
        sizes.add(s / 2);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   285
        sizes.add(3 * s / 4);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   286
        sizes.add(Math.max(0, s - 1));
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   287
        sizes.add(s);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   288
        sizes.add(Integer.MAX_VALUE);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   289
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   290
        return sizes;
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   291
    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   292
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   293
    private void testWhileMulti(TestData.OfRef<Integer> data,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   294
                                ResultAsserter<Iterable<Integer>> ra,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   295
                                Function<Stream<Integer>, Stream<Integer>> mRef,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   296
                                Function<IntStream, IntStream> mInt,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   297
                                Function<LongStream, LongStream> mLong,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   298
                                Function<DoubleStream, DoubleStream> mDouble) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   299
        Map<String, Function<Stream<Integer>, Stream<Integer>>> ms = new HashMap<>();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   300
        ms.put("Ref", mRef);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   301
        ms.put("Int", s -> mInt.apply(s.mapToInt(e -> e)).mapToObj(e -> e));
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   302
        ms.put("Long", s -> mLong.apply(s.mapToLong(e -> e)).mapToObj(e -> (int) e));
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   303
        ms.put("Double", s -> mDouble.apply(s.mapToDouble(e -> e)).mapToObj(e -> (int) e));
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   304
        ms.put("Ref using defaults", s -> mRef.apply(DefaultMethodStreams.delegateTo(s)));
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   305
        ms.put("Int using defaults", s -> mInt.apply(DefaultMethodStreams.delegateTo(s.mapToInt(e -> e))).mapToObj(e -> e));
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   306
        ms.put("Long using defaults", s -> mLong.apply(DefaultMethodStreams.delegateTo(s.mapToLong(e -> e))).mapToObj(e -> (int) e));
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   307
        ms.put("Double using defaults", s -> mDouble.apply(DefaultMethodStreams.delegateTo(s.mapToDouble(e -> e))).mapToObj(e -> (int) e));
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   308
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   309
        testWhileMulti(data, ra, ms);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   310
    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   311
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   312
    private final void testWhileMulti(TestData.OfRef<Integer> data,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   313
                                      ResultAsserter<Iterable<Integer>> ra,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   314
                                      Map<String, Function<Stream<Integer>, Stream<Integer>>> ms) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   315
        for (Map.Entry<String, Function<Stream<Integer>, Stream<Integer>>> e : ms.entrySet()) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   316
            setContext("shape", e.getKey());
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   317
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   318
            withData(data)
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   319
                    .stream(e.getValue())
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   320
                    .resultAsserter(ra)
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   321
                    .exercise();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   322
        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   323
    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   324
31726
50b5db23b528 8131140: Mark some tests from WhileOpStatefulTest.java and WhileOpTest.java as serialization hostile
amlu
parents: 31644
diff changeset
   325
    @Test(groups = { "serialization-hostile" })
31644
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   326
    public void testRefDefaultClose() {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   327
        AtomicBoolean isClosed = new AtomicBoolean();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   328
        Stream<Integer> s = Stream.of(1, 2, 3).onClose(() -> isClosed.set(true));
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   329
        try (Stream<Integer> ds = DefaultMethodStreams.delegateTo(s).takeWhile(e -> e < 3)) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   330
            ds.count();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   331
        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   332
        assertTrue(isClosed.get());
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   333
    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   334
31726
50b5db23b528 8131140: Mark some tests from WhileOpStatefulTest.java and WhileOpTest.java as serialization hostile
amlu
parents: 31644
diff changeset
   335
    @Test(groups = { "serialization-hostile" })
31644
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   336
    public void testIntDefaultClose() {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   337
        AtomicBoolean isClosed = new AtomicBoolean();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   338
        IntStream s = IntStream.of(1, 2, 3).onClose(() -> isClosed.set(true));
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   339
        try (IntStream ds = DefaultMethodStreams.delegateTo(s).takeWhile(e -> e < 3)) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   340
            ds.count();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   341
        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   342
        assertTrue(isClosed.get());
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   343
    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   344
31726
50b5db23b528 8131140: Mark some tests from WhileOpStatefulTest.java and WhileOpTest.java as serialization hostile
amlu
parents: 31644
diff changeset
   345
    @Test(groups = { "serialization-hostile" })
31644
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   346
    public void testLongDefaultClose() {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   347
        AtomicBoolean isClosed = new AtomicBoolean();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   348
        LongStream s = LongStream.of(1, 2, 3).onClose(() -> isClosed.set(true));
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   349
        try (LongStream ds = DefaultMethodStreams.delegateTo(s).takeWhile(e -> e < 3)) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   350
            ds.count();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   351
        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   352
        assertTrue(isClosed.get());
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   353
    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   354
31726
50b5db23b528 8131140: Mark some tests from WhileOpStatefulTest.java and WhileOpTest.java as serialization hostile
amlu
parents: 31644
diff changeset
   355
    @Test(groups = { "serialization-hostile" })
31644
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   356
    public void testDoubleDefaultClose() {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   357
        AtomicBoolean isClosed = new AtomicBoolean();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   358
        DoubleStream s = DoubleStream.of(1, 2, 3).onClose(() -> isClosed.set(true));
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   359
        try (DoubleStream ds = DefaultMethodStreams.delegateTo(s).takeWhile(e -> e < 3)) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   360
            ds.count();
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   361
        }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   362
        assertTrue(isClosed.get());
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   363
    }
48410
8aab0cea56bf 8193856: takeWhile produces incorrect result with elements produced by flatMap
psandoz
parents: 47216
diff changeset
   364
8aab0cea56bf 8193856: takeWhile produces incorrect result with elements produced by flatMap
psandoz
parents: 47216
diff changeset
   365
    @Test(groups = { "serialization-hostile" })
8aab0cea56bf 8193856: takeWhile produces incorrect result with elements produced by flatMap
psandoz
parents: 47216
diff changeset
   366
    public void testFlatMapThenTake() {
8aab0cea56bf 8193856: takeWhile produces incorrect result with elements produced by flatMap
psandoz
parents: 47216
diff changeset
   367
        TestData.OfRef<Integer> range = TestData.Factory.ofSupplier(
8aab0cea56bf 8193856: takeWhile produces incorrect result with elements produced by flatMap
psandoz
parents: 47216
diff changeset
   368
                "range", () -> IntStream.range(0, 100).boxed());
8aab0cea56bf 8193856: takeWhile produces incorrect result with elements produced by flatMap
psandoz
parents: 47216
diff changeset
   369
8aab0cea56bf 8193856: takeWhile produces incorrect result with elements produced by flatMap
psandoz
parents: 47216
diff changeset
   370
        exerciseOpsMulti(range,
8aab0cea56bf 8193856: takeWhile produces incorrect result with elements produced by flatMap
psandoz
parents: 47216
diff changeset
   371
                         // Reference result
8aab0cea56bf 8193856: takeWhile produces incorrect result with elements produced by flatMap
psandoz
parents: 47216
diff changeset
   372
                         s -> s.takeWhile(e -> e != 50),
8aab0cea56bf 8193856: takeWhile produces incorrect result with elements produced by flatMap
psandoz
parents: 47216
diff changeset
   373
                         // For other results collect into array,
8aab0cea56bf 8193856: takeWhile produces incorrect result with elements produced by flatMap
psandoz
parents: 47216
diff changeset
   374
                         // stream the single array (not the elements),
8aab0cea56bf 8193856: takeWhile produces incorrect result with elements produced by flatMap
psandoz
parents: 47216
diff changeset
   375
                         // then flat map to stream the array elements
8aab0cea56bf 8193856: takeWhile produces incorrect result with elements produced by flatMap
psandoz
parents: 47216
diff changeset
   376
                         s -> Stream.<Integer[]>of(s.toArray(Integer[]::new)).
8aab0cea56bf 8193856: takeWhile produces incorrect result with elements produced by flatMap
psandoz
parents: 47216
diff changeset
   377
                                 flatMap(Stream::of).
8aab0cea56bf 8193856: takeWhile produces incorrect result with elements produced by flatMap
psandoz
parents: 47216
diff changeset
   378
                                 takeWhile(e -> e != 50),
8aab0cea56bf 8193856: takeWhile produces incorrect result with elements produced by flatMap
psandoz
parents: 47216
diff changeset
   379
                         s -> Stream.of(s.mapToInt(e -> e).toArray()).
8aab0cea56bf 8193856: takeWhile produces incorrect result with elements produced by flatMap
psandoz
parents: 47216
diff changeset
   380
                                 flatMapToInt(IntStream::of).
8aab0cea56bf 8193856: takeWhile produces incorrect result with elements produced by flatMap
psandoz
parents: 47216
diff changeset
   381
                                 takeWhile(e -> e != 50).
8aab0cea56bf 8193856: takeWhile produces incorrect result with elements produced by flatMap
psandoz
parents: 47216
diff changeset
   382
                                 mapToObj(e -> e),
8aab0cea56bf 8193856: takeWhile produces incorrect result with elements produced by flatMap
psandoz
parents: 47216
diff changeset
   383
                         s -> Stream.of(s.mapToLong(e -> e).toArray()).
8aab0cea56bf 8193856: takeWhile produces incorrect result with elements produced by flatMap
psandoz
parents: 47216
diff changeset
   384
                                 flatMapToLong(LongStream::of).
8aab0cea56bf 8193856: takeWhile produces incorrect result with elements produced by flatMap
psandoz
parents: 47216
diff changeset
   385
                                 takeWhile(e -> e != 50L).
8aab0cea56bf 8193856: takeWhile produces incorrect result with elements produced by flatMap
psandoz
parents: 47216
diff changeset
   386
                                 mapToObj(e -> (int) e),
8aab0cea56bf 8193856: takeWhile produces incorrect result with elements produced by flatMap
psandoz
parents: 47216
diff changeset
   387
                         s -> Stream.of(s.mapToDouble(e -> e).toArray()).
8aab0cea56bf 8193856: takeWhile produces incorrect result with elements produced by flatMap
psandoz
parents: 47216
diff changeset
   388
                                 flatMapToDouble(DoubleStream::of).
8aab0cea56bf 8193856: takeWhile produces incorrect result with elements produced by flatMap
psandoz
parents: 47216
diff changeset
   389
                                 takeWhile(e -> e != 50.0).
8aab0cea56bf 8193856: takeWhile produces incorrect result with elements produced by flatMap
psandoz
parents: 47216
diff changeset
   390
                                 mapToObj(e -> (int) e)
8aab0cea56bf 8193856: takeWhile produces incorrect result with elements produced by flatMap
psandoz
parents: 47216
diff changeset
   391
                         );
8aab0cea56bf 8193856: takeWhile produces incorrect result with elements produced by flatMap
psandoz
parents: 47216
diff changeset
   392
    }
31644
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
diff changeset
   393
}