1 /* 2 * Copyright (c) 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 java.util.stream; 24 25 import org.testng.annotations.DataProvider; 26 import org.testng.annotations.Test; 27 28 import java.util.ArrayList; 29 import java.util.Arrays; 30 import java.util.Collection; 31 import java.util.List; 32 import java.util.Spliterator; 33 34 import static java.util.stream.Collectors.toList; 35 import static org.testng.Assert.assertEquals; 36 37 /** 38 * @bug 8012987 39 */ 40 @Test 41 public class SliceSpliteratorTest extends LoggingTestCase { 42 43 static class UnorderedContentAsserter<T> implements SpliteratorTestHelper.ContentAsserter<T> { 44 Collection<T> source; 45 UnorderedContentAsserter(Collection<T> source)46 UnorderedContentAsserter(Collection<T> source) { 47 this.source = source; 48 } 49 50 @Override assertContents(Collection<T> actual, Collection<T> expected, boolean isOrdered)51 public void assertContents(Collection<T> actual, Collection<T> expected, boolean isOrdered) { 52 if (isOrdered) { 53 assertEquals(actual, expected); 54 } 55 else { 56 assertEquals(actual.size(), expected.size()); 57 assertTrue(source.containsAll(actual)); 58 } 59 } 60 } 61 62 interface SliceTester { test(int size, int skip, int limit)63 void test(int size, int skip, int limit); 64 } 65 66 @DataProvider(name = "sliceSpliteratorDataProvider") sliceSpliteratorDataProvider()67 public static Object[][] sliceSpliteratorDataProvider() { 68 List<Object[]> data = new ArrayList<>(); 69 70 // SIZED/SUBSIZED slice spliterator 71 72 { 73 SliceTester r = (size, skip, limit) -> { 74 final Collection<Integer> source = IntStream.range(0, size).boxed().collect(toList()); 75 76 SpliteratorTestHelper.testSpliterator(() -> { 77 Spliterator<Integer> s = Arrays.spliterator(source.stream().toArray(Integer[]::new)); 78 79 return new StreamSpliterators.SliceSpliterator.OfRef<>(s, skip, limit); 80 }); 81 }; 82 data.add(new Object[]{"StreamSpliterators.SliceSpliterator.OfRef", r}); 83 } 84 85 { 86 SliceTester r = (size, skip, limit) -> { 87 final Collection<Integer> source = IntStream.range(0, size).boxed().collect(toList()); 88 89 SpliteratorTestHelper.testIntSpliterator(() -> { 90 Spliterator.OfInt s = Arrays.spliterator(source.stream().mapToInt(i->i).toArray()); 91 92 return new StreamSpliterators.SliceSpliterator.OfInt(s, skip, limit); 93 }); 94 }; 95 data.add(new Object[]{"StreamSpliterators.SliceSpliterator.OfInt", r}); 96 } 97 98 { 99 SliceTester r = (size, skip, limit) -> { 100 final Collection<Long> source = LongStream.range(0, size).boxed().collect(toList()); 101 102 SpliteratorTestHelper.testLongSpliterator(() -> { 103 Spliterator.OfLong s = Arrays.spliterator(source.stream().mapToLong(i->i).toArray()); 104 105 return new StreamSpliterators.SliceSpliterator.OfLong(s, skip, limit); 106 }); 107 }; 108 data.add(new Object[]{"StreamSpliterators.SliceSpliterator.OfLong", r}); 109 } 110 111 { 112 SliceTester r = (size, skip, limit) -> { 113 final Collection<Double> source = LongStream.range(0, size).asDoubleStream().boxed().collect(toList()); 114 115 SpliteratorTestHelper.testDoubleSpliterator(() -> { 116 Spliterator.OfDouble s = Arrays.spliterator(source.stream().mapToDouble(i->i).toArray()); 117 118 return new StreamSpliterators.SliceSpliterator.OfDouble(s, skip, limit); 119 }); 120 }; 121 data.add(new Object[]{"StreamSpliterators.SliceSpliterator.OfLong", r}); 122 } 123 124 125 // Unordered slice spliterator 126 127 { 128 SliceTester r = (size, skip, limit) -> { 129 final Collection<Integer> source = IntStream.range(0, size).boxed().collect(toList()); 130 final UnorderedContentAsserter<Integer> uca = new UnorderedContentAsserter<>(source); 131 132 SpliteratorTestHelper.testSpliterator(() -> { 133 Spliterator<Integer> s = Arrays.spliterator(source.stream().toArray(Integer[]::new)); 134 135 return new StreamSpliterators.UnorderedSliceSpliterator.OfRef<>(s, skip, limit); 136 }, uca); 137 }; 138 data.add(new Object[]{"StreamSpliterators.UnorderedSliceSpliterator.OfRef", r}); 139 } 140 141 { 142 SliceTester r = (size, skip, limit) -> { 143 final Collection<Integer> source = IntStream.range(0, size).boxed().collect(toList()); 144 final UnorderedContentAsserter<Integer> uca = new UnorderedContentAsserter<>(source); 145 146 SpliteratorTestHelper.testIntSpliterator(() -> { 147 Spliterator.OfInt s = Arrays.spliterator(source.stream().mapToInt(i->i).toArray()); 148 149 return new StreamSpliterators.UnorderedSliceSpliterator.OfInt(s, skip, limit); 150 }, uca); 151 }; 152 data.add(new Object[]{"StreamSpliterators.UnorderedSliceSpliterator.OfInt", r}); 153 } 154 155 { 156 SliceTester r = (size, skip, limit) -> { 157 final Collection<Long> source = LongStream.range(0, size).boxed().collect(toList()); 158 final UnorderedContentAsserter<Long> uca = new UnorderedContentAsserter<>(source); 159 160 SpliteratorTestHelper.testLongSpliterator(() -> { 161 Spliterator.OfLong s = Arrays.spliterator(source.stream().mapToLong(i->i).toArray()); 162 163 return new StreamSpliterators.UnorderedSliceSpliterator.OfLong(s, skip, limit); 164 }, uca); 165 }; 166 data.add(new Object[]{"StreamSpliterators.UnorderedSliceSpliterator.OfLong", r}); 167 } 168 169 { 170 SliceTester r = (size, skip, limit) -> { 171 final Collection<Double> source = LongStream.range(0, size).asDoubleStream().boxed().collect(toList()); 172 final UnorderedContentAsserter<Double> uca = new UnorderedContentAsserter<>(source); 173 174 SpliteratorTestHelper.testDoubleSpliterator(() -> { 175 Spliterator.OfDouble s = Arrays.spliterator(LongStream.range(0, SIZE).asDoubleStream().toArray()); 176 177 return new StreamSpliterators.UnorderedSliceSpliterator.OfDouble(s, skip, limit); 178 }, uca); 179 }; 180 data.add(new Object[]{"StreamSpliterators.UnorderedSliceSpliterator.OfLong", r}); 181 } 182 183 return data.toArray(new Object[0][]); 184 } 185 186 static final int SIZE = 256; 187 188 static final int STEP = 32; 189 190 @Test(dataProvider = "sliceSpliteratorDataProvider") testSliceSpliterator(String description, SliceTester r)191 public void testSliceSpliterator(String description, SliceTester r) { 192 setContext("size", SIZE); 193 for (int skip = 0; skip < SIZE; skip += STEP) { 194 setContext("skip", skip); 195 for (int limit = 0; limit < SIZE; limit += STEP) { 196 setContext("limit", skip); 197 r.test(SIZE, skip, limit); 198 } 199 } 200 } 201 } 202