1 package org.apache.lucene.util; 2 3 18 19 import junit.framework.TestCase; 20 import org.apache.lucene.store.Directory; 21 import org.apache.lucene.store.RAMDirectory; 22 23 29 public class TestBitVector extends TestCase 30 { 31 public TestBitVector(String s) { 32 super(s); 33 } 34 35 39 public void testConstructSize() throws Exception { 40 doTestConstructOfSize(8); 41 doTestConstructOfSize(20); 42 doTestConstructOfSize(100); 43 doTestConstructOfSize(1000); 44 } 45 46 private void doTestConstructOfSize(int n) { 47 BitVector bv = new BitVector(n); 48 assertEquals(n,bv.size()); 49 } 50 51 55 public void testGetSet() throws Exception { 56 doTestGetSetVectorOfSize(8); 57 doTestGetSetVectorOfSize(20); 58 doTestGetSetVectorOfSize(100); 59 doTestGetSetVectorOfSize(1000); 60 } 61 62 private void doTestGetSetVectorOfSize(int n) { 63 BitVector bv = new BitVector(n); 64 for(int i=0;i<bv.size();i++) { 65 assertFalse(bv.get(i)); 67 bv.set(i); 68 assertTrue(bv.get(i)); 69 } 70 } 71 72 76 public void testClear() throws Exception { 77 doTestClearVectorOfSize(8); 78 doTestClearVectorOfSize(20); 79 doTestClearVectorOfSize(100); 80 doTestClearVectorOfSize(1000); 81 } 82 83 private void doTestClearVectorOfSize(int n) { 84 BitVector bv = new BitVector(n); 85 for(int i=0;i<bv.size();i++) { 86 assertFalse(bv.get(i)); 88 bv.set(i); 89 assertTrue(bv.get(i)); 90 bv.clear(i); 91 assertFalse(bv.get(i)); 92 } 93 } 94 95 99 public void testCount() throws Exception { 100 doTestCountVectorOfSize(8); 101 doTestCountVectorOfSize(20); 102 doTestCountVectorOfSize(100); 103 doTestCountVectorOfSize(1000); 104 } 105 106 private void doTestCountVectorOfSize(int n) { 107 BitVector bv = new BitVector(n); 108 for(int i=0;i<bv.size();i++) { 110 assertFalse(bv.get(i)); 111 assertEquals(i,bv.count()); 112 bv.set(i); 113 assertTrue(bv.get(i)); 114 assertEquals(i+1,bv.count()); 115 } 116 117 bv = new BitVector(n); 118 for(int i=0;i<bv.size();i++) { 120 assertFalse(bv.get(i)); 121 assertEquals(0,bv.count()); 122 bv.set(i); 123 assertTrue(bv.get(i)); 124 assertEquals(1,bv.count()); 125 bv.clear(i); 126 assertFalse(bv.get(i)); 127 assertEquals(0,bv.count()); 128 } 129 } 130 131 135 public void testWriteRead() throws Exception { 136 doTestWriteRead(8); 137 doTestWriteRead(20); 138 doTestWriteRead(100); 139 doTestWriteRead(1000); 140 } 141 142 private void doTestWriteRead(int n) throws Exception { 143 Directory d = new RAMDirectory(); 144 145 BitVector bv = new BitVector(n); 146 for(int i=0;i<bv.size();i++) { 148 assertFalse(bv.get(i)); 149 assertEquals(i,bv.count()); 150 bv.set(i); 151 assertTrue(bv.get(i)); 152 assertEquals(i+1,bv.count()); 153 bv.write(d, "TESTBV"); 154 BitVector compare = new BitVector(d, "TESTBV"); 155 assertTrue(doCompare(bv,compare)); 157 } 158 } 159 160 166 private boolean doCompare(BitVector bv, BitVector compare) { 167 boolean equal = true; 168 for(int i=0;i<bv.size();i++) { 169 if(bv.get(i)!=compare.get(i)) { 171 equal = false; 172 break; 173 } 174 } 175 return equal; 176 } 177 } 178 | Popular Tags |