Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/BitSet/BitSetStreamTest.java
38811 views
/*1* Copyright (c) 2012, 2013, 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 org.testng.annotations.DataProvider;24import org.testng.annotations.Test;2526import java.lang.Integer;27import java.lang.Object;28import java.lang.System;29import java.util.BitSet;30import java.util.OptionalInt;31import java.util.PrimitiveIterator;32import java.util.Random;33import java.util.function.IntSupplier;34import java.util.stream.Collectors;35import java.util.stream.IntStream;3637import static org.testng.Assert.assertEquals;38import static org.testng.Assert.assertFalse;39import static org.testng.Assert.assertTrue;40import static org.testng.Assert.fail;4142/**43* @test44* @summary test BitSet stream45* @bug 801264546* @run testng BitSetStreamTest47*/48public class BitSetStreamTest {49static class Fibs implements IntSupplier {50private int n1 = 0;51private int n2 = 1;5253static int fibs(int n) {54Fibs f = new Fibs();55while (n-- > 0) f.getAsInt();56return f.getAsInt();57}5859public int getAsInt() { int s = n1; n1 = n2; n2 = s + n1; return s; }60}6162private static final Object[][] testcases = new Object[][] {63{ "none", IntStream.empty() },64{ "index 0", IntStream.of(0) },65{ "index 255", IntStream.of(255) },66{ "every bit", IntStream.range(0, 255) },67{ "step 2", IntStream.range(0, 255).map(f -> f * 2) },68{ "step 3", IntStream.range(0, 255).map(f -> f * 3) },69{ "step 5", IntStream.range(0, 255).map(f -> f * 5) },70{ "step 7", IntStream.range(0, 255).map(f -> f * 7) },71{ "1, 10, 100, 1000", IntStream.of(1, 10, 100, 1000) },72{ "25 fibs", IntStream.generate(new Fibs()).limit(25) }73};7475@DataProvider(name = "cases")76public static Object[][] produceCases() {77return testcases;78}7980@Test81public void testFibs() {82Fibs f = new Fibs();83assertEquals(0, f.getAsInt());84assertEquals(1, f.getAsInt());85assertEquals(1, f.getAsInt());86assertEquals(2, f.getAsInt());87assertEquals(3, f.getAsInt());88assertEquals(5, f.getAsInt());89assertEquals(8, f.getAsInt());90assertEquals(13, f.getAsInt());91assertEquals(987, Fibs.fibs(16));92}9394@Test(dataProvider = "cases")95public void testBitsetStream(String name, IntStream data) {96BitSet bs = new BitSet();97long setBits = data.distinct()98.peek(i -> bs.set(i))99.count();100101assertEquals(bs.cardinality(), setBits);102assertEquals(bs.cardinality(), bs.stream().reduce(0, (s, i) -> s+1));103104PrimitiveIterator.OfInt it = bs.stream().iterator();105for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) {106assertTrue(it.hasNext());107assertEquals(it.nextInt(), i);108}109assertFalse(it.hasNext());110}111112@Test113public void testRandomStream() {114final int size = 1024 * 1024;115final int[] seeds = {1162, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,11743, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};118final byte[] bytes = new byte[size];119for (int seed : seeds) {120final Random random = new Random(seed);121random.nextBytes(bytes);122final BitSet bitSet = BitSet.valueOf(bytes);123final int cardinality = bitSet.cardinality();124final IntStream stream = bitSet.stream();125final int[] array = stream.toArray();126assertEquals(array.length, cardinality);127int nextSetBit = -1;128for (int i=0; i < cardinality; i++) {129nextSetBit = bitSet.nextSetBit(nextSetBit + 1);130assertEquals(array[i], nextSetBit);131}132}133}134}135136137