Skip to content

Commit 2d0571a

Browse files
author
David Noble
committed
Refined test_validators.test_integer for use on systems with 32-bit ints as well as 64-bit ints
Windows is the notable case: an int is always 32-bits. See the code comments for additional info.
1 parent 73e1f1f commit 2d0571a

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

tests/searchcommands/test_validators.py

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,20 @@ def test_file(self):
140140

141141
def test_integer(self):
142142

143-
maxint = sys.maxint
144-
minint = -(sys.maxint - 1)
143+
# Point of interest:
144+
#
145+
# On all *nix operating systems an int is 32-bits long on 32-bit systems and 64-bits long on 64-bit systems so
146+
# that you can count on this equality:
147+
#
148+
# sys.maxint == sys.maxsize
149+
#
150+
# On Windows an int is always 32-bits long and you cannot count on the same equality. Specifically, on 64-bit
151+
# systems:
152+
#
153+
# sys.maxint != sys.maxsize
154+
155+
maxsize = sys.maxsize
156+
minsize = -(sys.maxsize - 1)
145157

146158
# The Integer validator should convert values in the range of a Python long which has unlimited precision
147159
# Anecdotal evidence: This portion of the test checks 5-10 K integer values and runs for less than 2-3 seconds
@@ -155,42 +167,35 @@ def test(integer):
155167
self.assertIsInstance(value, long)
156168
self.assertEqual(validator.format(integer), unicode(integer))
157169

158-
test(2L * minint)
159-
test(minint)
170+
test(2L * minsize)
171+
test(minsize)
160172
test(-1)
161173
test(0)
162174
test(1)
163-
test(2L * maxint)
175+
test(2L * maxsize)
164176

165-
count = 3
166-
167-
start = -randint(0, maxint - 1)
168-
stop = maxint
169-
step = randint(1000000, 2000000)
170-
171-
for i in xrange(start, stop, step):
172-
test(i)
173-
count += 1
177+
for i in xrange(0, 10000):
178+
test(randint(minsize, maxsize))
174179

175180
# The Integer validator can impose a range restriction
176181

177182
validator = validators.Integer(minimum=0)
178183
self.assertEqual(validator.__call__(0), 0)
179-
self.assertEqual(validator.__call__(2L * maxint), 2L * maxint)
184+
self.assertEqual(validator.__call__(2L * maxsize), 2L * maxsize)
180185
self.assertRaises(ValueError, validator.__call__, -1)
181186

182-
validator = validators.Integer(minimum=1, maximum=maxint)
187+
validator = validators.Integer(minimum=1, maximum=maxsize)
183188
self.assertEqual(validator.__call__(1), 1)
184-
self.assertEqual(validator.__call__(maxint), maxint)
189+
self.assertEqual(validator.__call__(maxsize), maxsize)
185190
self.assertRaises(ValueError, validator.__call__, 0)
186-
self.assertRaises(ValueError, validator.__call__, maxint + 1)
191+
self.assertRaises(ValueError, validator.__call__, maxsize + 1)
187192

188-
validator = validators.Integer(minimum=minint, maximum=maxint)
189-
self.assertEqual(validator.__call__(minint), minint)
193+
validator = validators.Integer(minimum=minsize, maximum=maxsize)
194+
self.assertEqual(validator.__call__(minsize), minsize)
190195
self.assertEqual(validator.__call__(0), 0)
191-
self.assertEqual(validator.__call__(maxint), maxint)
192-
self.assertRaises(ValueError, validator.__call__, minint - 1L)
193-
self.assertRaises(ValueError, validator.__call__, maxint + 1L)
196+
self.assertEqual(validator.__call__(maxsize), maxsize)
197+
self.assertRaises(ValueError, validator.__call__, minsize - 1L)
198+
self.assertRaises(ValueError, validator.__call__, maxsize + 1L)
194199

195200
return
196201

0 commit comments

Comments
 (0)