tests: expand TestSort to cover more cases

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@996340 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yonik Seeley 2010-09-12 17:04:01 +00:00
parent 0ed3b6be9a
commit 070c79cc4c
1 changed files with 52 additions and 31 deletions

View File

@ -24,6 +24,7 @@ import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.search.*; import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory; import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.OpenBitSet; import org.apache.lucene.util.OpenBitSet;
import org.apache.solr.util.AbstractSolrTestCase; import org.apache.solr.util.AbstractSolrTestCase;
@ -37,7 +38,7 @@ public class TestSort extends AbstractSolrTestCase {
Random r = new Random(); Random r = new Random();
int ndocs = 77; int ndocs = 77;
int iter = 100; int iter = 50;
int qiter = 1000; int qiter = 1000;
int commitCount = ndocs/5 + 1; int commitCount = ndocs/5 + 1;
int maxval = ndocs*2; int maxval = ndocs*2;
@ -45,37 +46,45 @@ public class TestSort extends AbstractSolrTestCase {
static class MyDoc { static class MyDoc {
int doc; int doc;
String val; String val;
String val2;
public String toString() {
return "{id=" +doc + " val1="+val + " val2="+val2 + "}";
}
} }
public void testSort() throws Exception { public void testSort() throws Exception {
Directory dir = newDirectory(); Directory dir = new RAMDirectory();
Document smallDoc = new Document();
// Field id = new Field("id","0", Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS);
Field f = new Field("f","0", Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS); Field f = new Field("f","0", Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS);
smallDoc.add(f); Field f2 = new Field("f2","0", Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS);
Document emptyDoc = new Document();
for (int iterCnt = 0; iterCnt<iter; iterCnt++) { for (int iterCnt = 0; iterCnt<iter; iterCnt++) {
IndexWriter iw = new IndexWriter(dir, new SimpleAnalyzer(), true, IndexWriter.MaxFieldLength.UNLIMITED); IndexWriter iw = new IndexWriter(dir, new SimpleAnalyzer(), true, IndexWriter.MaxFieldLength.UNLIMITED);
final MyDoc[] mydocs = new MyDoc[ndocs]; final MyDoc[] mydocs = new MyDoc[ndocs];
int v1EmptyPercent = 50;
int v2EmptyPercent = 50;
int commitCountdown = commitCount; int commitCountdown = commitCount;
for (int i=0; i< ndocs; i++) { for (int i=0; i< ndocs; i++) {
Document doc;
MyDoc mydoc = new MyDoc(); MyDoc mydoc = new MyDoc();
mydoc.doc = i; mydoc.doc = i;
mydocs[i] = mydoc; mydocs[i] = mydoc;
if (r.nextInt(3)==0) { Document document = new Document();
doc = emptyDoc; if (r.nextInt(100) < v1EmptyPercent) {
mydoc.val = null;
} else {
mydoc.val = Integer.toString(r.nextInt(maxval)); mydoc.val = Integer.toString(r.nextInt(maxval));
f.setValue(mydoc.val); f.setValue(mydoc.val);
doc = smallDoc; document.add(f);
} }
iw.addDocument(doc); if (r.nextInt(100) < v2EmptyPercent) {
mydoc.val2 = Integer.toString(r.nextInt(maxval));
f2.setValue(mydoc.val2);
document.add(f2);
}
iw.addDocument(document);
if (--commitCountdown <= 0) { if (--commitCountdown <= 0) {
commitCountdown = commitCount; commitCountdown = commitCount;
iw.commit(); iw.commit();
@ -83,17 +92,6 @@ public class TestSort extends AbstractSolrTestCase {
} }
iw.close(); iw.close();
/***
Arrays.sort(mydocs, new Comparator<MyDoc>() {
public int compare(MyDoc o1, MyDoc o2) {
String v1 = o1.val==null ? "zzz" : o1.val;
String v2 = o2.val==null ? "zzz" : o2.val;
int cmp = v1.compareTo(v2);
cmp = cmp==0 ? o1.doc-o2.doc : cmp;
return cmp;
}
});
***/
IndexSearcher searcher = new IndexSearcher(dir, true); IndexSearcher searcher = new IndexSearcher(dir, true);
// System.out.println("segments="+searcher.getIndexReader().getSequentialSubReaders().length); // System.out.println("segments="+searcher.getIndexReader().getSequentialSubReaders().length);
@ -108,19 +106,30 @@ public class TestSort extends AbstractSolrTestCase {
}; };
int top = r.nextInt((ndocs>>3)+1)+1; int top = r.nextInt((ndocs>>3)+1)+1;
final boolean sortMissingLast = r.nextBoolean(); final boolean luceneSort = r.nextBoolean();
final boolean reverse = !sortMissingLast; final boolean sortMissingLast = !luceneSort && r.nextBoolean();
final boolean sortMissingFirst = !luceneSort && !sortMissingLast;
final boolean reverse = r.nextBoolean();
List<SortField> sfields = new ArrayList<SortField>(); List<SortField> sfields = new ArrayList<SortField>();
final boolean secondary = r.nextBoolean();
final boolean luceneSort2 = r.nextBoolean();
final boolean sortMissingLast2 = !luceneSort2 && r.nextBoolean();
final boolean sortMissingFirst2 = !luceneSort2 && !sortMissingLast2;
final boolean reverse2 = r.nextBoolean();
if (r.nextBoolean()) sfields.add( new SortField(null, SortField.SCORE)); if (r.nextBoolean()) sfields.add( new SortField(null, SortField.SCORE));
// hit both use-cases of sort-missing-last // hit both use-cases of sort-missing-last
sfields.add( Sorting.getStringSortField("f", reverse, sortMissingLast, !sortMissingLast) ); sfields.add( Sorting.getStringSortField("f", reverse, sortMissingLast, sortMissingFirst) );
if (secondary) {
sfields.add( Sorting.getStringSortField("f2", reverse2, sortMissingLast2, sortMissingFirst2) );
}
if (r.nextBoolean()) sfields.add( new SortField(null, SortField.SCORE)); if (r.nextBoolean()) sfields.add( new SortField(null, SortField.SCORE));
Sort sort = new Sort(sfields.toArray(new SortField[sfields.size()])); Sort sort = new Sort(sfields.toArray(new SortField[sfields.size()]));
// final String nullRep = sortMissingLast ? "zzz" : ""; final String nullRep = luceneSort || sortMissingFirst && !reverse || sortMissingLast && reverse ? "" : "zzz";
final String nullRep = "zzz"; final String nullRep2 = luceneSort2 || sortMissingFirst2 && !reverse2 || sortMissingLast2 && reverse2 ? "" : "zzz";
boolean trackScores = r.nextBoolean(); boolean trackScores = r.nextBoolean();
boolean trackMaxScores = r.nextBoolean(); boolean trackMaxScores = r.nextBoolean();
@ -163,6 +172,15 @@ public class TestSort extends AbstractSolrTestCase {
String v2 = o2.val==null ? nullRep : o2.val; String v2 = o2.val==null ? nullRep : o2.val;
int cmp = v1.compareTo(v2); int cmp = v1.compareTo(v2);
if (reverse) cmp = -cmp; if (reverse) cmp = -cmp;
if (cmp != 0) return cmp;
if (secondary) {
v1 = o1.val2==null ? nullRep2 : o1.val2;
v2 = o2.val2==null ? nullRep2 : o2.val2;
cmp = v1.compareTo(v2);
if (reverse2) cmp = -cmp;
}
cmp = cmp==0 ? o1.doc-o2.doc : cmp; cmp = cmp==0 ? o1.doc-o2.doc : cmp;
return cmp; return cmp;
} }
@ -174,7 +192,10 @@ public class TestSort extends AbstractSolrTestCase {
for (int j=0; j<sdocs.length; j++) { for (int j=0; j<sdocs.length; j++) {
int id = sdocs[j].doc; int id = sdocs[j].doc;
if (id != collectedDocs.get(j).doc) { if (id != collectedDocs.get(j).doc) {
log.error("Error at pos " + j); log.error("Error at pos " + j
+ "\n\tsortMissingFirst=" + sortMissingFirst + " sortMissingLast=" + sortMissingLast + " reverse=" + reverse
+ "\n\tEXPECTED=" + collectedDocs
);
} }
assertEquals(id, collectedDocs.get(j).doc); assertEquals(id, collectedDocs.get(j).doc);
} }