add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / analyzers / common / src / test / org / apache / lucene / analysis / ngram / NGramTokenizerTest.java
1 package org.apache.lucene.analysis.ngram;
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
21 import java.io.StringReader;
22
23 import org.apache.lucene.analysis.BaseTokenStreamTestCase;
24
25 /**
26  * Tests {@link NGramTokenizer} for correctness.
27  */
28 public class NGramTokenizerTest extends BaseTokenStreamTestCase {
29     private StringReader input;
30     
31     @Override
32     public void setUp() throws Exception {
33         super.setUp();
34         input = new StringReader("abcde");
35     }
36
37     public void testInvalidInput() throws Exception {
38         boolean gotException = false;
39         try {        
40             new NGramTokenizer(input, 2, 1);
41         } catch (IllegalArgumentException e) {
42             gotException = true;
43         }
44         assertTrue(gotException);
45     }
46
47     public void testInvalidInput2() throws Exception {
48         boolean gotException = false;
49         try {        
50             new NGramTokenizer(input, 0, 1);
51         } catch (IllegalArgumentException e) {
52             gotException = true;
53         }
54         assertTrue(gotException);
55     }
56
57     public void testUnigrams() throws Exception {
58         NGramTokenizer tokenizer = new NGramTokenizer(input, 1, 1);
59         assertTokenStreamContents(tokenizer, new String[]{"a","b","c","d","e"}, new int[]{0,1,2,3,4}, new int[]{1,2,3,4,5}, 5 /* abcde */);
60     }
61
62     public void testBigrams() throws Exception {
63         NGramTokenizer tokenizer = new NGramTokenizer(input, 2, 2);
64         assertTokenStreamContents(tokenizer, new String[]{"ab","bc","cd","de"}, new int[]{0,1,2,3}, new int[]{2,3,4,5}, 5 /* abcde */);
65     }
66
67     public void testNgrams() throws Exception {
68         NGramTokenizer tokenizer = new NGramTokenizer(input, 1, 3);
69         assertTokenStreamContents(tokenizer,
70           new String[]{"a","b","c","d","e", "ab","bc","cd","de", "abc","bcd","cde"}, 
71           new int[]{0,1,2,3,4, 0,1,2,3, 0,1,2},
72           new int[]{1,2,3,4,5, 2,3,4,5, 3,4,5},
73           5 /* abcde */
74         );
75     }
76
77     public void testOversizedNgrams() throws Exception {
78         NGramTokenizer tokenizer = new NGramTokenizer(input, 6, 7);
79         assertTokenStreamContents(tokenizer, new String[0], new int[0], new int[0], 5 /* abcde */);
80     }
81     
82     public void testReset() throws Exception {
83       NGramTokenizer tokenizer = new NGramTokenizer(input, 1, 1);
84       assertTokenStreamContents(tokenizer, new String[]{"a","b","c","d","e"}, new int[]{0,1,2,3,4}, new int[]{1,2,3,4,5}, 5 /* abcde */);
85       tokenizer.reset(new StringReader("abcde"));
86       assertTokenStreamContents(tokenizer, new String[]{"a","b","c","d","e"}, new int[]{0,1,2,3,4}, new int[]{1,2,3,4,5}, 5 /* abcde */);
87     }
88 }