Commit e401c684 authored by Benjamin Peterson's avatar Benjamin Peterson

Merged revisions 82461 via svnmerge from

svn+ssh://pythondev@svn.python.org/python/trunk

........
  r82461 | benjamin.peterson | 2010-07-02 18:05:27 -0500 (Fri, 02 Jul 2010) | 1 line

  don't require the presence of __getformat__ or __setformat__; use requires_IEEE_754 globally
........
parent d2191e04
...@@ -11,8 +11,13 @@ import random, fractions ...@@ -11,8 +11,13 @@ import random, fractions
INF = float("inf") INF = float("inf")
NAN = float("nan") NAN = float("nan")
have_getformat = hasattr(float, "__getformat__")
requires_getformat = unittest.skipUnless(have_getformat,
"requires __getformat__")
requires_setformat = unittest.skipUnless(hasattr(float, "__setformat__"),
"requires __setformat__")
# decorator for skipping tests on non-IEEE 754 platforms # decorator for skipping tests on non-IEEE 754 platforms
requires_IEEE_754 = unittest.skipUnless( requires_IEEE_754 = unittest.skipUnless(have_getformat and
float.__getformat__("double").startswith("IEEE"), float.__getformat__("double").startswith("IEEE"),
"test requires IEEE 754 doubles") "test requires IEEE 754 doubles")
...@@ -377,6 +382,7 @@ class GeneralFloatCases(unittest.TestCase): ...@@ -377,6 +382,7 @@ class GeneralFloatCases(unittest.TestCase):
#self.assertTrue(0.0 > pow_op(-2.0, -1047) > -1e-315) #self.assertTrue(0.0 > pow_op(-2.0, -1047) > -1e-315)
@requires_setformat
class FormatFunctionsTestCase(unittest.TestCase): class FormatFunctionsTestCase(unittest.TestCase):
def setUp(self): def setUp(self):
...@@ -427,6 +433,7 @@ LE_FLOAT_NAN = bytes(reversed(BE_FLOAT_NAN)) ...@@ -427,6 +433,7 @@ LE_FLOAT_NAN = bytes(reversed(BE_FLOAT_NAN))
# on non-IEEE platforms, attempting to unpack a bit pattern # on non-IEEE platforms, attempting to unpack a bit pattern
# representing an infinity or a NaN should raise an exception. # representing an infinity or a NaN should raise an exception.
@requires_setformat
class UnknownFormatTestCase(unittest.TestCase): class UnknownFormatTestCase(unittest.TestCase):
def setUp(self): def setUp(self):
self.save_formats = {'double':float.__getformat__('double'), self.save_formats = {'double':float.__getformat__('double'),
...@@ -459,36 +466,25 @@ class UnknownFormatTestCase(unittest.TestCase): ...@@ -459,36 +466,25 @@ class UnknownFormatTestCase(unittest.TestCase):
# let's also try to guarantee that -0.0 and 0.0 don't get confused. # let's also try to guarantee that -0.0 and 0.0 don't get confused.
class IEEEFormatTestCase(unittest.TestCase): class IEEEFormatTestCase(unittest.TestCase):
if float.__getformat__("double").startswith("IEEE"):
def test_double_specials_do_unpack(self): @requires_IEEE_754
for fmt, data in [('>d', BE_DOUBLE_INF), def test_double_specials_do_unpack(self):
('>d', BE_DOUBLE_NAN), for fmt, data in [('>d', BE_DOUBLE_INF),
('<d', LE_DOUBLE_INF), ('>d', BE_DOUBLE_NAN),
('<d', LE_DOUBLE_NAN)]: ('<d', LE_DOUBLE_INF),
struct.unpack(fmt, data) ('<d', LE_DOUBLE_NAN)]:
struct.unpack(fmt, data)
if float.__getformat__("float").startswith("IEEE"):
def test_float_specials_do_unpack(self): @requires_IEEE_754
for fmt, data in [('>f', BE_FLOAT_INF), def test_float_specials_do_unpack(self):
('>f', BE_FLOAT_NAN), for fmt, data in [('>f', BE_FLOAT_INF),
('<f', LE_FLOAT_INF), ('>f', BE_FLOAT_NAN),
('<f', LE_FLOAT_NAN)]: ('<f', LE_FLOAT_INF),
struct.unpack(fmt, data) ('<f', LE_FLOAT_NAN)]:
struct.unpack(fmt, data)
if float.__getformat__("double").startswith("IEEE"):
def test_negative_zero(self):
def pos_pos():
return 0.0, math.atan2(0.0, -1)
def pos_neg():
return 0.0, math.atan2(-0.0, -1)
def neg_pos():
return -0.0, math.atan2(0.0, -1)
def neg_neg():
return -0.0, math.atan2(-0.0, -1)
self.assertEquals(pos_pos(), neg_pos())
self.assertEquals(pos_neg(), neg_neg())
class FormatTestCase(unittest.TestCase): class FormatTestCase(unittest.TestCase):
def test_format(self): def test_format(self):
# these should be rewritten to use both format(x, spec) and # these should be rewritten to use both format(x, spec) and
# x.__format__(spec) # x.__format__(spec)
...@@ -542,8 +538,7 @@ class FormatTestCase(unittest.TestCase): ...@@ -542,8 +538,7 @@ class FormatTestCase(unittest.TestCase):
self.assertEqual(format(INF, 'f'), 'inf') self.assertEqual(format(INF, 'f'), 'inf')
self.assertEqual(format(INF, 'F'), 'INF') self.assertEqual(format(INF, 'F'), 'INF')
@unittest.skipUnless(float.__getformat__("double").startswith("IEEE"), @requires_IEEE_754
"test requires IEEE 754 doubles")
def test_format_testfile(self): def test_format_testfile(self):
for line in open(format_testfile): for line in open(format_testfile):
if line.startswith('--'): if line.startswith('--'):
...@@ -623,9 +618,10 @@ class ReprTestCase(unittest.TestCase): ...@@ -623,9 +618,10 @@ class ReprTestCase(unittest.TestCase):
self.assertEqual(s, repr(float(s))) self.assertEqual(s, repr(float(s)))
self.assertEqual(negs, repr(float(negs))) self.assertEqual(negs, repr(float(negs)))
@requires_IEEE_754
class RoundTestCase(unittest.TestCase): class RoundTestCase(unittest.TestCase):
@unittest.skipUnless(float.__getformat__("double").startswith("IEEE"),
"test requires IEEE 754 doubles")
def test_inf_nan(self): def test_inf_nan(self):
self.assertRaises(OverflowError, round, INF) self.assertRaises(OverflowError, round, INF)
self.assertRaises(OverflowError, round, -INF) self.assertRaises(OverflowError, round, -INF)
...@@ -635,8 +631,6 @@ class RoundTestCase(unittest.TestCase): ...@@ -635,8 +631,6 @@ class RoundTestCase(unittest.TestCase):
self.assertRaises(TypeError, round, NAN, "ceci n'est pas un integer") self.assertRaises(TypeError, round, NAN, "ceci n'est pas un integer")
self.assertRaises(TypeError, round, -0.0, 1j) self.assertRaises(TypeError, round, -0.0, 1j)
@unittest.skipUnless(float.__getformat__("double").startswith("IEEE"),
"test requires IEEE 754 doubles")
def test_large_n(self): def test_large_n(self):
for n in [324, 325, 400, 2**31-1, 2**31, 2**32, 2**100]: for n in [324, 325, 400, 2**31-1, 2**31, 2**32, 2**100]:
self.assertEqual(round(123.456, n), 123.456) self.assertEqual(round(123.456, n), 123.456)
...@@ -649,8 +643,6 @@ class RoundTestCase(unittest.TestCase): ...@@ -649,8 +643,6 @@ class RoundTestCase(unittest.TestCase):
self.assertEqual(round(1e150, 309), 1e150) self.assertEqual(round(1e150, 309), 1e150)
self.assertEqual(round(1.4e-315, 315), 1e-315) self.assertEqual(round(1.4e-315, 315), 1e-315)
@unittest.skipUnless(float.__getformat__("double").startswith("IEEE"),
"test requires IEEE 754 doubles")
def test_small_n(self): def test_small_n(self):
for n in [-308, -309, -400, 1-2**31, -2**31, -2**31-1, -2**100]: for n in [-308, -309, -400, 1-2**31, -2**31, -2**31-1, -2**100]:
self.assertEqual(round(123.456, n), 0.0) self.assertEqual(round(123.456, n), 0.0)
...@@ -658,8 +650,6 @@ class RoundTestCase(unittest.TestCase): ...@@ -658,8 +650,6 @@ class RoundTestCase(unittest.TestCase):
self.assertEqual(round(1e300, n), 0.0) self.assertEqual(round(1e300, n), 0.0)
self.assertEqual(round(1e-320, n), 0.0) self.assertEqual(round(1e-320, n), 0.0)
@unittest.skipUnless(float.__getformat__("double").startswith("IEEE"),
"test requires IEEE 754 doubles")
def test_overflow(self): def test_overflow(self):
self.assertRaises(OverflowError, round, 1.6e308, -308) self.assertRaises(OverflowError, round, 1.6e308, -308)
self.assertRaises(OverflowError, round, -1.7e308, -308) self.assertRaises(OverflowError, round, -1.7e308, -308)
...@@ -707,9 +697,6 @@ class RoundTestCase(unittest.TestCase): ...@@ -707,9 +697,6 @@ class RoundTestCase(unittest.TestCase):
self.assertEqual(float(format(x, '.2f')), round(x, 2)) self.assertEqual(float(format(x, '.2f')), round(x, 2))
self.assertEqual(float(format(x, '.3f')), round(x, 3)) self.assertEqual(float(format(x, '.3f')), round(x, 3))
@unittest.skipUnless(float.__getformat__("double").startswith("IEEE"),
"test requires IEEE 754 doubles")
def test_format_specials(self): def test_format_specials(self):
# Test formatting of nans and infs. # Test formatting of nans and infs.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment