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