X-Git-Url: https://git.mdrn.pl/pylucene.git/blobdiff_plain/a2e61f0c04805cfcb8706176758d1283c7e3a55c..aaeed5504b982cf3545252ab528713250aa33eed:/lucene-java-3.5.0/lucene/backwards/src/test/org/apache/lucene/util/TestFixedBitSet.java diff --git a/lucene-java-3.5.0/lucene/backwards/src/test/org/apache/lucene/util/TestFixedBitSet.java b/lucene-java-3.5.0/lucene/backwards/src/test/org/apache/lucene/util/TestFixedBitSet.java new file mode 100644 index 0000000..b4e575e --- /dev/null +++ b/lucene-java-3.5.0/lucene/backwards/src/test/org/apache/lucene/util/TestFixedBitSet.java @@ -0,0 +1,289 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.lucene.util; + +import java.io.IOException; +import java.util.BitSet; + +import org.apache.lucene.search.DocIdSetIterator; + +public class TestFixedBitSet extends LuceneTestCase { + + void doGet(BitSet a, FixedBitSet b) { + int max = b.length(); + for (int i=0; i=0); + } + + void doPrevSetBit(BitSet a, FixedBitSet b) { + int aa = a.size() + random.nextInt(100); + int bb = aa; + do { + // aa = a.prevSetBit(aa-1); + aa--; + while ((aa >= 0) && (! a.get(aa))) { + aa--; + } + if (b.length() == 0) { + bb = -1; + } else if (bb > b.length()-1) { + bb = b.prevSetBit(b.length()-1); + } else if (bb < 1) { + bb = -1; + } else { + bb = bb >= 1 ? b.prevSetBit(bb-1) : -1; + } + assertEquals(aa,bb); + } while (aa>=0); + } + + // test interleaving different FixedBitSetIterator.next()/skipTo() + void doIterate(BitSet a, FixedBitSet b, int mode) throws IOException { + if (mode==1) doIterate1(a, b); + if (mode==2) doIterate2(a, b); + } + + void doIterate1(BitSet a, FixedBitSet b) throws IOException { + int aa=-1,bb=-1; + DocIdSetIterator iterator = b.iterator(); + do { + aa = a.nextSetBit(aa+1); + bb = (bb < b.length() && random.nextBoolean()) ? iterator.nextDoc() : iterator.advance(bb + 1); + assertEquals(aa == -1 ? DocIdSetIterator.NO_MORE_DOCS : aa, bb); + } while (aa>=0); + } + + void doIterate2(BitSet a, FixedBitSet b) throws IOException { + int aa=-1,bb=-1; + DocIdSetIterator iterator = b.iterator(); + do { + aa = a.nextSetBit(aa+1); + bb = random.nextBoolean() ? iterator.nextDoc() : iterator.advance(bb + 1); + assertEquals(aa == -1 ? DocIdSetIterator.NO_MORE_DOCS : aa, bb); + } while (aa>=0); + } + + void doRandomSets(int maxSize, int iter, int mode) throws IOException { + BitSet a0=null; + FixedBitSet b0=null; + + for (int i=0; i0) { + int nOper = random.nextInt(sz); + for (int j=0; j 0) { + b1.set(0, numBits); + assertEquals(numBits, b1.cardinality()); + b1.flip(0, numBits); + assertEquals(0, b1.cardinality()); + } + } + } + + private FixedBitSet makeFixedBitSet(int[] a, int numBits) { + FixedBitSet bs = new FixedBitSet(numBits); + for (int e: a) { + bs.set(e); + } + return bs; + } + + private BitSet makeBitSet(int[] a) { + BitSet bs = new BitSet(); + for (int e: a) { + bs.set(e); + } + return bs; + } + + private void checkPrevSetBitArray(int [] a, int numBits) { + FixedBitSet obs = makeFixedBitSet(a, numBits); + BitSet bs = makeBitSet(a); + doPrevSetBit(bs, obs); + } + + public void testPrevSetBit() { + checkPrevSetBitArray(new int[] {}, 0); + checkPrevSetBitArray(new int[] {0}, 1); + checkPrevSetBitArray(new int[] {0,2}, 3); + } +} + + +