1 /*
   2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package java.util.stream;
  24 
  25 import java.util.PrimitiveIterator;
  26 import java.util.Spliterator;
  27 import java.util.function.Consumer;
  28 import java.util.function.DoubleConsumer;
  29 import java.util.function.Function;
  30 
  31 /**
  32  * Test scenarios for double streams.
  33  *
  34  * Each scenario is provided with a data source, a function that maps a fresh
  35  * stream (as provided by the data source) to a new stream, and a sink to
  36  * receive results.  Each scenario describes a different way of computing the
  37  * stream contents.  The test driver will ensure that all scenarios produce
  38  * the same output (modulo allowable differences in ordering).
  39  */
  40 @SuppressWarnings({"rawtypes", "unchecked"})
  41 public enum DoubleStreamTestScenario implements OpTestCase.BaseStreamTestScenario {
  42 
  43     STREAM_FOR_EACH_WITH_CLOSE(false) {
  44         <T, S_IN extends BaseStream<T, S_IN>>
  45         void _run(TestData<T, S_IN> data, DoubleConsumer b, Function<S_IN, DoubleStream> m) {
  46             DoubleStream s = m.apply(data.stream());
  47             if (s.isParallel()) {
  48                 s = s.sequential();
  49             }
  50             s.forEach(b);
  51             s.close();
  52         }
  53     },
  54 
  55     STREAM_TO_ARRAY(false) {
  56         <T, S_IN extends BaseStream<T, S_IN>>
  57         void _run(TestData<T, S_IN> data, DoubleConsumer b, Function<S_IN, DoubleStream> m) {
  58             for (double t : m.apply(data.stream()).toArray()) {
  59                 b.accept(t);
  60             }
  61         }
  62     },
  63 
  64     STREAM_ITERATOR(false) {
  65         <T, S_IN extends BaseStream<T, S_IN>>
  66         void _run(TestData<T, S_IN> data, DoubleConsumer b, Function<S_IN, DoubleStream> m) {
  67             for (PrimitiveIterator.OfDouble seqIter = m.apply(data.stream()).iterator(); seqIter.hasNext(); )
  68                 b.accept(seqIter.nextDouble());
  69         }
  70     },
  71 
  72     // Wrap as stream, and spliterate then iterate in pull mode
  73     STREAM_SPLITERATOR(false) {
  74         <T, S_IN extends BaseStream<T, S_IN>>
  75         void _run(TestData<T, S_IN> data, DoubleConsumer b, Function<S_IN, DoubleStream> m) {
  76             for (Spliterator.OfDouble spl = m.apply(data.stream()).spliterator(); spl.tryAdvance(b); ) {
  77             }
  78         }
  79     },
  80 
  81     // Wrap as stream, spliterate, then split a few times mixing advances with forEach
  82     STREAM_SPLITERATOR_WITH_MIXED_TRAVERSE_AND_SPLIT(false) {
  83         <T, S_IN extends BaseStream<T, S_IN>>
  84         void _run(TestData<T, S_IN> data, DoubleConsumer b, Function<S_IN, DoubleStream> m) {
  85             SpliteratorTestHelper.mixedTraverseAndSplit(b, m.apply(data.stream()).spliterator());
  86         }
  87     },
  88 
  89     // Wrap as stream, and spliterate then iterate in pull mode
  90     STREAM_SPLITERATOR_FOREACH(false) {
  91         <T, S_IN extends BaseStream<T, S_IN>>
  92         void _run(TestData<T, S_IN> data, DoubleConsumer b, Function<S_IN, DoubleStream> m) {
  93             m.apply(data.stream()).spliterator().forEachRemaining(b);
  94         }
  95     },
  96 
  97     PAR_STREAM_SEQUENTIAL_FOR_EACH(true) {
  98         <T, S_IN extends BaseStream<T, S_IN>>
  99         void _run(TestData<T, S_IN> data, DoubleConsumer b, Function<S_IN, DoubleStream> m) {
 100             m.apply(data.parallelStream()).sequential().forEach(b);
 101         }
 102     },
 103 
 104     // Wrap as parallel stream + forEachOrdered
 105     PAR_STREAM_FOR_EACH_ORDERED(true) {
 106         <T, S_IN extends BaseStream<T, S_IN>>
 107         void _run(TestData<T, S_IN> data, DoubleConsumer b, Function<S_IN, DoubleStream> m) {
 108             // @@@ Want to explicitly select ordered equalator
 109             m.apply(data.parallelStream()).forEachOrdered(b);
 110         }
 111     },
 112 
 113     // Wrap as stream, and spliterate then iterate sequentially
 114     PAR_STREAM_SPLITERATOR(true) {
 115         <T, S_IN extends BaseStream<T, S_IN>>
 116         void _run(TestData<T, S_IN> data, DoubleConsumer b, Function<S_IN, DoubleStream> m) {
 117             for (Spliterator.OfDouble spl = m.apply(data.parallelStream()).spliterator(); spl.tryAdvance(b); ) {
 118             }
 119         }
 120     },
 121 
 122     // Wrap as stream, and spliterate then iterate sequentially
 123     PAR_STREAM_SPLITERATOR_FOREACH(true) {
 124         <T, S_IN extends BaseStream<T, S_IN>>
 125         void _run(TestData<T, S_IN> data, DoubleConsumer b, Function<S_IN, DoubleStream> m) {
 126             m.apply(data.parallelStream()).spliterator().forEachRemaining(b);
 127         }
 128     },
 129 
 130     PAR_STREAM_TO_ARRAY(true) {
 131         <T, S_IN extends BaseStream<T, S_IN>>
 132         void _run(TestData<T, S_IN> data, DoubleConsumer b, Function<S_IN, DoubleStream> m) {
 133             for (double t : m.apply(data.parallelStream()).toArray())
 134                 b.accept(t);
 135         }
 136     },
 137 
 138     // Wrap as parallel stream, get the spliterator, wrap as a stream + toArray
 139     PAR_STREAM_SPLITERATOR_STREAM_TO_ARRAY(true) {
 140         <T, S_IN extends BaseStream<T, S_IN>>
 141         void _run(TestData<T, S_IN> data, DoubleConsumer b, Function<S_IN, DoubleStream> m) {
 142             DoubleStream s = m.apply(data.parallelStream());
 143             Spliterator.OfDouble sp = s.spliterator();
 144             DoubleStream ss = StreamSupport.doubleStream(() -> sp,
 145                                                          StreamOpFlag.toCharacteristics(OpTestCase.getStreamFlags(s))
 146                                                          | (sp.getExactSizeIfKnown() < 0 ? 0 : Spliterator.SIZED), true);
 147             for (double t : ss.toArray())
 148                 b.accept(t);
 149         }
 150     },
 151 
 152     PAR_STREAM_TO_ARRAY_CLEAR_SIZED(true) {
 153         <T, S_IN extends BaseStream<T, S_IN>>
 154         void _run(TestData<T, S_IN> data, DoubleConsumer b, Function<S_IN, DoubleStream> m) {
 155             S_IN pipe1 = (S_IN) OpTestCase.chain(data.parallelStream(),
 156                                                  new FlagDeclaringOp(StreamOpFlag.NOT_SIZED, data.getShape()));
 157             DoubleStream pipe2 = m.apply(pipe1);
 158 
 159             for (double t : pipe2.toArray())
 160                 b.accept(t);
 161         }
 162     },;
 163 
 164     private boolean isParallel;
 165 
 166     DoubleStreamTestScenario(boolean isParallel) {
 167         this.isParallel = isParallel;
 168     }
 169 
 170     public StreamShape getShape() {
 171         return StreamShape.DOUBLE_VALUE;
 172     }
 173 
 174     public boolean isParallel() {
 175         return isParallel;
 176     }
 177 
 178     public <T, U, S_IN extends BaseStream<T, S_IN>, S_OUT extends BaseStream<U, S_OUT>>
 179     void run(TestData<T, S_IN> data, Consumer<U> b, Function<S_IN, S_OUT> m) {
 180         _run(data, (DoubleConsumer) b, (Function<S_IN, DoubleStream>) m);
 181     }
 182 
 183     abstract <T, S_IN extends BaseStream<T, S_IN>>
 184     void _run(TestData<T, S_IN> data, DoubleConsumer b, Function<S_IN, DoubleStream> m);
 185 
 186 }