PageRenderTime 571ms CodeModel.GetById 212ms app.highlight 85ms RepoModel.GetById 269ms app.codeStats 0ms

/Lib/test/test_abstract_numbers.py

http://unladen-swallow.googlecode.com/
Python | 54 lines | 40 code | 12 blank | 2 comment | 1 complexity | 3ac0139e29b29e0dbbfc2c32d8fb2f6f MD5 | raw file
 1"""Unit tests for numbers.py."""
 2
 3import math
 4import operator
 5import unittest
 6from numbers import Complex, Real, Rational, Integral
 7from numbers import Number
 8from test import test_support
 9
10class TestNumbers(unittest.TestCase):
11    def test_int(self):
12        self.failUnless(issubclass(int, Integral))
13        self.failUnless(issubclass(int, Complex))
14
15        self.assertEqual(7, int(7).real)
16        self.assertEqual(0, int(7).imag)
17        self.assertEqual(7, int(7).conjugate())
18        self.assertEqual(7, int(7).numerator)
19        self.assertEqual(1, int(7).denominator)
20
21    def test_long(self):
22        self.failUnless(issubclass(long, Integral))
23        self.failUnless(issubclass(long, Complex))
24
25        self.assertEqual(7, long(7).real)
26        self.assertEqual(0, long(7).imag)
27        self.assertEqual(7, long(7).conjugate())
28        self.assertEqual(7, long(7).numerator)
29        self.assertEqual(1, long(7).denominator)
30
31    def test_float(self):
32        self.failIf(issubclass(float, Rational))
33        self.failUnless(issubclass(float, Real))
34
35        self.assertEqual(7.3, float(7.3).real)
36        self.assertEqual(0, float(7.3).imag)
37        self.assertEqual(7.3, float(7.3).conjugate())
38
39    def test_complex(self):
40        self.failIf(issubclass(complex, Real))
41        self.failUnless(issubclass(complex, Complex))
42
43        c1, c2 = complex(3, 2), complex(4,1)
44        # XXX: This is not ideal, but see the comment in math_trunc().
45        self.assertRaises(AttributeError, math.trunc, c1)
46        self.assertRaises(TypeError, float, c1)
47        self.assertRaises(TypeError, int, c1)
48
49def test_main():
50    test_support.run_unittest(TestNumbers)
51
52
53if __name__ == "__main__":
54    unittest.main()