FileDocCategorySizeDatePackage
TestBitVector.javaAPI DocApache Lucene 2.1.06830Wed Feb 14 10:46:36 GMT 2007org.apache.lucene.util

TestBitVector

public class TestBitVector extends TestCase
TestBitVector tests the BitVector, obviously.
author
"Peter Mularien"
version
$Id: TestBitVector.java 485463 2006-12-11 02:03:38Z yonik $

Fields Summary
Constructors Summary
public TestBitVector(String s)

        super(s);
    
Methods Summary
private booleandoCompare(org.apache.lucene.util.BitVector bv, org.apache.lucene.util.BitVector compare)
Compare two BitVectors. This should really be an equals method on the BitVector itself.

param
bv One bit vector
param
compare The second to compare

        boolean equal = true;
        for(int i=0;i<bv.size();i++) {
            // bits must be equal
            if(bv.get(i)!=compare.get(i)) {
                equal = false;
                break;
            }
        }
        return equal;
    
private voiddoTestClearVectorOfSize(int n)

        BitVector bv = new BitVector(n);
        for(int i=0;i<bv.size();i++) {
            // ensure a set bit is cleared
            assertFalse(bv.get(i));
            bv.set(i);
            assertTrue(bv.get(i));
            bv.clear(i);
            assertFalse(bv.get(i));
        }
    
private voiddoTestConstructOfSize(int n)

        BitVector bv = new BitVector(n);
        assertEquals(n,bv.size());
    
private voiddoTestCountVectorOfSize(int n)

        BitVector bv = new BitVector(n);
        // test count when incrementally setting bits
        for(int i=0;i<bv.size();i++) {
            assertFalse(bv.get(i));
            assertEquals(i,bv.count());
            bv.set(i);
            assertTrue(bv.get(i));
            assertEquals(i+1,bv.count());
        }

        bv = new BitVector(n);
        // test count when setting then clearing bits
        for(int i=0;i<bv.size();i++) {
            assertFalse(bv.get(i));
            assertEquals(0,bv.count());
            bv.set(i);
            assertTrue(bv.get(i));
            assertEquals(1,bv.count());
            bv.clear(i);
            assertFalse(bv.get(i));
            assertEquals(0,bv.count());
        }
    
private voiddoTestDgaps(int size, int count1, int count2)

      Directory d = new  RAMDirectory();
      BitVector bv = new BitVector(size);
      for (int i=0; i<count1; i++) {
        bv.set(i);
        assertEquals(i+1,bv.count());
      }
      bv.write(d, "TESTBV");
      // gradually increase number of set bits
      for (int i=count1; i<count2; i++) {
        BitVector bv2 = new BitVector(d, "TESTBV");
        assertTrue(doCompare(bv,bv2));
        bv = bv2;
        bv.set(i);
        assertEquals(i+1,bv.count());
        bv.write(d, "TESTBV");
      }
      // now start decreasing number of set bits
      for (int i=count2-1; i>=count1; i--) {
        BitVector bv2 = new BitVector(d, "TESTBV");
        assertTrue(doCompare(bv,bv2));
        bv = bv2;
        bv.clear(i);
        assertEquals(i,bv.count());
        bv.write(d, "TESTBV");
      }
    
private voiddoTestGetSetVectorOfSize(int n)

        BitVector bv = new BitVector(n);
        for(int i=0;i<bv.size();i++) {
            // ensure a set bit can be git'
            assertFalse(bv.get(i));
            bv.set(i);
            assertTrue(bv.get(i));
        }
    
private voiddoTestWriteRead(int n)

        Directory d = new  RAMDirectory();

        BitVector bv = new BitVector(n);
        // test count when incrementally setting bits
        for(int i=0;i<bv.size();i++) {
            assertFalse(bv.get(i));
            assertEquals(i,bv.count());
            bv.set(i);
            assertTrue(bv.get(i));
            assertEquals(i+1,bv.count());
            bv.write(d, "TESTBV");
            BitVector compare = new BitVector(d, "TESTBV");
            // compare bit vectors with bits set incrementally
            assertTrue(doCompare(bv,compare));
        }
    
public voidtestClear()
Test the clear() method on BitVectors of various sizes.

throws
Exception

        doTestClearVectorOfSize(8);
        doTestClearVectorOfSize(20);
        doTestClearVectorOfSize(100);
        doTestClearVectorOfSize(1000);
    
public voidtestConstructSize()
Test the default constructor on BitVectors of various sizes.

throws
Exception

        doTestConstructOfSize(8);
        doTestConstructOfSize(20);
        doTestConstructOfSize(100);
        doTestConstructOfSize(1000);
    
public voidtestCount()
Test the count() method on BitVectors of various sizes.

throws
Exception

        doTestCountVectorOfSize(8);
        doTestCountVectorOfSize(20);
        doTestCountVectorOfSize(100);
        doTestCountVectorOfSize(1000);
    
public voidtestDgaps()
Test r/w when size/count cause switching between bit-set and d-gaps file formats.

throws
Exception

      doTestDgaps(1,0,1);
      doTestDgaps(10,0,1);
      doTestDgaps(100,0,1);
      doTestDgaps(1000,4,7);
      doTestDgaps(10000,40,43);
      doTestDgaps(100000,415,418);
      doTestDgaps(1000000,3123,3126);
    
public voidtestGetSet()
Test the get() and set() methods on BitVectors of various sizes.

throws
Exception

        doTestGetSetVectorOfSize(8);
        doTestGetSetVectorOfSize(20);
        doTestGetSetVectorOfSize(100);
        doTestGetSetVectorOfSize(1000);
    
public voidtestWriteRead()
Test writing and construction to/from Directory.

throws
Exception

        doTestWriteRead(8);
        doTestWriteRead(20);
        doTestWriteRead(100);
        doTestWriteRead(1000);