Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7011504

Browse files
committed
Improved test set for int() and long() string conversions.
1 parent 7e0e955 commit 7011504

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

Lib/test/test_b1.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,46 @@ def f(): pass
235235
if int(-3.9) <> -3: raise TestFailed, 'int(-3.9)'
236236
if int(3.5) <> 3: raise TestFailed, 'int(3.5)'
237237
if int(-3.5) <> -3: raise TestFailed, 'int(-3.5)'
238+
# Test conversion fron strings and various anomalies
239+
L = [
240+
('0', 0),
241+
('1', 1),
242+
('9', 9),
243+
('10', 10),
244+
('99', 99),
245+
('100', 100),
246+
('314', 314),
247+
(' 314', 314),
248+
('314 ', 314),
249+
(' \t\t 314 \t\t ', 314),
250+
(`sys.maxint`, sys.maxint),
251+
('', ValueError),
252+
(' ', ValueError),
253+
(' \t\t ', ValueError),
254+
]
255+
for s, v in L:
256+
for sign in "", "+", "-":
257+
for prefix in "", " ", "\t", " \t\t ":
258+
ss = prefix + sign + s
259+
vv = v
260+
if sign == "-" and v is not ValueError:
261+
vv = -v
262+
try:
263+
if int(ss) != vv:
264+
raise TestFailed, "int(%s)" % `ss`
265+
except v:
266+
pass
267+
except ValueError, e:
268+
raise TestFailed, "int(%s) raised ValueError: %s" % (`ss`, e)
269+
s = `-1-sys.maxint`
270+
if int(s)+1 != -sys.maxint:
271+
raise TestFailed, "int(%s)" % `s`
272+
try:
273+
int(s[1:])
274+
except ValueError:
275+
pass
276+
else:
277+
raise TestFailed, "int(%s)" % `s[1:]` + " should raise ValueError"
238278

239279
print 'isinstance'
240280
class C:
@@ -290,6 +330,25 @@ class E:
290330
if long(-3.9) <> -3L: raise TestFailed, 'long(-3.9)'
291331
if long(3.5) <> 3L: raise TestFailed, 'long(3.5)'
292332
if long(-3.5) <> -3L: raise TestFailed, 'long(-3.5)'
333+
# Check conversions from string (same test set as for int(), and then some)
334+
LL = [
335+
('1' + '0'*20, 10L**20),
336+
('1' + '0'*100, 10L**100),
337+
]
338+
for s, v in L + LL:
339+
for sign in "", "+", "-":
340+
for prefix in "", " ", "\t", " \t\t ":
341+
ss = prefix + sign + s
342+
vv = v
343+
if sign == "-" and v is not ValueError:
344+
vv = -v
345+
try:
346+
if long(ss) != long(vv):
347+
raise TestFailed, "int(%s)" % `ss`
348+
except v:
349+
pass
350+
except ValueError, e:
351+
raise TestFailed, "int(%s) raised ValueError: %s" % (`ss`, e)
293352

294353
print 'map'
295354
if map(None, 'hello world') <> ['h','e','l','l','o',' ','w','o','r','l','d']:

0 commit comments

Comments
 (0)