pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / backwards / src / test / org / apache / lucene / util / TestBitVector.java
1 package org.apache.lucene.util;
2
3 /**
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 import java.io.IOException;
21
22 import org.apache.lucene.store.MockDirectoryWrapper;
23 import org.apache.lucene.store.RAMDirectory;
24
25 /**
26  * <code>TestBitVector</code> tests the <code>BitVector</code>, obviously.
27  */
28 public class TestBitVector extends LuceneTestCase
29 {
30
31     /**
32      * Test the default constructor on BitVectors of various sizes.
33      * @throws Exception
34      */
35     public void testConstructSize() throws Exception {
36         doTestConstructOfSize(8);
37         doTestConstructOfSize(20);
38         doTestConstructOfSize(100);
39         doTestConstructOfSize(1000);
40     }
41
42     private void doTestConstructOfSize(int n) {
43         BitVector bv = new BitVector(n);
44         assertEquals(n,bv.size());
45     }
46
47     /**
48      * Test the get() and set() methods on BitVectors of various sizes.
49      * @throws Exception
50      */
51     public void testGetSet() throws Exception {
52         doTestGetSetVectorOfSize(8);
53         doTestGetSetVectorOfSize(20);
54         doTestGetSetVectorOfSize(100);
55         doTestGetSetVectorOfSize(1000);
56     }
57
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));
63             bv.set(i);
64             assertTrue(bv.get(i));
65         }
66     }
67
68     /**
69      * Test the clear() method on BitVectors of various sizes.
70      * @throws Exception
71      */
72     public void testClear() throws Exception {
73         doTestClearVectorOfSize(8);
74         doTestClearVectorOfSize(20);
75         doTestClearVectorOfSize(100);
76         doTestClearVectorOfSize(1000);
77     }
78
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));
84             bv.set(i);
85             assertTrue(bv.get(i));
86             bv.clear(i);
87             assertFalse(bv.get(i));
88         }
89     }
90
91     /**
92      * Test the count() method on BitVectors of various sizes.
93      * @throws Exception
94      */
95     public void testCount() throws Exception {
96         doTestCountVectorOfSize(8);
97         doTestCountVectorOfSize(20);
98         doTestCountVectorOfSize(100);
99         doTestCountVectorOfSize(1000);
100     }
101
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());
108             bv.set(i);
109             assertTrue(bv.get(i));
110             assertEquals(i+1,bv.count());
111         }
112
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());
118             bv.set(i);
119             assertTrue(bv.get(i));
120             assertEquals(1,bv.count());
121             bv.clear(i);
122             assertFalse(bv.get(i));
123             assertEquals(0,bv.count());
124         }
125     }
126
127     /**
128      * Test writing and construction to/from Directory.
129      * @throws Exception
130      */
131     public void testWriteRead() throws Exception {
132         doTestWriteRead(8);
133         doTestWriteRead(20);
134         doTestWriteRead(100);
135         doTestWriteRead(1000);
136     }
137
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());
146             bv.set(i);
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));
153         }
154     }
155
156     /**
157      * Test r/w when size/count cause switching between bit-set and d-gaps file formats.  
158      */
159     public void testDgaps() throws IOException {
160       doTestDgaps(1,0,1);
161       doTestDgaps(10,0,1);
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);
167     }
168     
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++) {
174         bv.set(i);
175         assertEquals(i+1,bv.count());
176       }
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));
182         bv = bv2;
183         bv.set(i);
184         assertEquals(i+1,bv.count());
185         bv.write(d, "TESTBV");
186       }
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));
191         bv = bv2;
192         bv.clear(i);
193         assertEquals(i,bv.count());
194         bv.write(d, "TESTBV");
195       }
196     }
197     /**
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
202      */
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)) {
208                 equal = false;
209                 break;
210             }
211         }
212         return equal;
213     }
214 }