Source Code Cross Referenced for TestBitVector.java in  » Net » lucene-connector » org » apache » lucene » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » lucene connector » org.apache.lucene.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.