1"""Unit tests for numbers.py.""" 2 3import math 4import operator 5import unittest 6from numbers import Complex, Real, Rational, Integral 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).conjugate()) 17 self.assertEqual(7, int(7).numerator) 18 self.assertEqual(1, int(7).denominator) 19 20 def test_float(self): 21 self.assertFalse(issubclass(float, Rational)) 22 self.assertTrue(issubclass(float, Real)) 23 24 self.assertEqual(7.3, float(7.3).real) 25 self.assertEqual(0, float(7.3).imag) 26 self.assertEqual(7.3, float(7.3).conjugate()) 27 self.assertEqual(-7.3, float(-7.3).conjugate()) 28 29 def test_complex(self): 30 self.assertFalse(issubclass(complex, Real)) 31 self.assertTrue(issubclass(complex, Complex)) 32 33 c1, c2 = complex(3, 2), complex(4,1) 34 # XXX: This is not ideal, but see the comment in math_trunc(). 35 self.assertRaises(TypeError, math.trunc, c1) 36 self.assertRaises(TypeError, operator.mod, c1, c2) 37 self.assertRaises(TypeError, divmod, c1, c2) 38 self.assertRaises(TypeError, operator.floordiv, c1, c2) 39 self.assertRaises(TypeError, float, c1) 40 self.assertRaises(TypeError, int, c1) 41 42 43if __name__ == "__main__": 44 unittest.main() 45