summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1998-06-30 17:02:20 (GMT)
committerGuido van Rossum <guido@python.org>1998-06-30 17:02:20 (GMT)
commit7011504e27fd36a7182afe07e1ffdd4ccd6f3c5a (patch)
tree904c6a7f3d0ea5aa3f6458f8df588b6322bc8197 /Lib/test
parent7e0e9555b73feb664e5fa92f593fcb61b7135e45 (diff)
downloadcpython-7011504e27fd36a7182afe07e1ffdd4ccd6f3c5a.zip
cpython-7011504e27fd36a7182afe07e1ffdd4ccd6f3c5a.tar.gz
cpython-7011504e27fd36a7182afe07e1ffdd4ccd6f3c5a.tar.bz2
Improved test set for int() and long() string conversions.
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_b1.py59
1 files changed, 59 insertions, 0 deletions
diff --git a/Lib/test/test_b1.py b/Lib/test/test_b1.py
index 5a3e833..a53facb 100644
--- a/Lib/test/test_b1.py
+++ b/Lib/test/test_b1.py
@@ -235,6 +235,46 @@ if int(3.9) <> 3: raise TestFailed, 'int(3.9)'
if int(-3.9) <> -3: raise TestFailed, 'int(-3.9)'
if int(3.5) <> 3: raise TestFailed, 'int(3.5)'
if int(-3.5) <> -3: raise TestFailed, 'int(-3.5)'
+# Test conversion fron strings and various anomalies
+L = [
+ ('0', 0),
+ ('1', 1),
+ ('9', 9),
+ ('10', 10),
+ ('99', 99),
+ ('100', 100),
+ ('314', 314),
+ (' 314', 314),
+ ('314 ', 314),
+ (' \t\t 314 \t\t ', 314),
+ (`sys.maxint`, sys.maxint),
+ ('', ValueError),
+ (' ', ValueError),
+ (' \t\t ', ValueError),
+]
+for s, v in L:
+ for sign in "", "+", "-":
+ for prefix in "", " ", "\t", " \t\t ":
+ ss = prefix + sign + s
+ vv = v
+ if sign == "-" and v is not ValueError:
+ vv = -v
+ try:
+ if int(ss) != vv:
+ raise TestFailed, "int(%s)" % `ss`
+ except v:
+ pass
+ except ValueError, e:
+ raise TestFailed, "int(%s) raised ValueError: %s" % (`ss`, e)
+s = `-1-sys.maxint`
+if int(s)+1 != -sys.maxint:
+ raise TestFailed, "int(%s)" % `s`
+try:
+ int(s[1:])
+except ValueError:
+ pass
+else:
+ raise TestFailed, "int(%s)" % `s[1:]` + " should raise ValueError"
print 'isinstance'
class C:
@@ -290,6 +330,25 @@ if long(3.9) <> 3L: raise TestFailed, 'long(3.9)'
if long(-3.9) <> -3L: raise TestFailed, 'long(-3.9)'
if long(3.5) <> 3L: raise TestFailed, 'long(3.5)'
if long(-3.5) <> -3L: raise TestFailed, 'long(-3.5)'
+# Check conversions from string (same test set as for int(), and then some)
+LL = [
+ ('1' + '0'*20, 10L**20),
+ ('1' + '0'*100, 10L**100),
+]
+for s, v in L + LL:
+ for sign in "", "+", "-":
+ for prefix in "", " ", "\t", " \t\t ":
+ ss = prefix + sign + s
+ vv = v
+ if sign == "-" and v is not ValueError:
+ vv = -v
+ try:
+ if long(ss) != long(vv):
+ raise TestFailed, "int(%s)" % `ss`
+ except v:
+ pass
+ except ValueError, e:
+ raise TestFailed, "int(%s) raised ValueError: %s" % (`ss`, e)
print 'map'
if map(None, 'hello world') <> ['h','e','l','l','o',' ','w','o','r','l','d']: