add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / spellchecker / src / test / org / apache / lucene / search / suggest / PersistenceTest.java
1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 package org.apache.lucene.search.suggest;
18
19 import java.io.File;
20
21 import org.apache.lucene.search.suggest.Lookup;
22 import org.apache.lucene.search.suggest.fst.FSTLookup;
23 import org.apache.lucene.search.suggest.jaspell.JaspellLookup;
24 import org.apache.lucene.search.suggest.tst.TSTLookup;
25 import org.apache.lucene.util.LuceneTestCase;
26
27 public class PersistenceTest extends LuceneTestCase {
28   public final String[] keys = new String[] {
29       "one", 
30       "two", 
31       "three", 
32       "four",
33       "oneness", 
34       "onerous", 
35       "onesimus", 
36       "twofold", 
37       "twonk", 
38       "thrive",
39       "through", 
40       "threat", 
41       "foundation", 
42       "fourier", 
43       "fourty"};
44
45   public void testTSTPersistence() throws Exception {
46     runTest(TSTLookup.class, true);
47   }
48   
49   public void testJaspellPersistence() throws Exception {
50     runTest(JaspellLookup.class, true);
51   }
52
53   public void testFSTPersistence() throws Exception {
54     runTest(FSTLookup.class, false);
55   }
56   
57   private void runTest(Class<? extends Lookup> lookupClass,
58       boolean supportsExactWeights) throws Exception {
59
60     // Add all input keys.
61     Lookup lookup = lookupClass.newInstance();
62     TermFreq[] keys = new TermFreq[this.keys.length];
63     for (int i = 0; i < keys.length; i++)
64       keys[i] = new TermFreq(this.keys[i], (float) i);
65     lookup.build(new TermFreqArrayIterator(keys));
66
67     // Store the suggester.
68     File storeDir = TEMP_DIR;
69     lookup.store(storeDir);
70
71     // Re-read it from disk.
72     lookup = lookupClass.newInstance();
73     lookup.load(storeDir);
74
75     // Assert validity.
76     float previous = Float.NEGATIVE_INFINITY;
77     for (TermFreq k : keys) {
78       Float val = (Float) lookup.get(k.term);
79       assertNotNull(k.term, val);
80
81       if (supportsExactWeights) { 
82         assertEquals(k.term, Float.valueOf(k.v), val);
83       } else {
84         assertTrue(val + ">=" + previous, val >= previous);
85         previous = val.floatValue();
86       }
87     }
88   }
89 }