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