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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package java.util.stream;
  26 
  27 import org.testng.annotations.DataProvider;
  28 
  29 import java.util.*;
  30 import java.util.Spliterators;
  31 import java.util.function.Supplier;
  32 
  33 /** TestNG DataProvider for double-valued streams */
  34 public class DoubleStreamTestDataProvider {
  35     private static final double[] to0 = new double[0];
  36     private static final double[] to1 = new double[1];
  37     private static final double[] to10 = new double[10];
  38     private static final double[] to100 = new double[100];
  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         double[][] arrays = {to0, to1, to10, to100, to1000};
  50         for (double[] arr : arrays) {
  51             for (int i = 0; i < arr.length; i++) {
  52                 arr[i] = i;
  53             }
  54         }
  55         for (int i = 0; i < reversed.length; i++) {
  56             reversed[i] = reversed.length - i;
  57         }
  58         for (int i = 0; i < ones.length; i++) {
  59             ones[i] = 1;
  60         }
  61         System.arraycopy(to100, 0, twice, 0, to100.length);
  62         System.arraycopy(to100, 0, twice, to100.length, to100.length);
  63         pseudoRandom = new double[LambdaTestHelpers.LONG_STRING.length()];
  64         for (int i = 0; i < LambdaTestHelpers.LONG_STRING.length(); i++) {
  65             pseudoRandom[i] = (double) LambdaTestHelpers.LONG_STRING.charAt(i);
  66         }
  67     }
  68 
  69     static final Object[][] arrays = {
  70             {"empty", to0},
  71             {"0..1", to1},
  72             {"0..10", to10},
  73             {"0..100", to100},
  74             {"0..1000", to1000},
  75             {"100x[1]", ones},
  76             {"2x[0..100]", twice},
  77             {"reverse 0..100", reversed},
  78             {"pseudorandom", pseudoRandom}
  79     };
  80 
  81     static {
  82         {
  83             List<Object[]> list = new ArrayList<>();
  84             for (Object[] data : arrays) {
  85                 final Object name = data[0];
  86                 final double[] doubles = (double[]) data[1];
  87 
  88                 list.add(new Object[]{"array:" + name,
  89                         TestData.Factory.ofArray("array:" + name, doubles)});
  90 
  91                 SpinedBuffer.OfDouble isl = new SpinedBuffer.OfDouble();
  92                 for (double i : doubles) {
  93                     isl.accept(i);
  94                 }
  95                 list.add(new Object[]{"SpinedList:" + name,
  96                         TestData.Factory.ofSpinedBuffer("SpinedList:" + name, isl)});
  97 
  98                 list.add(streamDataDescr("Primitives.range(0,l): " + doubles.length,
  99                                          () -> DoubleStream.range(0, doubles.length)));
 100                 list.add(streamDataDescr("Primitives.range(0,l,2): " + doubles.length,
 101                                          () -> DoubleStream.range(0, doubles.length, 2)));
 102                 list.add(streamDataDescr("Primitives.range(0,l,3): " + doubles.length,
 103                                          () -> DoubleStream.range(0, doubles.length, 3)));
 104                 list.add(streamDataDescr("Primitives.range(0,l,7): " + doubles.length,
 105                                          () -> DoubleStream.range(0, doubles.length, 7)));
 106             }
 107             testData = list.toArray(new Object[0][]);
 108         }
 109 
 110         {
 111             List<Object[]> spliterators = new ArrayList<>();
 112             for (Object[] data : arrays) {
 113                 final Object name = data[0];
 114                 final double[] doubles = (double[]) data[1];
 115 
 116                 SpinedBuffer.OfDouble isl = new SpinedBuffer.OfDouble();
 117                 for (double i : doubles) {
 118                     isl.accept(i);
 119                 }
 120 
 121                 spliterators.add(splitDescr("Arrays.s(array):" + name,
 122                                             () -> Arrays.spliterator(doubles)));
 123                 spliterators.add(splitDescr("Arrays.s(array,o,l):" + name,
 124                                             () -> Arrays.spliterator(doubles, 0, doubles.length / 2)));
 125 
 126                 spliterators.add(splitDescr("SpinedBuffer.s():" + name,
 127                                             () -> isl.spliterator()));
 128 
 129                 spliterators.add(splitDescr("Primitives.s(SpinedBuffer.iterator(), size):" + name,
 130                                             () -> Spliterators.spliterator(isl.iterator(), doubles.length, 0)));
 131                 spliterators.add(splitDescr("Primitives.s(SpinedBuffer.iterator()):" + name,
 132                                             () -> Spliterators.spliteratorUnknownSize(isl.iterator(), 0)));
 133 
 134                 spliterators.add(splitDescr("Primitives.range(0,l):" + name,
 135                                             () -> DoubleStream.range(0, doubles.length).spliterator()));
 136                 spliterators.add(splitDescr("Primitives.range(0,l,2):" + name,
 137                                             () -> DoubleStream.range(0, doubles.length, 2).spliterator()));
 138                 spliterators.add(splitDescr("Primitives.range(0,l,3):" + name,
 139                                             () -> DoubleStream.range(0, doubles.length, 3).spliterator()));
 140                 spliterators.add(splitDescr("Primitives.range(0,l,7):" + name,
 141                                             () -> DoubleStream.range(0, doubles.length, 7).spliterator()));
 142                 // Need more!
 143             }
 144             spliteratorTestData = spliterators.toArray(new Object[0][]);
 145         }
 146 
 147     }
 148 
 149     static <T> Object[] streamDataDescr(String description, Supplier<DoubleStream> s) {
 150         return new Object[] { description, TestData.Factory.ofDoubleSupplier(description, s) };
 151     }
 152 
 153     static <T> Object[] splitDescr(String description, Supplier<Spliterator.OfDouble> s) {
 154         return new Object[] { description, s };
 155     }
 156 
 157     // Return an array of ( String name, DoubleStreamTestData )
 158     @DataProvider(name = "DoubleStreamTestData")
 159     public static Object[][] makeDoubleStreamTestData() {
 160         return testData;
 161     }
 162 
 163     // returns an array of (String name, Supplier<PrimitiveSpliterator<Double>>)
 164     @DataProvider(name = "DoubleSpliterator")
 165     public static Object[][] spliteratorProvider() {
 166         return spliteratorTestData;
 167     }
 168 }