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 */
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 }
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 }
|
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 */
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[][] withNullTestData;
51 private static final Object[][] spliteratorTestData;
52
53 static {
54 Integer[][] arrays = {to0, to1, to10, to100, to1000};
55 for (Integer[] arr : arrays) {
56 for (int i = 0; i < arr.length; i++) {
57 arr[i] = i;
58 }
59 }
60 for (int i = 0; i < reversed.length; i++) {
61 reversed[i] = reversed.length - i;
62 }
63 for (int i = 0; i < ones.length; i++) {
64 ones[i] = 1;
65 }
66 System.arraycopy(to100, 0, twice, 0, to100.length);
67 System.arraycopy(to100, 0, twice, to100.length, to100.length);
68 pseudoRandom = new Integer[LambdaTestHelpers.LONG_STRING.length()];
69 for (int i = 0; i < LambdaTestHelpers.LONG_STRING.length(); i++) {
70 pseudoRandom[i] = (int) LambdaTestHelpers.LONG_STRING.charAt(i);
71 }
72 }
73
74 static final Object[][] arrays = {
75 {"empty", to0},
76 {"0..1", to1},
77 {"0..10", to10},
78 {"0..100", to100},
79 {"0..1000", to1000},
80 {"100x[1]", ones},
81 {"2x[0..100]", twice},
82 {"reverse 0..100", reversed},
83 {"pseudorandom", pseudoRandom}
84 };
85
86 static {
87 {
88 List<Object[]> listSmall = new ArrayList<>();
89 List<Object[]> list1000 = new ArrayList<>();
90 List<Object[]> list = null;
91 for (Object[] data : arrays) {
92 final Object name = data[0];
93 final Integer[] ints = (Integer[])data[1];
94 final List<Integer> intsAsList = Arrays.asList(ints);
95
96 list = ints.length >= 1000 ? list1000 : listSmall;
97
98 list.add(arrayDataDescr("array:" + name, ints));
99 list.add(collectionDataDescr("ArrayList.asList:" + name, intsAsList));
100 list.add(collectionDataDescr("ArrayList:" + name, new ArrayList<>(intsAsList)));
101 list.add(streamDataDescr("DelegatingStream(ArrayList):" + name,
102 () -> new ArrayList<>(intsAsList).stream()));
103 List<Integer> aList = new ArrayList<>(intsAsList);
104 if (LambdaTestMode.isNormalMode()) {
105 // Only include sub-lists for normal test execution mode
106 // This data is serialization-hostile since the state of the
107 // deserialized sub-list will be out of sync with the
108 // enclosing list.
109 list.add(collectionDataDescr("ArrayList.Sublist:" + name,
110 (ints.length) <= 1 ? aList.subList(0, 0) : aList.subList(1, ints.length / 2)));
111 }
112 list.add(collectionDataDescr("LinkedList:" + name, new LinkedList<>(intsAsList)));
113 list.add(collectionDataDescr("HashSet:" + name, new HashSet<>(intsAsList)));
114 list.add(collectionDataDescr("LinkedHashSet:" + name, new LinkedHashSet<>(intsAsList)));
115 list.add(collectionDataDescr("TreeSet:" + name, new TreeSet<>(intsAsList)));
116 SpinedBuffer<Integer> spinedBuffer = new SpinedBuffer<>();
117 intsAsList.forEach(spinedBuffer);
118 list.add(sbDataDescr("SpinedBuffer:" + name, spinedBuffer));
119
120 // @@@ Add more
121 }
122 testSmallData = listSmall.toArray(new Object[0][]);
123 list1000.addAll(listSmall);
124 testData = list1000.toArray(new Object[0][]);
125 }
126
127 // Simple combination of numbers and null values, probably excessive but may catch
128 // errors for initialization/termination/sequence
129 // @@@ This is separate from the other data for now until nulls are consistently supported by
130 // all operations
131 {
132 List<Object[]> list = new ArrayList<>();
133 int size = 5;
134 for (int i = 0; i < (1 << size) - 2; i++) {
135 Integer[] content = new Integer[size];
136 for (int e = 0; e < size; e++) {
137 content[e] = (i & (1 << e)) > 0 ? e + 1 : null;
138 }
139
140 // ORDERED
141 list.add(arrayDataDescr("array:" + i, content));
142 // not ORDERED, DISTINCT
143 list.add(collectionDataDescr("HashSet:" + i, new HashSet<>(Arrays.asList(content))));
144 }
182 }
183
184 static <T> Object[] collectionDataDescr(String description, Collection<T> data) {
185 return new Object[] { description, TestData.Factory.ofCollection(description, data)};
186 }
187
188 static <T> Object[] sbDataDescr(String description, SpinedBuffer<T> data) {
189 return new Object[] { description, TestData.Factory.ofSpinedBuffer(description, data)};
190 }
191
192 static <T> Object[] splitDescr(String description, Supplier<Spliterator<T>> ss) {
193 return new Object[] { description, ss };
194 }
195
196 // Return an array of ( String name, StreamTestData<Integer> )
197 @DataProvider(name = "StreamTestData<Integer>")
198 public static Object[][] makeStreamTestData() {
199 return testData;
200 }
201
202 @DataProvider(name = "StreamTestData<Integer>.small")
203 public static Object[][] makeSmallStreamTestData() {
204 return testSmallData;
205 }
206
207 @DataProvider(name = "withNull:StreamTestData<Integer>")
208 public static Object[][] makeStreamWithNullTestData() {
209 return withNullTestData;
210 }
211
212 // returns an array of (String name, Supplier<Spliterator<Integer>>)
213 @DataProvider(name = "Spliterator<Integer>")
214 public static Object[][] spliteratorProvider() {
215 return spliteratorTestData;
216 }
217 }
|