Permalink
Browse files

fix the NotDocIdSet bug where it will loop forever if inner docidset …

…is exhausted and added unit tests
  • Loading branch information...
1 parent 9fe1627 commit baedcf00d2f583adafdcbc2523fe4171d9980cb2 @hyan hyan committed Feb 5, 2011
@@ -16,6 +16,7 @@
private int max = -1;
public NotDocIdSet(DocIdSet docSet, int maxVal) {
+ System.out.println("yanhao: hahaha in notdocidset");
innerSet = docSet;
max = maxVal;
}
@@ -26,6 +27,7 @@ public NotDocIdSet(DocIdSet docSet, int maxVal) {
private int innerDocid = -1;
NotDocIdSetIterator() throws IOException{
+ System.out.println("yanhao: hahaha in notdocidsetiterator");
initialize();
}
@@ -58,6 +60,10 @@ public int advance(int target) throws IOException {
if (target <= lastReturn) target = lastReturn + 1;
+ if (target >= max) {
+ return (lastReturn = DocIdSetIterator.NO_MORE_DOCS);
+ }
+
if (it1 != null && innerDocid < target) {
if ((innerDocid = it1.advance(target)) == DocIdSetIterator.NO_MORE_DOCS) {
it1 = null;
@@ -1,5 +1,8 @@
package com.kamikaze.test;
+import static org.junit.Assert.assertFalse;
+
+import java.util.ArrayList;
import java.util.Arrays;
import junit.framework.TestCase;
@@ -10,54 +13,84 @@
import com.kamikaze.docidset.impl.AndDocIdSet;
import com.kamikaze.docidset.impl.IntArrayDocIdSet;
+import com.kamikaze.docidset.impl.NotDocIdSet;
import com.kamikaze.docidset.impl.OrDocIdSet;
public class TestBooleanDocIdSet extends TestCase {
@Test
- public void testOrDocIdSet() throws Exception
+ public void _testOrDocIdSet() throws Exception
{
System.out.println("Running testOrDocIdSet() Test case...");
DocIdSet[] DocList;
DocList = new DocIdSet[5];
- int maxdoc = 100000;
+ int maxdoc = 2;
+ ArrayList<Integer> intSet = new ArrayList<Integer>();
+
for (int i=0;i<DocList.length;++i)
{
IntArrayDocIdSet docset = new IntArrayDocIdSet(maxdoc);
- for (int k=0;k<maxdoc;k++)
- {
- docset.addDoc(k);
- }
+ docset.addDoc((i+1)*10);
+ intSet.add((i+1)*10);
+ docset.addDoc((i+1)*100);
+ intSet.add((i+1)*100);
DocList[i]=docset;
}
OrDocIdSet orset = new OrDocIdSet(Arrays.asList(DocList));
DocIdSetIterator iter = orset.iterator();
int doc;
while((doc=iter.nextDoc())!=DocIdSetIterator.NO_MORE_DOCS)
{
+ assertTrue("ERROR: testOrDocIdSet ", intSet.contains(doc));
+ }
+ System.out.println("--------completed-----------");
+ }
+
+ @Test
+ public void testNotDocIdSet() throws Exception
+ {
+ System.out.println("Running testNotDocIdSet() Test case...");
+ int maxdoc = 5;
+ IntArrayDocIdSet docset = new IntArrayDocIdSet(maxdoc);
+ ArrayList<Integer> intSet = new ArrayList<Integer>();
+ docset.addDoc(1);
+ intSet.add(1);
+ docset.addDoc(3);
+ intSet.add(3);
+
+ NotDocIdSet notset = new NotDocIdSet(docset, 5);
+ DocIdSetIterator iter = notset.iterator();
+ int doc;
+ while((doc=iter.nextDoc())!=DocIdSetIterator.NO_MORE_DOCS)
+ {
+ assertFalse("ERROR: testOrDocIdSet ", intSet.contains(doc));
}
System.out.println("--------completed-----------");
}
+
@Test
- public void testAndDocIdSet() throws Exception
+ public void _testAndDocIdSet() throws Exception
{
- System.out.println("Running testAndDocIdSet() Test case...");
+ System.out.println("Running testOrDocIdSet() Test case...");
DocIdSet[] DocList;
DocList = new DocIdSet[5];
- int maxdoc = 100000;
+ int maxdoc = 2;
+ ArrayList<Integer> intSet = new ArrayList<Integer>();
+
for (int i=0;i<DocList.length;++i)
{
IntArrayDocIdSet docset = new IntArrayDocIdSet(maxdoc);
- for (int k=0;k<maxdoc;k++)
- {
- docset.addDoc(k);
- }
+ docset.addDoc(5);
+ intSet.add(5);
+ docset.addDoc((i+1)*100);
+ intSet.add((i+1)*100);
DocList[i]=docset;
}
- AndDocIdSet orset = new AndDocIdSet(Arrays.asList(DocList));
- DocIdSetIterator iter = orset.iterator();
+ AndDocIdSet andset = new AndDocIdSet(Arrays.asList(DocList));
+ DocIdSetIterator iter = andset.iterator();
int doc;
while((doc=iter.nextDoc())!=DocIdSetIterator.NO_MORE_DOCS)
{
+ assertTrue("ERROR: testAndDocIdSet ", intSet.contains(doc));
}
System.out.println("--------completed-----------");
}
Binary file not shown.

0 comments on commit baedcf0

Please sign in to comment.