summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_long.py
diff options
context:
space:
mode:
authorTim Peters <tim.peters@gmail.com>2001-09-04 05:14:19 (GMT)
committerTim Peters <tim.peters@gmail.com>2001-09-04 05:14:19 (GMT)
commit9fffa3eea3a7e99b0179988e7a016a45bf63ab96 (patch)
tree1b891e323dff2790bcd2b74ad4071cb0d380803d /Lib/test/test_long.py
parent1832de4bc07e7ffd5938b41e8d7d8fcf8b5e12e2 (diff)
downloadcpython-9fffa3eea3a7e99b0179988e7a016a45bf63ab96.zip
cpython-9fffa3eea3a7e99b0179988e7a016a45bf63ab96.tar.gz
cpython-9fffa3eea3a7e99b0179988e7a016a45bf63ab96.tar.bz2
Raise OverflowError when appropriate on long->float conversion. Most of
the fiddling is simply due to that no caller of PyLong_AsDouble ever checked for failure (so that's fixing old bugs). PyLong_AsDouble is much faster for big inputs now too, but that's more of a happy consequence than a design goal.
Diffstat (limited to 'Lib/test/test_long.py')
-rw-r--r--Lib/test/test_long.py37
1 files changed, 37 insertions, 0 deletions
diff --git a/Lib/test/test_long.py b/Lib/test/test_long.py
index b0eaff7..ac345a6 100644
--- a/Lib/test/test_long.py
+++ b/Lib/test/test_long.py
@@ -328,6 +328,42 @@ def test_auto_overflow():
raise TestFailed("pow%r should have raised "
"TypeError" % ((longx, longy, long(z))))
+# ---------------------------------------- tests of long->float overflow
+
+def test_float_overflow():
+ import math
+
+ if verbose:
+ print "long->float overflow"
+
+ for x in -2.0, -1.0, 0.0, 1.0, 2.0:
+ verify(float(long(x)) == x)
+
+ huge = 1L << 30000
+ mhuge = -huge
+ namespace = {'huge': huge, 'mhuge': mhuge, 'math': math}
+ for test in ["float(huge)", "float(mhuge)",
+ "complex(huge)", "complex(mhuge)",
+ "complex(huge, 1)", "complex(mhuge, 1)",
+ "complex(1, huge)", "complex(1, mhuge)",
+ "1. + huge", "huge + 1.", "1. + mhuge", "mhuge + 1.",
+ "1. - huge", "huge - 1.", "1. - mhuge", "mhuge - 1.",
+ "1. * huge", "huge * 1.", "1. * mhuge", "mhuge * 1.",
+ "1. // huge", "huge // 1.", "1. // mhuge", "mhuge // 1.",
+ "1. / huge", "huge / 1.", "1. / mhuge", "mhuge / 1.",
+ "1. ** huge", "huge ** 1.", "1. ** mhuge", "mhuge ** 1.",
+ "math.sin(huge)", "math.sin(mhuge)",
+ "math.log(huge)", "math.log(mhuge)", # should do better
+ "math.sqrt(huge)", "math.sqrt(mhuge)", # should do better
+ "math.log10(huge)", "math.log10(mhuge)", # should do better
+ "math.floor(huge)", "math.floor(mhuge)"]:
+
+ try:
+ eval(test, namespace)
+ except OverflowError:
+ pass
+ else:
+ raise TestFailed("expected OverflowError from %s" % test)
# ---------------------------------------------------------------- do it
test_division()
@@ -335,3 +371,4 @@ test_bitop_identities()
test_format()
test_misc()
test_auto_overflow()
+test_float_overflow()