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