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