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).skip(0).boxed(), 0, 0); 48 assertCountSum(IntStream.range(0, 0).skip(4).boxed(), 0, 0); 49 assertCountSum(IntStream.range(1, 5).skip(4).boxed(), 0, 0); 50 assertCountSum(IntStream.range(1, 5).skip(2).boxed(), 2, 7); 51 assertCountSum(IntStream.range(1, 5).skip(0).boxed(), 4, 10); 52 53 assertCountSum(IntStream.range(0, 0).parallel().skip(0).boxed(), 0, 0); 54 assertCountSum(IntStream.range(0, 0).parallel().skip(4).boxed(), 0, 0); 55 assertCountSum(IntStream.range(1, 5).parallel().skip(4).boxed(), 0, 0); 56 assertCountSum(IntStream.range(1, 5).parallel().skip(2).boxed(), 2, 7); 57 assertCountSum(IntStream.range(1, 5).parallel().skip(0).boxed(), 4, 10); 58 59 exerciseOps(EMPTY_INT_ARRAY, s -> s.skip(0), EMPTY_INT_ARRAY); 60 exerciseOps(EMPTY_INT_ARRAY, s -> s.skip(10), EMPTY_INT_ARRAY); 61 62 exerciseOps(IntStream.range(1, 2).toArray(), s -> s.skip(0), IntStream.range(1, 2).toArray()); 63 exerciseOps(IntStream.range(1, 2).toArray(), s -> s.skip(1), EMPTY_INT_ARRAY); 64 exerciseOps(IntStream.range(1, 101).toArray(), s -> s.skip(0), IntStream.range(1, 101).toArray()); 65 exerciseOps(IntStream.range(1, 101).toArray(), s -> s.skip(10), IntStream.range(11, 101).toArray()); 66 exerciseOps(IntStream.range(1, 101).toArray(), s -> s.skip(100), EMPTY_INT_ARRAY); 67 exerciseOps(IntStream.range(1, 101).toArray(), s -> s.skip(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.skip(0).limit(0), EMPTY_INT_ARRAY); 96 exerciseOps(EMPTY_INT_ARRAY, s -> s.skip(0).limit(10), EMPTY_INT_ARRAY); 97 exerciseOps(EMPTY_INT_ARRAY, s -> s.skip(10).limit(0), EMPTY_INT_ARRAY); 98 exerciseOps(EMPTY_INT_ARRAY, s -> s.skip(10).limit(10), EMPTY_INT_ARRAY); 99 100 exerciseOps(IntStream.range(1, 101).toArray(), s -> s.skip(0).limit(100), IntStream.range(1, 101).toArray()); 101 exerciseOps(IntStream.range(1, 101).toArray(), s -> s.skip(0).limit(10), IntStream.range(1, 11).toArray()); 102 exerciseOps(IntStream.range(1, 101).toArray(), s -> s.skip(0).limit(0), EMPTY_INT_ARRAY); 103 exerciseOps(IntStream.range(1, 101).toArray(), s -> s.skip(10).limit(100), IntStream.range(11, 101).toArray()); 104 exerciseOps(IntStream.range(1, 101).toArray(), s -> s.skip(10).limit(10), IntStream.range(11, 21).toArray()); 105 exerciseOps(IntStream.range(1, 101).toArray(), s -> s.skip(10).limit(0), EMPTY_INT_ARRAY); 106 exerciseOps(IntStream.range(1, 101).toArray(), s -> s.skip(100).limit(100), EMPTY_INT_ARRAY); 107 exerciseOps(IntStream.range(1, 101).toArray(), s -> s.skip(100).limit(10), EMPTY_INT_ARRAY); 108 exerciseOps(IntStream.range(1, 101).toArray(), s -> s.skip(100).limit(0), EMPTY_INT_ARRAY); 109 exerciseOps(IntStream.range(1, 101).toArray(), s -> s.skip(200).limit(100), EMPTY_INT_ARRAY); 110 exerciseOps(IntStream.range(1, 101).toArray(), s -> s.skip(200).limit(10), EMPTY_INT_ARRAY); 111 exerciseOps(IntStream.range(1, 101).toArray(), s -> s.skip(200).limit(0), EMPTY_INT_ARRAY); 112 } 113 114 private int sliceSize(int dataSize, int skip, int limit) { 115 int size = Math.max(0, dataSize - skip); 116 if (limit >= 0) 117 size = Math.min(size, limit); 118 return size; 119 } 120 121 private int sliceSize(int dataSize, int skip) { 122 return Math.max(0, dataSize - skip); 123 } 124 125 @Test(dataProvider = "IntStreamTestData", dataProviderClass = IntStreamTestDataProvider.class) 126 public void testSkipOps(String name, TestData.OfInt data) { 127 List<Integer> skips = sizes(data.size()); 128 129 for (int s : skips) { 130 setContext("skip", s); 131 Collection<Integer> sr = exerciseOps(data, st -> st.skip(s)); 132 assertEquals(sr.size(), sliceSize(data.size(), s)); 133 134 sr = exerciseOps(data, st -> st.skip(s).skip(s / 2)); 135 assertEquals(sr.size(), sliceSize(sliceSize(data.size(), s), s / 2)); 136 } 137 } 138 139 @Test(dataProvider = "IntStreamTestData", dataProviderClass = IntStreamTestDataProvider.class) 140 public void testSkipLimitOps(String name, TestData.OfInt data) { 141 List<Integer> skips = sizes(data.size()); 142 List<Integer> limits = skips; 143 144 for (int s : skips) { 145 setContext("skip", s); 146 for (int limit : limits) { 147 setContext("limit", limit); 148 Collection<Integer> sr = exerciseOps(data, st -> st.skip(s).limit(limit)); 149 assertEquals(sr.size(), sliceSize(sliceSize(data.size(), s), 0, limit)); 150 151 sr = exerciseOps(data, st -> st.skip(s).limit(limit)); 152 assertEquals(sr.size(), sliceSize(data.size(), s, limit)); 153 } 154 } 155 } 156 157 @Test(dataProvider = "IntStreamTestData", dataProviderClass = IntStreamTestDataProvider.class) 158 public void testLimitOps(String name, TestData.OfInt data) { 159 List<Integer> limits = sizes(data.size()); 160 161 for (int limit : limits) { 162 setContext("limit", limit); 163 Collection<Integer> sr = exerciseOps(data, st -> st.limit(limit)); 164 assertEquals(sr.size(), sliceSize(data.size(), 0, limit)); 165 166 sr = exerciseOps(data, st -> st.limit(limit).limit(limit / 2)); 167 assertEquals(sr.size(), sliceSize(sliceSize(data.size(), 0, limit), 0, limit / 2)); 168 } 169 } 170 171 public void testLimitSort() { 172 exerciseOps(IntStream.range(1, 101).map(i -> 101 - i).toArray(), s -> s.limit(10).sorted()); 173 } 174 175 @Test(groups = { "serialization-hostile" }) 176 public void testLimitShortCircuit() { 177 for (int l : Arrays.asList(0, 10)) { 178 setContext("limit", l); 179 AtomicInteger ai = new AtomicInteger(); 180 IntStream.range(1, 101) 181 .peek(i -> ai.getAndIncrement()) 182 .limit(l).toArray(); 183 // For the case of a zero limit, one element will get pushed through the sink chain 184 assertEquals(ai.get(), l, "tee block was called too many times"); 185 } 186 } 187 188 public void testSkipParallel() { 189 int[] l = IntStream.range(1, 1001).parallel().skip(200).limit(200).sequential().toArray(); 190 assertEquals(l.length, 200); 191 assertEquals(l[l.length - 1], 400); 192 } 193 194 public void testLimitParallel() { 195 int[] l = IntStream.range(1, 1001).parallel().limit(500).sequential().toArray(); 196 assertEquals(l.length, 500); 197 assertEquals(l[l.length - 1], 500); 198 } 199 200 private List<Integer> sizes(int size) { 201 if (size < 4) { 202 return Arrays.asList(0, 1, 2, 3, 4, 6); 203 } 204 else { 205 return Arrays.asList(0, 1, size / 2, size - 1, size, size + 1, 2 * size); 206 } 207 } 208 }