KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > util > TestBitVector


1 package org.apache.lucene.util;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import junit.framework.TestCase;
20 import org.apache.lucene.store.Directory;
21 import org.apache.lucene.store.RAMDirectory;
22
23 /**
24  * <code>TestBitVector</code> tests the <code>BitVector</code>, obviously.
25  *
26  * @author "Peter Mularien" <pmularien@deploy.com>
27  * @version $Id: TestBitVector.java,v 1.3 2004/03/29 22:48:07 cutting Exp $
28  */

29 public class TestBitVector extends TestCase
30 {
31     public TestBitVector(String JavaDoc s) {
32         super(s);
33     }
34
35     /**
36      * Test the default constructor on BitVectors of various sizes.
37      * @throws Exception
38      */

39     public void testConstructSize() throws Exception JavaDoc {
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     /**
52      * Test the get() and set() methods on BitVectors of various sizes.
53      * @throws Exception
54      */

55     public void testGetSet() throws Exception JavaDoc {
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             // ensure a set bit can be git'
66
assertFalse(bv.get(i));
67             bv.set(i);
68             assertTrue(bv.get(i));
69         }
70     }
71
72     /**
73      * Test the clear() method on BitVectors of various sizes.
74      * @throws Exception
75      */

76     public void testClear() throws Exception JavaDoc {
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             // ensure a set bit is cleared
87
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     /**
96      * Test the count() method on BitVectors of various sizes.
97      * @throws Exception
98      */

99     public void testCount() throws Exception JavaDoc {
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         // test count when incrementally setting bits
109
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         // test count when setting then clearing bits
119
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     /**
132      * Test writing and construction to/from Directory.
133      * @throws Exception
134      */

135     public void testWriteRead() throws Exception JavaDoc {
136         doTestWriteRead(8);
137         doTestWriteRead(20);
138         doTestWriteRead(100);
139         doTestWriteRead(1000);
140     }
141
142     private void doTestWriteRead(int n) throws Exception JavaDoc {
143         Directory d = new RAMDirectory();
144
145         BitVector bv = new BitVector(n);
146         // test count when incrementally setting bits
147
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             // compare bit vectors with bits set incrementally
156
assertTrue(doCompare(bv,compare));
157         }
158     }
159
160     /**
161      * Compare two BitVectors.
162      * This should really be an equals method on the BitVector itself.
163      * @param bv One bit vector
164      * @param compare The second to compare
165      */

166     private boolean doCompare(BitVector bv, BitVector compare) {
167         boolean equal = true;
168         for(int i=0;i<bv.size();i++) {
169             // bits must be equal
170
if(bv.get(i)!=compare.get(i)) {
171                 equal = false;
172                 break;
173             }
174         }
175         return equal;
176     }
177 }
178
Popular Tags