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(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 } 52 }, 53 54 STREAM_TO_ARRAY(false) { 55 <T, S_IN extends BaseStream<T, S_IN>> 56 void _run(TestData<T, S_IN> data, DoubleConsumer b, Function<S_IN, DoubleStream> m) { 57 for (double t : m.apply(data.stream()).toArray()) { 58 b.accept(t); 59 } 60 } 61 }, 62 63 STREAM_ITERATOR(false) { 64 <T, S_IN extends BaseStream<T, S_IN>> 65 void _run(TestData<T, S_IN> data, DoubleConsumer b, Function<S_IN, DoubleStream> m) { 66 for (PrimitiveIterator.OfDouble seqIter = m.apply(data.stream()).iterator(); seqIter.hasNext(); ) 67 b.accept(seqIter.nextDouble()); 68 } 69 }, 70 71 // Wrap as stream, and spliterate then iterate in pull mode 72 STREAM_SPLITERATOR(false) { 73 <T, S_IN extends BaseStream<T, S_IN>> 74 void _run(TestData<T, S_IN> data, DoubleConsumer b, Function<S_IN, DoubleStream> m) { 75 for (Spliterator.OfDouble spl = m.apply(data.stream()).spliterator(); spl.tryAdvance(b); ) { 76 } 77 } 78 }, 79 80 // Wrap as stream, spliterate, then split a few times mixing advances with forEach 81 STREAM_SPLITERATOR_WITH_MIXED_TRAVERSE_AND_SPLIT(false) { 82 <T, S_IN extends BaseStream<T, S_IN>> 83 void _run(TestData<T, S_IN> data, DoubleConsumer b, Function<S_IN, DoubleStream> m) { 84 SpliteratorTestHelper.mixedTraverseAndSplit(b, m.apply(data.stream()).spliterator()); 85 } 86 }, 87 88 // Wrap as stream, and spliterate then iterate in pull mode 89 STREAM_SPLITERATOR_FOREACH(false) { 90 <T, S_IN extends BaseStream<T, S_IN>> 91 void _run(TestData<T, S_IN> data, DoubleConsumer b, Function<S_IN, DoubleStream> m) { 92 m.apply(data.stream()).spliterator().forEachRemaining(b); 93 } 94 }, 95 96 PAR_STREAM_SEQUENTIAL_FOR_EACH(true) { 97 <T, S_IN extends BaseStream<T, S_IN>> 98 void _run(TestData<T, S_IN> data, DoubleConsumer b, Function<S_IN, DoubleStream> m) { 99 m.apply(data.parallelStream()).sequential().forEach(b); 100 } 101 }, 102 103 // Wrap as parallel stream + forEachOrdered 104 PAR_STREAM_FOR_EACH_ORDERED(true) { 105 <T, S_IN extends BaseStream<T, S_IN>> 106 void _run(TestData<T, S_IN> data, DoubleConsumer b, Function<S_IN, DoubleStream> m) { 107 // @@@ Want to explicitly select ordered equalator 108 m.apply(data.parallelStream()).forEachOrdered(b); 109 } 110 }, 111 112 // Wrap as stream, and spliterate then iterate sequentially 113 PAR_STREAM_SPLITERATOR(true) { 114 <T, S_IN extends BaseStream<T, S_IN>> 115 void _run(TestData<T, S_IN> data, DoubleConsumer b, Function<S_IN, DoubleStream> m) { 116 for (Spliterator.OfDouble spl = m.apply(data.parallelStream()).spliterator(); spl.tryAdvance(b); ) { 117 } 118 } 119 }, 120 121 // Wrap as stream, and spliterate then iterate sequentially 122 PAR_STREAM_SPLITERATOR_FOREACH(true) { 123 <T, S_IN extends BaseStream<T, S_IN>> 124 void _run(TestData<T, S_IN> data, DoubleConsumer b, Function<S_IN, DoubleStream> m) { 125 m.apply(data.parallelStream()).spliterator().forEachRemaining(b); 126 } 127 }, 128 129 PAR_STREAM_TO_ARRAY(true) { 130 <T, S_IN extends BaseStream<T, S_IN>> 131 void _run(TestData<T, S_IN> data, DoubleConsumer b, Function<S_IN, DoubleStream> m) { 132 for (double t : m.apply(data.parallelStream()).toArray()) 133 b.accept(t); 134 } 135 }, 136 137 // Wrap as parallel stream, get the spliterator, wrap as a stream + toArray 138 PAR_STREAM_SPLITERATOR_STREAM_TO_ARRAY(true) { 139 <T, S_IN extends BaseStream<T, S_IN>> 140 void _run(TestData<T, S_IN> data, DoubleConsumer b, Function<S_IN, DoubleStream> m) { 141 DoubleStream s = m.apply(data.parallelStream()); 142 Spliterator.OfDouble sp = s.spliterator(); 143 DoubleStream ss = StreamSupport.doubleStream(() -> sp, 144 StreamOpFlag.toCharacteristics(OpTestCase.getStreamFlags(s)) 145 | (sp.getExactSizeIfKnown() < 0 ? 0 : Spliterator.SIZED), true); 146 for (double t : ss.toArray()) 147 b.accept(t); 148 } 149 }, 150 151 PAR_STREAM_TO_ARRAY_CLEAR_SIZED(true) { 152 <T, S_IN extends BaseStream<T, S_IN>> 153 void _run(TestData<T, S_IN> data, DoubleConsumer b, Function<S_IN, DoubleStream> m) { 154 S_IN pipe1 = (S_IN) OpTestCase.chain(data.parallelStream(), 155 new FlagDeclaringOp(StreamOpFlag.NOT_SIZED, data.getShape())); 156 DoubleStream pipe2 = m.apply(pipe1); 157 158 for (double t : pipe2.toArray()) 159 b.accept(t); 160 } 161 },; 162 163 private boolean isParallel; 164 165 DoubleStreamTestScenario(boolean isParallel) { 166 this.isParallel = isParallel; 167 } 168 169 public StreamShape getShape() { 170 return StreamShape.DOUBLE_VALUE; 171 } 172 173 public boolean isParallel() { 174 return isParallel; 175 } 176 177 public <T, U, S_IN extends BaseStream<T, S_IN>, S_OUT extends BaseStream<U, S_OUT>> 178 void run(TestData<T, S_IN> data, Consumer<U> b, Function<S_IN, S_OUT> m) { 179 _run(data, (DoubleConsumer) b, (Function<S_IN, DoubleStream>) m); 180 } 181 182 abstract <T, S_IN extends BaseStream<T, S_IN>> 183 void _run(TestData<T, S_IN> data, DoubleConsumer b, Function<S_IN, DoubleStream> m); 184 185 }