mirror of
https://github.com/python/cpython.git
synced 2025-07-12 13:55:34 +00:00
Merged revisions 79629 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r79629 | mark.dickinson | 2010-04-02 23:27:36 +0100 (Fri, 02 Apr 2010) | 2 lines Issue #8294: Allow float and Decimal arguments in Fraction constructor. ........
This commit is contained in:
parent
ac256ab284
commit
98127c3716
4 changed files with 121 additions and 20 deletions
|
@ -12,6 +12,11 @@ from pickle import dumps, loads
|
|||
F = fractions.Fraction
|
||||
gcd = fractions.gcd
|
||||
|
||||
# decorator for skipping tests on non-IEEE 754 platforms
|
||||
requires_IEEE_754 = unittest.skipUnless(
|
||||
float.__getformat__("double").startswith("IEEE"),
|
||||
"test requires IEEE 754 doubles")
|
||||
|
||||
class DummyFloat(object):
|
||||
"""Dummy float class for testing comparisons with Fractions"""
|
||||
|
||||
|
@ -130,13 +135,33 @@ class FractionTest(unittest.TestCase):
|
|||
|
||||
self.assertRaisesMessage(ZeroDivisionError, "Fraction(12, 0)",
|
||||
F, 12, 0)
|
||||
self.assertRaises(TypeError, F, 1.5)
|
||||
self.assertRaises(TypeError, F, 1.5 + 3j)
|
||||
|
||||
self.assertRaises(TypeError, F, "3/2", 3)
|
||||
self.assertRaises(TypeError, F, 3, 0j)
|
||||
self.assertRaises(TypeError, F, 3, 1j)
|
||||
|
||||
@requires_IEEE_754
|
||||
def testInitFromFloat(self):
|
||||
self.assertEquals((5, 2), _components(F(2.5)))
|
||||
self.assertEquals((0, 1), _components(F(-0.0)))
|
||||
self.assertEquals((3602879701896397, 36028797018963968),
|
||||
_components(F(0.1)))
|
||||
self.assertRaises(TypeError, F, float('nan'))
|
||||
self.assertRaises(TypeError, F, float('inf'))
|
||||
self.assertRaises(TypeError, F, float('-inf'))
|
||||
|
||||
def testInitFromDecimal(self):
|
||||
self.assertEquals((11, 10),
|
||||
_components(F(Decimal('1.1'))))
|
||||
self.assertEquals((7, 200),
|
||||
_components(F(Decimal('3.5e-2'))))
|
||||
self.assertEquals((0, 1),
|
||||
_components(F(Decimal('.000e20'))))
|
||||
self.assertRaises(TypeError, F, Decimal('nan'))
|
||||
self.assertRaises(TypeError, F, Decimal('snan'))
|
||||
self.assertRaises(TypeError, F, Decimal('inf'))
|
||||
self.assertRaises(TypeError, F, Decimal('-inf'))
|
||||
|
||||
def testFromString(self):
|
||||
self.assertEquals((5, 1), _components(F("5")))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue