diff options
author | Guido van Rossum <guido@python.org> | 2008-01-15 21:44:53 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2008-01-15 21:44:53 (GMT) |
commit | 7736b5becd273cb271c55fcce9155677a381c4f6 (patch) | |
tree | 2c18c15c4ee0475ab6b1394a2e5cb18358ddbbbc /Lib/test | |
parent | ae138cbfbbfb376917fd29abb6724d56ba5fc081 (diff) | |
download | cpython-7736b5becd273cb271c55fcce9155677a381c4f6.zip cpython-7736b5becd273cb271c55fcce9155677a381c4f6.tar.gz cpython-7736b5becd273cb271c55fcce9155677a381c4f6.tar.bz2 |
Merged revisions 59952-59984 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r59952 | thomas.heller | 2008-01-14 02:35:28 -0800 (Mon, 14 Jan 2008) | 1 line
Issue 1821: configure libffi for amd64 on FreeeBSD.
........
r59953 | andrew.kuchling | 2008-01-14 06:48:43 -0800 (Mon, 14 Jan 2008) | 1 line
Update description of float_info
........
r59959 | raymond.hettinger | 2008-01-14 14:58:05 -0800 (Mon, 14 Jan 2008) | 1 line
Fix 1698398: Zipfile.printdir() crashed because the format string expected a tuple object of length six instead of a time.struct_time object.
........
r59961 | andrew.kuchling | 2008-01-14 17:29:16 -0800 (Mon, 14 Jan 2008) | 1 line
Typo fixes
........
r59962 | andrew.kuchling | 2008-01-14 17:29:44 -0800 (Mon, 14 Jan 2008) | 1 line
Markup fix
........
r59963 | andrew.kuchling | 2008-01-14 17:47:32 -0800 (Mon, 14 Jan 2008) | 1 line
Add many items
........
r59964 | andrew.kuchling | 2008-01-14 17:55:32 -0800 (Mon, 14 Jan 2008) | 1 line
Repair unfinished sentence
........
r59967 | raymond.hettinger | 2008-01-14 19:02:37 -0800 (Mon, 14 Jan 2008) | 5 lines
Issue 1820: structseq objects did not work with the % formatting operator or isinstance(t, tuple).
Orignal patch (without tests) by Leif Walsh.
........
r59968 | raymond.hettinger | 2008-01-14 19:07:42 -0800 (Mon, 14 Jan 2008) | 1 line
Tighten the definition of a named tuple.
........
r59969 | skip.montanaro | 2008-01-14 19:40:20 -0800 (Mon, 14 Jan 2008) | 3 lines
Better (?) text describing the lack of guarantees provided by qsize(),
empty() and full().
........
r59970 | raymond.hettinger | 2008-01-14 21:39:59 -0800 (Mon, 14 Jan 2008) | 1 line
Temporarily revert 59967 until GC can be added.
........
r59971 | raymond.hettinger | 2008-01-14 21:46:43 -0800 (Mon, 14 Jan 2008) | 1 line
Small grammar nit
........
r59972 | georg.brandl | 2008-01-14 22:55:56 -0800 (Mon, 14 Jan 2008) | 2 lines
Typo.
........
r59973 | georg.brandl | 2008-01-14 22:58:15 -0800 (Mon, 14 Jan 2008) | 2 lines
Remove duplicate entry.
........
r59974 | jeffrey.yasskin | 2008-01-14 23:46:24 -0800 (Mon, 14 Jan 2008) | 12 lines
Add rational.Rational as an implementation of numbers.Rational with infinite
precision. This has been discussed at http://bugs.python.org/issue1682. It's
useful primarily for teaching, but it also demonstrates how to implement a
member of the numeric tower, including fallbacks for mixed-mode arithmetic.
I expect to write a couple more patches in this area:
* Rational.from_decimal()
* Rational.trim/approximate() (maybe with different names)
* Maybe remove the parentheses from Rational.__str__()
* Maybe rename one of the Rational classes
* Maybe make Rational('3/2') work.
........
r59978 | andrew.kuchling | 2008-01-15 06:38:05 -0800 (Tue, 15 Jan 2008) | 8 lines
Restore description of sys.dont_write_bytecode.
The duplication is intentional -- this paragraph is in a section
describing additions to the sys module, and there's a later section
that mentions the switch. I think most people scan the what's-new and
don't read it in detail, so a bit of duplication is OK.
........
r59984 | guido.van.rossum | 2008-01-15 09:59:29 -0800 (Tue, 15 Jan 2008) | 3 lines
Issue #1786 (by myself): pdb should use its own stdin/stdout around an
exec call and when creating a recursive instance.
........
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_rational.py | 283 | ||||
-rw-r--r-- | Lib/test/test_sys.py | 2 |
2 files changed, 284 insertions, 1 deletions
diff --git a/Lib/test/test_rational.py b/Lib/test/test_rational.py new file mode 100644 index 0000000..a8e1737 --- /dev/null +++ b/Lib/test/test_rational.py @@ -0,0 +1,283 @@ +"""Tests for Lib/rational.py.""" + +from decimal import Decimal +from test.test_support import run_unittest, verbose +import math +import operator +import rational +import unittest +R = rational.Rational + +def _components(r): + return (r.numerator, r.denominator) + +class RationalTest(unittest.TestCase): + + def assertTypedEquals(self, expected, actual): + """Asserts that both the types and values are the same.""" + self.assertEquals(type(expected), type(actual)) + self.assertEquals(expected, actual) + + def assertRaisesMessage(self, exc_type, message, + callable, *args, **kwargs): + """Asserts that callable(*args, **kwargs) raises exc_type(message).""" + try: + callable(*args, **kwargs) + except exc_type as e: + self.assertEquals(message, str(e)) + else: + self.fail("%s not raised" % exc_type.__name__) + + def testInit(self): + self.assertEquals((0, 1), _components(R())) + self.assertEquals((7, 1), _components(R(7))) + self.assertEquals((7, 3), _components(R(R(7, 3)))) + + self.assertEquals((-1, 1), _components(R(-1, 1))) + self.assertEquals((-1, 1), _components(R(1, -1))) + self.assertEquals((1, 1), _components(R(-2, -2))) + self.assertEquals((1, 2), _components(R(5, 10))) + self.assertEquals((7, 15), _components(R(7, 15))) + self.assertEquals((10**23, 1), _components(R(10**23))) + + self.assertRaisesMessage(ZeroDivisionError, "Rational(12, 0)", + R, 12, 0) + self.assertRaises(TypeError, R, 1.5) + self.assertRaises(TypeError, R, 1.5 + 3j) + + def testFromFloat(self): + self.assertRaisesMessage( + TypeError, "Rational.from_float() only takes floats, not 3 (int)", + R.from_float, 3) + + self.assertEquals((0, 1), _components(R.from_float(-0.0))) + self.assertEquals((10, 1), _components(R.from_float(10.0))) + self.assertEquals((-5, 2), _components(R.from_float(-2.5))) + self.assertEquals((99999999999999991611392, 1), + _components(R.from_float(1e23))) + self.assertEquals(float(10**23), float(R.from_float(1e23))) + self.assertEquals((3602879701896397, 1125899906842624), + _components(R.from_float(3.2))) + self.assertEquals(3.2, float(R.from_float(3.2))) + + inf = 1e1000 + nan = inf - inf + self.assertRaisesMessage( + TypeError, "Cannot convert inf to Rational.", + R.from_float, inf) + self.assertRaisesMessage( + TypeError, "Cannot convert -inf to Rational.", + R.from_float, -inf) + self.assertRaisesMessage( + TypeError, "Cannot convert nan to Rational.", + R.from_float, nan) + + def testConversions(self): + self.assertTypedEquals(-1, trunc(R(-11, 10))) + self.assertTypedEquals(-2, R(-11, 10).__floor__()) + self.assertTypedEquals(-1, R(-11, 10).__ceil__()) + self.assertTypedEquals(-1, R(-10, 10).__ceil__()) + + self.assertTypedEquals(0, R(-1, 10).__round__()) + self.assertTypedEquals(0, R(-5, 10).__round__()) + self.assertTypedEquals(-2, R(-15, 10).__round__()) + self.assertTypedEquals(-1, R(-7, 10).__round__()) + + self.assertEquals(False, bool(R(0, 1))) + self.assertEquals(True, bool(R(3, 2))) + self.assertTypedEquals(0.1, float(R(1, 10))) + + # Check that __float__ isn't implemented by converting the + # numerator and denominator to float before dividing. + self.assertRaises(OverflowError, float, int('2'*400+'7')) + self.assertAlmostEquals(2.0/3, + float(R(int('2'*400+'7'), int('3'*400+'1')))) + + self.assertTypedEquals(0.1+0j, complex(R(1,10))) + + def testRound(self): + self.assertTypedEquals(R(-200), R(-150).__round__(-2)) + self.assertTypedEquals(R(-200), R(-250).__round__(-2)) + self.assertTypedEquals(R(30), R(26).__round__(-1)) + self.assertTypedEquals(R(-2, 10), R(-15, 100).__round__(1)) + self.assertTypedEquals(R(-2, 10), R(-25, 100).__round__(1)) + + + def testArithmetic(self): + self.assertEquals(R(1, 2), R(1, 10) + R(2, 5)) + self.assertEquals(R(-3, 10), R(1, 10) - R(2, 5)) + self.assertEquals(R(1, 25), R(1, 10) * R(2, 5)) + self.assertEquals(R(1, 4), R(1, 10) / R(2, 5)) + self.assertTypedEquals(2, R(9, 10) // R(2, 5)) + self.assertTypedEquals(10**23, R(10**23, 1) // R(1)) + self.assertEquals(R(2, 3), R(-7, 3) % R(3, 2)) + self.assertEquals(R(8, 27), R(2, 3) ** R(3)) + self.assertEquals(R(27, 8), R(2, 3) ** R(-3)) + self.assertTypedEquals(2.0, R(4) ** R(1, 2)) + z = pow(R(-1), R(1, 2)) + self.assertAlmostEquals(z.real, 0) + self.assertEquals(z.imag, 1) + + def testMixedArithmetic(self): + self.assertTypedEquals(R(11, 10), R(1, 10) + 1) + self.assertTypedEquals(1.1, R(1, 10) + 1.0) + self.assertTypedEquals(1.1 + 0j, R(1, 10) + (1.0 + 0j)) + self.assertTypedEquals(R(11, 10), 1 + R(1, 10)) + self.assertTypedEquals(1.1, 1.0 + R(1, 10)) + self.assertTypedEquals(1.1 + 0j, (1.0 + 0j) + R(1, 10)) + + self.assertTypedEquals(R(-9, 10), R(1, 10) - 1) + self.assertTypedEquals(-0.9, R(1, 10) - 1.0) + self.assertTypedEquals(-0.9 + 0j, R(1, 10) - (1.0 + 0j)) + self.assertTypedEquals(R(9, 10), 1 - R(1, 10)) + self.assertTypedEquals(0.9, 1.0 - R(1, 10)) + self.assertTypedEquals(0.9 + 0j, (1.0 + 0j) - R(1, 10)) + + self.assertTypedEquals(R(1, 10), R(1, 10) * 1) + self.assertTypedEquals(0.1, R(1, 10) * 1.0) + self.assertTypedEquals(0.1 + 0j, R(1, 10) * (1.0 + 0j)) + self.assertTypedEquals(R(1, 10), 1 * R(1, 10)) + self.assertTypedEquals(0.1, 1.0 * R(1, 10)) + self.assertTypedEquals(0.1 + 0j, (1.0 + 0j) * R(1, 10)) + + self.assertTypedEquals(R(1, 10), R(1, 10) / 1) + self.assertTypedEquals(0.1, R(1, 10) / 1.0) + self.assertTypedEquals(0.1 + 0j, R(1, 10) / (1.0 + 0j)) + self.assertTypedEquals(R(10, 1), 1 / R(1, 10)) + self.assertTypedEquals(10.0, 1.0 / R(1, 10)) + self.assertTypedEquals(10.0 + 0j, (1.0 + 0j) / R(1, 10)) + + self.assertTypedEquals(0, R(1, 10) // 1) + # XXX Jeffrey: why does this fail? + ##self.assertTypedEquals(0.0, R(1, 10) // 1.0) + self.assertTypedEquals(10, 1 // R(1, 10)) + self.assertTypedEquals(10**23, 10**22 // R(1, 10)) + # XXX Jeffrey: why does this fail? + ##self.assertTypedEquals(10.0, 1.0 // R(1, 10)) + + self.assertTypedEquals(R(1, 10), R(1, 10) % 1) + self.assertTypedEquals(0.1, R(1, 10) % 1.0) + self.assertTypedEquals(R(0, 1), 1 % R(1, 10)) + self.assertTypedEquals(0.0, 1.0 % R(1, 10)) + + # No need for divmod since we don't override it. + + # ** has more interesting conversion rules. + self.assertTypedEquals(R(100, 1), R(1, 10) ** -2) + self.assertTypedEquals(R(100, 1), R(10, 1) ** 2) + self.assertTypedEquals(0.1, R(1, 10) ** 1.0) + self.assertTypedEquals(0.1 + 0j, R(1, 10) ** (1.0 + 0j)) + self.assertTypedEquals(4 , 2 ** R(2, 1)) + z = pow(-1, R(1, 2)) + self.assertAlmostEquals(0, z.real) + self.assertEquals(1, z.imag) + self.assertTypedEquals(R(1, 4) , 2 ** R(-2, 1)) + self.assertTypedEquals(2.0 , 4 ** R(1, 2)) + self.assertTypedEquals(0.25, 2.0 ** R(-2, 1)) + self.assertTypedEquals(1.0 + 0j, (1.0 + 0j) ** R(1, 10)) + + def testMixingWithDecimal(self): + """Decimal refuses mixed comparisons.""" + self.assertRaisesMessage( + TypeError, + "unsupported operand type(s) for +: 'Rational' and 'Decimal'", + operator.add, R(3,11), Decimal('3.1415926')) + self.assertNotEquals(R(5, 2), Decimal('2.5')) + + def testComparisons(self): + self.assertTrue(R(1, 2) < R(2, 3)) + self.assertFalse(R(1, 2) < R(1, 2)) + self.assertTrue(R(1, 2) <= R(2, 3)) + self.assertTrue(R(1, 2) <= R(1, 2)) + self.assertFalse(R(2, 3) <= R(1, 2)) + self.assertTrue(R(1, 2) == R(1, 2)) + self.assertFalse(R(1, 2) == R(1, 3)) + + def testMixedLess(self): + self.assertTrue(2 < R(5, 2)) + self.assertFalse(2 < R(4, 2)) + self.assertTrue(R(5, 2) < 3) + self.assertFalse(R(4, 2) < 2) + + self.assertTrue(R(1, 2) < 0.6) + self.assertFalse(R(1, 2) < 0.4) + self.assertTrue(0.4 < R(1, 2)) + self.assertFalse(0.5 < R(1, 2)) + + def testMixedLessEqual(self): + self.assertTrue(0.5 <= R(1, 2)) + self.assertFalse(0.6 <= R(1, 2)) + self.assertTrue(R(1, 2) <= 0.5) + self.assertFalse(R(1, 2) <= 0.4) + self.assertTrue(2 <= R(4, 2)) + self.assertFalse(2 <= R(3, 2)) + self.assertTrue(R(4, 2) <= 2) + self.assertFalse(R(5, 2) <= 2) + + def testBigFloatComparisons(self): + # Because 10**23 can't be represented exactly as a float: + self.assertFalse(R(10**23) == float(10**23)) + # The first test demonstrates why these are important. + self.assertFalse(1e23 < float(R(trunc(1e23) + 1))) + self.assertTrue(1e23 < R(trunc(1e23) + 1)) + self.assertFalse(1e23 <= R(trunc(1e23) - 1)) + self.assertTrue(1e23 > R(trunc(1e23) - 1)) + self.assertFalse(1e23 >= R(trunc(1e23) + 1)) + + def testBigComplexComparisons(self): + self.assertFalse(R(10**23) == complex(10**23)) + self.assertTrue(R(10**23) > complex(10**23)) + self.assertFalse(R(10**23) <= complex(10**23)) + + def testMixedEqual(self): + self.assertTrue(0.5 == R(1, 2)) + self.assertFalse(0.6 == R(1, 2)) + self.assertTrue(R(1, 2) == 0.5) + self.assertFalse(R(1, 2) == 0.4) + self.assertTrue(2 == R(4, 2)) + self.assertFalse(2 == R(3, 2)) + self.assertTrue(R(4, 2) == 2) + self.assertFalse(R(5, 2) == 2) + + def testStringification(self): + self.assertEquals("rational.Rational(7,3)", repr(R(7, 3))) + self.assertEquals("(7/3)", str(R(7, 3))) + self.assertEquals("7", str(R(7, 1))) + + def testHash(self): + self.assertEquals(hash(2.5), hash(R(5, 2))) + self.assertEquals(hash(10**50), hash(R(10**50))) + self.assertNotEquals(hash(float(10**23)), hash(R(10**23))) + + def testApproximatePi(self): + # Algorithm borrowed from + # http://docs.python.org/lib/decimal-recipes.html + three = R(3) + lasts, t, s, n, na, d, da = 0, three, 3, 1, 0, 0, 24 + while abs(s - lasts) > R(1, 10**9): + lasts = s + n, na = n+na, na+8 + d, da = d+da, da+32 + t = (t * n) / d + s += t + self.assertAlmostEquals(math.pi, s) + + def testApproximateCos1(self): + # Algorithm borrowed from + # http://docs.python.org/lib/decimal-recipes.html + x = R(1) + i, lasts, s, fact, num, sign = 0, 0, R(1), 1, 1, 1 + while abs(s - lasts) > R(1, 10**9): + lasts = s + i += 2 + fact *= i * (i-1) + num *= x * x + sign *= -1 + s += num / fact * sign + self.assertAlmostEquals(math.cos(1), s) + +def test_main(): + run_unittest(RationalTest) + +if __name__ == '__main__': + test_main() diff --git a/Lib/test/test_sys.py b/Lib/test/test_sys.py index 264544d..8979889 100644 --- a/Lib/test/test_sys.py +++ b/Lib/test/test_sys.py @@ -324,7 +324,7 @@ class SysModuleTest(unittest.TestCase): self.failUnless(sys.flags) attrs = ("debug", "division_warning", "inspect", "interactive", "optimize", "dont_write_bytecode", - "no_site", "ingnore_environment", "tabcheck", "verbose") + "no_site", "ignore_environment", "tabcheck", "verbose") for attr in attrs: self.assert_(hasattr(sys.flags, attr), attr) self.assertEqual(type(getattr(sys.flags, attr)), int, attr) |