Paul Elschots refactorings of span tests

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@150358 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Erik Hatcher 2004-05-25 17:34:58 +00:00
parent 9c2a2fee32
commit 5884c9c8a8
3 changed files with 99 additions and 66 deletions

View File

@ -0,0 +1,65 @@
package org.apache.lucene.search;
/**
* Copyright 2004 The Apache Software Foundation
*
* Licensed 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.
*/
/* 20 May 2004: Factored out of spans tests. Please leave this comment
until this class is evt. also used by tests in search package.
*/
import org.apache.lucene.search.Searcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Hits;
import junit.framework.TestCase;
import java.io.IOException;
import java.util.Set;
import java.util.TreeSet;
public class CheckHits {
public static void checkHits(
Query query,
String defaultFieldName,
Searcher searcher,
int[] results,
TestCase testCase)
throws IOException {
Hits hits = searcher.search(query);
Set correct = new TreeSet();
for (int i = 0; i < results.length; i++) {
correct.add(new Integer(results[i]));
}
Set actual = new TreeSet();
for (int i = 0; i < hits.length(); i++) {
actual.add(new Integer(hits.id(i)));
}
testCase.assertEquals(query.toString(defaultFieldName), correct, actual);
}
public static void printDocNrs(Hits hits) throws IOException {
System.out.print("new int[] {");
for (int i = 0; i < hits.length(); i++) {
System.out.print(hits.id(i));
if (i != hits.length()-1)
System.out.print(", ");
}
System.out.println("}");
}
}

View File

@ -1,4 +1,4 @@
package org.apache.lucene.search;
package org.apache.lucene.search.spans;
/**
* Copyright 2004 The Apache Software Foundation
@ -20,9 +20,6 @@ import junit.framework.TestCase;
import java.io.IOException;
import java.util.Set;
import java.util.TreeSet;
import org.apache.lucene.util.English;
import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.document.Document;
@ -31,7 +28,7 @@ import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.search.spans.*;
import org.apache.lucene.search.*;
/**
* Tests basic search capabilities.
@ -54,7 +51,6 @@ public class TestBasics extends TestCase {
IndexWriter writer
= new IndexWriter(directory, new SimpleAnalyzer(), true);
//writer.infoStream = System.out;
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < 1000; i++) {
Document doc = new Document();
doc.add(Field.Text("field", English.intToEnglish(i)));
@ -267,30 +263,6 @@ public class TestBasics extends TestCase {
private void checkHits(Query query, int[] results) throws IOException {
Hits hits = searcher.search(query);
Set correct = new TreeSet();
for (int i = 0; i < results.length; i++) {
correct.add(new Integer(results[i]));
CheckHits.checkHits(query, "field", searcher, results, this);
}
Set actual = new TreeSet();
for (int i = 0; i < hits.length(); i++) {
actual.add(new Integer(hits.id(i)));
}
assertEquals(query.toString("field"), correct, actual);
}
private void printHits(Query query) throws IOException {
Hits hits = searcher.search(query);
System.out.print("new int[] {");
for (int i = 0; i < hits.length(); i++) {
System.out.print(hits.id(i));
if (i != hits.length()-1)
System.out.print(", ");
}
System.out.println("}");
}
}

View File

@ -19,6 +19,7 @@ package org.apache.lucene.search.spans;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.CheckHits;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
@ -34,11 +35,11 @@ import java.util.TreeSet;
public class TestSpans extends TestCase {
private IndexSearcher searcher;
public final String field = "field";
public void setUp() throws Exception {
RAMDirectory directory = new RAMDirectory();
IndexWriter writer
= new IndexWriter(directory, new WhitespaceAnalyzer(), true);
//writer.infoStream = System.out;
IndexWriter writer= new IndexWriter(directory, new WhitespaceAnalyzer(), true);
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < docFields.length; i++) {
Document doc = new Document();
@ -52,50 +53,45 @@ public class TestSpans extends TestCase {
private String[] docFields = {
"w1 w2 w3 w4 w5",
"w1 w3 w2 w3",
"w1 xx w2 yy w3",
"w1 w3 xx w2 yy w3",
""
};
public final String field = "field";
public Term makeTerm(String text) {return new Term(field, text);}
public SpanTermQuery makeSpanTermQuery(String text) {
return new SpanTermQuery(makeTerm(text));
}
public void testSpanNearOrdered02() throws Exception {
SpanTermQuery w1 = makeSpanTermQuery("w1");
SpanTermQuery w2 = makeSpanTermQuery("w2");
SpanTermQuery w3 = makeSpanTermQuery("w3");
int slop = 0;
boolean ordered = true;
SpanNearQuery snq = new SpanNearQuery( new SpanQuery[]{w1,w2,w3}, slop, ordered);
checkHits(snq, new int[] {0});
}
public void testSpanNearOrdered03() throws Exception {
SpanTermQuery w1 = makeSpanTermQuery("w1");
SpanTermQuery w2 = makeSpanTermQuery("w2");
SpanTermQuery w3 = makeSpanTermQuery("w3");
int slop = 1;
boolean ordered = true;
SpanNearQuery snq = new SpanNearQuery( new SpanQuery[]{w1,w2,w3}, slop, ordered);
checkHits(snq, new int[] {0,1});
return new SpanTermQuery(new Term(field, text));
}
private void checkHits(Query query, int[] results) throws IOException {
Hits hits = searcher.search(query);
Set correct = new TreeSet();
for (int i = 0; i < results.length; i++) {
correct.add(new Integer(results[i]));
CheckHits.checkHits(query, field, searcher, results, this);
}
Set actual = new TreeSet();
for (int i = 0; i < hits.length(); i++) {
actual.add(new Integer(hits.id(i)));
public void orderedSlopTest3(int slop, int[] expectedDocs) throws IOException {
SpanTermQuery w1 = makeSpanTermQuery("w1");
SpanTermQuery w2 = makeSpanTermQuery("w2");
SpanTermQuery w3 = makeSpanTermQuery("w3");
boolean ordered = true;
SpanNearQuery snq = new SpanNearQuery( new SpanQuery[]{w1,w2,w3}, slop, ordered);
checkHits(snq, expectedDocs);
}
assertEquals(query.toString(field), correct, actual);
public void testSpanNearOrdered01() throws Exception {
orderedSlopTest3(0, new int[] {0});
}
public void testSpanNearOrdered02() throws Exception {
orderedSlopTest3(1, new int[] {0,1});
}
public void testSpanNearOrdered03() throws Exception {
orderedSlopTest3(2, new int[] {0,1,2});
}
public void testSpanNearOrdered04() throws Exception {
orderedSlopTest3(3, new int[] {0,1,2,3});
}
public void testSpanNearOrdered05() throws Exception {
orderedSlopTest3(4, new int[] {0,1,2,3});
}
}