Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(80918)

Delta Between Two Patch Sets: Lib/test/test_fractions.py

Issue 7652: Merge C version of decimal into py3k.
Left Patch Set: Created 8 years, 4 months ago
Right Patch Set: Created 7 years, 8 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/test/test_decimal.py ('k') | Lib/test/test_numeric_tower.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 """Tests for Lib/fractions.py.""" 1 """Tests for Lib/fractions.py."""
2 2
3 from decimal import Decimal 3 from decimal import Decimal
4 from test.support import run_unittest, requires_IEEE_754 4 from test.support import run_unittest, requires_IEEE_754
5 import math 5 import math
6 import numbers 6 import numbers
7 import operator 7 import operator
8 import fractions 8 import fractions
9 import sys
9 import unittest 10 import unittest
10 from copy import copy, deepcopy 11 from copy import copy, deepcopy
11 from pickle import dumps, loads 12 from pickle import dumps, loads
12 F = fractions.Fraction 13 F = fractions.Fraction
13 gcd = fractions.gcd 14 gcd = fractions.gcd
14 15
15 class DummyFloat(object): 16 class DummyFloat(object):
16 """Dummy float class for testing comparisons with Fractions""" 17 """Dummy float class for testing comparisons with Fractions"""
17 18
18 def __init__(self, value): 19 def __init__(self, value):
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 def __le__(self, other): 69 def __le__(self, other):
69 return(self.num * other._denominator <= self.den * other._numerator) 70 return(self.num * other._denominator <= self.den * other._numerator)
70 71
71 def __ge__(self, other): 72 def __ge__(self, other):
72 return(self.num * other._denominator >= self.den * other._numerator) 73 return(self.num * other._denominator >= self.den * other._numerator)
73 74
74 # this class is for testing comparisons; conversion to float 75 # this class is for testing comparisons; conversion to float
75 # should never be used for a comparison, since it loses accuracy 76 # should never be used for a comparison, since it loses accuracy
76 def __float__(self): 77 def __float__(self):
77 assert False, "__float__ should not be invoked" 78 assert False, "__float__ should not be invoked"
79
80 class DummyFraction(fractions.Fraction):
81 """Dummy Fraction subclass for copy and deepcopy testing."""
78 82
79 class GcdTest(unittest.TestCase): 83 class GcdTest(unittest.TestCase):
80 84
81 def testMisc(self): 85 def testMisc(self):
82 self.assertEqual(0, gcd(0, 0)) 86 self.assertEqual(0, gcd(0, 0))
83 self.assertEqual(1, gcd(1, 0)) 87 self.assertEqual(1, gcd(1, 0))
84 self.assertEqual(-1, gcd(-1, 0)) 88 self.assertEqual(-1, gcd(-1, 0))
85 self.assertEqual(1, gcd(0, 1)) 89 self.assertEqual(1, gcd(0, 1))
86 self.assertEqual(-1, gcd(0, -1)) 90 self.assertEqual(-1, gcd(0, -1))
87 self.assertEqual(1, gcd(7, 1)) 91 self.assertEqual(1, gcd(7, 1))
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
279 283
280 def testLimitDenominator(self): 284 def testLimitDenominator(self):
281 rpi = F('3.1415926535897932') 285 rpi = F('3.1415926535897932')
282 self.assertEqual(rpi.limit_denominator(10000), F(355, 113)) 286 self.assertEqual(rpi.limit_denominator(10000), F(355, 113))
283 self.assertEqual(-rpi.limit_denominator(10000), F(-355, 113)) 287 self.assertEqual(-rpi.limit_denominator(10000), F(-355, 113))
284 self.assertEqual(rpi.limit_denominator(113), F(355, 113)) 288 self.assertEqual(rpi.limit_denominator(113), F(355, 113))
285 self.assertEqual(rpi.limit_denominator(112), F(333, 106)) 289 self.assertEqual(rpi.limit_denominator(112), F(333, 106))
286 self.assertEqual(F(201, 200).limit_denominator(100), F(1)) 290 self.assertEqual(F(201, 200).limit_denominator(100), F(1))
287 self.assertEqual(F(201, 200).limit_denominator(101), F(102, 101)) 291 self.assertEqual(F(201, 200).limit_denominator(101), F(102, 101))
288 self.assertEqual(F(0).limit_denominator(10000), F(0)) 292 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)
289 297
290 def testConversions(self): 298 def testConversions(self):
291 self.assertTypedEquals(-1, math.trunc(F(-11, 10))) 299 self.assertTypedEquals(-1, math.trunc(F(-11, 10)))
300 self.assertTypedEquals(1, math.trunc(F(11, 10)))
292 self.assertTypedEquals(-2, math.floor(F(-11, 10))) 301 self.assertTypedEquals(-2, math.floor(F(-11, 10)))
293 self.assertTypedEquals(-1, math.ceil(F(-11, 10))) 302 self.assertTypedEquals(-1, math.ceil(F(-11, 10)))
294 self.assertTypedEquals(-1, math.ceil(F(-10, 10))) 303 self.assertTypedEquals(-1, math.ceil(F(-10, 10)))
295 self.assertTypedEquals(-1, int(F(-11, 10))) 304 self.assertTypedEquals(-1, int(F(-11, 10)))
296 self.assertTypedEquals(0, round(F(-1, 10))) 305 self.assertTypedEquals(0, round(F(-1, 10)))
297 self.assertTypedEquals(0, round(F(-5, 10))) 306 self.assertTypedEquals(0, round(F(-5, 10)))
298 self.assertTypedEquals(-2, round(F(-15, 10))) 307 self.assertTypedEquals(-2, round(F(-15, 10)))
299 self.assertTypedEquals(-1, round(F(-7, 10))) 308 self.assertTypedEquals(-1, round(F(-7, 10)))
300 309
301 self.assertEqual(False, bool(F(0, 1))) 310 self.assertEqual(False, bool(F(0, 1)))
(...skipping 20 matching lines...) Expand all
322 self.assertEqual(F(1, 2), F(1, 10) + F(2, 5)) 331 self.assertEqual(F(1, 2), F(1, 10) + F(2, 5))
323 self.assertEqual(F(-3, 10), F(1, 10) - F(2, 5)) 332 self.assertEqual(F(-3, 10), F(1, 10) - F(2, 5))
324 self.assertEqual(F(1, 25), F(1, 10) * F(2, 5)) 333 self.assertEqual(F(1, 25), F(1, 10) * F(2, 5))
325 self.assertEqual(F(1, 4), F(1, 10) / F(2, 5)) 334 self.assertEqual(F(1, 4), F(1, 10) / F(2, 5))
326 self.assertTypedEquals(2, F(9, 10) // F(2, 5)) 335 self.assertTypedEquals(2, F(9, 10) // F(2, 5))
327 self.assertTypedEquals(10**23, F(10**23, 1) // F(1)) 336 self.assertTypedEquals(10**23, F(10**23, 1) // F(1))
328 self.assertEqual(F(2, 3), F(-7, 3) % F(3, 2)) 337 self.assertEqual(F(2, 3), F(-7, 3) % F(3, 2))
329 self.assertEqual(F(8, 27), F(2, 3) ** F(3)) 338 self.assertEqual(F(8, 27), F(2, 3) ** F(3))
330 self.assertEqual(F(27, 8), F(2, 3) ** F(-3)) 339 self.assertEqual(F(27, 8), F(2, 3) ** F(-3))
331 self.assertTypedEquals(2.0, F(4) ** F(1, 2)) 340 self.assertTypedEquals(2.0, F(4) ** F(1, 2))
341 self.assertEqual(F(1, 1), +F(1, 1))
332 z = pow(F(-1), F(1, 2)) 342 z = pow(F(-1), F(1, 2))
333 self.assertAlmostEqual(z.real, 0) 343 self.assertAlmostEqual(z.real, 0)
334 self.assertEqual(z.imag, 1) 344 self.assertEqual(z.imag, 1)
335 345
336 def testMixedArithmetic(self): 346 def testMixedArithmetic(self):
337 self.assertTypedEquals(F(11, 10), F(1, 10) + 1) 347 self.assertTypedEquals(F(11, 10), F(1, 10) + 1)
338 self.assertTypedEquals(1.1, F(1, 10) + 1.0) 348 self.assertTypedEquals(1.1, F(1, 10) + 1.0)
339 self.assertTypedEquals(1.1 + 0j, F(1, 10) + (1.0 + 0j)) 349 self.assertTypedEquals(1.1 + 0j, F(1, 10) + (1.0 + 0j))
340 self.assertTypedEquals(F(11, 10), 1 + F(1, 10)) 350 self.assertTypedEquals(F(11, 10), 1 + F(1, 10))
341 self.assertTypedEquals(1.1, 1.0 + F(1, 10)) 351 self.assertTypedEquals(1.1, 1.0 + F(1, 10))
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
386 self.assertEqual(1, z.imag) 396 self.assertEqual(1, z.imag)
387 self.assertTypedEquals(F(1, 4) , 2 ** F(-2, 1)) 397 self.assertTypedEquals(F(1, 4) , 2 ** F(-2, 1))
388 self.assertTypedEquals(2.0 , 4 ** F(1, 2)) 398 self.assertTypedEquals(2.0 , 4 ** F(1, 2))
389 self.assertTypedEquals(0.25, 2.0 ** F(-2, 1)) 399 self.assertTypedEquals(0.25, 2.0 ** F(-2, 1))
390 self.assertTypedEquals(1.0 + 0j, (1.0 + 0j) ** F(1, 10)) 400 self.assertTypedEquals(1.0 + 0j, (1.0 + 0j) ** F(1, 10))
391 401
392 def testMixingWithDecimal(self): 402 def testMixingWithDecimal(self):
393 # Decimal refuses mixed arithmetic (but not mixed comparisons) 403 # Decimal refuses mixed arithmetic (but not mixed comparisons)
394 self.assertRaises(TypeError, operator.add, 404 self.assertRaises(TypeError, operator.add,
395 F(3,11), Decimal('3.1415926')) 405 F(3,11), Decimal('3.1415926'))
406 self.assertRaises(TypeError, operator.add,
407 Decimal('3.1415926'), F(3,11))
396 408
397 def testComparisons(self): 409 def testComparisons(self):
398 self.assertTrue(F(1, 2) < F(2, 3)) 410 self.assertTrue(F(1, 2) < F(2, 3))
399 self.assertFalse(F(1, 2) < F(1, 2)) 411 self.assertFalse(F(1, 2) < F(1, 2))
400 self.assertTrue(F(1, 2) <= F(2, 3)) 412 self.assertTrue(F(1, 2) <= F(2, 3))
401 self.assertTrue(F(1, 2) <= F(1, 2)) 413 self.assertTrue(F(1, 2) <= F(1, 2))
402 self.assertFalse(F(2, 3) <= F(1, 2)) 414 self.assertFalse(F(2, 3) <= F(1, 2))
403 self.assertTrue(F(1, 2) == F(1, 2)) 415 self.assertTrue(F(1, 2) == F(1, 2))
404 self.assertFalse(F(1, 2) == F(1, 3)) 416 self.assertFalse(F(1, 2) == F(1, 3))
405 self.assertFalse(F(1, 2) != F(1, 2)) 417 self.assertFalse(F(1, 2) != F(1, 2))
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
529 def testStringification(self): 541 def testStringification(self):
530 self.assertEqual("Fraction(7, 3)", repr(F(7, 3))) 542 self.assertEqual("Fraction(7, 3)", repr(F(7, 3)))
531 self.assertEqual("Fraction(6283185307, 2000000000)", 543 self.assertEqual("Fraction(6283185307, 2000000000)",
532 repr(F('3.1415926535'))) 544 repr(F('3.1415926535')))
533 self.assertEqual("Fraction(-1, 100000000000000000000)", 545 self.assertEqual("Fraction(-1, 100000000000000000000)",
534 repr(F(1, -10**20))) 546 repr(F(1, -10**20)))
535 self.assertEqual("7/3", str(F(7, 3))) 547 self.assertEqual("7/3", str(F(7, 3)))
536 self.assertEqual("7", str(F(7, 1))) 548 self.assertEqual("7", str(F(7, 1)))
537 549
538 def testHash(self): 550 def testHash(self):
551 hmod = sys.hash_info.modulus
552 hinf = sys.hash_info.inf
539 self.assertEqual(hash(2.5), hash(F(5, 2))) 553 self.assertEqual(hash(2.5), hash(F(5, 2)))
540 self.assertEqual(hash(10**50), hash(F(10**50))) 554 self.assertEqual(hash(10**50), hash(F(10**50)))
541 self.assertNotEqual(hash(float(10**23)), hash(F(10**23))) 555 self.assertNotEqual(hash(float(10**23)), hash(F(10**23)))
556 self.assertEqual(hinf, hash(F(1, hmod)))
542 # Check that __hash__ produces the same value as hash(), for 557 # Check that __hash__ produces the same value as hash(), for
543 # consistency with int and Decimal. (See issue #10356.) 558 # consistency with int and Decimal. (See issue #10356.)
544 self.assertEqual(hash(F(-1)), F(-1).__hash__()) 559 self.assertEqual(hash(F(-1)), F(-1).__hash__())
545 560
546 def testApproximatePi(self): 561 def testApproximatePi(self):
547 # Algorithm borrowed from 562 # Algorithm borrowed from
548 # http://docs.python.org/lib/decimal-recipes.html 563 # http://docs.python.org/lib/decimal-recipes.html
549 three = F(3) 564 three = F(3)
550 lasts, t, s, n, na, d, da = 0, three, 3, 1, 0, 0, 24 565 lasts, t, s, n, na, d, da = 0, three, 3, 1, 0, 0, 24
551 while abs(s - lasts) > F(1, 10**9): 566 while abs(s - lasts) > F(1, 10**9):
(...skipping 13 matching lines...) Expand all
565 lasts = s 580 lasts = s
566 i += 2 581 i += 2
567 fact *= i * (i-1) 582 fact *= i * (i-1)
568 num *= x * x 583 num *= x * x
569 sign *= -1 584 sign *= -1
570 s += num / fact * sign 585 s += num / fact * sign
571 self.assertAlmostEqual(math.cos(1), s) 586 self.assertAlmostEqual(math.cos(1), s)
572 587
573 def test_copy_deepcopy_pickle(self): 588 def test_copy_deepcopy_pickle(self):
574 r = F(13, 7) 589 r = F(13, 7)
590 dr = DummyFraction(13, 7)
575 self.assertEqual(r, loads(dumps(r))) 591 self.assertEqual(r, loads(dumps(r)))
576 self.assertEqual(id(r), id(copy(r))) 592 self.assertEqual(id(r), id(copy(r)))
577 self.assertEqual(id(r), id(deepcopy(r))) 593 self.assertEqual(id(r), id(deepcopy(r)))
594 self.assertNotEqual(id(dr), id(copy(dr)))
595 self.assertNotEqual(id(dr), id(deepcopy(dr)))
596 self.assertTypedEquals(dr, copy(dr))
597 self.assertTypedEquals(dr, deepcopy(dr))
578 598
579 def test_slots(self): 599 def test_slots(self):
580 # Issue 4998 600 # Issue 4998
581 r = F(13, 7) 601 r = F(13, 7)
582 self.assertRaises(AttributeError, setattr, r, 'a', 10) 602 self.assertRaises(AttributeError, setattr, r, 'a', 10)
583 603
584 def test_main(): 604 def test_main():
585 run_unittest(FractionTest, GcdTest) 605 run_unittest(FractionTest, GcdTest)
586 606
587 if __name__ == '__main__': 607 if __name__ == '__main__':
588 test_main() 608 test_main()
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+