Path: blob/master/test/jdk/java/util/BitSet/stream/BitSetStreamTest.java
41155 views
/*1* Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.util.ArrayList;24import java.util.BitSet;25import java.util.Collection;26import java.util.List;27import java.util.PrimitiveIterator;28import java.util.Random;29import java.util.Spliterator;30import java.util.SpliteratorOfIntDataBuilder;31import java.util.SpliteratorTestHelper;32import java.util.function.IntConsumer;33import java.util.function.IntSupplier;34import java.util.function.Supplier;35import java.util.stream.IntStream;3637import org.testng.annotations.DataProvider;38import org.testng.annotations.Test;3940import static java.util.stream.Collectors.toList;4142import static org.testng.Assert.assertEquals;43import static org.testng.Assert.assertFalse;44import static org.testng.Assert.assertThrows;45import static org.testng.Assert.assertTrue;4647/**48* @test49* @summary test BitSet stream50* @bug 8012645 807644251* @requires os.maxMemory >= 2g52* @library /lib/testlibrary/bootlib53* @build java.base/java.util.SpliteratorTestHelper54* java.base/java.util.SpliteratorOfIntDataBuilder55* @run testng/othervm -Xms512m -Xmx1024m BitSetStreamTest56*/57public class BitSetStreamTest extends SpliteratorTestHelper {58static class Fibs implements IntSupplier {59private int n1 = 0;60private int n2 = 1;6162static int fibs(int n) {63Fibs f = new Fibs();64while (n-- > 0) f.getAsInt();65return f.getAsInt();66}6768public int getAsInt() { int s = n1; n1 = n2; n2 = s + n1; return s; }69}7071@Test72public void testFibs() {73Fibs f = new Fibs();74assertEquals(0, f.getAsInt());75assertEquals(1, f.getAsInt());76assertEquals(1, f.getAsInt());77assertEquals(2, f.getAsInt());78assertEquals(3, f.getAsInt());79assertEquals(5, f.getAsInt());80assertEquals(8, f.getAsInt());81assertEquals(13, f.getAsInt());82assertEquals(987, Fibs.fibs(16));83}848586@DataProvider(name = "cases")87public static Object[][] produceCases() {88return new Object[][] {89{ "none", IntStream.empty() },90{ "index 0", IntStream.of(0) },91{ "index 255", IntStream.of(255) },92{ "index 0 and 255", IntStream.of(0, 255) },93{ "index Integer.MAX_VALUE", IntStream.of(Integer.MAX_VALUE) },94{ "index Integer.MAX_VALUE - 1", IntStream.of(Integer.MAX_VALUE - 1) },95{ "index 0 and Integer.MAX_VALUE", IntStream.of(0, Integer.MAX_VALUE) },96{ "every bit", IntStream.range(0, 255) },97{ "step 2", IntStream.range(0, 255).map(f -> f * 2) },98{ "step 3", IntStream.range(0, 255).map(f -> f * 3) },99{ "step 5", IntStream.range(0, 255).map(f -> f * 5) },100{ "step 7", IntStream.range(0, 255).map(f -> f * 7) },101{ "1, 10, 100, 1000", IntStream.of(1, 10, 100, 1000) },102{ "25 fibs", IntStream.generate(new Fibs()).limit(25) }103};104}105106@Test(dataProvider = "cases")107public void testBitsetStream(String name, IntStream data) {108BitSet bs = data.collect(BitSet::new, BitSet::set, BitSet::or);109110assertEquals(bs.cardinality(), bs.stream().count());111112int[] indexHolder = new int[] { -1 };113bs.stream().forEach(i -> {114int ei = indexHolder[0];115indexHolder[0] = bs.nextSetBit(ei + 1);116assertEquals(i, indexHolder[0]);117});118119PrimitiveIterator.OfInt it = bs.stream().iterator();120for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i + 1)) {121assertTrue(it.hasNext());122assertEquals(it.nextInt(), i);123if (i == Integer.MAX_VALUE)124break; // or (i + 1) would overflow125}126assertFalse(it.hasNext());127}128129static Object[][] spliteratorOfIntDataProvider;130131@DataProvider(name = "BitSet.stream.spliterator")132public static Object[][] spliteratorOfIntDataProvider() {133if (spliteratorOfIntDataProvider != null) {134return spliteratorOfIntDataProvider;135}136137List<Object[]> data = new ArrayList<>();138139Object[][] bitStreamTestcases = new Object[][] {140{ "none", IntStream.empty().toArray() },141{ "index 0", IntStream.of(0).toArray() },142{ "index 255", IntStream.of(255).toArray() },143{ "index 0 and 255", IntStream.of(0, 255).toArray() },144{ "index Integer.MAX_VALUE", IntStream.of(Integer.MAX_VALUE).toArray() },145{ "index Integer.MAX_VALUE - 1", IntStream.of(Integer.MAX_VALUE - 1).toArray() },146{ "index 0 and Integer.MAX_VALUE", IntStream.of(0, Integer.MAX_VALUE).toArray() },147{ "every bit", IntStream.range(0, 255).toArray() },148{ "step 2", IntStream.range(0, 255).map(f -> f * 2).toArray() },149{ "step 3", IntStream.range(0, 255).map(f -> f * 3).toArray() },150{ "step 5", IntStream.range(0, 255).map(f -> f * 5).toArray() },151{ "step 7", IntStream.range(0, 255).map(f -> f * 7).toArray() },152{ "1, 10, 100, 1000", IntStream.of(1, 10, 100, 1000).toArray() },153};154for (Object[] tc : bitStreamTestcases) {155String description = (String)tc[0];156int[] exp = (int[])tc[1];157SpliteratorOfIntDataBuilder db = new SpliteratorOfIntDataBuilder(158data, IntStream.of(exp).boxed().collect(toList()));159160db.add("BitSet.stream.spliterator() {" + description + "}", () ->161IntStream.of(exp).collect(BitSet::new, BitSet::set, BitSet::or).162stream().spliterator()163);164}165return spliteratorOfIntDataProvider = data.toArray(new Object[0][]);166}167168@Test(dataProvider = "BitSet.stream.spliterator")169public void testIntNullPointerException(String description, Collection<Integer> exp, Supplier<Spliterator.OfInt> s) {170assertThrows(NullPointerException.class, () -> s.get().forEachRemaining((IntConsumer) null));171assertThrows(NullPointerException.class, () -> s.get().tryAdvance((IntConsumer) null));172}173174@Test(dataProvider = "BitSet.stream.spliterator")175public void testIntForEach(String description, Collection<Integer> exp, Supplier<Spliterator.OfInt> s) {176testForEach(exp, s, intBoxingConsumer());177}178179@Test(dataProvider = "BitSet.stream.spliterator")180public void testIntTryAdvance(String description, Collection<Integer> exp, Supplier<Spliterator.OfInt> s) {181testTryAdvance(exp, s, intBoxingConsumer());182}183184@Test(dataProvider = "BitSet.stream.spliterator")185public void testIntMixedTryAdvanceForEach(String description, Collection<Integer> exp, Supplier<Spliterator.OfInt> s) {186testMixedTryAdvanceForEach(exp, s, intBoxingConsumer());187}188189@Test(dataProvider = "BitSet.stream.spliterator")190public void testIntMixedTraverseAndSplit(String description, Collection<Integer> exp, Supplier<Spliterator.OfInt> s) {191testMixedTraverseAndSplit(exp, s, intBoxingConsumer());192}193194@Test(dataProvider = "BitSet.stream.spliterator")195public void testIntSplitAfterFullTraversal(String description, Collection<Integer> exp, Supplier<Spliterator.OfInt> s) {196testSplitAfterFullTraversal(s, intBoxingConsumer());197}198199@Test(dataProvider = "BitSet.stream.spliterator")200public void testIntSplitOnce(String description, Collection<Integer> exp, Supplier<Spliterator.OfInt> s) {201testSplitOnce(exp, s, intBoxingConsumer());202}203204@Test(dataProvider = "BitSet.stream.spliterator")205public void testIntSplitSixDeep(String description, Collection<Integer> exp, Supplier<Spliterator.OfInt> s) {206testSplitSixDeep(exp, s, intBoxingConsumer());207}208209@Test(dataProvider = "BitSet.stream.spliterator")210public void testIntSplitUntilNull(String description, Collection<Integer> exp, Supplier<Spliterator.OfInt> s) {211testSplitUntilNull(exp, s, intBoxingConsumer());212}213214@Test215public void testRandomStream() {216final int size = 1024 * 1024;217final int[] seeds = {2182, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,21943, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};220final byte[] bytes = new byte[size];221for (int seed : seeds) {222final Random random = new Random(seed);223random.nextBytes(bytes);224225BitSet bitSet = BitSet.valueOf(bytes);226testBitSetContents(bitSet, bitSet.stream().toArray());227testBitSetContents(bitSet, bitSet.stream().parallel().toArray());228}229}230231void testBitSetContents(BitSet bitSet, int[] array) {232int cardinality = bitSet.cardinality();233assertEquals(array.length, cardinality);234int nextSetBit = -1;235for (int i = 0; i < cardinality; i++) {236nextSetBit = bitSet.nextSetBit(nextSetBit + 1);237assertEquals(array[i], nextSetBit);238}239}240}241242243