diff --git a/Lib/test/test_abstract_numbers.py b/Lib/test/test_abstract_numbers.py index 2e06f0d16fdd05..f8075339fd42a2 100644 --- a/Lib/test/test_abstract_numbers.py +++ b/Lib/test/test_abstract_numbers.py @@ -3,6 +3,7 @@ import math import operator import unittest +from random import random from numbers import Complex, Real, Rational, Integral class TestNumbers(unittest.TestCase): @@ -40,5 +41,92 @@ def test_complex(self): self.assertRaises(TypeError, int, c1) +class TestAbstractNumbers(unittest.TestCase): + def testComplex(self): + + class SubComplex(Complex): + def __complex__(self): + pass + + @property + def real(self): + return self._imag + + @property + def imag(self): + return self._real + + def __add__(self, other): + pass + + def __radd__(self, other): + pass + + def __neg__(self): + pass + + def __pos__(self): + pass + + def __mul__(self, other): + pass + + def __rmul__(self, other): + pass + + def __truediv__(self, other): + pass + + def __rtruediv__(self, other): + pass + + def __pow__(self, exponent): + pass + + def __rpow__(self, base): + pass + + def __abs__(self): + pass + + def conjugate(self): + pass + + def __init__(self, r, i): + self.real = r + self.imag = i + + def __eq__(self, other): + return isinstance(other, SubComplex) and self.imag == other.imag and self.real == other.real + + @imag.setter + def imag(self, value): + self._imag = value + + @real.setter + def real(self, value): + self._real = value + + sc = SubComplex(0,0) + self.assertIsInstance(sc, Complex) + + nonInstances = [None, "hat", lambda x: x + 1] + + for nonInstance in nonInstances: + self.assertNotIsInstance(nonInstance, Complex) + + self.assertFalse(SubComplex.__bool__(0)) + for i in range(100): + self.assertTrue(SubComplex.__bool__(random() + 1e-6)) + x = random() + y = random() + self.assertEqual(SubComplex.__sub__(x, y), x - y) + self.assertEqual(SubComplex.__rsub__(x, y), - x + y) + sc1 = SubComplex(x, y) + sc2 = SubComplex(x, y) + self.assertEqual(sc1, sc2) + self.assertNotEqual(x, y + 1e-6) + + if __name__ == "__main__": unittest.main()