pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / test / org / apache / lucene / search / BaseTestRangeFilter.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 import java.util.Random;
22
23 import org.apache.lucene.analysis.MockAnalyzer;
24 import org.apache.lucene.document.Document;
25 import org.apache.lucene.document.Field;
26 import org.apache.lucene.index.IndexReader;
27 import org.apache.lucene.index.IndexWriterConfig.OpenMode;
28 import org.apache.lucene.index.RandomIndexWriter;
29 import org.apache.lucene.store.Directory;
30 import org.apache.lucene.util.LuceneTestCase;
31 import org.apache.lucene.util._TestUtil;
32 import org.junit.AfterClass;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35
36 public class BaseTestRangeFilter extends LuceneTestCase {
37   
38   public static final boolean F = false;
39   public static final boolean T = true;
40   
41   /**
42    * Collation interacts badly with hyphens -- collation produces different
43    * ordering than Unicode code-point ordering -- so two indexes are created:
44    * one which can't have negative random integers, for testing collated ranges,
45    * and the other which can have negative random integers, for all other tests.
46    */
47   static class TestIndex {
48     int maxR;
49     int minR;
50     boolean allowNegativeRandomInts;
51     Directory index;
52     
53     TestIndex(Random random, int minR, int maxR, boolean allowNegativeRandomInts) {
54       this.minR = minR;
55       this.maxR = maxR;
56       this.allowNegativeRandomInts = allowNegativeRandomInts;
57       try {
58         index = newDirectory(random);
59       } catch (IOException e) { throw new RuntimeException(e); }
60     }
61   }
62   
63   static IndexReader signedIndexReader;
64   static IndexReader unsignedIndexReader;
65   
66   static TestIndex signedIndexDir;
67   static TestIndex unsignedIndexDir;
68   
69   static int minId = 0;
70   static int maxId;
71   
72   static final int intLength = Integer.toString(Integer.MAX_VALUE).length();
73   
74   /**
75    * a simple padding function that should work with any int
76    */
77   public static String pad(int n) {
78     StringBuilder b = new StringBuilder(40);
79     String p = "0";
80     if (n < 0) {
81       p = "-";
82       n = Integer.MAX_VALUE + n + 1;
83     }
84     b.append(p);
85     String s = Integer.toString(n);
86     for (int i = s.length(); i <= intLength; i++) {
87       b.append("0");
88     }
89     b.append(s);
90     
91     return b.toString();
92   }
93   
94   @BeforeClass
95   public static void beforeClassBaseTestRangeFilter() throws Exception {
96     maxId = atLeast(500);
97     signedIndexDir = new TestIndex(random, Integer.MAX_VALUE, Integer.MIN_VALUE, true);
98     unsignedIndexDir = new TestIndex(random, Integer.MAX_VALUE, 0, false);
99     signedIndexReader = build(random, signedIndexDir);
100     unsignedIndexReader = build(random, unsignedIndexDir);
101   }
102   
103   @AfterClass
104   public static void afterClassBaseTestRangeFilter() throws Exception {
105     signedIndexReader.close();
106     unsignedIndexReader.close();
107     signedIndexDir.index.close();
108     unsignedIndexDir.index.close();
109     signedIndexReader = null;
110     unsignedIndexReader = null;
111     signedIndexDir = null;
112     unsignedIndexDir = null;
113   }
114   
115   private static IndexReader build(Random random, TestIndex index) throws IOException {
116     /* build an index */
117
118     Document doc = new Document();
119     Field idField = newField(random, "id", "", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS);
120     Field randField = newField(random, "rand", "", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS);
121     Field bodyField = newField(random, "body", "", Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS);
122     doc.add(idField);
123     doc.add(randField);
124     doc.add(bodyField);
125
126     RandomIndexWriter writer = new RandomIndexWriter(random, index.index, 
127                                                      newIndexWriterConfig(random, TEST_VERSION_CURRENT, new MockAnalyzer(random))
128                                                      .setOpenMode(OpenMode.CREATE).setMaxBufferedDocs(_TestUtil.nextInt(random, 50, 1000)).setMergePolicy(newLogMergePolicy()));
129     _TestUtil.reduceOpenFiles(writer.w);
130     while(true) {
131
132       int minCount = 0;
133       int maxCount = 0;
134
135       for (int d = minId; d <= maxId; d++) {
136         idField.setValue(pad(d));
137         int r = index.allowNegativeRandomInts ? random.nextInt() : random
138           .nextInt(Integer.MAX_VALUE);
139         if (index.maxR < r) {
140           index.maxR = r;
141           maxCount = 1;
142         } else if (index.maxR == r) {
143           maxCount++;
144         }
145
146         if (r < index.minR) {
147           index.minR = r;
148           minCount = 1;
149         } else if (r == index.minR) {
150           minCount++;
151         }
152         randField.setValue(pad(r));
153         bodyField.setValue("body");
154         writer.addDocument(doc);
155       }
156
157       if (minCount == 1 && maxCount == 1) {
158         // our subclasses rely on only 1 doc having the min or
159         // max, so, we loop until we satisfy that.  it should be
160         // exceedingly rare (Yonik calculates 1 in ~429,000)
161         // times) that this loop requires more than one try:
162         IndexReader ir = writer.getReader();
163         writer.close();
164         return ir;
165       }
166
167       // try again
168       writer.deleteAll();
169     }
170   }
171   
172   @Test
173   public void testPad() {
174     
175     int[] tests = new int[] {-9999999, -99560, -100, -3, -1, 0, 3, 9, 10, 1000,
176         999999999};
177     for (int i = 0; i < tests.length - 1; i++) {
178       int a = tests[i];
179       int b = tests[i + 1];
180       String aa = pad(a);
181       String bb = pad(b);
182       String label = a + ":" + aa + " vs " + b + ":" + bb;
183       assertEquals("length of " + label, aa.length(), bb.length());
184       assertTrue("compare less than " + label, aa.compareTo(bb) < 0);
185     }
186     
187   }
188   
189 }