Path: blob/master/test/jdk/java/util/BitSet/BSMethods.java
41149 views
/*1* Copyright (c) 1998, 2014, 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*/2223/* @test24* @bug 4098239 4107540 4080736 4261102 4274710 430527225* 4979017 4979028 4979031 5030267 6222207 804080626* @summary Test the operation of the methods of BitSet class27* @author Mike McCloskey, Martin Buchholz28* @run main/othervm BSMethods29* @key randomness30*/3132import java.util.*;3334/**35* This is a simple test class created to run tests on the BitSet class.36*37*/38public class BSMethods {3940private static Random generator = new Random();41private static boolean failure = false;4243private static void fail(String diagnostic) {44new Error(diagnostic).printStackTrace();45failure = true;46}4748private static void check(boolean condition) {49check(condition, "something's fishy");50}5152private static void check(boolean condition, String diagnostic) {53if (! condition)54fail(diagnostic);55}5657private static void checkEmpty(BitSet s) {58check(s.isEmpty(), "isEmpty");59check(s.length() == 0, "length");60check(s.cardinality() == 0, "cardinality");61check(s.equals(new BitSet()) , "equals");62check(s.equals(new BitSet(0)) , "equals");63check(s.equals(new BitSet(127)), "equals");64check(s.equals(new BitSet(128)), "equals");65check(s.nextSetBit(0) == -1, "nextSetBit");66check(s.nextSetBit(127) == -1, "nextSetBit");67check(s.nextSetBit(128) == -1, "nextSetBit");68check(s.nextClearBit(0) == 0, "nextClearBit");69check(s.nextClearBit(127) == 127, "nextClearBit");70check(s.nextClearBit(128) == 128, "nextClearBit");71check(s.toString().equals("{}"), "toString");72check(! s.get(0), "get");73}7475private static BitSet makeSet(int... elts) {76BitSet s = new BitSet();77for (int elt : elts)78s.set(elt);79return s;80}8182private static void checkEquality(BitSet s, BitSet t) {83checkSanity(s, t);84check(s.equals(t), "equals");85check(s.toString().equals(t.toString()), "equal strings");86check(s.length() == t.length(), "equal lengths");87check(s.cardinality() == t.cardinality(), "equal cardinalities");88}8990private static void checkSanity(BitSet... sets) {91for (BitSet s : sets) {92int len = s.length();93int cardinality1 = s.cardinality();94int cardinality2 = 0;95for (int i = s.nextSetBit(0); i >= 0; i = s.nextSetBit(i+1)) {96check(s.get(i));97cardinality2++;98}99check(s.nextSetBit(len) == -1, "last set bit");100check(s.nextClearBit(len) == len, "last set bit");101check(s.isEmpty() == (len == 0), "emptiness");102check(cardinality1 == cardinality2, "cardinalities");103check(len <= s.size(), "length <= size");104check(len >= 0, "length >= 0");105check(cardinality1 >= 0, "cardinality >= 0");106}107}108109public static void main(String[] args) {110111//testFlipTime();112113// These are the single bit versions114testSetGetClearFlip();115116// Test the ranged versions117testClear();118119testFlip();120testSet();121testGet();122123// BitSet interaction calls124testAndNot();125testAnd();126testOr();127testXor();128129// Miscellaneous calls130testLength();131testEquals();132testNextSetBit();133testNextClearBit();134testIntersects();135testCardinality();136testEmpty();137testEmpty2();138testToString();139testLogicalIdentities();140141if (failure)142throw new RuntimeException("One or more BitSet failures.");143}144145private static void report(String testName, int failCount) {146System.err.println(testName+": " +147(failCount==0 ? "Passed":"Failed("+failCount+")"));148if (failCount > 0)149failure = true;150}151152private static void testFlipTime() {153// Make a fairly random bitset154BitSet b1 = new BitSet();155b1.set(1000);156long startTime = System.currentTimeMillis();157for(int x=0; x<100000; x++) {158b1.flip(100, 900);159}160long endTime = System.currentTimeMillis();161long total = endTime - startTime;162System.out.println("Multiple word flip Time "+total);163164startTime = System.currentTimeMillis();165for(int x=0; x<100000; x++) {166b1.flip(2, 44);167}168endTime = System.currentTimeMillis();169total = endTime - startTime;170System.out.println("Single word flip Time "+total);171}172173private static void testNextSetBit() {174int failCount = 0;175176for (int i=0; i<100; i++) {177int numberOfSetBits = generator.nextInt(100) + 1;178BitSet testSet = new BitSet();179int[] history = new int[numberOfSetBits];180181// Set some random bits and remember them182int nextBitToSet = 0;183for (int x=0; x<numberOfSetBits; x++) {184nextBitToSet += generator.nextInt(30)+1;185history[x] = nextBitToSet;186testSet.set(nextBitToSet);187}188189// Verify their retrieval using nextSetBit()190int historyIndex = 0;191for(int x=testSet.nextSetBit(0); x>=0; x=testSet.nextSetBit(x+1)) {192if (x != history[historyIndex++])193failCount++;194}195196checkSanity(testSet);197}198199report("NextSetBit ", failCount);200}201202private static void testNextClearBit() {203int failCount = 0;204205for (int i=0; i<1000; i++) {206BitSet b = new BitSet(256);207int[] history = new int[10];208209// Set all the bits210for (int x=0; x<256; x++)211b.set(x);212213// Clear some random bits and remember them214int nextBitToClear = 0;215for (int x=0; x<10; x++) {216nextBitToClear += generator.nextInt(24)+1;217history[x] = nextBitToClear;218b.clear(nextBitToClear);219}220221// Verify their retrieval using nextClearBit()222int historyIndex = 0;223for(int x=b.nextClearBit(0); x<256; x=b.nextClearBit(x+1)) {224if (x != history[historyIndex++])225failCount++;226}227228checkSanity(b);229}230231// regression test for 4350178232BitSet bs = new BitSet();233if (bs.nextClearBit(0) != 0)234failCount++;235for (int i = 0; i < 64; i++) {236bs.set(i);237if (bs.nextClearBit(0) != i+1)238failCount++;239}240241checkSanity(bs);242243report("NextClearBit ", failCount);244}245246private static void testSetGetClearFlip() {247int failCount = 0;248249for (int i=0; i<100; i++) {250BitSet testSet = new BitSet();251HashSet<Integer> history = new HashSet<Integer>();252253// Set a random number of bits in random places254// up to a random maximum255int nextBitToSet = 0;256int numberOfSetBits = generator.nextInt(100) + 1;257int highestPossibleSetBit = generator.nextInt(1000) + 1;258for (int x=0; x<numberOfSetBits; x++) {259nextBitToSet = generator.nextInt(highestPossibleSetBit);260history.add(new Integer(nextBitToSet));261testSet.set(nextBitToSet);262}263264// Make sure each bit is set appropriately265for (int x=0; x<highestPossibleSetBit; x++) {266if (testSet.get(x) != history.contains(new Integer(x)))267failCount++;268}269270// Clear the bits271Iterator<Integer> setBitIterator = history.iterator();272while (setBitIterator.hasNext()) {273Integer setBit = setBitIterator.next();274testSet.clear(setBit.intValue());275}276277// Verify they were cleared278for (int x=0; x<highestPossibleSetBit; x++)279if (testSet.get(x))280failCount++;281if(testSet.length() != 0)282failCount++;283284// Set them with set(int, boolean)285setBitIterator = history.iterator();286while (setBitIterator.hasNext()) {287Integer setBit = setBitIterator.next();288testSet.set(setBit.intValue(), true);289}290291// Make sure each bit is set appropriately292for (int x=0; x<highestPossibleSetBit; x++) {293if (testSet.get(x) != history.contains(new Integer(x)))294failCount++;295}296297// Clear them with set(int, boolean)298setBitIterator = history.iterator();299while (setBitIterator.hasNext()) {300Integer setBit = (Integer)setBitIterator.next();301testSet.set(setBit.intValue(), false);302}303304// Verify they were cleared305for (int x=0; x<highestPossibleSetBit; x++)306if (testSet.get(x))307failCount++;308if(testSet.length() != 0)309failCount++;310311// Flip them on312setBitIterator = history.iterator();313while (setBitIterator.hasNext()) {314Integer setBit = (Integer)setBitIterator.next();315testSet.flip(setBit.intValue());316}317318// Verify they were flipped319for (int x=0; x<highestPossibleSetBit; x++) {320if (testSet.get(x) != history.contains(new Integer(x)))321failCount++;322}323324// Flip them off325setBitIterator = history.iterator();326while (setBitIterator.hasNext()) {327Integer setBit = (Integer)setBitIterator.next();328testSet.flip(setBit.intValue());329}330331// Verify they were flipped332for (int x=0; x<highestPossibleSetBit; x++)333if (testSet.get(x))334failCount++;335if(testSet.length() != 0)336failCount++;337338checkSanity(testSet);339}340341report("SetGetClearFlip ", failCount);342}343344private static void testAndNot() {345int failCount = 0;346347for (int i=0; i<100; i++) {348BitSet b1 = new BitSet(256);349BitSet b2 = new BitSet(256);350351// Set some random bits in first set and remember them352int nextBitToSet = 0;353for (int x=0; x<10; x++)354b1.set(generator.nextInt(255));355356// Set some random bits in second set and remember them357for (int x=10; x<20; x++)358b2.set(generator.nextInt(255));359360// andNot the sets together361BitSet b3 = (BitSet)b1.clone();362b3.andNot(b2);363364// Examine each bit of b3 for errors365for(int x=0; x<256; x++) {366boolean bit1 = b1.get(x);367boolean bit2 = b2.get(x);368boolean bit3 = b3.get(x);369if (!(bit3 == (bit1 & (!bit2))))370failCount++;371}372checkSanity(b1, b2, b3);373}374375report("AndNot ", failCount);376}377378private static void testAnd() {379int failCount = 0;380381for (int i=0; i<100; i++) {382BitSet b1 = new BitSet(256);383BitSet b2 = new BitSet(256);384385// Set some random bits in first set and remember them386int nextBitToSet = 0;387for (int x=0; x<10; x++)388b1.set(generator.nextInt(255));389390// Set more random bits in second set and remember them391for (int x=10; x<20; x++)392b2.set(generator.nextInt(255));393394// And the sets together395BitSet b3 = (BitSet)b1.clone();396b3.and(b2);397398// Examine each bit of b3 for errors399for(int x=0; x<256; x++) {400boolean bit1 = b1.get(x);401boolean bit2 = b2.get(x);402boolean bit3 = b3.get(x);403if (!(bit3 == (bit1 & bit2)))404failCount++;405}406checkSanity(b1, b2, b3);407}408409// `and' that happens to clear the last word410BitSet b4 = makeSet(2, 127);411b4.and(makeSet(2, 64));412checkSanity(b4);413if (!(b4.equals(makeSet(2))))414failCount++;415416report("And ", failCount);417}418419private static void testOr() {420int failCount = 0;421422for (int i=0; i<100; i++) {423BitSet b1 = new BitSet(256);424BitSet b2 = new BitSet(256);425int[] history = new int[20];426427// Set some random bits in first set and remember them428int nextBitToSet = 0;429for (int x=0; x<10; x++) {430nextBitToSet = generator.nextInt(255);431history[x] = nextBitToSet;432b1.set(nextBitToSet);433}434435// Set more random bits in second set and remember them436for (int x=10; x<20; x++) {437nextBitToSet = generator.nextInt(255);438history[x] = nextBitToSet;439b2.set(nextBitToSet);440}441442// Or the sets together443BitSet b3 = (BitSet)b1.clone();444b3.or(b2);445446// Verify the set bits of b3 from the history447int historyIndex = 0;448for(int x=0; x<20; x++) {449if (!b3.get(history[x]))450failCount++;451}452453// Examine each bit of b3 for errors454for(int x=0; x<256; x++) {455boolean bit1 = b1.get(x);456boolean bit2 = b2.get(x);457boolean bit3 = b3.get(x);458if (!(bit3 == (bit1 | bit2)))459failCount++;460}461checkSanity(b1, b2, b3);462}463464report("Or ", failCount);465}466467private static void testXor() {468int failCount = 0;469470for (int i=0; i<100; i++) {471BitSet b1 = new BitSet(256);472BitSet b2 = new BitSet(256);473474// Set some random bits in first set and remember them475int nextBitToSet = 0;476for (int x=0; x<10; x++)477b1.set(generator.nextInt(255));478479// Set more random bits in second set and remember them480for (int x=10; x<20; x++)481b2.set(generator.nextInt(255));482483// Xor the sets together484BitSet b3 = (BitSet)b1.clone();485b3.xor(b2);486487// Examine each bit of b3 for errors488for(int x=0; x<256; x++) {489boolean bit1 = b1.get(x);490boolean bit2 = b2.get(x);491boolean bit3 = b3.get(x);492if (!(bit3 == (bit1 ^ bit2)))493failCount++;494}495checkSanity(b1, b2, b3);496b3.xor(b3); checkEmpty(b3);497}498499// xor that happens to clear the last word500BitSet b4 = makeSet(2, 64, 127);501b4.xor(makeSet(64, 127));502checkSanity(b4);503if (!(b4.equals(makeSet(2))))504failCount++;505506report("Xor ", failCount);507}508509private static void testEquals() {510int failCount = 0;511512for (int i=0; i<100; i++) {513// Create BitSets of different sizes514BitSet b1 = new BitSet(generator.nextInt(1000)+1);515BitSet b2 = new BitSet(generator.nextInt(1000)+1);516517// Set some random bits518int nextBitToSet = 0;519for (int x=0; x<10; x++) {520nextBitToSet += generator.nextInt(50)+1;521b1.set(nextBitToSet);522b2.set(nextBitToSet);523}524525// Verify their equality despite different storage sizes526if (!b1.equals(b2))527failCount++;528checkEquality(b1,b2);529}530531report("Equals ", failCount);532}533534private static void testLength() {535int failCount = 0;536537// Test length after set538for (int i=0; i<100; i++) {539BitSet b1 = new BitSet(256);540int highestSetBit = 0;541542for(int x=0; x<100; x++) {543int nextBitToSet = generator.nextInt(255);544if (nextBitToSet > highestSetBit)545highestSetBit = nextBitToSet;546b1.set(nextBitToSet);547if (b1.length() != highestSetBit + 1)548failCount++;549}550checkSanity(b1);551}552553// Test length after flip554for (int i=0; i<100; i++) {555BitSet b1 = new BitSet(256);556for(int x=0; x<100; x++) {557// Flip a random range twice558int rangeStart = generator.nextInt(100);559int rangeEnd = rangeStart + generator.nextInt(100);560b1.flip(rangeStart);561b1.flip(rangeStart);562if (b1.length() != 0)563failCount++;564b1.flip(rangeStart, rangeEnd);565b1.flip(rangeStart, rangeEnd);566if (b1.length() != 0)567failCount++;568}569checkSanity(b1);570}571572// Test length after or573for (int i=0; i<100; i++) {574BitSet b1 = new BitSet(256);575BitSet b2 = new BitSet(256);576int bit1 = generator.nextInt(100);577int bit2 = generator.nextInt(100);578int highestSetBit = (bit1 > bit2) ? bit1 : bit2;579b1.set(bit1);580b2.set(bit2);581b1.or(b2);582if (b1.length() != highestSetBit + 1)583failCount++;584checkSanity(b1, b2);585}586587report("Length ", failCount);588}589590private static void testClear() {591int failCount = 0;592593for (int i=0; i<1000; i++) {594BitSet b1 = new BitSet();595596// Make a fairly random bitset597int numberOfSetBits = generator.nextInt(100) + 1;598int highestPossibleSetBit = generator.nextInt(1000) + 1;599600for (int x=0; x<numberOfSetBits; x++)601b1.set(generator.nextInt(highestPossibleSetBit));602603BitSet b2 = (BitSet)b1.clone();604605// Clear out a random range606int rangeStart = generator.nextInt(100);607int rangeEnd = rangeStart + generator.nextInt(100);608609// Use the clear(int, int) call on b1610b1.clear(rangeStart, rangeEnd);611612// Use a loop on b2613for (int x=rangeStart; x<rangeEnd; x++)614b2.clear(x);615616// Verify their equality617if (!b1.equals(b2)) {618System.out.println("rangeStart = " + rangeStart);619System.out.println("rangeEnd = " + rangeEnd);620System.out.println("b1 = " + b1);621System.out.println("b2 = " + b2);622failCount++;623}624checkEquality(b1,b2);625}626627report("Clear ", failCount);628}629630private static void testSet() {631int failCount = 0;632633// Test set(int, int)634for (int i=0; i<1000; i++) {635BitSet b1 = new BitSet();636637// Make a fairly random bitset638int numberOfSetBits = generator.nextInt(100) + 1;639int highestPossibleSetBit = generator.nextInt(1000) + 1;640641for (int x=0; x<numberOfSetBits; x++)642b1.set(generator.nextInt(highestPossibleSetBit));643644BitSet b2 = (BitSet)b1.clone();645646// Set a random range647int rangeStart = generator.nextInt(100);648int rangeEnd = rangeStart + generator.nextInt(100);649650// Use the set(int, int) call on b1651b1.set(rangeStart, rangeEnd);652653// Use a loop on b2654for (int x=rangeStart; x<rangeEnd; x++)655b2.set(x);656657// Verify their equality658if (!b1.equals(b2)) {659System.out.println("Set 1");660System.out.println("rangeStart = " + rangeStart);661System.out.println("rangeEnd = " + rangeEnd);662System.out.println("b1 = " + b1);663System.out.println("b2 = " + b2);664failCount++;665}666checkEquality(b1,b2);667}668669// Test set(int, int, boolean)670for (int i=0; i<100; i++) {671BitSet b1 = new BitSet();672673// Make a fairly random bitset674int numberOfSetBits = generator.nextInt(100) + 1;675int highestPossibleSetBit = generator.nextInt(1000) + 1;676677for (int x=0; x<numberOfSetBits; x++)678b1.set(generator.nextInt(highestPossibleSetBit));679680BitSet b2 = (BitSet)b1.clone();681boolean setOrClear = generator.nextBoolean();682683// Set a random range684int rangeStart = generator.nextInt(100);685int rangeEnd = rangeStart + generator.nextInt(100);686687// Use the set(int, int, boolean) call on b1688b1.set(rangeStart, rangeEnd, setOrClear);689690// Use a loop on b2691for (int x=rangeStart; x<rangeEnd; x++)692b2.set(x, setOrClear);693694// Verify their equality695if (!b1.equals(b2)) {696System.out.println("Set 2");697System.out.println("b1 = " + b1);698System.out.println("b2 = " + b2);699failCount++;700}701checkEquality(b1,b2);702}703704report("Set ", failCount);705}706707private static void testFlip() {708int failCount = 0;709710for (int i=0; i<1000; i++) {711BitSet b1 = new BitSet();712713// Make a fairly random bitset714int numberOfSetBits = generator.nextInt(100) + 1;715int highestPossibleSetBit = generator.nextInt(1000) + 1;716717for (int x=0; x<numberOfSetBits; x++)718b1.set(generator.nextInt(highestPossibleSetBit));719720BitSet b2 = (BitSet)b1.clone();721722// Flip a random range723int rangeStart = generator.nextInt(100);724int rangeEnd = rangeStart + generator.nextInt(100);725726// Use the flip(int, int) call on b1727b1.flip(rangeStart, rangeEnd);728729// Use a loop on b2730for (int x=rangeStart; x<rangeEnd; x++)731b2.flip(x);732733// Verify their equality734if (!b1.equals(b2))735failCount++;736checkEquality(b1,b2);737}738739report("Flip ", failCount);740}741742private static void testGet() {743int failCount = 0;744745for (int i=0; i<1000; i++) {746BitSet b1 = new BitSet();747748// Make a fairly random bitset749int numberOfSetBits = generator.nextInt(100) + 1;750int highestPossibleSetBit = generator.nextInt(1000) + 1;751752for (int x=0; x<numberOfSetBits; x++)753b1.set(generator.nextInt(highestPossibleSetBit));754755// Get a new set from a random range756int rangeStart = generator.nextInt(100);757int rangeEnd = rangeStart + generator.nextInt(100);758759BitSet b2 = b1.get(rangeStart, rangeEnd);760761BitSet b3 = new BitSet();762for(int x=rangeStart; x<rangeEnd; x++)763b3.set(x-rangeStart, b1.get(x));764765// Verify their equality766if (!b2.equals(b3)) {767System.out.println("start="+rangeStart);768System.out.println("end="+rangeEnd);769System.out.println(b1);770System.out.println(b2);771System.out.println(b3);772failCount++;773}774checkEquality(b2,b3);775}776777report("Get ", failCount);778}779780781private static void testIntersects() {782int failCount = 0;783784for (int i=0; i<100; i++) {785BitSet b1 = new BitSet(256);786BitSet b2 = new BitSet(256);787788// Set some random bits in first set789int nextBitToSet = 0;790for (int x=0; x<30; x++) {791nextBitToSet = generator.nextInt(255);792b1.set(nextBitToSet);793}794795// Set more random bits in second set796for (int x=0; x<30; x++) {797nextBitToSet = generator.nextInt(255);798b2.set(nextBitToSet);799}800801// Make sure they intersect802nextBitToSet = generator.nextInt(255);803b1.set(nextBitToSet);804b2.set(nextBitToSet);805806if (!b1.intersects(b2))807failCount++;808809// Remove the common set bits810b1.andNot(b2);811812// Make sure they don't intersect813if (b1.intersects(b2))814failCount++;815816checkSanity(b1, b2);817}818819report("Intersects ", failCount);820}821822private static void testCardinality() {823int failCount = 0;824825for (int i=0; i<100; i++) {826BitSet b1 = new BitSet(256);827828// Set a random number of increasing bits829int nextBitToSet = 0;830int iterations = generator.nextInt(20)+1;831for (int x=0; x<iterations; x++) {832nextBitToSet += generator.nextInt(20)+1;833b1.set(nextBitToSet);834}835836if (b1.cardinality() != iterations) {837System.out.println("Iterations is "+iterations);838System.out.println("Cardinality is "+b1.cardinality());839failCount++;840}841842checkSanity(b1);843}844845report("Cardinality ", failCount);846}847848private static void testEmpty() {849int failCount = 0;850851BitSet b1 = new BitSet();852if (!b1.isEmpty())853failCount++;854855int nextBitToSet = 0;856int numberOfSetBits = generator.nextInt(100) + 1;857int highestPossibleSetBit = generator.nextInt(1000) + 1;858for (int x=0; x<numberOfSetBits; x++) {859nextBitToSet = generator.nextInt(highestPossibleSetBit);860b1.set(nextBitToSet);861if (b1.isEmpty())862failCount++;863b1.clear(nextBitToSet);864if (!b1.isEmpty())865failCount++;866}867868report("Empty ", failCount);869}870871private static void testEmpty2() {872{BitSet t = new BitSet(); t.set(100); t.clear(3,600); checkEmpty(t);}873checkEmpty(new BitSet(0));874checkEmpty(new BitSet(342));875BitSet s = new BitSet(0);876checkEmpty(s);877s.clear(92); checkEmpty(s);878s.clear(127,127); checkEmpty(s);879s.set(127,127); checkEmpty(s);880s.set(128,128); checkEmpty(s);881BitSet empty = new BitSet();882{BitSet t = new BitSet(); t.and (empty); checkEmpty(t);}883{BitSet t = new BitSet(); t.or (empty); checkEmpty(t);}884{BitSet t = new BitSet(); t.xor (empty); checkEmpty(t);}885{BitSet t = new BitSet(); t.andNot(empty); checkEmpty(t);}886{BitSet t = new BitSet(); t.and (t); checkEmpty(t);}887{BitSet t = new BitSet(); t.or (t); checkEmpty(t);}888{BitSet t = new BitSet(); t.xor (t); checkEmpty(t);}889{BitSet t = new BitSet(); t.andNot(t); checkEmpty(t);}890{BitSet t = new BitSet(); t.and(makeSet(1)); checkEmpty(t);}891{BitSet t = new BitSet(); t.and(makeSet(127)); checkEmpty(t);}892{BitSet t = new BitSet(); t.and(makeSet(128)); checkEmpty(t);}893{BitSet t = new BitSet(); t.flip(7);t.flip(7); checkEmpty(t);}894{BitSet t = new BitSet(); checkEmpty(t.get(200,300));}895{BitSet t = makeSet(2,5); check(t.get(2,6).equals(makeSet(0,3)),"");}896}897898private static void testToString() {899check(new BitSet().toString().equals("{}"));900check(makeSet(2,3,42,43,234).toString().equals("{2, 3, 42, 43, 234}"));901902final long MB = 1024*1024;903if (Runtime.getRuntime().maxMemory() >= 512*MB) {904// only run it if we have enough memory905try {906check(makeSet(Integer.MAX_VALUE-1).toString().equals(907"{" + (Integer.MAX_VALUE-1) + "}"));908check(makeSet(Integer.MAX_VALUE).toString().equals(909"{" + Integer.MAX_VALUE + "}"));910check(makeSet(0, 1, Integer.MAX_VALUE-1, Integer.MAX_VALUE).toString().equals(911"{0, 1, " + (Integer.MAX_VALUE-1) + ", " + Integer.MAX_VALUE + "}"));912} catch (IndexOutOfBoundsException exc) {913fail("toString() with indices near MAX_VALUE");914}915}916}917918private static void testLogicalIdentities() {919int failCount = 0;920921// Verify that (!b1)|(!b2) == !(b1&b2)922for (int i=0; i<50; i++) {923// Construct two fairly random bitsets924BitSet b1 = new BitSet();925BitSet b2 = new BitSet();926927int numberOfSetBits = generator.nextInt(100) + 1;928int highestPossibleSetBit = generator.nextInt(1000) + 1;929930for (int x=0; x<numberOfSetBits; x++) {931b1.set(generator.nextInt(highestPossibleSetBit));932b2.set(generator.nextInt(highestPossibleSetBit));933}934935BitSet b3 = (BitSet) b1.clone();936BitSet b4 = (BitSet) b2.clone();937938for (int x=0; x<highestPossibleSetBit; x++) {939b1.flip(x);940b2.flip(x);941}942b1.or(b2);943b3.and(b4);944for (int x=0; x<highestPossibleSetBit; x++)945b3.flip(x);946if (!b1.equals(b3))947failCount++;948checkSanity(b1, b2, b3, b4);949}950951// Verify that (b1&(!b2)|(b2&(!b1) == b1^b2952for (int i=0; i<50; i++) {953// Construct two fairly random bitsets954BitSet b1 = new BitSet();955BitSet b2 = new BitSet();956957int numberOfSetBits = generator.nextInt(100) + 1;958int highestPossibleSetBit = generator.nextInt(1000) + 1;959960for (int x=0; x<numberOfSetBits; x++) {961b1.set(generator.nextInt(highestPossibleSetBit));962b2.set(generator.nextInt(highestPossibleSetBit));963}964965BitSet b3 = (BitSet) b1.clone();966BitSet b4 = (BitSet) b2.clone();967BitSet b5 = (BitSet) b1.clone();968BitSet b6 = (BitSet) b2.clone();969970for (int x=0; x<highestPossibleSetBit; x++)971b2.flip(x);972b1.and(b2);973for (int x=0; x<highestPossibleSetBit; x++)974b3.flip(x);975b3.and(b4);976b1.or(b3);977b5.xor(b6);978if (!b1.equals(b5))979failCount++;980checkSanity(b1, b2, b3, b4, b5, b6);981}982report("Logical Identities ", failCount);983}984985}986987988