• 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.DoubleStreamTestDataProvider;
26 import org.openjdk.testlib.java.util.stream.IntStreamTestDataProvider;
27 import org.openjdk.testlib.java.util.stream.LambdaTestHelpers;
28 import org.openjdk.testlib.java.util.stream.LongStreamTestDataProvider;
29 import org.openjdk.testlib.java.util.stream.OpTestCase;
30 import org.openjdk.testlib.java.util.stream.StreamTestDataProvider;
31 import org.openjdk.testlib.java.util.stream.TestData;
32 
33 import java.util.Collection;
34 import java.util.stream.BaseStream;
35 import java.util.stream.Stream;
36 import java.util.stream.IntStream;
37 import java.util.stream.LongStream;
38 import java.util.stream.DoubleStream;
39 
40 import org.testng.annotations.Test;
41 
42 import java.util.Arrays;
43 import java.util.List;
44 import java.util.concurrent.atomic.AtomicInteger;
45 
46 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.assertCountSum;
47 
48 /**
49  * SliceOpTest
50  *
51  * @author Brian Goetz
52  */
53 @Test
54 public class IntSliceOpTest extends OpTestCase {
55 
56     private static final int[] EMPTY_INT_ARRAY = new int[0];
57 
testSkip()58     public void testSkip() {
59         assertCountSum(IntStream.range(0, 0).skip(0).boxed(), 0, 0);
60         assertCountSum(IntStream.range(0, 0).skip(4).boxed(), 0, 0);
61         assertCountSum(IntStream.range(1, 5).skip(4).boxed(), 0, 0);
62         assertCountSum(IntStream.range(1, 5).skip(2).boxed(), 2, 7);
63         assertCountSum(IntStream.range(1, 5).skip(0).boxed(), 4, 10);
64 
65         assertCountSum(IntStream.range(0, 0).parallel().skip(0).boxed(), 0, 0);
66         assertCountSum(IntStream.range(0, 0).parallel().skip(4).boxed(), 0, 0);
67         assertCountSum(IntStream.range(1, 5).parallel().skip(4).boxed(), 0, 0);
68         assertCountSum(IntStream.range(1, 5).parallel().skip(2).boxed(), 2, 7);
69         assertCountSum(IntStream.range(1, 5).parallel().skip(0).boxed(), 4, 10);
70 
71         exerciseOps(EMPTY_INT_ARRAY, s -> s.skip(0), EMPTY_INT_ARRAY);
72         exerciseOps(EMPTY_INT_ARRAY, s -> s.skip(10), EMPTY_INT_ARRAY);
73 
74         exerciseOps(IntStream.range(1, 2).toArray(), s -> s.skip(0), IntStream.range(1, 2).toArray());
75         exerciseOps(IntStream.range(1, 2).toArray(), s -> s.skip(1), EMPTY_INT_ARRAY);
76         exerciseOps(IntStream.range(1, 101).toArray(), s -> s.skip(0), IntStream.range(1, 101).toArray());
77         exerciseOps(IntStream.range(1, 101).toArray(), s -> s.skip(10), IntStream.range(11, 101).toArray());
78         exerciseOps(IntStream.range(1, 101).toArray(), s -> s.skip(100), EMPTY_INT_ARRAY);
79         exerciseOps(IntStream.range(1, 101).toArray(), s -> s.skip(200), EMPTY_INT_ARRAY);
80     }
81 
testLimit()82     public void testLimit() {
83         assertCountSum(IntStream.range(0, 0).limit(4).boxed(), 0, 0);
84         assertCountSum(IntStream.range(1, 3).limit(4).boxed(), 2, 3);
85         assertCountSum(IntStream.range(1, 5).limit(4).boxed(), 4, 10);
86         assertCountSum(IntStream.range(1, 9).limit(4).boxed(), 4, 10);
87 
88         assertCountSum(IntStream.range(0, 0).parallel().limit(4).boxed(), 0, 0);
89         assertCountSum(IntStream.range(1, 3).parallel().limit(4).boxed(), 2, 3);
90         assertCountSum(IntStream.range(1, 5).parallel().limit(4).boxed(), 4, 10);
91         assertCountSum(IntStream.range(1, 9).parallel().limit(4).boxed(), 4, 10);
92 
93         exerciseOps(EMPTY_INT_ARRAY, s -> s.limit(0), EMPTY_INT_ARRAY);
94         exerciseOps(EMPTY_INT_ARRAY, s -> s.limit(10), EMPTY_INT_ARRAY);
95 
96         exerciseOps(IntStream.range(1, 2).toArray(), s -> s.limit(0), EMPTY_INT_ARRAY);
97         exerciseOps(IntStream.range(1, 2).toArray(), s -> s.limit(1), IntStream.range(1, 2).toArray());
98         exerciseOps(IntStream.range(1, 101).toArray(), s -> s.limit(0), EMPTY_INT_ARRAY);
99         exerciseOps(IntStream.range(1, 101).toArray(), s -> s.limit(10), IntStream.range(1, 11).toArray());
100         exerciseOps(IntStream.range(1, 101).toArray(), s -> s.limit(10).limit(10), IntStream.range(1, 11).toArray());
101         exerciseOps(IntStream.range(1, 101).toArray(), s -> s.limit(100), IntStream.range(1, 101).toArray());
102         exerciseOps(IntStream.range(1, 101).toArray(), s -> s.limit(100).limit(10), IntStream.range(1, 11).toArray());
103         exerciseOps(IntStream.range(1, 101).toArray(), s -> s.limit(200), IntStream.range(1, 101).toArray());
104     }
105 
testSkipLimit()106     public void testSkipLimit() {
107         exerciseOps(EMPTY_INT_ARRAY, s -> s.skip(0).limit(0), EMPTY_INT_ARRAY);
108         exerciseOps(EMPTY_INT_ARRAY, s -> s.skip(0).limit(10), EMPTY_INT_ARRAY);
109         exerciseOps(EMPTY_INT_ARRAY, s -> s.skip(10).limit(0), EMPTY_INT_ARRAY);
110         exerciseOps(EMPTY_INT_ARRAY, s -> s.skip(10).limit(10), EMPTY_INT_ARRAY);
111 
112         exerciseOps(IntStream.range(1, 101).toArray(), s -> s.skip(0).limit(100), IntStream.range(1, 101).toArray());
113         exerciseOps(IntStream.range(1, 101).toArray(), s -> s.skip(0).limit(10), IntStream.range(1, 11).toArray());
114         exerciseOps(IntStream.range(1, 101).toArray(), s -> s.skip(0).limit(0), EMPTY_INT_ARRAY);
115         exerciseOps(IntStream.range(1, 101).toArray(), s -> s.skip(10).limit(100), IntStream.range(11, 101).toArray());
116         exerciseOps(IntStream.range(1, 101).toArray(), s -> s.skip(10).limit(10), IntStream.range(11, 21).toArray());
117         exerciseOps(IntStream.range(1, 101).toArray(), s -> s.skip(10).limit(0), EMPTY_INT_ARRAY);
118         exerciseOps(IntStream.range(1, 101).toArray(), s -> s.skip(100).limit(100), EMPTY_INT_ARRAY);
119         exerciseOps(IntStream.range(1, 101).toArray(), s -> s.skip(100).limit(10), EMPTY_INT_ARRAY);
120         exerciseOps(IntStream.range(1, 101).toArray(), s -> s.skip(100).limit(0), EMPTY_INT_ARRAY);
121         exerciseOps(IntStream.range(1, 101).toArray(), s -> s.skip(200).limit(100), EMPTY_INT_ARRAY);
122         exerciseOps(IntStream.range(1, 101).toArray(), s -> s.skip(200).limit(10), EMPTY_INT_ARRAY);
123         exerciseOps(IntStream.range(1, 101).toArray(), s -> s.skip(200).limit(0), EMPTY_INT_ARRAY);
124     }
125 
sliceSize(int dataSize, int skip, int limit)126     private int sliceSize(int dataSize, int skip, int limit) {
127         int size = Math.max(0, dataSize - skip);
128         if (limit >= 0)
129             size = Math.min(size, limit);
130         return size;
131     }
132 
sliceSize(int dataSize, int skip)133     private int sliceSize(int dataSize, int skip) {
134         return Math.max(0, dataSize - skip);
135     }
136 
137     @Test(dataProvider = "IntStreamTestData", dataProviderClass = IntStreamTestDataProvider.class)
testSkipOps(String name, TestData.OfInt data)138     public void testSkipOps(String name, TestData.OfInt data) {
139         List<Integer> skips = sizes(data.size());
140 
141         for (int s : skips) {
142             setContext("skip", s);
143             Collection<Integer> sr = exerciseOps(data, st -> st.skip(s));
144             assertEquals(sr.size(), sliceSize(data.size(), s));
145 
146             sr = exerciseOps(data, st -> st.skip(s).skip(s / 2));
147             assertEquals(sr.size(), sliceSize(sliceSize(data.size(), s), s / 2));
148         }
149     }
150 
151     @Test(dataProvider = "IntStreamTestData", dataProviderClass = IntStreamTestDataProvider.class)
testSkipLimitOps(String name, TestData.OfInt data)152     public void testSkipLimitOps(String name, TestData.OfInt data) {
153         List<Integer> skips = sizes(data.size());
154         List<Integer> limits = skips;
155 
156         for (int s : skips) {
157             setContext("skip", s);
158             for (int limit : limits) {
159                 setContext("limit", limit);
160                 Collection<Integer> sr = exerciseOps(data, st -> st.skip(s).limit(limit));
161                 assertEquals(sr.size(), sliceSize(sliceSize(data.size(), s), 0, limit));
162 
163                 sr = exerciseOps(data, st -> st.skip(s).limit(limit));
164                 assertEquals(sr.size(), sliceSize(data.size(), s, limit));
165             }
166         }
167     }
168 
169     @Test(dataProvider = "IntStreamTestData", dataProviderClass = IntStreamTestDataProvider.class)
testLimitOps(String name, TestData.OfInt data)170     public void testLimitOps(String name, TestData.OfInt data) {
171         List<Integer> limits = sizes(data.size());
172 
173         for (int limit : limits) {
174             setContext("limit", limit);
175             Collection<Integer> sr = exerciseOps(data, st -> st.limit(limit));
176             assertEquals(sr.size(), sliceSize(data.size(), 0, limit));
177 
178             sr = exerciseOps(data, st -> st.limit(limit).limit(limit / 2));
179             assertEquals(sr.size(), sliceSize(sliceSize(data.size(), 0, limit), 0, limit / 2));
180         }
181     }
182 
testLimitSort()183     public void testLimitSort() {
184         exerciseOps(IntStream.range(1, 101).map(i -> 101 - i).toArray(), s -> s.limit(10).sorted());
185     }
186 
187     @Test(groups = { "serialization-hostile" })
testLimitShortCircuit()188     public void testLimitShortCircuit() {
189         for (int l : Arrays.asList(0, 10)) {
190             setContext("limit", l);
191             AtomicInteger ai = new AtomicInteger();
192             IntStream.range(1, 101)
193                     .peek(i -> ai.getAndIncrement())
194                     .limit(l).toArray();
195             // For the case of a zero limit, one element will get pushed through the sink chain
196             assertEquals(ai.get(), l, "tee block was called too many times");
197         }
198     }
199 
testSkipParallel()200     public void testSkipParallel() {
201         int[] l = IntStream.range(1, 1001).parallel().skip(200).limit(200).sequential().toArray();
202         assertEquals(l.length, 200);
203         assertEquals(l[l.length - 1], 400);
204     }
205 
testLimitParallel()206     public void testLimitParallel() {
207         int[] l = IntStream.range(1, 1001).parallel().limit(500).sequential().toArray();
208         assertEquals(l.length, 500);
209         assertEquals(l[l.length - 1], 500);
210     }
211 
sizes(int size)212     private List<Integer> sizes(int size) {
213         if (size < 4) {
214             return Arrays.asList(0, 1, 2, 3, 4, 6);
215         }
216         else {
217             return Arrays.asList(0, 1, size / 2, size - 1, size, size + 1, 2 * size);
218         }
219     }
220 }
221