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