1 /*
   2  * Copyright (c) 2012, 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 
  24 import java.util.ArrayList;
  25 import java.util.BitSet;
  26 import java.util.Collection;
  27 import java.util.List;
  28 import java.util.PrimitiveIterator;
  29 import java.util.Random;
  30 import java.util.Spliterator;
  31 import java.util.SpliteratorOfIntDataBuilder;
  32 import java.util.SpliteratorTestHelper;
  33 import java.util.function.IntConsumer;
  34 import java.util.function.IntSupplier;
  35 import java.util.function.Supplier;
  36 import java.util.stream.IntStream;
  37 
  38 import org.testng.annotations.DataProvider;
  39 import org.testng.annotations.Test;
  40 
  41 import static java.util.stream.Collectors.toList;
  42 
  43 import static org.testng.Assert.assertEquals;
  44 import static org.testng.Assert.assertFalse;
  45 import static org.testng.Assert.assertThrows;
  46 import static org.testng.Assert.assertTrue;
  47 
  48 /**
  49  * @test
  50  * @summary test BitSet stream
  51  * @bug 8012645 8076442
  52  * @requires os.maxMemory >= 2g
  53  * @library ../../stream/bootlib
  54  * @build java.base/java.util.SpliteratorTestHelper
  55  *        java.base/java.util.SpliteratorOfIntDataBuilder
  56  * @run testng/othervm -Xms512m -Xmx1024m BitSetStreamTest
  57  */
  58 public class BitSetStreamTest extends SpliteratorTestHelper {
  59     static class Fibs implements IntSupplier {
  60         private int n1 = 0;
  61         private int n2 = 1;
  62 
  63         static int fibs(int n) {
  64             Fibs f = new Fibs();
  65             while (n-- > 0) f.getAsInt();
  66             return f.getAsInt();
  67         }
  68 
  69         public int getAsInt() { int s = n1; n1 = n2; n2 = s + n1; return s; }
  70     }
  71 
  72     @Test
  73     public void testFibs() {
  74         Fibs f = new Fibs();
  75         assertEquals(0, f.getAsInt());
  76         assertEquals(1, f.getAsInt());
  77         assertEquals(1, f.getAsInt());
  78         assertEquals(2, f.getAsInt());
  79         assertEquals(3, f.getAsInt());
  80         assertEquals(5, f.getAsInt());
  81         assertEquals(8, f.getAsInt());
  82         assertEquals(13, f.getAsInt());
  83         assertEquals(987, Fibs.fibs(16));
  84     }
  85 
  86 
  87     @DataProvider(name = "cases")
  88     public static Object[][] produceCases() {
  89         return new Object[][] {
  90                 { "none", IntStream.empty() },
  91                 { "index 0", IntStream.of(0) },
  92                 { "index 255", IntStream.of(255) },
  93                 { "index 0 and 255", IntStream.of(0, 255) },
  94                 { "index Integer.MAX_VALUE", IntStream.of(Integer.MAX_VALUE) },
  95                 { "index Integer.MAX_VALUE - 1", IntStream.of(Integer.MAX_VALUE - 1) },
  96                 { "index 0 and Integer.MAX_VALUE", IntStream.of(0, Integer.MAX_VALUE) },
  97                 { "every bit", IntStream.range(0, 255) },
  98                 { "step 2", IntStream.range(0, 255).map(f -> f * 2) },
  99                 { "step 3", IntStream.range(0, 255).map(f -> f * 3) },
 100                 { "step 5", IntStream.range(0, 255).map(f -> f * 5) },
 101                 { "step 7", IntStream.range(0, 255).map(f -> f * 7) },
 102                 { "1, 10, 100, 1000", IntStream.of(1, 10, 100, 1000) },
 103                 { "25 fibs", IntStream.generate(new Fibs()).limit(25) }
 104         };
 105     }
 106 
 107     @Test(dataProvider = "cases")
 108     public void testBitsetStream(String name, IntStream data) {
 109         BitSet bs = data.collect(BitSet::new, BitSet::set, BitSet::or);
 110 
 111         assertEquals(bs.cardinality(), bs.stream().count());
 112 
 113         int[] indexHolder = new int[] { -1 };
 114         bs.stream().forEach(i -> {
 115             int ei = indexHolder[0];
 116             indexHolder[0] = bs.nextSetBit(ei + 1);
 117             assertEquals(i, indexHolder[0]);
 118         });
 119 
 120         PrimitiveIterator.OfInt it = bs.stream().iterator();
 121         for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i + 1)) {
 122             assertTrue(it.hasNext());
 123             assertEquals(it.nextInt(), i);
 124             if (i == Integer.MAX_VALUE)
 125                 break; // or (i + 1) would overflow
 126         }
 127         assertFalse(it.hasNext());
 128     }
 129 
 130     static Object[][] spliteratorOfIntDataProvider;
 131 
 132     @DataProvider(name = "BitSet.stream.spliterator")
 133     public static Object[][] spliteratorOfIntDataProvider() {
 134         if (spliteratorOfIntDataProvider != null) {
 135             return spliteratorOfIntDataProvider;
 136         }
 137 
 138         List<Object[]> data = new ArrayList<>();
 139 
 140         Object[][] bitStreamTestcases = new Object[][] {
 141                 { "none", IntStream.empty().toArray() },
 142                 { "index 0", IntStream.of(0).toArray() },
 143                 { "index 255", IntStream.of(255).toArray() },
 144                 { "index 0 and 255", IntStream.of(0, 255).toArray() },
 145                 { "index Integer.MAX_VALUE", IntStream.of(Integer.MAX_VALUE).toArray() },
 146                 { "index Integer.MAX_VALUE - 1", IntStream.of(Integer.MAX_VALUE - 1).toArray() },
 147                 { "index 0 and Integer.MAX_VALUE", IntStream.of(0, Integer.MAX_VALUE).toArray() },
 148                 { "every bit", IntStream.range(0, 255).toArray() },
 149                 { "step 2", IntStream.range(0, 255).map(f -> f * 2).toArray() },
 150                 { "step 3", IntStream.range(0, 255).map(f -> f * 3).toArray() },
 151                 { "step 5", IntStream.range(0, 255).map(f -> f * 5).toArray() },
 152                 { "step 7", IntStream.range(0, 255).map(f -> f * 7).toArray() },
 153                 { "1, 10, 100, 1000", IntStream.of(1, 10, 100, 1000).toArray() },
 154         };
 155         for (Object[] tc : bitStreamTestcases) {
 156             String description = (String)tc[0];
 157             int[] exp = (int[])tc[1];
 158             SpliteratorOfIntDataBuilder db = new SpliteratorOfIntDataBuilder(
 159                     data, IntStream.of(exp).boxed().collect(toList()));
 160 
 161             db.add("BitSet.stream.spliterator() {" + description + "}", () ->
 162                 IntStream.of(exp).collect(BitSet::new, BitSet::set, BitSet::or).
 163                         stream().spliterator()
 164             );
 165         }
 166         return spliteratorOfIntDataProvider = data.toArray(new Object[0][]);
 167     }
 168 
 169     @Test(dataProvider = "BitSet.stream.spliterator")
 170     public void testIntNullPointerException(String description, Collection<Integer> exp, Supplier<Spliterator.OfInt> s) {
 171         assertThrows(NullPointerException.class, () -> s.get().forEachRemaining((IntConsumer) null));
 172         assertThrows(NullPointerException.class, () -> s.get().tryAdvance((IntConsumer) null));
 173     }
 174 
 175     @Test(dataProvider = "BitSet.stream.spliterator")
 176     public void testIntForEach(String description, Collection<Integer> exp, Supplier<Spliterator.OfInt> s) {
 177         testForEach(exp, s, intBoxingConsumer());
 178     }
 179 
 180     @Test(dataProvider = "BitSet.stream.spliterator")
 181     public void testIntTryAdvance(String description, Collection<Integer> exp, Supplier<Spliterator.OfInt> s) {
 182         testTryAdvance(exp, s, intBoxingConsumer());
 183     }
 184 
 185     @Test(dataProvider = "BitSet.stream.spliterator")
 186     public void testIntMixedTryAdvanceForEach(String description, Collection<Integer> exp, Supplier<Spliterator.OfInt> s) {
 187         testMixedTryAdvanceForEach(exp, s, intBoxingConsumer());
 188     }
 189 
 190     @Test(dataProvider = "BitSet.stream.spliterator")
 191     public void testIntMixedTraverseAndSplit(String description, Collection<Integer> exp, Supplier<Spliterator.OfInt> s) {
 192         testMixedTraverseAndSplit(exp, s, intBoxingConsumer());
 193     }
 194 
 195     @Test(dataProvider = "BitSet.stream.spliterator")
 196     public void testIntSplitAfterFullTraversal(String description, Collection<Integer> exp, Supplier<Spliterator.OfInt> s) {
 197         testSplitAfterFullTraversal(s, intBoxingConsumer());
 198     }
 199 
 200     @Test(dataProvider = "BitSet.stream.spliterator")
 201     public void testIntSplitOnce(String description, Collection<Integer> exp, Supplier<Spliterator.OfInt> s) {
 202         testSplitOnce(exp, s, intBoxingConsumer());
 203     }
 204 
 205     @Test(dataProvider = "BitSet.stream.spliterator")
 206     public void testIntSplitSixDeep(String description, Collection<Integer> exp, Supplier<Spliterator.OfInt> s) {
 207         testSplitSixDeep(exp, s, intBoxingConsumer());
 208     }
 209 
 210     @Test(dataProvider = "BitSet.stream.spliterator")
 211     public void testIntSplitUntilNull(String description, Collection<Integer> exp, Supplier<Spliterator.OfInt> s) {
 212         testSplitUntilNull(exp, s, intBoxingConsumer());
 213     }
 214 
 215     @Test
 216     public void testRandomStream() {
 217         final int size = 1024 * 1024;
 218         final int[] seeds = {
 219                 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,
 220                 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
 221         final byte[] bytes = new byte[size];
 222         for (int seed : seeds) {
 223             final Random random = new Random(seed);
 224             random.nextBytes(bytes);
 225 
 226             BitSet bitSet = BitSet.valueOf(bytes);
 227             testBitSetContents(bitSet, bitSet.stream().toArray());
 228             testBitSetContents(bitSet, bitSet.stream().parallel().toArray());
 229         }
 230     }
 231 
 232     void testBitSetContents(BitSet bitSet, int[] array) {
 233         int cardinality = bitSet.cardinality();
 234         assertEquals(array.length, cardinality);
 235         int nextSetBit = -1;
 236         for (int i = 0; i < cardinality; i++) {
 237             nextSetBit = bitSet.nextSetBit(nextSetBit + 1);
 238             assertEquals(array[i], nextSetBit);
 239         }
 240     }
 241 }