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