Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
39   121   12   7.8
12   79   0.31   5
5     2.4  
1    
 
  BaseTestRangeFilter       Line # 29 39 12 94.6% 0.9464286
 
  (6)
 
1    package org.apache.lucene.search;
2   
3    /**
4    * Copyright 2004 The Apache Software Foundation
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10    * http://www.apache.org/licenses/LICENSE-2.0
11    *
12    * Unless required by applicable law or agreed to in writing, software
13    * distributed under the License is distributed on an "AS IS" BASIS,
14    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    * See the License for the specific language governing permissions and
16    * limitations under the License.
17    */
18   
19    import java.util.Random;
20   
21    import junit.framework.TestCase;
22   
23    import org.apache.lucene.analysis.SimpleAnalyzer;
24    import org.apache.lucene.document.Document;
25    import org.apache.lucene.document.Field;
26    import org.apache.lucene.index.IndexWriter;
27    import org.apache.lucene.store.RAMDirectory;
28   
 
29    public class BaseTestRangeFilter extends TestCase {
30   
31    public static final boolean F = false;
32    public static final boolean T = true;
33   
34    RAMDirectory index = new RAMDirectory();
35    Random rand = new Random(101); // use a set seed to test is deterministic
36   
37    int maxR = Integer.MIN_VALUE;
38    int minR = Integer.MAX_VALUE;
39   
40    int minId = 0;
41    int maxId = 10000;
42   
43    static final int intLength = Integer.toString(Integer.MAX_VALUE).length();
44   
45    /**
46    * a simple padding function that should work with any int
47    */
 
48  200070 toggle public static String pad(int n) {
49  200070 StringBuffer b = new StringBuffer(40);
50  200070 String p = "0";
51  200070 if (n < 0) {
52  50510 p = "-";
53  50510 n = Integer.MAX_VALUE + n + 1;
54    }
55  200070 b.append(p);
56  200070 String s = Integer.toString(n);
57  1062974 for (int i = s.length(); i <= intLength; i++) {
58  862904 b.append("0");
59    }
60  200070 b.append(s);
61   
62  200070 return b.toString();
63    }
64   
 
65  10 toggle public BaseTestRangeFilter(String name) {
66  10 super(name);
67  10 build();
68    }
 
69  0 toggle public BaseTestRangeFilter() {
70  0 build();
71    }
72   
 
73  10 toggle private void build() {
74  10 try {
75   
76    /* build an index */
77  10 IndexWriter writer = new IndexWriter(index,
78    new SimpleAnalyzer(), T);
79   
80  100020 for (int d = minId; d <= maxId; d++) {
81  100010 Document doc = new Document();
82  100010 doc.add(new Field("id",pad(d), Field.Store.YES, Field.Index.UN_TOKENIZED));
83  100010 int r= rand.nextInt();
84  100010 if (maxR < r) {
85  110 maxR = r;
86    }
87  100010 if (r < minR) {
88  120 minR = r;
89    }
90  100010 doc.add(new Field("rand",pad(r), Field.Store.YES, Field.Index.UN_TOKENIZED));
91  100010 doc.add(new Field("body","body", Field.Store.YES, Field.Index.UN_TOKENIZED));
92  100010 writer.addDocument(doc);
93    }
94   
95  10 writer.optimize();
96  10 writer.close();
97   
98    } catch (Exception e) {
99  0 throw new RuntimeException("can't build index", e);
100    }
101   
102    }
103   
 
104  2 toggle public void testPad() {
105   
106  2 int[] tests = new int[] {
107    -9999999, -99560, -100, -3, -1, 0, 3, 9, 10, 1000, 999999999
108    };
109  22 for (int i = 0; i < tests.length - 1; i++) {
110  20 int a = tests[i];
111  20 int b = tests[i+1];
112  20 String aa = pad(a);
113  20 String bb = pad(b);
114  20 String label = a + ":" + aa + " vs " + b + ":" + bb;
115  20 assertEquals("length of " + label, aa.length(), bb.length());
116  20 assertTrue("compare less than " + label, aa.compareTo(bb) < 0);
117    }
118   
119    }
120   
121    }