add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / queryparser / src / test / org / apache / lucene / queryParser / surround / query / Test03Distance.java
1 package org.apache.lucene.queryParser.surround.query;
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 junit.framework.TestSuite;
21 import junit.textui.TestRunner;
22
23 import org.apache.lucene.util.LuceneTestCase;
24
25 public class Test03Distance extends LuceneTestCase {
26   public static void main(String args[]) {
27     TestRunner.run(new TestSuite(Test03Distance.class));
28   }
29   boolean verbose = false;
30   int maxBasicQueries = 16;
31   
32   String [] exceptionQueries = {
33     "(aa and bb) w cc",
34     "(aa or bb) w (cc and dd)",
35     "(aa opt bb) w cc",
36     "(aa not bb) w cc",
37     "(aa or bb) w (bi:cc)",
38     "(aa or bb) w bi:cc",
39     "(aa or bi:bb) w cc",
40     "(aa or (bi:bb)) w cc",
41     "(aa or (bb and dd)) w cc"
42   };
43   
44   public void test00Exceptions() throws Exception {
45     String m = ExceptionQueryTst.getFailQueries(exceptionQueries, verbose);
46     if (m.length() > 0) {
47       fail("No ParseException for:\n" + m);
48     }
49   }
50
51   final String fieldName = "bi";
52
53   String[] docs1 = {
54     "word1 word2 word3",
55     "word4 word5",
56     "ord1 ord2 ord3",
57     "orda1 orda2 orda3 word2 worda3",
58     "a c e a b c"
59   };
60
61   SingleFieldTestDb db1 = new SingleFieldTestDb(random, docs1, fieldName);
62
63   private void distanceTst(String query, int[] expdnrs, SingleFieldTestDb db) throws Exception {
64     BooleanQueryTst bqt = new BooleanQueryTst( query, expdnrs, db, fieldName, this,
65                                                 new BasicQueryFactory(maxBasicQueries));
66     bqt.setVerbose(verbose);
67     bqt.doTest();
68   }
69
70   public void distanceTest1(String query, int[] expdnrs) throws Exception {
71     distanceTst(query, expdnrs, db1);
72   }
73   
74   public void test0W01() throws Exception {
75     int[] expdnrs = {0}; distanceTest1( "word1 w word2", expdnrs);
76   }
77   public void test0N01() throws Exception {
78     int[] expdnrs = {0}; distanceTest1( "word1 n word2", expdnrs);
79   }
80   public void test0N01r() throws Exception { /* r reverse */
81     int[] expdnrs = {0}; distanceTest1( "word2 n word1", expdnrs);
82   }
83   
84   public void test0W02() throws Exception {
85     int[] expdnrs = {}; distanceTest1( "word2 w word1", expdnrs);
86   }
87   
88   public void test0W03() throws Exception {
89     int[] expdnrs = {}; distanceTest1( "word2 2W word1", expdnrs);
90   }
91   public void test0N03() throws Exception {
92     int[] expdnrs = {0}; distanceTest1( "word2 2N word1", expdnrs);
93   }
94   public void test0N03r() throws Exception {
95     int[] expdnrs = {0}; distanceTest1( "word1 2N word2", expdnrs);
96   }
97   
98   public void test0W04() throws Exception {
99     int[] expdnrs = {}; distanceTest1( "word2 3w word1", expdnrs);
100   }
101
102   public void test0N04() throws Exception {
103     int[] expdnrs = {0}; distanceTest1( "word2 3n word1", expdnrs);
104   }
105   public void test0N04r() throws Exception {
106     int[] expdnrs = {0}; distanceTest1( "word1 3n word2", expdnrs);
107   }
108
109   public void test0W05() throws Exception {
110     int[] expdnrs = {}; distanceTest1( "orda1 w orda3", expdnrs);
111   }
112   public void test0W06() throws Exception {
113     int[] expdnrs = {3}; distanceTest1( "orda1 2w orda3", expdnrs);
114   }
115   
116   public void test1Wtrunc01() throws Exception {
117     int[] expdnrs = {0}; distanceTest1( "word1* w word2", expdnrs);
118   }
119   public void test1Wtrunc02() throws Exception {
120     int[] expdnrs = {0}; distanceTest1( "word* w word2", expdnrs);
121   }
122   public void test1Wtrunc02r() throws Exception {
123     int[] expdnrs = {0,3}; distanceTest1( "word2 w word*", expdnrs);
124   }
125   public void test1Ntrunc02() throws Exception {
126     int[] expdnrs = {0,3}; distanceTest1( "word* n word2", expdnrs);
127   }
128   public void test1Ntrunc02r() throws Exception {
129     int[] expdnrs = {0,3}; distanceTest1( "word2 n word*", expdnrs);
130   }
131
132   public void test1Wtrunc03() throws Exception {
133     int[] expdnrs = {0}; distanceTest1( "word1* w word2*", expdnrs);
134   }
135   public void test1Ntrunc03() throws Exception {
136     int[] expdnrs = {0}; distanceTest1( "word1* N word2*", expdnrs);
137   }
138   
139   public void test1Wtrunc04() throws Exception {
140     int[] expdnrs = {}; distanceTest1( "kxork* w kxor*", expdnrs);
141   }
142   public void test1Ntrunc04() throws Exception {
143     int[] expdnrs = {}; distanceTest1( "kxork* 99n kxor*", expdnrs);
144   }
145
146   public void test1Wtrunc05() throws Exception {
147     int[] expdnrs = {}; distanceTest1( "word2* 2W word1*", expdnrs);
148   }
149   public void test1Ntrunc05() throws Exception {
150     int[] expdnrs = {0}; distanceTest1( "word2* 2N word1*", expdnrs);
151   }
152
153   public void test1Wtrunc06() throws Exception {
154     int[] expdnrs = {3}; distanceTest1( "ord* W word*", expdnrs);
155   }
156   public void test1Ntrunc06() throws Exception {
157     int[] expdnrs = {3}; distanceTest1( "ord* N word*", expdnrs);
158   }
159   public void test1Ntrunc06r() throws Exception {
160     int[] expdnrs = {3}; distanceTest1( "word* N ord*", expdnrs);
161   }
162   
163   public void test1Wtrunc07() throws Exception {
164     int[] expdnrs = {3}; distanceTest1( "(orda2 OR orda3) W word*", expdnrs);
165   }
166   public void test1Wtrunc08() throws Exception {
167     int[] expdnrs = {3}; distanceTest1( "(orda2 OR orda3) W (word2 OR worda3)", expdnrs);
168   }
169   public void test1Wtrunc09() throws Exception {
170     int[] expdnrs = {3}; distanceTest1( "(orda2 OR orda3) 2W (word2 OR worda3)", expdnrs);
171   }
172   public void test1Ntrunc09() throws Exception {
173     int[] expdnrs = {3}; distanceTest1( "(orda2 OR orda3) 2N (word2 OR worda3)", expdnrs);
174   }
175
176   String[] docs2 = {
177     "w1 w2 w3 w4 w5",
178     "w1 w3 w2 w3",
179     ""
180   };
181
182   SingleFieldTestDb db2 = new SingleFieldTestDb(random, docs2, fieldName);
183   
184   public void distanceTest2(String query, int[] expdnrs) throws Exception {
185     distanceTst(query, expdnrs, db2);
186   }
187   
188   public void test2Wprefix01() throws Exception {
189     int[] expdnrs = {0}; distanceTest2( "W (w1, w2, w3)", expdnrs);
190   }
191   public void test2Nprefix01a() throws Exception {
192     int[] expdnrs = {0,1}; distanceTest2( "N(w1, w2, w3)", expdnrs);
193   }
194   public void test2Nprefix01b() throws Exception {
195     int[] expdnrs = {0,1}; distanceTest2( "N(w3, w1, w2)", expdnrs);
196   }
197   
198   public void test2Wprefix02() throws Exception {
199     int[] expdnrs = {0,1}; distanceTest2( "2W(w1,w2,w3)", expdnrs);
200   }
201
202   public void test2Nprefix02a() throws Exception {
203     int[] expdnrs = {0,1}; distanceTest2( "2N(w1,w2,w3)", expdnrs);
204   }
205   public void test2Nprefix02b() throws Exception {
206     int[] expdnrs = {0,1}; distanceTest2( "2N(w2,w3,w1)", expdnrs);
207   }
208
209   public void test2Wnested01() throws Exception {
210     int[] expdnrs = {0}; distanceTest2( "w1 W w2 W w3", expdnrs);
211   }
212   public void test2Nnested01() throws Exception {
213     int[] expdnrs = {0}; distanceTest2( "w1 N w2 N w3", expdnrs);
214   }
215   
216   public void test2Wnested02() throws Exception {
217     int[] expdnrs = {0,1}; distanceTest2( "w1 2W w2 2W w3", expdnrs);
218   }
219   public void test2Nnested02() throws Exception {
220     int[] expdnrs = {0,1}; distanceTest2( "w1 2N w2 2N w3", expdnrs);
221   }
222   
223   String[] docs3 = {
224     "low pressure temperature inversion and rain",
225     "when the temperature has a negative height above a depression no precipitation gradient is expected",
226     "when the temperature has a negative height gradient above a depression no precipitation is expected",
227     ""
228   };
229
230   SingleFieldTestDb db3 = new SingleFieldTestDb(random, docs3, fieldName);
231
232   public void distanceTest3(String query, int[] expdnrs) throws Exception {
233     distanceTst(query, expdnrs, db3);
234   }
235
236   public void test3Example01() throws Exception {
237     int[] expdnrs = {0,2}; // query does not match doc 1 because "gradient" is in wrong place there.
238     distanceTest3("50n((low w pressure*) or depression*,"
239                    + "5n(temperat*, (invers* or (negativ* 3n gradient*))),"
240                    + "rain* or precipitat*)",
241                    expdnrs);
242   }
243 }