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