1/*
2 * Copyright (c) 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;
30import java.util.stream.*;
31
32/** TestNG DataProvider for double-valued streams */
33public class DoubleStreamTestDataProvider {
34    private static final double[] to0 = new double[0];
35    private static final double[] to1 = new double[1];
36    private static final double[] to10 = new double[10];
37    private static final double[] to100 = new double[100];
38    private static final double[] to1000 = new double[1000];
39    private static final double[] reversed = new double[100];
40    private static final double[] ones = new double[100];
41    private static final double[] twice = new double[200];
42    private static final double[] pseudoRandom;
43
44    private static final Object[][] testData;
45    private static final Object[][] spliteratorTestData;
46
47    static {
48        double[][] arrays = {to0, to1, to10, to100, to1000};
49        for (double[] arr : arrays) {
50            for (int i = 0; i < arr.length; i++) {
51                arr[i] = i;
52            }
53        }
54        for (int i = 0; i < reversed.length; i++) {
55            reversed[i] = reversed.length - i;
56        }
57        for (int i = 0; i < ones.length; i++) {
58            ones[i] = 1;
59        }
60        System.arraycopy(to100, 0, twice, 0, to100.length);
61        System.arraycopy(to100, 0, twice, to100.length, to100.length);
62        pseudoRandom = new double[LambdaTestHelpers.LONG_STRING.length()];
63        for (int i = 0; i < LambdaTestHelpers.LONG_STRING.length(); i++) {
64            pseudoRandom[i] = (double) LambdaTestHelpers.LONG_STRING.charAt(i);
65        }
66    }
67
68    static final Object[][] arrays = {
69            {"empty", to0},
70            {"0..1", to1},
71            {"0..10", to10},
72            {"0..100", to100},
73            {"0..1000", to1000},
74            {"100x[1]", ones},
75            {"2x[0..100]", twice},
76            {"reverse 0..100", reversed},
77            {"pseudorandom", pseudoRandom}
78    };
79
80    static {
81        {
82            List<Object[]> list = new ArrayList<>();
83            for (Object[] data : arrays) {
84                final Object name = data[0];
85                final double[] doubles = (double[]) data[1];
86
87                list.add(new Object[]{"array:" + name,
88                        TestData.Factory.ofArray("array:" + name, doubles)});
89
90                SpinedBuffer.OfDouble isl = new SpinedBuffer.OfDouble();
91                for (double i : doubles) {
92                    isl.accept(i);
93                }
94                list.add(new Object[]{"SpinedList:" + name,
95                        TestData.Factory.ofSpinedBuffer("SpinedList:" + name, isl)});
96            }
97            testData = list.toArray(new Object[0][]);
98        }
99
100        {
101            List<Object[]> spliterators = new ArrayList<>();
102            for (Object[] data : arrays) {
103                final Object name = data[0];
104                final double[] doubles = (double[]) data[1];
105
106                SpinedBuffer.OfDouble isl = new SpinedBuffer.OfDouble();
107                for (double i : doubles) {
108                    isl.accept(i);
109                }
110
111                spliterators.add(splitDescr("Arrays.s(array):" + name,
112                                            () -> Arrays.spliterator(doubles)));
113                spliterators.add(splitDescr("Arrays.s(array,o,l):" + name,
114                                            () -> Arrays.spliterator(doubles, 0, doubles.length / 2)));
115
116                spliterators.add(splitDescr("SpinedBuffer.s():" + name,
117                                            () -> isl.spliterator()));
118
119                spliterators.add(splitDescr("Primitives.s(SpinedBuffer.iterator(), size):" + name,
120                                            () -> Spliterators.spliterator(isl.iterator(), doubles.length, 0)));
121                spliterators.add(splitDescr("Primitives.s(SpinedBuffer.iterator()):" + name,
122                                            () -> Spliterators.spliteratorUnknownSize(isl.iterator(), 0)));
123                // Need more!
124            }
125            spliteratorTestData = spliterators.toArray(new Object[0][]);
126        }
127
128    }
129
130    static <T> Object[] splitDescr(String description, Supplier<Spliterator.OfDouble> s) {
131        return new Object[] { description, s };
132    }
133
134    // Return an array of ( String name, DoubleStreamTestData )
135    @DataProvider(name = "DoubleStreamTestData")
136    public static Object[][] makeDoubleStreamTestData() {
137        return testData;
138    }
139
140    // returns an array of (String name, Supplier<PrimitiveSpliterator<Double>>)
141    @DataProvider(name = "DoubleSpliterator")
142    public static Object[][] spliteratorProvider() {
143        return spliteratorTestData;
144    }
145}
146