1 package org.apache.lucene.util;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import java.io.IOException;
22 import org.apache.lucene.store.MockDirectoryWrapper;
23 import org.apache.lucene.store.RAMDirectory;
26 * <code>TestBitVector</code> tests the <code>BitVector</code>, obviously.
28 public class TestBitVector extends LuceneTestCase
32 * Test the default constructor on BitVectors of various sizes.
35 public void testConstructSize() throws Exception {
36 doTestConstructOfSize(8);
37 doTestConstructOfSize(20);
38 doTestConstructOfSize(100);
39 doTestConstructOfSize(1000);
42 private void doTestConstructOfSize(int n) {
43 BitVector bv = new BitVector(n);
44 assertEquals(n,bv.size());
48 * Test the get() and set() methods on BitVectors of various sizes.
51 public void testGetSet() throws Exception {
52 doTestGetSetVectorOfSize(8);
53 doTestGetSetVectorOfSize(20);
54 doTestGetSetVectorOfSize(100);
55 doTestGetSetVectorOfSize(1000);
58 private void doTestGetSetVectorOfSize(int n) {
59 BitVector bv = new BitVector(n);
60 for(int i=0;i<bv.size();i++) {
61 // ensure a set bit can be git'
62 assertFalse(bv.get(i));
64 assertTrue(bv.get(i));
69 * Test the clear() method on BitVectors of various sizes.
72 public void testClear() throws Exception {
73 doTestClearVectorOfSize(8);
74 doTestClearVectorOfSize(20);
75 doTestClearVectorOfSize(100);
76 doTestClearVectorOfSize(1000);
79 private void doTestClearVectorOfSize(int n) {
80 BitVector bv = new BitVector(n);
81 for(int i=0;i<bv.size();i++) {
82 // ensure a set bit is cleared
83 assertFalse(bv.get(i));
85 assertTrue(bv.get(i));
87 assertFalse(bv.get(i));
92 * Test the count() method on BitVectors of various sizes.
95 public void testCount() throws Exception {
96 doTestCountVectorOfSize(8);
97 doTestCountVectorOfSize(20);
98 doTestCountVectorOfSize(100);
99 doTestCountVectorOfSize(1000);
102 private void doTestCountVectorOfSize(int n) {
103 BitVector bv = new BitVector(n);
104 // test count when incrementally setting bits
105 for(int i=0;i<bv.size();i++) {
106 assertFalse(bv.get(i));
107 assertEquals(i,bv.count());
109 assertTrue(bv.get(i));
110 assertEquals(i+1,bv.count());
113 bv = new BitVector(n);
114 // test count when setting then clearing bits
115 for(int i=0;i<bv.size();i++) {
116 assertFalse(bv.get(i));
117 assertEquals(0,bv.count());
119 assertTrue(bv.get(i));
120 assertEquals(1,bv.count());
122 assertFalse(bv.get(i));
123 assertEquals(0,bv.count());
128 * Test writing and construction to/from Directory.
131 public void testWriteRead() throws Exception {
134 doTestWriteRead(100);
135 doTestWriteRead(1000);
138 private void doTestWriteRead(int n) throws Exception {
139 MockDirectoryWrapper d = new MockDirectoryWrapper(random, new RAMDirectory());
140 d.setPreventDoubleWrite(false);
141 BitVector bv = new BitVector(n);
142 // test count when incrementally setting bits
143 for(int i=0;i<bv.size();i++) {
144 assertFalse(bv.get(i));
145 assertEquals(i,bv.count());
147 assertTrue(bv.get(i));
148 assertEquals(i+1,bv.count());
149 bv.write(d, "TESTBV");
150 BitVector compare = new BitVector(d, "TESTBV");
151 // compare bit vectors with bits set incrementally
152 assertTrue(doCompare(bv,compare));
157 * Test r/w when size/count cause switching between bit-set and d-gaps file formats.
159 public void testDgaps() throws IOException {
162 doTestDgaps(100,0,1);
163 doTestDgaps(1000,4,7);
164 doTestDgaps(10000,40,43);
165 doTestDgaps(100000,415,418);
166 doTestDgaps(1000000,3123,3126);
169 private void doTestDgaps(int size, int count1, int count2) throws IOException {
170 MockDirectoryWrapper d = new MockDirectoryWrapper(random, new RAMDirectory());
171 d.setPreventDoubleWrite(false);
172 BitVector bv = new BitVector(size);
173 for (int i=0; i<count1; i++) {
175 assertEquals(i+1,bv.count());
177 bv.write(d, "TESTBV");
178 // gradually increase number of set bits
179 for (int i=count1; i<count2; i++) {
180 BitVector bv2 = new BitVector(d, "TESTBV");
181 assertTrue(doCompare(bv,bv2));
184 assertEquals(i+1,bv.count());
185 bv.write(d, "TESTBV");
187 // now start decreasing number of set bits
188 for (int i=count2-1; i>=count1; i--) {
189 BitVector bv2 = new BitVector(d, "TESTBV");
190 assertTrue(doCompare(bv,bv2));
193 assertEquals(i,bv.count());
194 bv.write(d, "TESTBV");
198 * Compare two BitVectors.
199 * This should really be an equals method on the BitVector itself.
200 * @param bv One bit vector
201 * @param compare The second to compare
203 private boolean doCompare(BitVector bv, BitVector compare) {
204 boolean equal = true;
205 for(int i=0;i<bv.size();i++) {
206 // bits must be equal
207 if(bv.get(i)!=compare.get(i)) {