add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / src / test / org / apache / lucene / index / TestUniqueTermCount.java
1 package org.apache.lucene.index;
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.util.ArrayList;
21 import java.util.HashSet;
22
23 import org.apache.lucene.analysis.MockAnalyzer;
24 import org.apache.lucene.analysis.MockTokenizer;
25 import org.apache.lucene.document.Document;
26 import org.apache.lucene.document.Field;
27 import org.apache.lucene.search.DefaultSimilarity;
28 import org.apache.lucene.search.Similarity;
29 import org.apache.lucene.store.Directory;
30 import org.apache.lucene.util.LuceneTestCase;
31 import org.apache.lucene.util._TestUtil;
32
33 /**
34  * Tests the uniqueTermCount statistic in FieldInvertState
35  */
36 public class TestUniqueTermCount extends LuceneTestCase { 
37   Directory dir;
38   IndexReader reader;
39   /* expected uniqueTermCount values for our documents */
40   ArrayList<Integer> expected = new ArrayList<Integer>();
41   
42   @Override
43   public void setUp() throws Exception {
44     super.setUp();
45     dir = newDirectory();
46     IndexWriterConfig config = newIndexWriterConfig(TEST_VERSION_CURRENT, 
47                                                     new MockAnalyzer(random, MockTokenizer.SIMPLE, true)).setMergePolicy(newLogMergePolicy());
48     config.setSimilarity(new TestSimilarity());
49     RandomIndexWriter writer = new RandomIndexWriter(random, dir, config);
50     Document doc = new Document();
51     Field foo = newField("foo", "", Field.Store.NO, Field.Index.ANALYZED);
52     doc.add(foo);
53     for (int i = 0; i < 100; i++) {
54       foo.setValue(addValue());
55       writer.addDocument(doc);
56     }
57     reader = writer.getReader();
58     writer.close();
59   }
60   
61   @Override
62   public void tearDown() throws Exception {
63     reader.close();
64     dir.close();
65     super.tearDown();
66   }
67   
68   public void test() throws Exception {
69     byte fooNorms[] = reader.norms("foo");
70     for (int i = 0; i < reader.maxDoc(); i++)
71       assertEquals(expected.get(i).intValue(), fooNorms[i] & 0xff);
72   }
73
74   /**
75    * Makes a bunch of single-char tokens (the max # unique terms will at most be 26).
76    * puts the # unique terms into expected, to be checked against the norm.
77    */
78   private String addValue() {
79     StringBuilder sb = new StringBuilder();
80     HashSet<String> terms = new HashSet<String>();
81     int num = _TestUtil.nextInt(random, 0, 255);
82     for (int i = 0; i < num; i++) {
83       sb.append(' ');
84       char term = (char) _TestUtil.nextInt(random, 'a', 'z');
85       sb.append(term);
86       terms.add("" + term);
87     }
88     expected.add(terms.size());
89     return sb.toString();
90   }
91   
92   /**
93    * Simple similarity that encodes maxTermFrequency directly as a byte
94    */
95   class TestSimilarity extends DefaultSimilarity {
96
97     @Override
98     public byte encodeNormValue(float f) {
99       return (byte) f;
100     }
101
102     @Override
103     public float computeNorm(String field, FieldInvertState state) {
104       return (float) state.getUniqueTermCount();
105     }
106   }
107 }