PyLucene 3.4.0-1 import
[pylucene.git] / test / BaseTestRangeFilter.py
1 # ====================================================================
2 #   Licensed under the Apache License, Version 2.0 (the "License");
3 #   you may not use this file except in compliance with the License.
4 #   You may obtain a copy of the License at
5 #
6 #       http://www.apache.org/licenses/LICENSE-2.0
7 #
8 #   Unless required by applicable law or agreed to in writing, software
9 #   distributed under the License is distributed on an "AS IS" BASIS,
10 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 #   See the License for the specific language governing permissions and
12 #   limitations under the License.
13 # ====================================================================
14
15 from random import seed, randint
16 from unittest import TestCase
17
18 from lucene import *
19
20
21 class BaseTestRangeFilter(TestCase):
22
23     def __init__(self, *args):
24
25         super(BaseTestRangeFilter, self).__init__(*args)
26
27         # 
28         # Collation interacts badly with hyphens -- collation produces
29         # different ordering than Unicode code-point ordering -- so two
30         # indexes are created: one which can't have negative random
31         # integers, for testing collated ranges, and the other which can
32         # have negative random integers, for all other tests.
33         #
34
35         self.MAX_INT = 0x7fffffff
36
37         class TestIndex(object):
38             def __init__(_self, minR, maxR, allowNegativeRandomInts):
39                 _self.minR = minR
40                 _self.maxR = maxR
41                 _self.allowNegativeRandomInts = allowNegativeRandomInts
42                 _self.index = RAMDirectory()
43
44         self.signedIndex = TestIndex(self.MAX_INT, ~self.MAX_INT, True)
45         self.unsignedIndex = TestIndex(self.MAX_INT, 0, False)
46
47         self.minId = 0
48         self.maxId = 10000
49
50         self.build(self.signedIndex)
51         self.build(self.unsignedIndex)
52
53     #
54     # a simple padding function that should work with any int
55     #
56
57     def pad(self, n):
58
59         if n < 0:
60             return "-%0.10d" % (self.MAX_INT + n + 1)
61         else:
62             return "0%0.10d" % n
63
64     def build(self, index):
65
66         writer = IndexWriter(index.index, SimpleAnalyzer(), True, 
67                              IndexWriter.MaxFieldLength.LIMITED)
68
69         seed(101)
70         for d in xrange(self.minId, self.maxId + 1):
71             doc = Document()
72             doc.add(Field("id", self.pad(d), Field.Store.YES,
73                           Field.Index.NOT_ANALYZED));
74             if index.allowNegativeRandomInts:
75                 r = randint(~self.MAX_INT, self.MAX_INT)
76             else:
77                 r = randint(0, self.MAX_INT)
78
79             if index.maxR < r:
80                 index.maxR = r
81
82             if r < index.minR:
83                 index.minR = r
84
85             doc.add(Field("rand", self.pad(r), Field.Store.YES,
86                           Field.Index.NOT_ANALYZED))
87             doc.add(Field("body", "body", Field.Store.YES,
88                           Field.Index.NOT_ANALYZED));
89             writer.addDocument(doc)
90             
91         writer.optimize()
92         writer.close()
93
94     def testPad(self):
95
96         tests = [-9999999, -99560, -100, -3, -1, 0, 3, 9, 10, 1000, 999999999]
97
98         for i in xrange(0, len(tests) - 1):
99             a = tests[i]
100             b = tests[i + 1]
101             aa = self.pad(a)
102             bb = self.pad(b)
103             label = "%s:%s vs %s:%s" %(a, aa, b, bb)
104             self.assertEqual(len(aa), len(bb), "length of %s" %label)
105             self.assert_(aa < bb, "compare less than %s" %label)
106