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 */
23package java.util.stream;
24
25import org.testng.annotations.DataProvider;
26
27import java.util.*;
28import java.util.Spliterators;
29import java.util.function.Supplier;
30
31/**
32 * StreamTestDataProvider
33 *
34 * @author Brian Goetz
35 */
36/** TestNG DataProvider for ref-valued streams */
37public 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                spliterators.add(splitDescr("Stream.iterate(0,x->x<l,x->x+1): " + name,
175                                            () -> Stream.iterate(0, x -> x < ints.length, x -> x + 1).spliterator()));
176                // @@@ Add map and collection spliterators when spliterator() is exposed on Collection or Iterable
177            }
178            spliteratorTestData = spliterators.toArray(new Object[0][]);
179        }
180    }
181
182    static <T> Object[] arrayDataDescr(String description, T[] data) {
183        return new Object[] { description, TestData.Factory.ofArray(description, data)};
184    }
185
186    static <T> Object[] streamDataDescr(String description, Supplier<Stream<T>> supplier) {
187        return new Object[] { description, TestData.Factory.ofSupplier(description, supplier)};
188    }
189
190    static <T> Object[] collectionDataDescr(String description, Collection<T> data) {
191        return new Object[] { description, TestData.Factory.ofCollection(description, data)};
192    }
193
194    static <T> Object[] sbDataDescr(String description, SpinedBuffer<T> data) {
195        return new Object[] { description, TestData.Factory.ofSpinedBuffer(description, data)};
196    }
197
198    static <T> Object[] splitDescr(String description, Supplier<Spliterator<T>> ss) {
199        return new Object[] { description, ss };
200    }
201
202    // Return an array of ( String name, StreamTestData<Integer> )
203    @DataProvider(name = "StreamTestData<Integer>")
204    public static Object[][] makeStreamTestData() {
205        return testData;
206    }
207
208    @DataProvider(name = "StreamTestData<Integer>.small")
209    public static Object[][] makeSmallStreamTestData() {
210        return testSmallData;
211    }
212
213    @DataProvider(name = "StreamTestData<Integer>.mini")
214    public static Object[][] makeMiniStreamTestData() {
215        return testMiniData;
216    }
217
218    @DataProvider(name = "withNull:StreamTestData<Integer>")
219    public static Object[][] makeStreamWithNullTestData() {
220        return withNullTestData;
221    }
222
223    // returns an array of (String name, Supplier<Spliterator<Integer>>)
224    @DataProvider(name = "Spliterator<Integer>")
225    public static Object[][] spliteratorProvider() {
226        return spliteratorTestData;
227    }
228}
229