Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
the complex_pow part), r56649, r56652, r56715, r57296, r57302, r57359, r57361,
r57372, r57738, r57739, r58017, r58039, r58040, and r59390, and new
documentation. The only significant difference is that round(x) returns a float
to preserve backward-compatibility. See http://bugs.python.org/issue1689.
2008-01-02 22:21:52 -04:00
|
|
|
"""Unit tests for numbers.py."""
|
|
|
|
|
2008-02-01 02:22:46 -04:00
|
|
|
import math
|
|
|
|
import operator
|
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
the complex_pow part), r56649, r56652, r56715, r57296, r57302, r57359, r57361,
r57372, r57738, r57739, r58017, r58039, r58040, and r59390, and new
documentation. The only significant difference is that round(x) returns a float
to preserve backward-compatibility. See http://bugs.python.org/issue1689.
2008-01-02 22:21:52 -04:00
|
|
|
import unittest
|
|
|
|
from numbers import Complex, Real, Rational, Integral
|
2008-02-01 02:22:46 -04:00
|
|
|
from numbers import Number
|
|
|
|
from test import test_support
|
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
the complex_pow part), r56649, r56652, r56715, r57296, r57302, r57359, r57361,
r57372, r57738, r57739, r58017, r58039, r58040, and r59390, and new
documentation. The only significant difference is that round(x) returns a float
to preserve backward-compatibility. See http://bugs.python.org/issue1689.
2008-01-02 22:21:52 -04:00
|
|
|
|
|
|
|
class TestNumbers(unittest.TestCase):
|
|
|
|
def test_int(self):
|
|
|
|
self.failUnless(issubclass(int, Integral))
|
|
|
|
self.failUnless(issubclass(int, Complex))
|
|
|
|
|
|
|
|
self.assertEqual(7, int(7).real)
|
|
|
|
self.assertEqual(0, int(7).imag)
|
|
|
|
self.assertEqual(7, int(7).conjugate())
|
|
|
|
self.assertEqual(7, int(7).numerator)
|
|
|
|
self.assertEqual(1, int(7).denominator)
|
|
|
|
|
|
|
|
def test_long(self):
|
|
|
|
self.failUnless(issubclass(long, Integral))
|
|
|
|
self.failUnless(issubclass(long, Complex))
|
|
|
|
|
|
|
|
self.assertEqual(7, long(7).real)
|
|
|
|
self.assertEqual(0, long(7).imag)
|
|
|
|
self.assertEqual(7, long(7).conjugate())
|
|
|
|
self.assertEqual(7, long(7).numerator)
|
|
|
|
self.assertEqual(1, long(7).denominator)
|
|
|
|
|
|
|
|
def test_float(self):
|
|
|
|
self.failIf(issubclass(float, Rational))
|
|
|
|
self.failUnless(issubclass(float, Real))
|
|
|
|
|
|
|
|
self.assertEqual(7.3, float(7.3).real)
|
|
|
|
self.assertEqual(0, float(7.3).imag)
|
|
|
|
self.assertEqual(7.3, float(7.3).conjugate())
|
|
|
|
|
|
|
|
def test_complex(self):
|
|
|
|
self.failIf(issubclass(complex, Real))
|
|
|
|
self.failUnless(issubclass(complex, Complex))
|
|
|
|
|
|
|
|
c1, c2 = complex(3, 2), complex(4,1)
|
2008-02-01 02:22:46 -04:00
|
|
|
# XXX: This is not ideal, but see the comment in math_trunc().
|
|
|
|
self.assertRaises(AttributeError, math.trunc, c1)
|
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
the complex_pow part), r56649, r56652, r56715, r57296, r57302, r57359, r57361,
r57372, r57738, r57739, r58017, r58039, r58040, and r59390, and new
documentation. The only significant difference is that round(x) returns a float
to preserve backward-compatibility. See http://bugs.python.org/issue1689.
2008-01-02 22:21:52 -04:00
|
|
|
self.assertRaises(TypeError, float, c1)
|
|
|
|
self.assertRaises(TypeError, int, c1)
|
|
|
|
|
|
|
|
def test_main():
|
|
|
|
test_support.run_unittest(TestNumbers)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|