001: package org.apache.lucene.util;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import java.io.IOException;
021:
022: import org.apache.lucene.util.LuceneTestCase;
023: import org.apache.lucene.store.Directory;
024: import org.apache.lucene.store.RAMDirectory;
025:
026: /**
027: * <code>TestBitVector</code> tests the <code>BitVector</code>, obviously.
028: *
029: *
030: * @version $Id: TestBitVector.java 583534 2007-10-10 16:46:35Z mikemccand $
031: */
032: public class TestBitVector extends LuceneTestCase {
033: public TestBitVector(String s) {
034: super (s);
035: }
036:
037: /**
038: * Test the default constructor on BitVectors of various sizes.
039: * @throws Exception
040: */
041: public void testConstructSize() throws Exception {
042: doTestConstructOfSize(8);
043: doTestConstructOfSize(20);
044: doTestConstructOfSize(100);
045: doTestConstructOfSize(1000);
046: }
047:
048: private void doTestConstructOfSize(int n) {
049: BitVector bv = new BitVector(n);
050: assertEquals(n, bv.size());
051: }
052:
053: /**
054: * Test the get() and set() methods on BitVectors of various sizes.
055: * @throws Exception
056: */
057: public void testGetSet() throws Exception {
058: doTestGetSetVectorOfSize(8);
059: doTestGetSetVectorOfSize(20);
060: doTestGetSetVectorOfSize(100);
061: doTestGetSetVectorOfSize(1000);
062: }
063:
064: private void doTestGetSetVectorOfSize(int n) {
065: BitVector bv = new BitVector(n);
066: for (int i = 0; i < bv.size(); i++) {
067: // ensure a set bit can be git'
068: assertFalse(bv.get(i));
069: bv.set(i);
070: assertTrue(bv.get(i));
071: }
072: }
073:
074: /**
075: * Test the clear() method on BitVectors of various sizes.
076: * @throws Exception
077: */
078: public void testClear() throws Exception {
079: doTestClearVectorOfSize(8);
080: doTestClearVectorOfSize(20);
081: doTestClearVectorOfSize(100);
082: doTestClearVectorOfSize(1000);
083: }
084:
085: private void doTestClearVectorOfSize(int n) {
086: BitVector bv = new BitVector(n);
087: for (int i = 0; i < bv.size(); i++) {
088: // ensure a set bit is cleared
089: assertFalse(bv.get(i));
090: bv.set(i);
091: assertTrue(bv.get(i));
092: bv.clear(i);
093: assertFalse(bv.get(i));
094: }
095: }
096:
097: /**
098: * Test the count() method on BitVectors of various sizes.
099: * @throws Exception
100: */
101: public void testCount() throws Exception {
102: doTestCountVectorOfSize(8);
103: doTestCountVectorOfSize(20);
104: doTestCountVectorOfSize(100);
105: doTestCountVectorOfSize(1000);
106: }
107:
108: private void doTestCountVectorOfSize(int n) {
109: BitVector bv = new BitVector(n);
110: // test count when incrementally setting bits
111: for (int i = 0; i < bv.size(); i++) {
112: assertFalse(bv.get(i));
113: assertEquals(i, bv.count());
114: bv.set(i);
115: assertTrue(bv.get(i));
116: assertEquals(i + 1, bv.count());
117: }
118:
119: bv = new BitVector(n);
120: // test count when setting then clearing bits
121: for (int i = 0; i < bv.size(); i++) {
122: assertFalse(bv.get(i));
123: assertEquals(0, bv.count());
124: bv.set(i);
125: assertTrue(bv.get(i));
126: assertEquals(1, bv.count());
127: bv.clear(i);
128: assertFalse(bv.get(i));
129: assertEquals(0, bv.count());
130: }
131: }
132:
133: /**
134: * Test writing and construction to/from Directory.
135: * @throws Exception
136: */
137: public void testWriteRead() throws Exception {
138: doTestWriteRead(8);
139: doTestWriteRead(20);
140: doTestWriteRead(100);
141: doTestWriteRead(1000);
142: }
143:
144: private void doTestWriteRead(int n) throws Exception {
145: Directory d = new RAMDirectory();
146:
147: BitVector bv = new BitVector(n);
148: // test count when incrementally setting bits
149: for (int i = 0; i < bv.size(); i++) {
150: assertFalse(bv.get(i));
151: assertEquals(i, bv.count());
152: bv.set(i);
153: assertTrue(bv.get(i));
154: assertEquals(i + 1, bv.count());
155: bv.write(d, "TESTBV");
156: BitVector compare = new BitVector(d, "TESTBV");
157: // compare bit vectors with bits set incrementally
158: assertTrue(doCompare(bv, compare));
159: }
160: }
161:
162: /**
163: * Test r/w when size/count cause switching between bit-set and d-gaps file formats.
164: * @throws Exception
165: */
166: public void testDgaps() throws IOException {
167: doTestDgaps(1, 0, 1);
168: doTestDgaps(10, 0, 1);
169: doTestDgaps(100, 0, 1);
170: doTestDgaps(1000, 4, 7);
171: doTestDgaps(10000, 40, 43);
172: doTestDgaps(100000, 415, 418);
173: doTestDgaps(1000000, 3123, 3126);
174: }
175:
176: private void doTestDgaps(int size, int count1, int count2)
177: throws IOException {
178: Directory d = new RAMDirectory();
179: BitVector bv = new BitVector(size);
180: for (int i = 0; i < count1; i++) {
181: bv.set(i);
182: assertEquals(i + 1, bv.count());
183: }
184: bv.write(d, "TESTBV");
185: // gradually increase number of set bits
186: for (int i = count1; i < count2; i++) {
187: BitVector bv2 = new BitVector(d, "TESTBV");
188: assertTrue(doCompare(bv, bv2));
189: bv = bv2;
190: bv.set(i);
191: assertEquals(i + 1, bv.count());
192: bv.write(d, "TESTBV");
193: }
194: // now start decreasing number of set bits
195: for (int i = count2 - 1; i >= count1; i--) {
196: BitVector bv2 = new BitVector(d, "TESTBV");
197: assertTrue(doCompare(bv, bv2));
198: bv = bv2;
199: bv.clear(i);
200: assertEquals(i, bv.count());
201: bv.write(d, "TESTBV");
202: }
203: }
204:
205: /**
206: * Compare two BitVectors.
207: * This should really be an equals method on the BitVector itself.
208: * @param bv One bit vector
209: * @param compare The second to compare
210: */
211: private boolean doCompare(BitVector bv, BitVector compare) {
212: boolean equal = true;
213: for (int i = 0; i < bv.size(); i++) {
214: // bits must be equal
215: if (bv.get(i) != compare.get(i)) {
216: equal = false;
217: break;
218: }
219: }
220: return equal;
221: }
222: }
|