1 package org.apache.lucene.search;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 import java.io.IOException;
21 import java.util.Random;
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;
36 public class BaseTestRangeFilter extends LuceneTestCase {
38 public static final boolean F = false;
39 public static final boolean T = true;
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.
47 static class TestIndex {
50 boolean allowNegativeRandomInts;
53 TestIndex(Random random, int minR, int maxR, boolean allowNegativeRandomInts) {
56 this.allowNegativeRandomInts = allowNegativeRandomInts;
58 index = newDirectory(random);
59 } catch (IOException e) { throw new RuntimeException(e); }
63 static IndexReader signedIndexReader;
64 static IndexReader unsignedIndexReader;
66 static TestIndex signedIndexDir;
67 static TestIndex unsignedIndexDir;
70 static int maxId = atLeast(500);
72 static final int intLength = Integer.toString(Integer.MAX_VALUE).length();
75 * a simple padding function that should work with any int
77 public static String pad(int n) {
78 StringBuilder b = new StringBuilder(40);
82 n = Integer.MAX_VALUE + n + 1;
85 String s = Integer.toString(n);
86 for (int i = s.length(); i <= intLength; i++) {
95 public static void beforeClassBaseTestRangeFilter() throws Exception {
96 signedIndexDir = new TestIndex(random, Integer.MAX_VALUE, Integer.MIN_VALUE, true);
97 unsignedIndexDir = new TestIndex(random, Integer.MAX_VALUE, 0, false);
98 signedIndexReader = build(random, signedIndexDir);
99 unsignedIndexReader = build(random, unsignedIndexDir);
103 public static void afterClassBaseTestRangeFilter() throws Exception {
104 signedIndexReader.close();
105 unsignedIndexReader.close();
106 signedIndexDir.index.close();
107 unsignedIndexDir.index.close();
108 signedIndexReader = null;
109 unsignedIndexReader = null;
110 signedIndexDir = null;
111 unsignedIndexDir = null;
114 private static IndexReader build(Random random, TestIndex index) throws IOException {
117 Document doc = new Document();
118 Field idField = newField(random, "id", "", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS);
119 Field randField = newField(random, "rand", "", Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS);
120 Field bodyField = newField(random, "body", "", Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS);
125 RandomIndexWriter writer = new RandomIndexWriter(random, index.index,
126 newIndexWriterConfig(random, TEST_VERSION_CURRENT, new MockAnalyzer(random))
127 .setOpenMode(OpenMode.CREATE).setMaxBufferedDocs(_TestUtil.nextInt(random, 50, 1000)).setMergePolicy(newLogMergePolicy()));
128 _TestUtil.reduceOpenFiles(writer.w);
134 for (int d = minId; d <= maxId; d++) {
135 idField.setValue(pad(d));
136 int r = index.allowNegativeRandomInts ? random.nextInt() : random
137 .nextInt(Integer.MAX_VALUE);
138 if (index.maxR < r) {
141 } else if (index.maxR == r) {
145 if (r < index.minR) {
148 } else if (r == index.minR) {
151 randField.setValue(pad(r));
152 bodyField.setValue("body");
153 writer.addDocument(doc);
156 if (minCount == 1 && maxCount == 1) {
157 // our subclasses rely on only 1 doc having the min or
158 // max, so, we loop until we satisfy that. it should be
159 // exceedingly rare (Yonik calculates 1 in ~429,000)
160 // times) that this loop requires more than one try:
161 IndexReader ir = writer.getReader();
172 public void testPad() {
174 int[] tests = new int[] {-9999999, -99560, -100, -3, -1, 0, 3, 9, 10, 1000,
176 for (int i = 0; i < tests.length - 1; i++) {
178 int b = tests[i + 1];
181 String label = a + ":" + aa + " vs " + b + ":" + bb;
182 assertEquals("length of " + label, aa.length(), bb.length());
183 assertTrue("compare less than " + label, aa.compareTo(bb) < 0);