1 | n/a | """Unit tests for numbers.py.""" |
---|

2 | n/a | |
---|

3 | n/a | import math |
---|

4 | n/a | import operator |
---|

5 | n/a | import unittest |
---|

6 | n/a | from numbers import Complex, Real, Rational, Integral |
---|

7 | n/a | |
---|

8 | n/a | class TestNumbers(unittest.TestCase): |
---|

9 | n/a | def test_int(self): |
---|

10 | n/a | self.assertTrue(issubclass(int, Integral)) |
---|

11 | n/a | self.assertTrue(issubclass(int, Complex)) |
---|

12 | n/a | |
---|

13 | n/a | self.assertEqual(7, int(7).real) |
---|

14 | n/a | self.assertEqual(0, int(7).imag) |
---|

15 | n/a | self.assertEqual(7, int(7).conjugate()) |
---|

16 | n/a | self.assertEqual(-7, int(-7).conjugate()) |
---|

17 | n/a | self.assertEqual(7, int(7).numerator) |
---|

18 | n/a | self.assertEqual(1, int(7).denominator) |
---|

19 | n/a | |
---|

20 | n/a | def test_float(self): |
---|

21 | n/a | self.assertFalse(issubclass(float, Rational)) |
---|

22 | n/a | self.assertTrue(issubclass(float, Real)) |
---|

23 | n/a | |
---|

24 | n/a | self.assertEqual(7.3, float(7.3).real) |
---|

25 | n/a | self.assertEqual(0, float(7.3).imag) |
---|

26 | n/a | self.assertEqual(7.3, float(7.3).conjugate()) |
---|

27 | n/a | self.assertEqual(-7.3, float(-7.3).conjugate()) |
---|

28 | n/a | |
---|

29 | n/a | def test_complex(self): |
---|

30 | n/a | self.assertFalse(issubclass(complex, Real)) |
---|

31 | n/a | self.assertTrue(issubclass(complex, Complex)) |
---|

32 | n/a | |
---|

33 | n/a | c1, c2 = complex(3, 2), complex(4,1) |
---|

34 | n/a | # XXX: This is not ideal, but see the comment in math_trunc(). |
---|

35 | n/a | self.assertRaises(TypeError, math.trunc, c1) |
---|

36 | n/a | self.assertRaises(TypeError, operator.mod, c1, c2) |
---|

37 | n/a | self.assertRaises(TypeError, divmod, c1, c2) |
---|

38 | n/a | self.assertRaises(TypeError, operator.floordiv, c1, c2) |
---|

39 | n/a | self.assertRaises(TypeError, float, c1) |
---|

40 | n/a | self.assertRaises(TypeError, int, c1) |
---|

41 | n/a | |
---|

42 | n/a | |
---|

43 | n/a | if __name__ == "__main__": |
---|

44 | n/a | unittest.main() |
---|