1 /*
   2  * Copyright (c) 2013, 2017, 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 java.util.stream;
  24 
  25 import org.testng.annotations.DataProvider;
  26 import org.testng.annotations.Test;
  27 
  28 import java.util.ArrayList;
  29 import java.util.Arrays;
  30 import java.util.Collection;
  31 import java.util.List;
  32 import java.util.Spliterator;
  33 import java.util.SpliteratorTestHelper;
  34 
  35 import static java.util.stream.Collectors.toList;
  36 import static org.testng.Assert.assertEquals;
  37 
  38 /**
  39  * @bug 8012987
  40  */
  41 @Test
  42 public class SliceSpliteratorTest extends LoggingTestCase {
  43 
  44     static class UnorderedContentAsserter<T> implements SpliteratorTestHelper.ContentAsserter<T> {
  45         Collection<T> source;
  46 
  47         UnorderedContentAsserter(Collection<T> source) {
  48             this.source = source;
  49         }
  50 
  51         @Override
  52         public void assertContents(Collection<T> actual, Collection<T> expected, boolean isOrdered) {
  53             if (isOrdered) {
  54                 assertEquals(actual, expected);
  55             }
  56             else {
  57                 assertEquals(actual.size(), expected.size());
  58                 assertTrue(source.containsAll(actual));
  59             }
  60         }
  61     }
  62 
  63     interface SliceTester {
  64         void test(int size, int skip, int limit);
  65     }
  66 
  67     @DataProvider(name = "sliceSpliteratorDataProvider")
  68     public static Object[][] sliceSpliteratorDataProvider() {
  69         List<Object[]> data = new ArrayList<>();
  70 
  71         // SIZED/SUBSIZED slice spliterator
  72 
  73         {
  74             SliceTester r = (size, skip, limit) -> {
  75                 final Collection<Integer> source =  IntStream.range(0, size).boxed().collect(toList());
  76 
  77                 SpliteratorTestHelper.testSpliterator(() -> {
  78                     Spliterator<Integer> s = Arrays.spliterator(source.stream().toArray(Integer[]::new));
  79 
  80                     return new StreamSpliterators.SliceSpliterator.OfRef<>(s, skip, limit);
  81                 });
  82             };
  83             data.add(new Object[]{"StreamSpliterators.SliceSpliterator.OfRef", r});
  84         }
  85 
  86         {
  87             SliceTester r = (size, skip, limit) -> {
  88                 final Collection<Integer> source =  IntStream.range(0, size).boxed().collect(toList());
  89 
  90                 SpliteratorTestHelper.testIntSpliterator(() -> {
  91                     Spliterator.OfInt s = Arrays.spliterator(source.stream().mapToInt(i->i).toArray());
  92 
  93                     return new StreamSpliterators.SliceSpliterator.OfInt(s, skip, limit);
  94                 });
  95             };
  96             data.add(new Object[]{"StreamSpliterators.SliceSpliterator.OfInt", r});
  97         }
  98 
  99         {
 100             SliceTester r = (size, skip, limit) -> {
 101                 final Collection<Long> source =  LongStream.range(0, size).boxed().collect(toList());
 102 
 103                 SpliteratorTestHelper.testLongSpliterator(() -> {
 104                     Spliterator.OfLong s = Arrays.spliterator(source.stream().mapToLong(i->i).toArray());
 105 
 106                     return new StreamSpliterators.SliceSpliterator.OfLong(s, skip, limit);
 107                 });
 108             };
 109             data.add(new Object[]{"StreamSpliterators.SliceSpliterator.OfLong", r});
 110         }
 111 
 112         {
 113             SliceTester r = (size, skip, limit) -> {
 114                 final Collection<Double> source =  LongStream.range(0, size).asDoubleStream().boxed().collect(toList());
 115 
 116                 SpliteratorTestHelper.testDoubleSpliterator(() -> {
 117                     Spliterator.OfDouble s = Arrays.spliterator(source.stream().mapToDouble(i->i).toArray());
 118 
 119                     return new StreamSpliterators.SliceSpliterator.OfDouble(s, skip, limit);
 120                 });
 121             };
 122             data.add(new Object[]{"StreamSpliterators.SliceSpliterator.OfLong", r});
 123         }
 124 
 125 
 126         // Unordered slice spliterator
 127 
 128         {
 129             SliceTester r = (size, skip, limit) -> {
 130                 final Collection<Integer> source =  IntStream.range(0, size).boxed().collect(toList());
 131                 final UnorderedContentAsserter<Integer> uca = new UnorderedContentAsserter<>(source);
 132 
 133                 SpliteratorTestHelper.testSpliterator(() -> {
 134                     Spliterator<Integer> s = Arrays.spliterator(source.stream().toArray(Integer[]::new));
 135 
 136                     return new StreamSpliterators.UnorderedSliceSpliterator.OfRef<>(s, skip, limit);
 137                 }, uca);
 138             };
 139             data.add(new Object[]{"StreamSpliterators.UnorderedSliceSpliterator.OfRef", r});
 140         }
 141 
 142         {
 143             SliceTester r = (size, skip, limit) -> {
 144                 final Collection<Integer> source =  IntStream.range(0, size).boxed().collect(toList());
 145                 final UnorderedContentAsserter<Integer> uca = new UnorderedContentAsserter<>(source);
 146 
 147                 SpliteratorTestHelper.testIntSpliterator(() -> {
 148                     Spliterator.OfInt s = Arrays.spliterator(source.stream().mapToInt(i->i).toArray());
 149 
 150                     return new StreamSpliterators.UnorderedSliceSpliterator.OfInt(s, skip, limit);
 151                 }, uca);
 152             };
 153             data.add(new Object[]{"StreamSpliterators.UnorderedSliceSpliterator.OfInt", r});
 154         }
 155 
 156         {
 157             SliceTester r = (size, skip, limit) -> {
 158                 final Collection<Long> source =  LongStream.range(0, size).boxed().collect(toList());
 159                 final UnorderedContentAsserter<Long> uca = new UnorderedContentAsserter<>(source);
 160 
 161                 SpliteratorTestHelper.testLongSpliterator(() -> {
 162                     Spliterator.OfLong s = Arrays.spliterator(source.stream().mapToLong(i->i).toArray());
 163 
 164                     return new StreamSpliterators.UnorderedSliceSpliterator.OfLong(s, skip, limit);
 165                 }, uca);
 166             };
 167             data.add(new Object[]{"StreamSpliterators.UnorderedSliceSpliterator.OfLong", r});
 168         }
 169 
 170         {
 171             SliceTester r = (size, skip, limit) -> {
 172                 final Collection<Double> source =  LongStream.range(0, size).asDoubleStream().boxed().collect(toList());
 173                 final UnorderedContentAsserter<Double> uca = new UnorderedContentAsserter<>(source);
 174 
 175                 SpliteratorTestHelper.testDoubleSpliterator(() -> {
 176                     Spliterator.OfDouble s = Arrays.spliterator(LongStream.range(0, SIZE).asDoubleStream().toArray());
 177 
 178                     return new StreamSpliterators.UnorderedSliceSpliterator.OfDouble(s, skip, limit);
 179                 }, uca);
 180             };
 181             data.add(new Object[]{"StreamSpliterators.UnorderedSliceSpliterator.OfLong", r});
 182         }
 183 
 184         return data.toArray(new Object[0][]);
 185     }
 186 
 187     static final int SIZE = 256;
 188 
 189     static final int STEP = 32;
 190 
 191     @Test(dataProvider = "sliceSpliteratorDataProvider")
 192     public void testSliceSpliterator(String description, SliceTester r) {
 193         setContext("size", SIZE);
 194         for (int skip = 0; skip < SIZE; skip += STEP) {
 195             setContext("skip", skip);
 196             for (int limit = 0; limit < SIZE; limit += STEP) {
 197                 setContext("limit", skip);
 198                 r.test(SIZE, skip, limit);
 199             }
 200         }
 201     }
 202 }