@@ -235,6 +235,46 @@ def f(): pass
235235if int (- 3.9 ) <> - 3 : raise TestFailed , 'int(-3.9)'
236236if int (3.5 ) <> 3 : raise TestFailed , 'int(3.5)'
237237if 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
239279print 'isinstance'
240280class C :
@@ -290,6 +330,25 @@ class E:
290330if long (- 3.9 ) <> - 3L : raise TestFailed , 'long(-3.9)'
291331if long (3.5 ) <> 3L : raise TestFailed , 'long(3.5)'
292332if 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
294353print 'map'
295354if map (None , 'hello world' ) <> ['h' ,'e' ,'l' ,'l' ,'o' ,' ' ,'w' ,'o' ,'r' ,'l' ,'d' ]:
0 commit comments