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
6 # http://www.apache.org/licenses/LICENSE-2.0
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 # ====================================================================
15 from random import seed, randint
16 from unittest import TestCase
21 class BaseTestRangeFilter(TestCase):
23 def __init__(self, *args):
25 super(BaseTestRangeFilter, self).__init__(*args)
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.
35 self.MAX_INT = 0x7fffffff
37 class TestIndex(object):
38 def __init__(_self, minR, maxR, allowNegativeRandomInts):
41 _self.allowNegativeRandomInts = allowNegativeRandomInts
42 _self.index = RAMDirectory()
44 self.signedIndex = TestIndex(self.MAX_INT, ~self.MAX_INT, True)
45 self.unsignedIndex = TestIndex(self.MAX_INT, 0, False)
50 self.build(self.signedIndex)
51 self.build(self.unsignedIndex)
54 # a simple padding function that should work with any int
60 return "-%0.10d" % (self.MAX_INT + n + 1)
64 def build(self, index):
66 writer = IndexWriter(index.index, SimpleAnalyzer(), True,
67 IndexWriter.MaxFieldLength.LIMITED)
70 for d in xrange(self.minId, self.maxId + 1):
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)
77 r = randint(0, self.MAX_INT)
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)
96 tests = [-9999999, -99560, -100, -3, -1, 0, 3, 9, 10, 1000, 999999999]
98 for i in xrange(0, len(tests) - 1):
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)