diff options
Diffstat (limited to 'Lib/test/test_fractions.py')
-rw-r--r-- | Lib/test/test_fractions.py | 55 |
1 files changed, 45 insertions, 10 deletions
diff --git a/Lib/test/test_fractions.py b/Lib/test/test_fractions.py index 17fdb7f6ea1..26e132f4e56 100644 --- a/Lib/test/test_fractions.py +++ b/Lib/test/test_fractions.py @@ -1,7 +1,7 @@ """Tests for Lib/fractions.py.""" from decimal import Decimal -from test.support import run_unittest +from test.support import run_unittest, requires_IEEE_754 import math import numbers import operator @@ -130,13 +130,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.assertEqual((5, 2), _components(F(2.5))) + self.assertEqual((0, 1), _components(F(-0.0))) + self.assertEqual((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.assertEqual((11, 10), + _components(F(Decimal('1.1')))) + self.assertEqual((7, 200), + _components(F(Decimal('3.5e-2')))) + self.assertEqual((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.assertEqual((5, 1), _components(F("5"))) @@ -216,10 +236,10 @@ class FractionTest(unittest.TestCase): self.assertEqual((10, 1), _components(F.from_float(10.0))) self.assertEqual((-5, 2), _components(F.from_float(-2.5))) self.assertEqual((99999999999999991611392, 1), - _components(F.from_float(1e23))) + _components(F.from_float(1e23))) self.assertEqual(float(10**23), float(F.from_float(1e23))) self.assertEqual((3602879701896397, 1125899906842624), - _components(F.from_float(3.2))) + _components(F.from_float(3.2))) self.assertEqual(3.2, float(F.from_float(3.2))) inf = 1e1000 @@ -370,12 +390,11 @@ class FractionTest(unittest.TestCase): self.assertTypedEquals(1.0 + 0j, (1.0 + 0j) ** F(1, 10)) def testMixingWithDecimal(self): - # Decimal refuses mixed comparisons. + # Decimal refuses mixed arithmetic (but not mixed comparisons) self.assertRaisesMessage( TypeError, "unsupported operand type(s) for +: 'Fraction' and 'Decimal'", operator.add, F(3,11), Decimal('3.1415926')) - self.assertNotEqual(F(5, 2), Decimal('2.5')) def testComparisons(self): self.assertTrue(F(1, 2) < F(2, 3)) @@ -479,8 +498,21 @@ class FractionTest(unittest.TestCase): def testBigComplexComparisons(self): self.assertFalse(F(10**23) == complex(10**23)) - self.assertTrue(F(10**23) > complex(10**23)) - self.assertFalse(F(10**23) <= complex(10**23)) + self.assertRaises(TypeError, operator.gt, F(10**23), complex(10**23)) + self.assertRaises(TypeError, operator.le, F(10**23), complex(10**23)) + + x = F(3, 8) + z = complex(0.375, 0.0) + w = complex(0.375, 0.2) + self.assertTrue(x == z) + self.assertFalse(x != z) + self.assertFalse(x == w) + self.assertTrue(x != w) + for op in operator.lt, operator.le, operator.gt, operator.ge: + self.assertRaises(TypeError, op, x, z) + self.assertRaises(TypeError, op, z, x) + self.assertRaises(TypeError, op, x, w) + self.assertRaises(TypeError, op, w, x) def testMixedEqual(self): self.assertTrue(0.5 == F(1, 2)) @@ -499,9 +531,9 @@ class FractionTest(unittest.TestCase): def testStringification(self): self.assertEqual("Fraction(7, 3)", repr(F(7, 3))) self.assertEqual("Fraction(6283185307, 2000000000)", - repr(F('3.1415926535'))) + repr(F('3.1415926535'))) self.assertEqual("Fraction(-1, 100000000000000000000)", - repr(F(1, -10**20))) + repr(F(1, -10**20))) self.assertEqual("7/3", str(F(7, 3))) self.assertEqual("7", str(F(7, 1))) @@ -509,6 +541,9 @@ class FractionTest(unittest.TestCase): self.assertEqual(hash(2.5), hash(F(5, 2))) self.assertEqual(hash(10**50), hash(F(10**50))) self.assertNotEqual(hash(float(10**23)), hash(F(10**23))) + # Check that __hash__ produces the same value as hash(), for + # consistency with int and Decimal. (See issue #10356.) + self.assertEqual(hash(F(-1)), F(-1).__hash__()) def testApproximatePi(self): # Algorithm borrowed from |