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.testng.annotations.Test; 26 27 import java.util.ArrayList; 28 import java.util.Collections; 29 import java.util.List; 30 import java.util.concurrent.atomic.AtomicInteger; 31 import java.util.function.Function; 32 import java.util.stream.*; 33 34 import static java.util.stream.LambdaTestHelpers.countTo; 35 36 /** 37 * ForEachOpTest 38 */ 39 @Test 40 public class ForEachOpTest extends OpTestCase { 41 42 @Test(groups = { "serialization-hostile" }) testForEach()43 public void testForEach() { 44 exerciseTerminalOps(countTo(10), 45 s -> { 46 AtomicInteger count = new AtomicInteger(0); 47 s.forEach(e -> count.incrementAndGet()); 48 return count.get(); 49 }, 50 10); 51 52 exerciseTerminalOps(countTo(10), 53 s -> { 54 AtomicInteger sum = new AtomicInteger(0); 55 s.forEach(sum::addAndGet); 56 return sum.get(); 57 }, 58 55); 59 } 60 resultAsserter()61 private <U> ResultAsserter<List<U>> resultAsserter() { 62 return (act, exp, ord, par) -> { 63 if (par) { 64 LambdaTestHelpers.assertContentsUnordered(act, exp); 65 } 66 else { 67 LambdaTestHelpers.assertContents(act, exp); 68 } 69 }; 70 } 71 72 @Test(groups = { "serialization-hostile" }) 73 public void testForEachOrdered() { 74 List<Integer> input = countTo(10000); 75 TestData.OfRef<Integer> data = TestData.Factory.ofCollection("[1, 10000]", input); 76 77 Function<Stream<Integer>, List<Integer>> terminalFunc = s -> { 78 List<Integer> l = new ArrayList<>(); 79 s.forEachOrdered(l::add); 80 return l; 81 }; 82 83 // Test head 84 withData(data). 85 terminal(terminalFunc). 86 expectedResult(input). 87 exercise(); 88 89 // Test multiple stages 90 withData(data). 91 terminal(s -> s.map(LambdaTestHelpers.identity()), terminalFunc). 92 expectedResult(input). 93 exercise(); 94 } 95 96 @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class) 97 public void testForEach(String name, TestData.OfRef<Integer> data) { 98 Function<Stream<Integer>, List<Integer>> terminalFunc = s -> { 99 List<Integer> l = Collections.synchronizedList(new ArrayList<>()); 100 s.forEach(l::add); 101 return l; 102 }; 103 104 // Test head 105 withData(data). 106 terminal(terminalFunc). 107 resultAsserter(resultAsserter()). 108 exercise(); 109 110 // Test multiple stages 111 withData(data). 112 terminal(s -> s.map(LambdaTestHelpers.identity()), terminalFunc). 113 resultAsserter(resultAsserter()). 114 exercise(); 115 } 116 117 // 118 119 @Test(groups = { "serialization-hostile" }) 120 public void testIntForEachOrdered() { 121 List<Integer> input = countTo(10000); 122 TestData.OfInt data = TestData.Factory.ofIntSupplier("[1, 10000]", 123 () -> IntStream.range(1, 10001)); 124 125 Function<IntStream, List<Integer>> terminalFunc = s -> { 126 List<Integer> l = new ArrayList<>(); 127 s.forEachOrdered(l::add); 128 return l; 129 }; 130 131 // Test head 132 withData(data). 133 terminal(terminalFunc). 134 expectedResult(input). 135 exercise(); 136 137 // Test multiple stages 138 withData(data). 139 terminal(s -> s.map(i -> i), terminalFunc). 140 expectedResult(input). 141 exercise(); 142 } 143 144 @Test(dataProvider = "IntStreamTestData", dataProviderClass = IntStreamTestDataProvider.class) 145 public void testIntForEach(String name, TestData.OfInt data) { 146 Function<IntStream, List<Integer>> terminalFunc = s -> { 147 List<Integer> l = Collections.synchronizedList(new ArrayList<Integer>()); 148 s.forEach(l::add); 149 return l; 150 }; 151 152 // Test head 153 withData(data). 154 terminal(terminalFunc). 155 resultAsserter(resultAsserter()). 156 exercise(); 157 158 // Test multiple stages 159 withData(data). 160 terminal(s -> s.map(i -> i), terminalFunc). 161 resultAsserter(resultAsserter()). 162 exercise(); 163 } 164 165 // 166 167 @Test(groups = { "serialization-hostile" }) 168 public void testLongForEachOrdered() { 169 List<Integer> input = countTo(10000); 170 TestData.OfLong data = TestData.Factory.ofLongSupplier("[1, 10000]", 171 () -> LongStream.range(1, 10001)); 172 173 Function<LongStream, List<Integer>> terminalFunc = s -> { 174 List<Integer> l = new ArrayList<>(); 175 s.forEachOrdered(e -> l.add((int) e)); 176 return l; 177 }; 178 179 // Test head 180 withData(data). 181 terminal(terminalFunc). 182 expectedResult(input). 183 exercise(); 184 185 // Test multiple stages 186 withData(data). 187 terminal(s -> s.map(i -> i), terminalFunc). 188 expectedResult(input). 189 exercise(); 190 } 191 192 @Test(dataProvider = "LongStreamTestData", dataProviderClass = LongStreamTestDataProvider.class) 193 public void testLongOps(String name, TestData.OfLong data) { 194 Function<LongStream, List<Long>> terminalFunc = s -> { 195 List<Long> l = Collections.synchronizedList(new ArrayList<Long>()); 196 s.forEach(l::add); 197 return l; 198 }; 199 200 // Test head 201 withData(data). 202 terminal(terminalFunc). 203 resultAsserter(resultAsserter()). 204 exercise(); 205 206 // Test multiple stages 207 withData(data). 208 terminal(s -> s.map(i -> i), terminalFunc). 209 resultAsserter(resultAsserter()). 210 exercise(); 211 } 212 213 // 214 215 @Test(groups = { "serialization-hostile" }) 216 public void testDoubleForEachOrdered() { 217 List<Integer> input = countTo(10000); 218 TestData.OfDouble data = TestData.Factory.ofDoubleSupplier("[1, 10000]", 219 () -> IntStream.range(1, 10001).asDoubleStream()); 220 221 Function<DoubleStream, List<Integer>> terminalFunc = s -> { 222 List<Integer> l = new ArrayList<>(); 223 s.forEachOrdered(e -> l.add((int) e)); 224 return l; 225 }; 226 227 // Test head 228 withData(data). 229 terminal(terminalFunc). 230 expectedResult(input). 231 exercise(); 232 233 // Test multiple stages 234 withData(data). 235 terminal(s -> s.map(i -> i), terminalFunc). 236 expectedResult(input). 237 exercise(); 238 } 239 240 @Test(dataProvider = "DoubleStreamTestData", dataProviderClass = DoubleStreamTestDataProvider.class) 241 public void testDoubleOps(String name, TestData.OfDouble data) { 242 Function<DoubleStream, List<Double>> terminalFunc = s -> { 243 List<Double> l = Collections.synchronizedList(new ArrayList<Double>()); 244 s.forEach(l::add); 245 return l; 246 }; 247 248 // Test head 249 withData(data). 250 terminal(terminalFunc). 251 resultAsserter(resultAsserter()). 252 exercise(); 253 254 // Test multiple stages 255 withData(data). 256 terminal(s -> s.map(i -> i), terminalFunc). 257 resultAsserter(resultAsserter()). 258 exercise(); 259 } 260 261 } 262