1 /*
   2  * Copyright (c) 2012, 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 org.openjdk.tests.java.util.stream;
  24 
  25 import java.util.stream.LambdaTestHelpers;
  26 import java.util.stream.OpTestCase;
  27 import java.util.stream.StreamTestDataProvider;
  28 import org.testng.annotations.Test;
  29 
  30 import java.util.Iterator;
  31 import java.util.Comparator;
  32 import java.util.concurrent.atomic.AtomicInteger;
  33 import java.util.function.Function;
  34 import java.util.function.Supplier;
  35 import java.util.function.UnaryOperator;
  36 import java.util.Spliterator;
  37 import java.util.stream.Stream;
  38 import java.util.stream.TestData;
  39 
  40 import static org.testng.Assert.assertEquals;
  41 import static org.testng.Assert.assertTrue;
  42 
  43 /**
  44  * SequentialOpTest
  45  *
  46  * @author Brian Goetz
  47  */
  48 public class SequentialOpTest extends OpTestCase {
  49     @SuppressWarnings({"rawtypes", "unchecked"})
  50     @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class,
  51           groups = { "serialization-hostile" })
  52     public void testLazy(String name, TestData.OfRef<Integer> data) {
  53         Function<Integer, Integer> id = LambdaTestHelpers.identity();
  54         AtomicInteger counter = new AtomicInteger();
  55         Supplier<Stream<Integer>>[] suppliers = new Supplier[] { () -> data.stream(), () -> data.parallelStream() };
  56         UnaryOperator<Stream<Integer>>[] configs
  57                 = new UnaryOperator[] {
  58                     (UnaryOperator<Stream<Integer>>) s -> s.peek(e -> { counter.incrementAndGet(); }),
  59                     (UnaryOperator<Stream<Integer>>) s -> s.map(id).peek(e -> { counter.incrementAndGet(); }).sequential().map(id),
  60                     (UnaryOperator<Stream<Integer>>) s -> s.map(id).peek(e -> { counter.incrementAndGet(); }).parallel().map(id),
  61                     (UnaryOperator<Stream<Integer>>) s -> s.sequential().map(id).peek(e -> {
  62                         counter.incrementAndGet();
  63                     }).map(id),
  64                     (UnaryOperator<Stream<Integer>>) s -> s.parallel().map(id).peek(e -> { counter.incrementAndGet(); }).map(id)
  65         };
  66 
  67         for (int i = 0; i < suppliers.length; i++) {
  68             setContext("supplierIndex", i);
  69             Supplier<Stream<Integer>> supp = suppliers[i];
  70             for (int j = 0; j < configs.length; j++) {
  71                 setContext("configIndex", j);
  72                 UnaryOperator<Stream<Integer>> config = configs[j];
  73                 counter.set(0);
  74                 Stream<Integer> stream = config.apply(supp.get());
  75                 assertEquals(0, counter.get());
  76 
  77                 Iterator<Integer> iterator = stream.iterator();
  78                 assertEquals(0, counter.get());
  79 
  80                 if (iterator.hasNext())
  81                     iterator.next();
  82                 assertTrue(data.size() == 0 || counter.get() > 0);
  83 
  84                 counter.set(0);
  85                 stream = config.apply(supp.get());
  86                 Spliterator<Integer> spliterator = stream.spliterator();
  87                 assertEquals(0, counter.get());
  88 
  89                 spliterator.forEachRemaining(e -> {
  90                 });
  91                 assertTrue(data.size() == 0 || counter.get() > 0);
  92             }
  93         }
  94     }
  95 
  96     @SuppressWarnings({"rawtypes", "unchecked"})
  97     @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
  98     public void testMixedSeqPar(String name, TestData.OfRef<Integer> data) {
  99         Function<Integer, Integer> id = LambdaTestHelpers.identity();
 100         UnaryOperator<Stream<Integer>>[] changers
 101                 = new UnaryOperator[] {
 102                 (UnaryOperator<Stream<Integer>>) s -> s,
 103                 (UnaryOperator<Stream<Integer>>) s -> s.sequential(),
 104                 (UnaryOperator<Stream<Integer>>) s -> s.parallel(),
 105                 (UnaryOperator<Stream<Integer>>) s -> s.unordered()
 106         };
 107         UnaryOperator<Stream<Integer>>[] stuff
 108                 = new UnaryOperator[] {
 109                 (UnaryOperator<Stream<Integer>>) s -> s,
 110                 (UnaryOperator<Stream<Integer>>) s -> s.map(id),
 111                 (UnaryOperator<Stream<Integer>>) s -> s.sorted(Comparator.naturalOrder()),
 112                 (UnaryOperator<Stream<Integer>>) s -> s.map(id).sorted(Comparator.naturalOrder()).map(id),
 113                 (UnaryOperator<Stream<Integer>>) s -> s.filter(LambdaTestHelpers.pEven).sorted(Comparator.naturalOrder()).map(id),
 114         };
 115 
 116         for (int c1Index = 0; c1Index < changers.length; c1Index++) {
 117             setContext("c1Index", c1Index);
 118             UnaryOperator<Stream<Integer>> c1 = changers[c1Index];
 119             for (int s1Index = 0; s1Index < stuff.length; s1Index++) {
 120                 setContext("s1Index", s1Index);
 121                 UnaryOperator<Stream<Integer>> s1 = stuff[s1Index];
 122                 for (int c2Index = 0; c2Index < changers.length; c2Index++) {
 123                     setContext("c2Index", c2Index);
 124                     UnaryOperator<Stream<Integer>> c2 = changers[c2Index];
 125                     for (int s2Index = 0; s2Index < stuff.length; s2Index++) {
 126                         setContext("s2Index", s2Index);
 127                         UnaryOperator<Stream<Integer>> s2 = stuff[s2Index];
 128                         UnaryOperator<Stream<Integer>> composed = s -> s2.apply(c2.apply(s1.apply(c1.apply(s))));
 129                         exerciseOps(data, composed);
 130                     }
 131                 }
 132             }
 133         }
 134     }
 135 }