add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / src / test / org / apache / lucene / search / TestPositiveScoresOnlyCollector.java
1 package org.apache.lucene.search;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.io.IOException;
21
22 import org.apache.lucene.index.IndexReader;
23 import org.apache.lucene.index.RandomIndexWriter;
24 import org.apache.lucene.index.Term;
25 import org.apache.lucene.store.Directory;
26 import org.apache.lucene.util.LuceneTestCase;
27
28 public class TestPositiveScoresOnlyCollector extends LuceneTestCase {
29
30   private static final class SimpleScorer extends Scorer {
31     private int idx = -1;
32     
33     public SimpleScorer(Weight weight) {
34       super(weight);
35     }
36     
37     @Override public float score() throws IOException {
38       return idx == scores.length ? Float.NaN : scores[idx];
39     }
40
41     @Override public int docID() { return idx; }
42
43     @Override public int nextDoc() throws IOException {
44       return ++idx != scores.length ? idx : NO_MORE_DOCS;
45     }
46     
47     @Override public int advance(int target) throws IOException {
48       idx = target;
49       return idx < scores.length ? idx : NO_MORE_DOCS;
50     }
51   }
52
53   // The scores must have positive as well as negative values
54   private static final float[] scores = new float[] { 0.7767749f, -1.7839992f,
55       8.9925785f, 7.9608946f, -0.07948637f, 2.6356435f, 7.4950366f, 7.1490803f,
56       -8.108544f, 4.961808f, 2.2423935f, -7.285586f, 4.6699767f };
57
58   public void testNegativeScores() throws Exception {
59   
60     // The Top*Collectors previously filtered out documents with <= scores. This
61     // behavior has changed. This test checks that if PositiveOnlyScoresFilter
62     // wraps one of these collectors, documents with <= 0 scores are indeed
63     // filtered.
64     
65     int numPositiveScores = 0;
66     for (int i = 0; i < scores.length; i++) {
67       if (scores[i] > 0) {
68         ++numPositiveScores;
69       }
70     }
71     
72     Directory directory = newDirectory();
73     RandomIndexWriter writer = new RandomIndexWriter(random, directory);
74     writer.commit();
75     IndexReader ir = writer.getReader();
76     writer.close();
77     IndexSearcher searcher = newSearcher(ir);
78     Weight fake = new TermQuery(new Term("fake", "weight")).createWeight(searcher);
79     Scorer s = new SimpleScorer(fake);
80     TopDocsCollector<ScoreDoc> tdc = TopScoreDocCollector.create(scores.length, true);
81     Collector c = new PositiveScoresOnlyCollector(tdc);
82     c.setScorer(s);
83     while (s.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
84       c.collect(0);
85     }
86     TopDocs td = tdc.topDocs();
87     ScoreDoc[] sd = td.scoreDocs;
88     assertEquals(numPositiveScores, td.totalHits);
89     for (int i = 0; i < sd.length; i++) {
90       assertTrue("only positive scores should return: " + sd[i].score, sd[i].score > 0);
91     }
92     searcher.close();
93     ir.close();
94     directory.close();
95   }
96   
97 }