• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.OpTestCase;
26 import org.openjdk.testlib.java.util.stream.SpliteratorTestHelper;
27 import org.openjdk.testlib.java.util.stream.TestData;
28 
29 import java.util.Arrays;
30 import java.util.Optional;
31 import java.util.Spliterator;
32 import java.util.stream.IntStream;
33 import java.util.stream.LongStream;
34 import java.util.stream.Stream;
35 
36 import org.testng.annotations.Test;
37 
38 /**
39  * Primitive range tests
40  *
41  * @author Brian Goetz
42  */
43 @Test
44 public class RangeTest extends OpTestCase {
45 
testInfiniteRangeFindFirst()46     public void testInfiniteRangeFindFirst() {
47         Integer first = Stream.iterate(0, i -> i + 1).filter(i -> i > 10000).findFirst().get();
48         assertEquals(first, Stream.iterate(0, i -> i + 1).parallel().filter(i -> i > 10000).findFirst().get());
49 
50         // Limit is required to transform the infinite stream to a finite stream
51         // since the exercising requires a finite stream
52         withData(TestData.Factory.ofSupplier(
53                 "", () -> Stream.iterate(0, i -> i + 1).filter(i -> i > 10000).limit(20000))).
54                 terminal(s->s.findFirst()).expectedResult(Optional.of(10001)).exercise();
55     }
56 
57     //
58 
testIntRange()59     public void testIntRange() {
60         // Half-open
61         for (int start : Arrays.asList(1, 10, -1, -10)) {
62             setContext("start", start);
63             for (int end : Arrays.asList(1, 10, -1, -10)) {
64                 setContext("end", end);
65                 int size = (start < end) ? end - start : 0;
66                 int[] exp = new int[size];
67                 for (int i = start, p = 0; i < end; i++, p++) {
68                     exp[p] = i;
69                 }
70 
71                 int[] inc = IntStream.range(start, end).toArray();
72                 assertEquals(inc.length, size);
73                 assertTrue(Arrays.equals(exp, inc));
74 
75                 withData(intRangeData(start, end)).stream(s -> s).
76                         expectedResult(exp).exercise();
77             }
78         }
79 
80         // Closed
81         for (int start : Arrays.asList(1, 10, -1, -10)) {
82             setContext("start", start);
83             for (int end : Arrays.asList(1, 10, -1, -10)) {
84                 setContext("end", end);
85                 int size = (start <= end) ? end - start + 1 : 0;
86                 int[] exp = new int[size];
87                 for (int i = start, p = 0; i <= end; i++, p++) {
88                     exp[p] = i;
89                 }
90 
91                 int[] inc = IntStream.rangeClosed(start, end).toArray();
92                 assertEquals(inc.length, size);
93                 assertTrue(Arrays.equals(exp, inc));
94 
95                 withData(intRangeClosedData(start, end)).stream(s -> s).
96                         expectedResult(exp).exercise();
97             }
98         }
99 
100         // Closed, maximum upper bound of Integer.MAX_VALUE
101         {
102             int[] inc = IntStream.rangeClosed(Integer.MAX_VALUE - 1, Integer.MAX_VALUE).toArray();
103             assertEquals(2, inc.length);
104             assertEquals(Integer.MAX_VALUE - 1, inc[0]);
105             assertEquals(Integer.MAX_VALUE, inc[1]);
106 
107             inc = IntStream.rangeClosed(Integer.MAX_VALUE, Integer.MAX_VALUE).toArray();
108             assertEquals(1, inc.length);
109             assertEquals(Integer.MAX_VALUE, inc[0]);
110 
111             SpliteratorTestHelper.testIntSpliterator(
112                     () -> IntStream.rangeClosed(Integer.MAX_VALUE - 8, Integer.MAX_VALUE).spliterator());
113         }
114 
115         // Range wider than Integer.MAX_VALUE
116         {
117             Spliterator.OfInt s = IntStream.rangeClosed(Integer.MIN_VALUE, Integer.MAX_VALUE).
118                     spliterator();
119             assertEquals(s.estimateSize(), 1L << 32);
120         }
121     }
122 
intRangeData(int start, int end)123     TestData.OfInt intRangeData(int start, int end) {
124         return TestData.Factory.ofIntSupplier("int range", () -> IntStream.range(start, end));
125     }
126 
intRangeClosedData(int start, int end)127     TestData.OfInt intRangeClosedData(int start, int end) {
128         return TestData.Factory.ofIntSupplier("int rangeClosed", () -> IntStream.rangeClosed(start, end));
129     }
130 
tesIntRangeReduce()131     public void tesIntRangeReduce() {
132         withData(intRangeData(0, 10000)).
133                 terminal(s -> s.reduce(0, Integer::sum)).exercise();
134     }
135 
testIntInfiniteRangeLimit()136     public void testIntInfiniteRangeLimit() {
137         withData(TestData.Factory.ofIntSupplier(
138                 "int range", () -> IntStream.iterate(0, i -> i + 1).limit(10000))).
139                 terminal(s -> s.reduce(0, Integer::sum)).exercise();
140     }
141 
testIntInfiniteRangeFindFirst()142     public void testIntInfiniteRangeFindFirst() {
143         int first = IntStream.iterate(0, i -> i + 1).filter(i -> i > 10000).findFirst().getAsInt();
144         assertEquals(first, IntStream.iterate(0, i -> i + 1).parallel().filter(i -> i > 10000).findFirst().getAsInt());
145     }
146 
147     //
148 
testLongRange()149     public void testLongRange() {
150         // Half-open
151         for (long start : Arrays.asList(1, 1000, -1, -1000)) {
152             setContext("start", start);
153             for (long end : Arrays.asList(1, 1000, -1, -1000)) {
154                 setContext("end", end);
155                 long size = start < end ? end - start : 0;
156                 long[] exp = new long[(int) size];
157                 for (long i = start, p = 0; i < end; i++, p++) {
158                     exp[(int) p] = i;
159                 }
160 
161                 long[] inc = LongStream.range(start, end).toArray();
162                 assertEquals(inc.length, size);
163                 assertTrue(Arrays.equals(exp, inc));
164 
165                 withData(longRangeData(start, end)).stream(s -> s).
166                         expectedResult(exp).exercise();
167             }
168         }
169 
170         // Closed
171         for (long start : Arrays.asList(1, 1000, -1, -1000)) {
172             setContext("start", start);
173             for (long end : Arrays.asList(1, 1000, -1, -1000)) {
174                 setContext("end", end);
175                 long size = start <= end ? end - start + 1: 0;
176                 long[] exp = new long[(int) size];
177                 for (long i = start, p = 0; i <= end; i++, p++) {
178                     exp[(int) p] = i;
179                 }
180 
181                 long[] inc = LongStream.rangeClosed(start, end).toArray();
182                 assertEquals(inc.length, size);
183                 assertTrue(Arrays.equals(exp, inc));
184 
185                 withData(longRangeClosedData(start, end)).stream(s -> s).
186                         expectedResult(exp).exercise();
187             }
188         }
189 
190         // Closed, maximum upper bound of Long.MAX_VALUE
191         {
192             long[] inc = LongStream.rangeClosed(Long.MAX_VALUE - 1, Long.MAX_VALUE).toArray();
193             assertEquals(2, inc.length);
194             assertEquals(Long.MAX_VALUE - 1, inc[0]);
195             assertEquals(Long.MAX_VALUE, inc[1]);
196 
197             inc = LongStream.rangeClosed(Long.MAX_VALUE, Long.MAX_VALUE).toArray();
198             assertEquals(1, inc.length);
199             assertEquals(Long.MAX_VALUE, inc[0]);
200 
201             SpliteratorTestHelper.testLongSpliterator(
202                     () -> LongStream.rangeClosed(Long.MAX_VALUE - 8, Long.MAX_VALUE).spliterator());
203         }
204     }
205 
206     TestData.OfLong longRangeData(long start, long end) {
207         return TestData.Factory.ofLongSupplier("long range", () -> LongStream.range(start, end));
208     }
209 
longRangeClosedData(long start, long end)210     TestData.OfLong longRangeClosedData(long start, long end) {
211         return TestData.Factory.ofLongSupplier("long rangeClosed", () -> LongStream.rangeClosed(start, end));
212     }
213 
testLongRangeReduce()214     public void testLongRangeReduce() {
215         withData(longRangeData(0, 10000)).
216                 terminal(s -> s.reduce(0, Long::sum)).exercise();
217     }
218 
testLongInfiniteRangeLimit()219     public void testLongInfiniteRangeLimit() {
220         withData(TestData.Factory.ofLongSupplier(
221                 "long range", () -> LongStream.iterate(0, i -> i + 1).limit(10000))).
222                 terminal(s -> s.reduce(0, Long::sum)).exercise();
223     }
224 
testLongInfiniteRangeFindFirst()225     public void testLongInfiniteRangeFindFirst() {
226         long first = LongStream.iterate(0, i -> i + 1).filter(i -> i > 10000).findFirst().getAsLong();
227         assertEquals(first, LongStream.iterate(0, i -> i + 1).parallel().filter(i -> i > 10000).findFirst().getAsLong());
228     }
229 
assertSizedAndSubSized(Spliterator<?> s)230     private static void assertSizedAndSubSized(Spliterator<?> s) {
231         assertTrue(s.hasCharacteristics(Spliterator.SIZED | Spliterator.SUBSIZED));
232     }
233 
assertNotSizedAndSubSized(Spliterator<?> s)234     private static void assertNotSizedAndSubSized(Spliterator<?> s) {
235         assertFalse(s.hasCharacteristics(Spliterator.SIZED | Spliterator.SUBSIZED));
236     }
237 
testLongLongRange()238     public void testLongLongRange() {
239         // Test [Long.MIN_VALUE, Long.MAX_VALUE)
240         // This will concatenate streams of three ranges
241         //   [Long.MIN_VALUE, x) [x, 0) [0, Long.MAX_VALUE)
242         // where x = Long.divideUnsigned(0 - Long.MIN_VALUE, 2) + 1
243         {
244             Spliterator.OfLong s = LongStream.range(Long.MIN_VALUE, Long.MAX_VALUE).spliterator();
245 
246             assertEquals(s.estimateSize(), Long.MAX_VALUE);
247             assertNotSizedAndSubSized(s);
248 
249             Spliterator.OfLong s1 = s.trySplit();
250             assertNotSizedAndSubSized(s1);
251             assertSizedAndSubSized(s);
252 
253             Spliterator.OfLong s2 = s1.trySplit();
254             assertSizedAndSubSized(s1);
255             assertSizedAndSubSized(s2);
256 
257             assertTrue(s.estimateSize() == Long.MAX_VALUE);
258             assertTrue(s1.estimateSize() < Long.MAX_VALUE);
259             assertTrue(s2.estimateSize() < Long.MAX_VALUE);
260 
261             assertEquals(s.estimateSize() + s1.estimateSize() + s2.estimateSize(),
262                          Long.MAX_VALUE - Long.MIN_VALUE);
263         }
264 
265         long[][] ranges = { {Long.MIN_VALUE, 0}, {-1, Long.MAX_VALUE} };
266         for (int i = 0; i < ranges.length; i++) {
267             long start = ranges[i][0];
268             long end = ranges[i][1];
269 
270             Spliterator.OfLong s = LongStream.range(start, end).spliterator();
271 
272             assertEquals(s.estimateSize(), Long.MAX_VALUE);
273             assertNotSizedAndSubSized(s);
274 
275             Spliterator.OfLong s1 = s.trySplit();
276             assertSizedAndSubSized(s1);
277             assertSizedAndSubSized(s);
278 
279             assertTrue(s.estimateSize() < Long.MAX_VALUE);
280             assertTrue(s1.estimateSize() < Long.MAX_VALUE);
281 
282             assertEquals(s.estimateSize() + s1.estimateSize(), end - start);
283         }
284     }
285 
286     public void testLongLongRangeClosed() {
287         // Test [Long.MIN_VALUE, Long.MAX_VALUE]
288         // This will concatenate streams of four ranges
289         //   [Long.MIN_VALUE, x) [x, 0) [0, y) [y, Long.MAX_VALUE]
290         // where x = Long.divideUnsigned(0 - Long.MIN_VALUE, 2) + 1
291         //       y = Long.divideUnsigned(Long.MAX_VALUE, 2) + 1
292 
293         {
294             Spliterator.OfLong s = LongStream.rangeClosed(Long.MIN_VALUE, Long.MAX_VALUE).spliterator();
295 
296             assertEquals(s.estimateSize(), Long.MAX_VALUE);
297             assertNotSizedAndSubSized(s);
298 
299             Spliterator.OfLong s1 = s.trySplit();
300             assertNotSizedAndSubSized(s1);
301             assertNotSizedAndSubSized(s);
302 
303             Spliterator.OfLong s2 = s1.trySplit();
304             assertSizedAndSubSized(s1);
305             assertSizedAndSubSized(s2);
306 
307             Spliterator.OfLong s3 = s.trySplit();
308             assertSizedAndSubSized(s3);
309             assertSizedAndSubSized(s);
310 
311             assertTrue(s.estimateSize() < Long.MAX_VALUE);
312             assertTrue(s3.estimateSize() < Long.MAX_VALUE);
313             assertTrue(s1.estimateSize() < Long.MAX_VALUE);
314             assertTrue(s2.estimateSize() < Long.MAX_VALUE);
315 
316             assertEquals(s.estimateSize() + s3.estimateSize() + s1.estimateSize() + s2.estimateSize(),
317                          Long.MAX_VALUE - Long.MIN_VALUE + 1);
318         }
319 
320         long[][] ranges = { {Long.MIN_VALUE, 0}, {-1, Long.MAX_VALUE} };
321         for (int i = 0; i < ranges.length; i++) {
322             long start = ranges[i][0];
323             long end = ranges[i][1];
324 
325             Spliterator.OfLong s = LongStream.rangeClosed(start, end).spliterator();
326 
327             assertEquals(s.estimateSize(), Long.MAX_VALUE);
328             assertNotSizedAndSubSized(s);
329 
330             Spliterator.OfLong s1 = s.trySplit();
331             assertSizedAndSubSized(s1);
332             assertSizedAndSubSized(s);
333 
334             assertTrue(s.estimateSize() < Long.MAX_VALUE);
335             assertTrue(s1.estimateSize() < Long.MAX_VALUE);
336 
337             assertEquals(s.estimateSize() + s1.estimateSize(), end - start + 1);
338         }
339     }
340 }
341