001: package org.apache.lucene.search;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import java.util.Random;
021:
022: import org.apache.lucene.util.LuceneTestCase;
023:
024: import org.apache.lucene.analysis.SimpleAnalyzer;
025: import org.apache.lucene.document.Document;
026: import org.apache.lucene.document.Field;
027: import org.apache.lucene.index.IndexWriter;
028: import org.apache.lucene.store.RAMDirectory;
029:
030: public class BaseTestRangeFilter extends LuceneTestCase {
031:
032: public static final boolean F = false;
033: public static final boolean T = true;
034:
035: RAMDirectory index = new RAMDirectory();
036: Random rand = new Random(101); // use a set seed to test is deterministic
037:
038: int maxR = Integer.MIN_VALUE;
039: int minR = Integer.MAX_VALUE;
040:
041: int minId = 0;
042: int maxId = 10000;
043:
044: static final int intLength = Integer.toString(Integer.MAX_VALUE)
045: .length();
046:
047: /**
048: * a simple padding function that should work with any int
049: */
050: public static String pad(int n) {
051: StringBuffer b = new StringBuffer(40);
052: String p = "0";
053: if (n < 0) {
054: p = "-";
055: n = Integer.MAX_VALUE + n + 1;
056: }
057: b.append(p);
058: String s = Integer.toString(n);
059: for (int i = s.length(); i <= intLength; i++) {
060: b.append("0");
061: }
062: b.append(s);
063:
064: return b.toString();
065: }
066:
067: public BaseTestRangeFilter(String name) {
068: super (name);
069: build();
070: }
071:
072: public BaseTestRangeFilter() {
073: build();
074: }
075:
076: private void build() {
077: try {
078:
079: /* build an index */
080: IndexWriter writer = new IndexWriter(index,
081: new SimpleAnalyzer(), T);
082:
083: for (int d = minId; d <= maxId; d++) {
084: Document doc = new Document();
085: doc.add(new Field("id", pad(d), Field.Store.YES,
086: Field.Index.UN_TOKENIZED));
087: int r = rand.nextInt();
088: if (maxR < r) {
089: maxR = r;
090: }
091: if (r < minR) {
092: minR = r;
093: }
094: doc.add(new Field("rand", pad(r), Field.Store.YES,
095: Field.Index.UN_TOKENIZED));
096: doc.add(new Field("body", "body", Field.Store.YES,
097: Field.Index.UN_TOKENIZED));
098: writer.addDocument(doc);
099: }
100:
101: writer.optimize();
102: writer.close();
103:
104: } catch (Exception e) {
105: throw new RuntimeException("can't build index", e);
106: }
107:
108: }
109:
110: public void testPad() {
111:
112: int[] tests = new int[] { -9999999, -99560, -100, -3, -1, 0, 3,
113: 9, 10, 1000, 999999999 };
114: for (int i = 0; i < tests.length - 1; i++) {
115: int a = tests[i];
116: int b = tests[i + 1];
117: String aa = pad(a);
118: String bb = pad(b);
119: String label = a + ":" + aa + " vs " + b + ":" + bb;
120: assertEquals("length of " + label, aa.length(), bb.length());
121: assertTrue("compare less than " + label,
122: aa.compareTo(bb) < 0);
123: }
124:
125: }
126:
127: }
|