/* * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /* @test * @bug 4098239 4107540 4080736 4261102 4274710 4305272 * 4979017 4979028 4979031 5030267 6222207 8040806 * @summary Test the operation of the methods of BitSet class * @author Mike McCloskey, Martin Buchholz * @run main/othervm BSMethods * @key randomness */ import java.util.*; /** * This is a simple test class created to run tests on the BitSet class. * */ public class BSMethods { private static Random generator = new Random(); private static boolean failure = false; private static void fail(String diagnostic) { new Error(diagnostic).printStackTrace(); failure = true; } private static void check(boolean condition) { check(condition, "something's fishy"); } private static void check(boolean condition, String diagnostic) { if (! condition) fail(diagnostic); } private static void checkEmpty(BitSet s) { check(s.isEmpty(), "isEmpty"); check(s.length() == 0, "length"); check(s.cardinality() == 0, "cardinality"); check(s.equals(new BitSet()) , "equals"); check(s.equals(new BitSet(0)) , "equals"); check(s.equals(new BitSet(127)), "equals"); check(s.equals(new BitSet(128)), "equals"); check(s.nextSetBit(0) == -1, "nextSetBit"); check(s.nextSetBit(127) == -1, "nextSetBit"); check(s.nextSetBit(128) == -1, "nextSetBit"); check(s.nextClearBit(0) == 0, "nextClearBit"); check(s.nextClearBit(127) == 127, "nextClearBit"); check(s.nextClearBit(128) == 128, "nextClearBit"); check(s.toString().equals("{}"), "toString"); check(! s.get(0), "get"); } private static BitSet makeSet(int... elts) { BitSet s = new BitSet(); for (int elt : elts) s.set(elt); return s; } private static void checkEquality(BitSet s, BitSet t) { checkSanity(s, t); check(s.equals(t), "equals"); check(s.toString().equals(t.toString()), "equal strings"); check(s.length() == t.length(), "equal lengths"); check(s.cardinality() == t.cardinality(), "equal cardinalities"); } private static void checkSanity(BitSet... sets) { for (BitSet s : sets) { int len = s.length(); int cardinality1 = s.cardinality(); int cardinality2 = 0; for (int i = s.nextSetBit(0); i >= 0; i = s.nextSetBit(i+1)) { check(s.get(i)); cardinality2++; } check(s.nextSetBit(len) == -1, "last set bit"); check(s.nextClearBit(len) == len, "last set bit"); check(s.isEmpty() == (len == 0), "emptiness"); check(cardinality1 == cardinality2, "cardinalities"); check(len <= s.size(), "length <= size"); check(len >= 0, "length >= 0"); check(cardinality1 >= 0, "cardinality >= 0"); } } public static void main(String[] args) { //testFlipTime(); // These are the single bit versions testSetGetClearFlip(); // Test the ranged versions testClear(); testFlip(); testSet(); testGet(); // BitSet interaction calls testAndNot(); testAnd(); testOr(); testXor(); // Miscellaneous calls testLength(); testEquals(); testNextSetBit(); testNextClearBit(); testIntersects(); testCardinality(); testEmpty(); testEmpty2(); testToString(); testLogicalIdentities(); if (failure) throw new RuntimeException("One or more BitSet failures."); } private static void report(String testName, int failCount) { System.err.println(testName+": " + (failCount==0 ? "Passed":"Failed("+failCount+")")); if (failCount > 0) failure = true; } private static void testFlipTime() { // Make a fairly random bitset BitSet b1 = new BitSet(); b1.set(1000); long startTime = System.currentTimeMillis(); for(int x=0; x<100000; x++) { b1.flip(100, 900); } long endTime = System.currentTimeMillis(); long total = endTime - startTime; System.out.println("Multiple word flip Time "+total); startTime = System.currentTimeMillis(); for(int x=0; x<100000; x++) { b1.flip(2, 44); } endTime = System.currentTimeMillis(); total = endTime - startTime; System.out.println("Single word flip Time "+total); } private static void testNextSetBit() { int failCount = 0; for (int i=0; i<100; i++) { int numberOfSetBits = generator.nextInt(100) + 1; BitSet testSet = new BitSet(); int[] history = new int[numberOfSetBits]; // Set some random bits and remember them int nextBitToSet = 0; for (int x=0; x=0; x=testSet.nextSetBit(x+1)) { if (x != history[historyIndex++]) failCount++; } checkSanity(testSet); } report("NextSetBit ", failCount); } private static void testNextClearBit() { int failCount = 0; for (int i=0; i<1000; i++) { BitSet b = new BitSet(256); int[] history = new int[10]; // Set all the bits for (int x=0; x<256; x++) b.set(x); // Clear some random bits and remember them int nextBitToClear = 0; for (int x=0; x<10; x++) { nextBitToClear += generator.nextInt(24)+1; history[x] = nextBitToClear; b.clear(nextBitToClear); } // Verify their retrieval using nextClearBit() int historyIndex = 0; for(int x=b.nextClearBit(0); x<256; x=b.nextClearBit(x+1)) { if (x != history[historyIndex++]) failCount++; } checkSanity(b); } // regression test for 4350178 BitSet bs = new BitSet(); if (bs.nextClearBit(0) != 0) failCount++; for (int i = 0; i < 64; i++) { bs.set(i); if (bs.nextClearBit(0) != i+1) failCount++; } checkSanity(bs); report("NextClearBit ", failCount); } private static void testSetGetClearFlip() { int failCount = 0; for (int i=0; i<100; i++) { BitSet testSet = new BitSet(); HashSet history = new HashSet(); // Set a random number of bits in random places // up to a random maximum int nextBitToSet = 0; int numberOfSetBits = generator.nextInt(100) + 1; int highestPossibleSetBit = generator.nextInt(1000) + 1; for (int x=0; x setBitIterator = history.iterator(); while (setBitIterator.hasNext()) { Integer setBit = setBitIterator.next(); testSet.clear(setBit.intValue()); } // Verify they were cleared for (int x=0; x highestSetBit) highestSetBit = nextBitToSet; b1.set(nextBitToSet); if (b1.length() != highestSetBit + 1) failCount++; } checkSanity(b1); } // Test length after flip for (int i=0; i<100; i++) { BitSet b1 = new BitSet(256); for(int x=0; x<100; x++) { // Flip a random range twice int rangeStart = generator.nextInt(100); int rangeEnd = rangeStart + generator.nextInt(100); b1.flip(rangeStart); b1.flip(rangeStart); if (b1.length() != 0) failCount++; b1.flip(rangeStart, rangeEnd); b1.flip(rangeStart, rangeEnd); if (b1.length() != 0) failCount++; } checkSanity(b1); } // Test length after or for (int i=0; i<100; i++) { BitSet b1 = new BitSet(256); BitSet b2 = new BitSet(256); int bit1 = generator.nextInt(100); int bit2 = generator.nextInt(100); int highestSetBit = (bit1 > bit2) ? bit1 : bit2; b1.set(bit1); b2.set(bit2); b1.or(b2); if (b1.length() != highestSetBit + 1) failCount++; checkSanity(b1, b2); } report("Length ", failCount); } private static void testClear() { int failCount = 0; for (int i=0; i<1000; i++) { BitSet b1 = new BitSet(); // Make a fairly random bitset int numberOfSetBits = generator.nextInt(100) + 1; int highestPossibleSetBit = generator.nextInt(1000) + 1; for (int x=0; x= 512*MB) { // only run it if we have enough memory try { check(makeSet(Integer.MAX_VALUE-1).toString().equals( "{" + (Integer.MAX_VALUE-1) + "}")); check(makeSet(Integer.MAX_VALUE).toString().equals( "{" + Integer.MAX_VALUE + "}")); check(makeSet(0, 1, Integer.MAX_VALUE-1, Integer.MAX_VALUE).toString().equals( "{0, 1, " + (Integer.MAX_VALUE-1) + ", " + Integer.MAX_VALUE + "}")); } catch (IndexOutOfBoundsException exc) { fail("toString() with indices near MAX_VALUE"); } } } private static void testLogicalIdentities() { int failCount = 0; // Verify that (!b1)|(!b2) == !(b1&b2) for (int i=0; i<50; i++) { // Construct two fairly random bitsets BitSet b1 = new BitSet(); BitSet b2 = new BitSet(); int numberOfSetBits = generator.nextInt(100) + 1; int highestPossibleSetBit = generator.nextInt(1000) + 1; for (int x=0; x