1 /* 2 * Copyright (c) 2012, 2016, 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 27 import java.util.*; 28 import java.util.Spliterators; 29 import java.util.function.Supplier; 30 31 /** 32 * StreamTestDataProvider 33 * 34 * @author Brian Goetz 35 */ 36 /** TestNG DataProvider for ref-valued streams */ 37 public class StreamTestDataProvider { 38 private static final Integer[] to0 = new Integer[0]; 39 private static final Integer[] to1 = new Integer[1]; 40 private static final Integer[] to10 = new Integer[10]; 41 private static final Integer[] to100 = new Integer[100]; 42 private static final Integer[] to1000 = new Integer[1000]; 43 private static final Integer[] reversed = new Integer[100]; 44 private static final Integer[] ones = new Integer[100]; 45 private static final Integer[] twice = new Integer[200]; 46 private static final Integer[] pseudoRandom; 47 48 private static final Object[][] testData; 49 private static final Object[][] testSmallData; 50 private static final Object[][] testMiniData; 51 private static final Object[][] withNullTestData; 52 private static final Object[][] spliteratorTestData; 53 54 static { 55 Integer[][] arrays = {to0, to1, to10, to100, to1000}; 56 for (Integer[] arr : arrays) { 57 for (int i = 0; i < arr.length; i++) { 58 arr[i] = i; 59 } 60 } 61 for (int i = 0; i < reversed.length; i++) { 62 reversed[i] = reversed.length - i; 63 } 64 for (int i = 0; i < ones.length; i++) { 65 ones[i] = 1; 66 } 67 System.arraycopy(to100, 0, twice, 0, to100.length); 68 System.arraycopy(to100, 0, twice, to100.length, to100.length); 69 pseudoRandom = new Integer[LambdaTestHelpers.LONG_STRING.length()]; 70 for (int i = 0; i < LambdaTestHelpers.LONG_STRING.length(); i++) { 71 pseudoRandom[i] = (int) LambdaTestHelpers.LONG_STRING.charAt(i); 72 } 73 } 74 75 static final Object[][] arrays = { 76 {"empty", to0}, 77 {"0..1", to1}, 78 {"0..10", to10}, 79 {"0..100", to100}, 80 {"0..1000", to1000}, 81 {"100x[1]", ones}, 82 {"2x[0..100]", twice}, 83 {"reverse 0..100", reversed}, 84 {"pseudorandom", pseudoRandom} 85 }; 86 87 static { 88 { 89 List<Object[]> listMini = new ArrayList<>(); 90 List<Object[]> listSmall = new ArrayList<>(); 91 List<Object[]> list1000 = new ArrayList<>(); 92 List<Object[]> list = null; 93 for (Object[] data : arrays) { 94 final Object name = data[0]; 95 final Integer[] ints = (Integer[])data[1]; 96 final List<Integer> intsAsList = Arrays.asList(ints); 97 98 list = ints.length >= 1000 ? list1000 : (ints.length >= 100 ? listSmall : listMini); 99 100 list.add(arrayDataDescr("array:" + name, ints)); 101 list.add(collectionDataDescr("ArrayList.asList:" + name, intsAsList)); 102 list.add(collectionDataDescr("ArrayList:" + name, new ArrayList<>(intsAsList))); 103 list.add(streamDataDescr("DelegatingStream(ArrayList):" + name, 104 () -> new ArrayList<>(intsAsList).stream())); 105 List<Integer> aList = new ArrayList<>(intsAsList); 106 if (LambdaTestMode.isNormalMode()) { 107 // Only include sub-lists for normal test execution mode 108 // This data is serialization-hostile since the state of the 109 // deserialized sub-list will be out of sync with the 110 // enclosing list. 111 list.add(collectionDataDescr("ArrayList.Sublist:" + name, 112 (ints.length) <= 1 ? aList.subList(0, 0) : aList.subList(1, ints.length / 2))); 113 } 114 list.add(collectionDataDescr("LinkedList:" + name, new LinkedList<>(intsAsList))); 115 list.add(collectionDataDescr("HashSet:" + name, new HashSet<>(intsAsList))); 116 list.add(collectionDataDescr("LinkedHashSet:" + name, new LinkedHashSet<>(intsAsList))); 117 list.add(collectionDataDescr("TreeSet:" + name, new TreeSet<>(intsAsList))); 118 SpinedBuffer<Integer> spinedBuffer = new SpinedBuffer<>(); 119 intsAsList.forEach(spinedBuffer); 120 list.add(sbDataDescr("SpinedBuffer:" + name, spinedBuffer)); 121 122 // @@@ Add more 123 } 124 testMiniData = listMini.toArray(new Object[0][]); 125 listSmall.addAll(listMini); 126 testSmallData = listSmall.toArray(new Object[0][]); 127 list1000.addAll(listSmall); 128 testData = list1000.toArray(new Object[0][]); 129 } 130 131 // Simple combination of numbers and null values, probably excessive but may catch 132 // errors for initialization/termination/sequence 133 // @@@ This is separate from the other data for now until nulls are consistently supported by 134 // all operations 135 { 136 List<Object[]> list = new ArrayList<>(); 137 int size = 5; 138 for (int i = 0; i < (1 << size) - 2; i++) { 139 Integer[] content = new Integer[size]; 140 for (int e = 0; e < size; e++) { 141 content[e] = (i & (1 << e)) > 0 ? e + 1 : null; 142 } 143 144 // ORDERED 145 list.add(arrayDataDescr("array:" + i, content)); 146 // not ORDERED, DISTINCT 147 list.add(collectionDataDescr("HashSet:" + i, new HashSet<>(Arrays.asList(content)))); 148 } 149 150 withNullTestData = list.toArray(new Object[0][]); 151 } 152 153 { 154 List<Object[]> spliterators = new ArrayList<>(); 155 for (Object[] data : arrays) { 156 final Object name = data[0]; 157 final Integer[] ints = (Integer[])data[1]; 158 159 spliterators.add(splitDescr("Arrays.s(array):" + name, 160 () -> Arrays.spliterator(ints))); 161 spliterators.add(splitDescr("arrays.s(array,o,l):" + name, 162 () -> Arrays.spliterator(ints, 0, ints.length/2))); 163 spliterators.add(splitDescr("SpinedBuffer.s():" + name, 164 () -> { 165 SpinedBuffer<Integer> sb = new SpinedBuffer<>(); 166 for (Integer i : ints) 167 sb.accept(i); 168 return sb.spliterator(); 169 })); 170 spliterators.add(splitDescr("Iterators.s(Arrays.s(array).iterator(), size):" + name, 171 () -> Spliterators.spliterator(Arrays.asList(ints).iterator(), ints.length, 0))); 172 spliterators.add(splitDescr("Iterators.s(Arrays.s(array).iterator()):" + name, 173 () -> Spliterators.spliteratorUnknownSize(Arrays.asList(ints).iterator(), 0))); 174 // @@@ Add map and collection spliterators when spliterator() is exposed on Collection or Iterable 175 } 176 spliteratorTestData = spliterators.toArray(new Object[0][]); 177 } 178 } 179 180 static <T> Object[] arrayDataDescr(String description, T[] data) { 181 return new Object[] { description, TestData.Factory.ofArray(description, data)}; 182 } 183 184 static <T> Object[] streamDataDescr(String description, Supplier<Stream<T>> supplier) { 185 return new Object[] { description, TestData.Factory.ofSupplier(description, supplier)}; 186 } 187 188 static <T> Object[] collectionDataDescr(String description, Collection<T> data) { 189 return new Object[] { description, TestData.Factory.ofCollection(description, data)}; 190 } 191 192 static <T> Object[] sbDataDescr(String description, SpinedBuffer<T> data) { 193 return new Object[] { description, TestData.Factory.ofSpinedBuffer(description, data)}; 194 } 195 196 static <T> Object[] splitDescr(String description, Supplier<Spliterator<T>> ss) { 197 return new Object[] { description, ss }; 198 } 199 200 // Return an array of ( String name, StreamTestData<Integer> ) 201 @DataProvider(name = "StreamTestData<Integer>") 202 public static Object[][] makeStreamTestData() { 203 return testData; 204 } 205 206 @DataProvider(name = "StreamTestData<Integer>.small") 207 public static Object[][] makeSmallStreamTestData() { 208 return testSmallData; 209 } 210 211 @DataProvider(name = "StreamTestData<Integer>.mini") 212 public static Object[][] makeMiniStreamTestData() { 213 return testMiniData; 214 } 215 216 @DataProvider(name = "withNull:StreamTestData<Integer>") 217 public static Object[][] makeStreamWithNullTestData() { 218 return withNullTestData; 219 } 220 221 // returns an array of (String name, Supplier<Spliterator<Integer>>) 222 @DataProvider(name = "Spliterator<Integer>") 223 public static Object[][] spliteratorProvider() { 224 return spliteratorTestData; 225 } 226 }