| 1 |
"""Unit tests for numbers.py.""" |
| 2 |
|
| 3 |
import abc |
| 4 |
import math |
| 5 |
import operator |
| 6 |
import unittest |
| 7 |
from numbers import Complex, Real, Rational, Integral, Number |
| 8 |
|
| 9 |
|
| 10 |
def concretize(cls): |
| 11 |
def not_implemented(*args, **kwargs): |
| 12 |
raise NotImplementedError() |
| 13 |
|
| 14 |
for name in dir(cls): |
| 15 |
try: |
| 16 |
value = getattr(cls, name) |
| 17 |
if value.__isabstractmethod__: |
| 18 |
setattr(cls, name, not_implemented) |
| 19 |
except AttributeError: |
| 20 |
pass |
| 21 |
abc.update_abstractmethods(cls) |
| 22 |
return cls |
| 23 |
|
| 24 |
|
| 25 |
class TestNumbers(unittest.TestCase): |
| 26 |
def test_int(self): |
| 27 |
self.assertIsSubclass(int, Integral) |
| 28 |
self.assertIsSubclass(int, Rational) |
| 29 |
self.assertIsSubclass(int, Real) |
| 30 |
self.assertIsSubclass(int, Complex) |
| 31 |
self.assertIsSubclass(int, Number) |
| 32 |
|
| 33 |
self.assertEqual(7, int(7).real) |
| 34 |
self.assertEqual(0, int(7).imag) |
| 35 |
self.assertEqual(7, int(7).conjugate()) |
| 36 |
self.assertEqual(-7, int(-7).conjugate()) |
| 37 |
self.assertEqual(7, int(7).numerator) |
| 38 |
self.assertEqual(1, int(7).denominator) |
| 39 |
|
| 40 |
def test_float(self): |
| 41 |
self.assertNotIsSubclass(float, Integral) |
| 42 |
self.assertNotIsSubclass(float, Rational) |
| 43 |
self.assertIsSubclass(float, Real) |
| 44 |
self.assertIsSubclass(float, Complex) |
| 45 |
self.assertIsSubclass(float, Number) |
| 46 |
|
| 47 |
self.assertEqual(7.3, float(7.3).real) |
| 48 |
self.assertEqual(0, float(7.3).imag) |
| 49 |
self.assertEqual(7.3, float(7.3).conjugate()) |
| 50 |
self.assertEqual(-7.3, float(-7.3).conjugate()) |
| 51 |
|
| 52 |
def test_complex(self): |
| 53 |
self.assertNotIsSubclass(complex, Integral) |
| 54 |
self.assertNotIsSubclass(complex, Rational) |
| 55 |
self.assertNotIsSubclass(complex, Real) |
| 56 |
self.assertIsSubclass(complex, Complex) |
| 57 |
self.assertIsSubclass(complex, Number) |
| 58 |
|
| 59 |
c1, c2 = complex(3, 2), complex(4,1) |
| 60 |
# XXX: This is not ideal, but see the comment in math_trunc(). |
| 61 |
self.assertRaises(TypeError, math.trunc, c1) |
| 62 |
self.assertRaises(TypeError, operator.mod, c1, c2) |
| 63 |
self.assertRaises(TypeError, divmod, c1, c2) |
| 64 |
self.assertRaises(TypeError, operator.floordiv, c1, c2) |
| 65 |
self.assertRaises(TypeError, float, c1) |
| 66 |
self.assertRaises(TypeError, int, c1) |
| 67 |
|
| 68 |
|
| 69 |
class TestNumbersDefaultMethods(unittest.TestCase): |
| 70 |
def test_complex(self): |
| 71 |
@concretize |
| 72 |
class MyComplex(Complex): |
| 73 |
def __init__(self, real, imag): |
| 74 |
self.r = real |
| 75 |
self.i = imag |
| 76 |
|
| 77 |
@property |
| 78 |
def real(self): |
| 79 |
return self.r |
| 80 |
|
| 81 |
@property |
| 82 |
def imag(self): |
| 83 |
return self.i |
| 84 |
|
| 85 |
def __add__(self, other): |
| 86 |
if isinstance(other, Complex): |
| 87 |
return MyComplex(self.imag + other.imag, |
| 88 |
self.real + other.real) |
| 89 |
raise NotImplementedError |
| 90 |
|
| 91 |
def __neg__(self): |
| 92 |
return MyComplex(-self.real, -self.imag) |
| 93 |
|
| 94 |
def __eq__(self, other): |
| 95 |
if isinstance(other, Complex): |
| 96 |
return self.imag == other.imag and self.real == other.real |
| 97 |
if isinstance(other, Number): |
| 98 |
return self.imag == 0 and self.real == other.real |
| 99 |
|
| 100 |
# test __bool__ |
| 101 |
self.assertTrue(bool(MyComplex(1, 1))) |
| 102 |
self.assertTrue(bool(MyComplex(0, 1))) |
| 103 |
self.assertTrue(bool(MyComplex(1, 0))) |
| 104 |
self.assertFalse(bool(MyComplex(0, 0))) |
| 105 |
|
| 106 |
# test __sub__ |
| 107 |
self.assertEqual(MyComplex(2, 3) - complex(1, 2), MyComplex(1, 1)) |
| 108 |
|
| 109 |
# test __rsub__ |
| 110 |
self.assertEqual(complex(2, 3) - MyComplex(1, 2), MyComplex(1, 1)) |
| 111 |
|
| 112 |
def test_real(self): |
| 113 |
@concretize |
| 114 |
class MyReal(Real): |
| 115 |
def __init__(self, n): |
| 116 |
self.n = n |
| 117 |
|
| 118 |
def __pos__(self): |
| 119 |
return self.n |
| 120 |
|
| 121 |
def __float__(self): |
| 122 |
return float(self.n) |
| 123 |
|
| 124 |
def __floordiv__(self, other): |
| 125 |
return self.n // other |
| 126 |
|
| 127 |
def __rfloordiv__(self, other): |
| 128 |
return other // self.n |
| 129 |
|
| 130 |
def __mod__(self, other): |
| 131 |
return self.n % other |
| 132 |
|
| 133 |
def __rmod__(self, other): |
| 134 |
return other % self.n |
| 135 |
|
| 136 |
# test __divmod__ |
| 137 |
self.assertEqual(divmod(MyReal(3), 2), (1, 1)) |
| 138 |
|
| 139 |
# test __rdivmod__ |
| 140 |
self.assertEqual(divmod(3, MyReal(2)), (1, 1)) |
| 141 |
|
| 142 |
# test __complex__ |
| 143 |
self.assertEqual(complex(MyReal(1)), 1+0j) |
| 144 |
|
| 145 |
# test real |
| 146 |
self.assertEqual(MyReal(3).real, 3) |
| 147 |
|
| 148 |
# test imag |
| 149 |
self.assertEqual(MyReal(3).imag, 0) |
| 150 |
|
| 151 |
# test conjugate |
| 152 |
self.assertEqual(MyReal(123).conjugate(), 123) |
| 153 |
|
| 154 |
|
| 155 |
def test_rational(self): |
| 156 |
@concretize |
| 157 |
class MyRational(Rational): |
| 158 |
def __init__(self, numerator, denominator): |
| 159 |
self.n = numerator |
| 160 |
self.d = denominator |
| 161 |
|
| 162 |
@property |
| 163 |
def numerator(self): |
| 164 |
return self.n |
| 165 |
|
| 166 |
@property |
| 167 |
def denominator(self): |
| 168 |
return self.d |
| 169 |
|
| 170 |
# test__float__ |
| 171 |
self.assertEqual(float(MyRational(5, 2)), 2.5) |
| 172 |
|
| 173 |
|
| 174 |
def test_integral(self): |
| 175 |
@concretize |
| 176 |
class MyIntegral(Integral): |
| 177 |
def __init__(self, n): |
| 178 |
self.n = n |
| 179 |
|
| 180 |
def __pos__(self): |
| 181 |
return self.n |
| 182 |
|
| 183 |
def __int__(self): |
| 184 |
return self.n |
| 185 |
|
| 186 |
# test __index__ |
| 187 |
self.assertEqual(operator.index(MyIntegral(123)), 123) |
| 188 |
|
| 189 |
# test __float__ |
| 190 |
self.assertEqual(float(MyIntegral(123)), 123.0) |
| 191 |
|
| 192 |
# test numerator |
| 193 |
self.assertEqual(MyIntegral(123).numerator, 123) |
| 194 |
|
| 195 |
# test denominator |
| 196 |
self.assertEqual(MyIntegral(123).denominator, 1) |
| 197 |
|
| 198 |
|
| 199 |
if __name__ == "__main__": |
| 200 |
unittest.main() |