• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013, 2016, 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.DataProvider;
31 import org.testng.annotations.Test;
32 
33 import java.util.Arrays;
34 import java.util.Collections;
35 import java.util.List;
36 import java.util.function.Function;
37 import java.util.stream.DoubleStream;
38 import java.util.stream.IntStream;
39 import java.util.stream.LongStream;
40 import java.util.stream.Stream;
41 
42 import static java.util.stream.Collectors.toList;
43 import static org.openjdk.testlib.java.util.stream.ThrowableHelper.checkISE;
44 
45 import android.platform.test.annotations.LargeTest;
46 
47 @Test
48 public class StreamBuilderTest extends OpTestCase {
49 
50     List<Integer> sizes = Arrays.asList(0, 1, 4, 16, 256,
51                                         1023, 1024, 1025,
52                                         2047, 2048, 2049,
53                                         1024 * 32 - 1, 1024 * 32, 1024 * 32 + 1);
54 
55     @DataProvider(name = "sizes")
createStreamBuilders()56     public Object[][] createStreamBuilders() {
57         return sizes.stream().map(i -> new Object[] { i }).toArray(Object[][]::new);
58     }
59 
60     @Test
testOfNullableWithNonNull()61     public void testOfNullableWithNonNull() {
62         TestData.OfRef<Integer> data = TestData.Factory.ofSupplier("{1}",
63                                                                    () -> Stream.ofNullable(1));
64 
65         withData(data).
66                 stream(s -> s).
67                 expectedResult(Collections.singletonList(1)).
68                 exercise();
69     }
70 
71     @Test
testOfNullableWithNull()72     public void testOfNullableWithNull() {
73         TestData.OfRef<Integer> data = TestData.Factory.ofSupplier("{null})",
74                                                                    () -> Stream.ofNullable(null));
75 
76         withData(data).
77                 stream(s -> s).
78                 expectedResult(Collections.emptyList()).
79                 exercise();
80     }
81 
82     @Test
testSingleton()83     public void testSingleton() {
84         TestData.OfRef<Integer> data = TestData.Factory.ofSupplier("{1}",
85                                                                    () -> Stream.of(1));
86 
87         withData(data).
88                 stream(s -> s).
89                 expectedResult(Collections.singletonList(1)).
90                 exercise();
91 
92         withData(data).
93                 stream(s -> s.map(LambdaTestHelpers.identity())).
94                 expectedResult(Collections.singletonList(1)).
95                 exercise();
96     }
97 
98     @Test(dataProvider = "sizes")
testAfterBuilding(int size)99     public void testAfterBuilding(int size) {
100         Stream.Builder<Integer> sb = Stream.builder();
101         IntStream.range(0, size).boxed().forEach(sb);
102         sb.build();
103 
104         checkISE(() -> sb.accept(1));
105         checkISE(() -> sb.add(1));
106         checkISE(() -> sb.build());
107     }
108 
109     @LargeTest
110     @Test(dataProvider = "sizes", groups = { "serialization-hostile" })
testStreamBuilder(int size)111     public void testStreamBuilder(int size) {
112         testStreamBuilder(size, (s) -> {
113             Stream.Builder<Integer> sb = Stream.builder();
114             IntStream.range(0, s).boxed().forEach(sb);
115             return sb.build();
116         });
117 
118         testStreamBuilder(size, (s) -> {
119             Stream.Builder<Integer> sb = Stream.builder();
120             IntStream.range(0, s).boxed().forEach(i -> {
121                 Stream.Builder<Integer> _sb = sb.add(i);
122                 assertTrue(sb == _sb);
123             });
124             return sb.build();
125         });
126     }
127 
testStreamBuilder(int size, Function<Integer, Stream<Integer>> supplier)128     private void testStreamBuilder(int size, Function<Integer, Stream<Integer>> supplier) {
129         TestData.OfRef<Integer> data = TestData.Factory.ofSupplier(String.format("[0, %d)", size),
130                                                                    () -> supplier.apply(size));
131 
132         withData(data).
133                 stream(s -> s).
134                 expectedResult(IntStream.range(0, size).boxed().collect(toList())).
135                 exercise();
136 
137         withData(data).
138                 stream(s -> s.map(LambdaTestHelpers.identity())).
139                 expectedResult(IntStream.range(0, size).boxed().collect(toList())).
140                 exercise();
141     }
142 
143     //
144 
145     @Test
testIntSingleton()146     public void testIntSingleton() {
147         TestData.OfInt data = TestData.Factory.ofIntSupplier("{1}",
148                                                              () -> IntStream.of(1));
149 
150         withData(data).
151                 stream(s -> s).
152                 expectedResult(Collections.singletonList(1)).
153                 exercise();
154 
155         withData(data).
156                 stream(s -> s.map(i -> i)).
157                 expectedResult(Collections.singletonList(1)).
158                 exercise();
159     }
160 
161     @Test(dataProvider = "sizes")
testIntAfterBuilding(int size)162     public void testIntAfterBuilding(int size) {
163         IntStream.Builder sb = IntStream.builder();
164         IntStream.range(0, size).forEach(sb);
165         sb.build();
166 
167         checkISE(() -> sb.accept(1));
168         checkISE(() -> sb.add(1));
169         checkISE(() -> sb.build());
170     }
171 
172     @LargeTest
173     @Test(dataProvider = "sizes", groups = { "serialization-hostile" })
testIntStreamBuilder(int size)174     public void testIntStreamBuilder(int size) {
175         testIntStreamBuilder(size, (s) -> {
176             IntStream.Builder sb = IntStream.builder();
177             IntStream.range(0, s).forEach(sb);
178             return sb.build();
179         });
180 
181         testIntStreamBuilder(size, (s) -> {
182             IntStream.Builder sb = IntStream.builder();
183             IntStream.range(0, s).forEach(i -> {
184                 IntStream.Builder _sb = sb.add(i);
185                 assertTrue(sb == _sb);
186             });
187             return sb.build();
188         });
189     }
190 
testIntStreamBuilder(int size, Function<Integer, IntStream> supplier)191     private void testIntStreamBuilder(int size, Function<Integer, IntStream> supplier) {
192         TestData.OfInt data = TestData.Factory.ofIntSupplier(String.format("[0, %d)", size),
193                                                              () -> supplier.apply(size));
194 
195         withData(data).
196                 stream(s -> s).
197                 expectedResult(IntStream.range(0, size).toArray()).
198                 exercise();
199 
200         withData(data).
201                 stream(s -> s.map(i -> i)).
202                 expectedResult(IntStream.range(0, size).toArray()).
203                 exercise();
204     }
205 
206     //
207 
208     @Test
testLongSingleton()209     public void testLongSingleton() {
210         TestData.OfLong data = TestData.Factory.ofLongSupplier("{1}",
211                                                                () -> LongStream.of(1));
212 
213         withData(data).
214                 stream(s -> s).
215                 expectedResult(Collections.singletonList(1L)).
216                 exercise();
217 
218         withData(data).
219                 stream(s -> s.map(i -> i)).
220                 expectedResult(Collections.singletonList(1L)).
221                 exercise();
222     }
223 
224     @Test(dataProvider = "sizes")
testLongAfterBuilding(int size)225     public void testLongAfterBuilding(int size) {
226         LongStream.Builder sb = LongStream.builder();
227         LongStream.range(0, size).forEach(sb);
228         sb.build();
229 
230         checkISE(() -> sb.accept(1));
231         checkISE(() -> sb.add(1));
232         checkISE(() -> sb.build());
233     }
234 
235     @LargeTest
236     @Test(dataProvider = "sizes", groups = { "serialization-hostile" })
testLongStreamBuilder(int size)237     public void testLongStreamBuilder(int size) {
238         testLongStreamBuilder(size, (s) -> {
239             LongStream.Builder sb = LongStream.builder();
240             LongStream.range(0, s).forEach(sb);
241             return sb.build();
242         });
243 
244         testLongStreamBuilder(size, (s) -> {
245             LongStream.Builder sb = LongStream.builder();
246             LongStream.range(0, s).forEach(i -> {
247                 LongStream.Builder _sb = sb.add(i);
248                 assertTrue(sb == _sb);
249             });
250             return sb.build();
251         });
252     }
253 
testLongStreamBuilder(int size, Function<Integer, LongStream> supplier)254     private void testLongStreamBuilder(int size, Function<Integer, LongStream> supplier) {
255         TestData.OfLong data = TestData.Factory.ofLongSupplier(String.format("[0, %d)", size),
256                                                                () -> supplier.apply(size));
257 
258         withData(data).
259                 stream(s -> s).
260                 expectedResult(LongStream.range(0, size).toArray()).
261                 exercise();
262 
263         withData(data).
264                 stream(s -> s.map(i -> i)).
265                 expectedResult(LongStream.range(0, size).toArray()).
266                 exercise();
267     }
268 
269     //
270 
271     @Test
testDoubleSingleton()272     public void testDoubleSingleton() {
273         TestData.OfDouble data = TestData.Factory.ofDoubleSupplier("{1}", () -> DoubleStream.of(1));
274 
275         withData(data).
276                 stream(s -> s).
277                 expectedResult(Collections.singletonList(1.0)).
278                 exercise();
279 
280         withData(data).
281                 stream(s -> s.map(i -> i)).
282                 expectedResult(Collections.singletonList(1.0)).
283                 exercise();
284     }
285 
286     @Test(dataProvider = "sizes")
testDoubleAfterBuilding(int size)287     public void testDoubleAfterBuilding(int size) {
288         DoubleStream.Builder sb = DoubleStream.builder();
289         IntStream.range(0, size).asDoubleStream().forEach(sb);
290         sb.build();
291 
292         checkISE(() -> sb.accept(1));
293         checkISE(() -> sb.add(1));
294         checkISE(() -> sb.build());
295     }
296 
297     @LargeTest
298     @Test(dataProvider = "sizes", groups = { "serialization-hostile" })
testDoubleStreamBuilder(int size)299     public void testDoubleStreamBuilder(int size) {
300         testDoubleStreamBuilder(size, (s) -> {
301             DoubleStream.Builder sb = DoubleStream.builder();
302             IntStream.range(0, s).asDoubleStream().forEach(sb);
303             return sb.build();
304         });
305 
306         testDoubleStreamBuilder(size, (s) -> {
307             DoubleStream.Builder sb = DoubleStream.builder();
308             IntStream.range(0, s).asDoubleStream().forEach(i -> {
309                 DoubleStream.Builder _sb = sb.add(i);
310                 assertTrue(sb == _sb);
311             });
312             return sb.build();
313         });
314     }
315 
testDoubleStreamBuilder(int size, Function<Integer, DoubleStream> supplier)316     private void testDoubleStreamBuilder(int size, Function<Integer, DoubleStream> supplier) {
317         TestData.OfDouble data = TestData.Factory.ofDoubleSupplier(String.format("[0, %d)", size),
318                                                                    () -> supplier.apply(size));
319 
320         withData(data).
321                 stream(s -> s).
322                 expectedResult(IntStream.range(0, size).asDoubleStream().toArray()).
323                 exercise();
324 
325         withData(data).
326                 stream(s -> s.map(i -> i)).
327                 expectedResult(IntStream.range(0, size).asDoubleStream().toArray()).
328                 exercise();
329     }
330 
331 }
332