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