Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
39   119   13   3
0   77   0.33   6.5
13     1  
2    
 
  TestSimilarity       Line # 36 32 6 100% 1.0
  TestSimilarity.SimpleSimilarity       Line # 41 7 7 100% 1.0
 
  (1)
 
1    package org.apache.lucene.search;
2   
3    /**
4    * Copyright 2004 The Apache Software Foundation
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10    * http://www.apache.org/licenses/LICENSE-2.0
11    *
12    * Unless required by applicable law or agreed to in writing, software
13    * distributed under the License is distributed on an "AS IS" BASIS,
14    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    * See the License for the specific language governing permissions and
16    * limitations under the License.
17    */
18   
19    import junit.framework.TestCase;
20   
21    import java.util.Collection;
22   
23    import org.apache.lucene.index.Term;
24    import org.apache.lucene.index.IndexWriter;
25    import org.apache.lucene.search.IndexSearcher;
26    import org.apache.lucene.store.RAMDirectory;
27    import org.apache.lucene.analysis.SimpleAnalyzer;
28    import org.apache.lucene.document.Document;
29    import org.apache.lucene.document.Field;
30   
31    /** Similarity unit test.
32    *
33    * @author Doug Cutting
34    * @version $Revision: 150497 $
35    */
 
36    public class TestSimilarity extends TestCase {
 
37  1 toggle public TestSimilarity(String name) {
38  1 super(name);
39    }
40   
 
41    public static class SimpleSimilarity extends Similarity {
 
42  2 toggle public float lengthNorm(String field, int numTerms) { return 1.0f; }
 
43  4 toggle public float queryNorm(float sumOfSquaredWeights) { return 1.0f; }
 
44  99 toggle public float tf(float freq) { return freq; }
 
45  2 toggle public float sloppyFreq(int distance) { return 2.0f; }
 
46  2 toggle public float idf(Collection terms, Searcher searcher) { return 1.0f; }
 
47  3 toggle public float idf(int docFreq, int numDocs) { return 1.0f; }
 
48  3 toggle public float coord(int overlap, int maxOverlap) { return 1.0f; }
49    }
50   
 
51  1 toggle public void testSimilarity() throws Exception {
52  1 RAMDirectory store = new RAMDirectory();
53  1 IndexWriter writer = new IndexWriter(store, new SimpleAnalyzer(), true);
54  1 writer.setSimilarity(new SimpleSimilarity());
55   
56  1 Document d1 = new Document();
57  1 d1.add(new Field("field", "a c", Field.Store.YES, Field.Index.TOKENIZED));
58   
59  1 Document d2 = new Document();
60  1 d2.add(new Field("field", "a b c", Field.Store.YES, Field.Index.TOKENIZED));
61   
62  1 writer.addDocument(d1);
63  1 writer.addDocument(d2);
64  1 writer.optimize();
65  1 writer.close();
66   
67  1 Searcher searcher = new IndexSearcher(store);
68  1 searcher.setSimilarity(new SimpleSimilarity());
69   
70  1 Term a = new Term("field", "a");
71  1 Term b = new Term("field", "b");
72  1 Term c = new Term("field", "c");
73   
74  1 searcher.search
75    (new TermQuery(b),
76    new HitCollector() {
 
77  1 toggle public final void collect(int doc, float score) {
78  1 assertTrue(score == 1.0f);
79    }
80    });
81   
82  1 BooleanQuery bq = new BooleanQuery();
83  1 bq.add(new TermQuery(a), BooleanClause.Occur.SHOULD);
84  1 bq.add(new TermQuery(b), BooleanClause.Occur.SHOULD);
85    //System.out.println(bq.toString("field"));
86  1 searcher.search
87    (bq,
88    new HitCollector() {
 
89  2 toggle public final void collect(int doc, float score) {
90    //System.out.println("Doc=" + doc + " score=" + score);
91  2 assertTrue(score == (float)doc+1);
92    }
93    });
94   
95  1 PhraseQuery pq = new PhraseQuery();
96  1 pq.add(a);
97  1 pq.add(c);
98    //System.out.println(pq.toString("field"));
99  1 searcher.search
100    (pq,
101    new HitCollector() {
 
102  1 toggle public final void collect(int doc, float score) {
103    //System.out.println("Doc=" + doc + " score=" + score);
104  1 assertTrue(score == 1.0f);
105    }
106    });
107   
108  1 pq.setSlop(2);
109    //System.out.println(pq.toString("field"));
110  1 searcher.search
111    (pq,
112    new HitCollector() {
 
113  2 toggle public final void collect(int doc, float score) {
114    //System.out.println("Doc=" + doc + " score=" + score);
115  2 assertTrue(score == 2.0f);
116    }
117    });
118    }
119    }