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