add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / src / test / org / apache / lucene / search / TestScoreCachingWrappingScorer.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.util.LuceneTestCase;
24
25 public class TestScoreCachingWrappingScorer extends LuceneTestCase {
26
27   private static final class SimpleScorer extends Scorer {
28     private int idx = 0;
29     private int doc = -1;
30     
31     public SimpleScorer() {
32       super((Similarity)null);
33     }
34     
35     @Override public float score() throws IOException {
36       // advance idx on purpose, so that consecutive calls to score will get
37       // different results. This is to emulate computation of a score. If
38       // ScoreCachingWrappingScorer is used, this should not be called more than
39       // once per document.
40       return idx == scores.length ? Float.NaN : scores[idx++];
41     }
42
43     @Override public int docID() { return doc; }
44
45     @Override public int nextDoc() throws IOException {
46       return ++doc < scores.length ? doc : NO_MORE_DOCS;
47     }
48     
49     @Override public int advance(int target) throws IOException {
50       doc = target;
51       return doc < scores.length ? doc : NO_MORE_DOCS;
52     }
53     
54   }
55   
56   private static final class ScoreCachingCollector extends Collector {
57
58     private int idx = 0;
59     private Scorer scorer;
60     float[] mscores;
61     
62     public ScoreCachingCollector(int numToCollect) {
63       mscores = new float[numToCollect];
64     }
65     
66     @Override public void collect(int doc) throws IOException {
67       // just a sanity check to avoid IOOB.
68       if (idx == mscores.length) {
69         return; 
70       }
71       
72       // just call score() a couple of times and record the score.
73       mscores[idx] = scorer.score();
74       mscores[idx] = scorer.score();
75       mscores[idx] = scorer.score();
76       ++idx;
77     }
78
79     @Override public void setNextReader(IndexReader reader, int docBase)
80         throws IOException {
81     }
82
83     @Override public void setScorer(Scorer scorer) throws IOException {
84       this.scorer = new ScoreCachingWrappingScorer(scorer);
85     }
86     
87     @Override public boolean acceptsDocsOutOfOrder() {
88       return true;
89     }
90
91   }
92
93   private static final float[] scores = new float[] { 0.7767749f, 1.7839992f,
94       8.9925785f, 7.9608946f, 0.07948637f, 2.6356435f, 7.4950366f, 7.1490803f,
95       8.108544f, 4.961808f, 2.2423935f, 7.285586f, 4.6699767f };
96   
97   public void testGetScores() throws Exception {
98     
99     Scorer s = new SimpleScorer();
100     ScoreCachingCollector scc = new ScoreCachingCollector(scores.length);
101     scc.setScorer(s);
102     
103     // We need to iterate on the scorer so that its doc() advances.
104     int doc;
105     while ((doc = s.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
106       scc.collect(doc);
107     }
108     
109     for (int i = 0; i < scores.length; i++) {
110       assertEquals(scores[i], scc.mscores[i], 0f);
111     }
112     
113   }
114   
115 }