< prev index next >

test/java/util/stream/bootlib/java.base/java/util/stream/IntStreamTestDataProvider.java

Print this page


   1 /*
   2  * Copyright (c) 2012, 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  */


  24 
  25 import org.testng.annotations.DataProvider;
  26 
  27 import java.util.*;
  28 import java.util.Spliterators;
  29 import java.util.function.Supplier;
  30 
  31 /** TestNG DataProvider for int-valued streams */
  32 public class IntStreamTestDataProvider {
  33     private static final int[] to0 = new int[0];
  34     private static final int[] to1 = new int[1];
  35     private static final int[] to10 = new int[10];
  36     private static final int[] to100 = new int[100];
  37     private static final int[] to1000 = new int[1000];
  38     private static final int[] reversed = new int[100];
  39     private static final int[] ones = new int[100];
  40     private static final int[] twice = new int[200];
  41     private static final int[] pseudoRandom;
  42 
  43     private static final Object[][] testData;

  44     private static final Object[][] spliteratorTestData;
  45 
  46     static {
  47         int[][] arrays = {to0, to1, to10, to100, to1000};
  48         for (int[] arr : arrays) {
  49             for (int i = 0; i < arr.length; i++) {
  50                 arr[i] = i;
  51             }
  52         }
  53         for (int i = 0; i < reversed.length; i++) {
  54             reversed[i] = reversed.length - i;
  55         }
  56         for (int i = 0; i < ones.length; i++) {
  57             ones[i] = 1;
  58         }
  59         System.arraycopy(to100, 0, twice, 0, to100.length);
  60         System.arraycopy(to100, 0, twice, to100.length, to100.length);
  61         pseudoRandom = new int[LambdaTestHelpers.LONG_STRING.length()];
  62         for (int i = 0; i < LambdaTestHelpers.LONG_STRING.length(); i++) {
  63             pseudoRandom[i] = (int) LambdaTestHelpers.LONG_STRING.charAt(i);
  64         }
  65     }
  66 
  67     static final Object[][] arrays = {
  68             {"empty", to0},
  69             {"0..1", to1},
  70             {"0..10", to10},
  71             {"0..100", to100},
  72             {"0..1000", to1000},
  73             {"100x[1]", ones},
  74             {"2x[0..100]", twice},
  75             {"reverse 0..100", reversed},
  76             {"pseudorandom", pseudoRandom}
  77     };
  78 
  79     static {
  80         {
  81             List<Object[]> list = new ArrayList<>();


  82             for (Object[] data : arrays) {
  83                 final Object name = data[0];
  84                 final int[] ints = (int[]) data[1];
  85 


  86                 list.add(new Object[]{"array:" +
  87                                       name, TestData.Factory.ofArray("array:" + name, ints)});
  88 
  89                 SpinedBuffer.OfInt isl = new SpinedBuffer.OfInt();
  90                 for (int i : ints) {
  91                     isl.accept(i);
  92                 }
  93                 list.add(new Object[]{"SpinedList:" + name,
  94                          TestData.Factory.ofSpinedBuffer("SpinedList:" + name, isl)});
  95 
  96                 list.add(streamDataDescr("IntStream.intRange(0,l): " + ints.length,
  97                                          () -> IntStream.range(0, ints.length)));
  98                 list.add(streamDataDescr("IntStream.rangeClosed(0,l): " + ints.length,
  99                                          () -> IntStream.rangeClosed(0, ints.length)));
 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 int[] ints = (int[]) data[1];
 109 
 110                 SpinedBuffer.OfInt isl = new SpinedBuffer.OfInt();
 111                 for (int i : ints) {
 112                     isl.accept(i);
 113                 }
 114 
 115                 spliterators.add(splitDescr("Arrays.s(array):" + name,
 116                                             () -> Arrays.spliterator(ints)));
 117                 spliterators.add(splitDescr("Arrays.s(array,o,l):" + name,
 118                                             () -> Arrays.spliterator(ints, 0, ints.length / 2)));
 119 
 120                 spliterators.add(splitDescr("SpinedBuffer.s():" + name,
 121                                             () -> isl.spliterator()));


 133             }
 134             spliteratorTestData = spliterators.toArray(new Object[0][]);
 135         }
 136 
 137     }
 138 
 139     static <T> Object[] streamDataDescr(String description, Supplier<IntStream> s) {
 140         return new Object[] { description, TestData.Factory.ofIntSupplier(description, s) };
 141     }
 142 
 143     static <T> Object[] splitDescr(String description, Supplier<Spliterator.OfInt> s) {
 144         return new Object[] { description, s };
 145     }
 146 
 147     // Return an array of ( String name, IntStreamTestData )
 148     @DataProvider(name = "IntStreamTestData")
 149     public static Object[][] makeIntStreamTestData() {
 150         return testData;
 151     }
 152 





 153     // returns an array of (String name, Supplier<PrimitiveSpliterator<Integer>>)
 154     @DataProvider(name = "IntSpliterator")
 155     public static Object[][] spliteratorProvider() {
 156         return spliteratorTestData;
 157     }
 158 }
   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  */


  24 
  25 import org.testng.annotations.DataProvider;
  26 
  27 import java.util.*;
  28 import java.util.Spliterators;
  29 import java.util.function.Supplier;
  30 
  31 /** TestNG DataProvider for int-valued streams */
  32 public class IntStreamTestDataProvider {
  33     private static final int[] to0 = new int[0];
  34     private static final int[] to1 = new int[1];
  35     private static final int[] to10 = new int[10];
  36     private static final int[] to100 = new int[100];
  37     private static final int[] to1000 = new int[1000];
  38     private static final int[] reversed = new int[100];
  39     private static final int[] ones = new int[100];
  40     private static final int[] twice = new int[200];
  41     private static final int[] pseudoRandom;
  42 
  43     private static final Object[][] testData;
  44     private static final Object[][] testSmallData;
  45     private static final Object[][] spliteratorTestData;
  46 
  47     static {
  48         int[][] arrays = {to0, to1, to10, to100, to1000};
  49         for (int[] 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 int[LambdaTestHelpers.LONG_STRING.length()];
  63         for (int i = 0; i < LambdaTestHelpers.LONG_STRING.length(); i++) {
  64             pseudoRandom[i] = (int) 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[]> listSmall = new ArrayList<>();
  83             List<Object[]> list1000 = new ArrayList<>();
  84             List<Object[]> list = null;
  85             for (Object[] data : arrays) {
  86                 final Object name = data[0];
  87                 final int[] ints = (int[]) data[1];
  88 
  89                 list = ints.length >= 1000 ? list1000 : listSmall;
  90 
  91                 list.add(new Object[]{"array:" +
  92                                       name, TestData.Factory.ofArray("array:" + name, ints)});
  93 
  94                 SpinedBuffer.OfInt isl = new SpinedBuffer.OfInt();
  95                 for (int i : ints) {
  96                     isl.accept(i);
  97                 }
  98                 list.add(new Object[]{"SpinedList:" + name,
  99                          TestData.Factory.ofSpinedBuffer("SpinedList:" + name, isl)});
 100 
 101                 list.add(streamDataDescr("IntStream.intRange(0,l): " + ints.length,
 102                                          () -> IntStream.range(0, ints.length)));
 103                 list.add(streamDataDescr("IntStream.rangeClosed(0,l): " + ints.length,
 104                                          () -> IntStream.rangeClosed(0, ints.length)));
 105             }
 106             testSmallData = listSmall.toArray(new Object[0][]);
 107             list1000.addAll(listSmall);
 108             testData = list1000.toArray(new Object[0][]);
 109         }
 110 
 111         {
 112             List<Object[]> spliterators = new ArrayList<>();
 113             for (Object[] data : arrays) {
 114                 final Object name = data[0];
 115                 final int[] ints = (int[]) data[1];
 116 
 117                 SpinedBuffer.OfInt isl = new SpinedBuffer.OfInt();
 118                 for (int i : ints) {
 119                     isl.accept(i);
 120                 }
 121 
 122                 spliterators.add(splitDescr("Arrays.s(array):" + name,
 123                                             () -> Arrays.spliterator(ints)));
 124                 spliterators.add(splitDescr("Arrays.s(array,o,l):" + name,
 125                                             () -> Arrays.spliterator(ints, 0, ints.length / 2)));
 126 
 127                 spliterators.add(splitDescr("SpinedBuffer.s():" + name,
 128                                             () -> isl.spliterator()));


 140             }
 141             spliteratorTestData = spliterators.toArray(new Object[0][]);
 142         }
 143 
 144     }
 145 
 146     static <T> Object[] streamDataDescr(String description, Supplier<IntStream> s) {
 147         return new Object[] { description, TestData.Factory.ofIntSupplier(description, s) };
 148     }
 149 
 150     static <T> Object[] splitDescr(String description, Supplier<Spliterator.OfInt> s) {
 151         return new Object[] { description, s };
 152     }
 153 
 154     // Return an array of ( String name, IntStreamTestData )
 155     @DataProvider(name = "IntStreamTestData")
 156     public static Object[][] makeIntStreamTestData() {
 157         return testData;
 158     }
 159 
 160     @DataProvider(name = "IntStreamTestData.small")
 161     public static Object[][] makeSmallIntStreamTestData() {
 162         return testSmallData;
 163     }
 164 
 165     // returns an array of (String name, Supplier<PrimitiveSpliterator<Integer>>)
 166     @DataProvider(name = "IntSpliterator")
 167     public static Object[][] spliteratorProvider() {
 168         return spliteratorTestData;
 169     }
 170 }
< prev index next >