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