Skip to content

Commit 682d374

Browse files
committed
python#14089: increase coverage of the fractions module. Patch by Oleg Plakhotnyuk.
1 parent 443f000 commit 682d374

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Lib/test/test_fractions.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import numbers
77
import operator
88
import fractions
9+
import sys
910
import unittest
1011
from copy import copy, deepcopy
1112
from pickle import dumps, loads
@@ -76,6 +77,9 @@ def __ge__(self, other):
7677
def __float__(self):
7778
assert False, "__float__ should not be invoked"
7879

80+
class DummyFraction(fractions.Fraction):
81+
"""Dummy Fraction subclass for copy and deepcopy testing."""
82+
7983
class GcdTest(unittest.TestCase):
8084

8185
def testMisc(self):
@@ -286,9 +290,14 @@ def testLimitDenominator(self):
286290
self.assertEqual(F(201, 200).limit_denominator(100), F(1))
287291
self.assertEqual(F(201, 200).limit_denominator(101), F(102, 101))
288292
self.assertEqual(F(0).limit_denominator(10000), F(0))
293+
for i in (0, -1):
294+
self.assertRaisesMessage(
295+
ValueError, "max_denominator should be at least 1",
296+
F(1).limit_denominator, i)
289297

290298
def testConversions(self):
291299
self.assertTypedEquals(-1, math.trunc(F(-11, 10)))
300+
self.assertTypedEquals(1, math.trunc(F(11, 10)))
292301
self.assertTypedEquals(-2, math.floor(F(-11, 10)))
293302
self.assertTypedEquals(-1, math.ceil(F(-11, 10)))
294303
self.assertTypedEquals(-1, math.ceil(F(-10, 10)))
@@ -329,6 +338,7 @@ def testArithmetic(self):
329338
self.assertEqual(F(8, 27), F(2, 3) ** F(3))
330339
self.assertEqual(F(27, 8), F(2, 3) ** F(-3))
331340
self.assertTypedEquals(2.0, F(4) ** F(1, 2))
341+
self.assertEqual(F(1, 1), +F(1, 1))
332342
z = pow(F(-1), F(1, 2))
333343
self.assertAlmostEqual(z.real, 0)
334344
self.assertEqual(z.imag, 1)
@@ -395,6 +405,10 @@ def testMixingWithDecimal(self):
395405
TypeError,
396406
"unsupported operand type(s) for +: 'Fraction' and 'Decimal'",
397407
operator.add, F(3,11), Decimal('3.1415926'))
408+
self.assertRaisesMessage(
409+
TypeError,
410+
"unsupported operand type(s) for +: 'Decimal' and 'Fraction'",
411+
operator.add, Decimal('3.1415926'), F(3,11))
398412

399413
def testComparisons(self):
400414
self.assertTrue(F(1, 2) < F(2, 3))
@@ -538,9 +552,12 @@ def testStringification(self):
538552
self.assertEqual("7", str(F(7, 1)))
539553

540554
def testHash(self):
555+
hmod = sys.hash_info.modulus
556+
hinf = sys.hash_info.inf
541557
self.assertEqual(hash(2.5), hash(F(5, 2)))
542558
self.assertEqual(hash(10**50), hash(F(10**50)))
543559
self.assertNotEqual(hash(float(10**23)), hash(F(10**23)))
560+
self.assertEqual(hinf, hash(F(1, hmod)))
544561
# Check that __hash__ produces the same value as hash(), for
545562
# consistency with int and Decimal. (See issue #10356.)
546563
self.assertEqual(hash(F(-1)), F(-1).__hash__())
@@ -574,9 +591,14 @@ def testApproximateCos1(self):
574591

575592
def test_copy_deepcopy_pickle(self):
576593
r = F(13, 7)
594+
dr = DummyFraction(13, 7)
577595
self.assertEqual(r, loads(dumps(r)))
578596
self.assertEqual(id(r), id(copy(r)))
579597
self.assertEqual(id(r), id(deepcopy(r)))
598+
self.assertNotEqual(id(dr), id(copy(dr)))
599+
self.assertNotEqual(id(dr), id(deepcopy(dr)))
600+
self.assertTypedEquals(dr, copy(dr))
601+
self.assertTypedEquals(dr, deepcopy(dr))
580602

581603
def test_slots(self):
582604
# Issue 4998

0 commit comments

Comments
 (0)