| 1 |
|
| 2 |
import os |
| 3 |
import sys |
| 4 |
from random import random |
| 5 |
from math import isnan, copysign |
| 6 |
|
| 7 |
|
| 8 |
def check_div(x, y): |
| 9 |
"""Compute complex z=x*y, and check that z/x==y and z/y==x.""" |
| 10 |
z = x * y |
| 11 |
if x != 0: |
| 12 |
q = z / x |
| 13 |
assert close(q, y) |
| 14 |
q = z.__truediv__(x) |
| 15 |
assert close(q, y) |
| 16 |
if y != 0: |
| 17 |
q = z / y |
| 18 |
assert close(q, x) |
| 19 |
q = z.__truediv__(y) |
| 20 |
assert close(q, x) |
| 21 |
|
| 22 |
def close(x, y): |
| 23 |
"""Return true iff complexes x and y "are close\"""" |
| 24 |
return close_abs(x.real, y.real) and close_abs(x.imag, y.imag) |
| 25 |
|
| 26 |
def close_abs(x, y, eps=1e-9): |
| 27 |
"""Return true iff floats x and y "are close\"""" |
| 28 |
|
| 29 |
if abs(x) > abs(y): |
| 30 |
x, y = y, x |
| 31 |
if y == 0: |
| 32 |
return abs(x) < eps |
| 33 |
if x == 0: |
| 34 |
return abs(y) < eps |
| 35 |
|
| 36 |
return abs((x - y) / y) < eps |
| 37 |
|
| 38 |
def almost_equal(a, b, eps=1e-9): |
| 39 |
if isinstance(a, complex): |
| 40 |
if isinstance(b, complex): |
| 41 |
return a.real - b.real < eps and a.imag - b.imag < eps |
| 42 |
else: |
| 43 |
return a.real - b < eps and a.imag < eps |
| 44 |
else: |
| 45 |
if isinstance(b, complex): |
| 46 |
return a - b.real < eps and b.imag < eps |
| 47 |
else: |
| 48 |
return a - b < eps |
| 49 |
|
| 50 |
def floats_identical(x, y): |
| 51 |
msg = 'floats {!r} and {!r} are not identical' |
| 52 |
|
| 53 |
if isnan(x) or isnan(y): |
| 54 |
if isnan(x) and isnan(y): |
| 55 |
return |
| 56 |
elif x == y: |
| 57 |
if x != 0.0: |
| 58 |
return |
| 59 |
|
| 60 |
elif copysign(1.0, x) == copysign(1.0, y): |
| 61 |
return |
| 62 |
else: |
| 63 |
msg += ': zeros have different signs' |
| 64 |
assert False, msg.format(x, y) |
| 65 |
|
| 66 |
def test_div(): |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
simple_real = [-2.0, 0.0, 1.0] |
| 71 |
simple_complex = [complex(x, y) for x in simple_real for y in simple_real] |
| 72 |
for x in simple_complex: |
| 73 |
for y in simple_complex: |
| 74 |
check_div(x, y) |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
check_div(complex(1e200, 1e200), 1+0j) |
| 79 |
check_div(complex(1e-200, 1e-200), 1+0j) |
| 80 |
|
| 81 |
|
| 82 |
for i in range(100): |
| 83 |
check_div(complex(random(), random()), complex(random(), random())) |
| 84 |
|
| 85 |
raises(ZeroDivisionError, complex.__truediv__, 1+1j, 0+0j) |
| 86 |
|
| 87 |
raises(OverflowError, pow, 1e200+1j, 1e200+1j) |
| 88 |
|
| 89 |
def test_truediv(): |
| 90 |
assert almost_equal(complex.__truediv__(2+0j, 1+1j), 1-1j) |
| 91 |
raises(ZeroDivisionError, complex.__truediv__, 1+1j, 0+0j) |
| 92 |
|
| 93 |
def test_floordiv(): |
| 94 |
with raises(TypeError): |
| 95 |
3+0j // 0+0j |
| 96 |
|
| 97 |
def test_convert(): |
| 98 |
exc = raises(TypeError, complex.__int__, 3j) |
| 99 |
assert str(exc.value) == "can't convert complex to int" |
| 100 |
exc = raises(TypeError, complex.__float__, 3j) |
| 101 |
assert str(exc.value) == "can't convert complex to float" |
| 102 |
|
| 103 |
def test_richcompare(): |
| 104 |
import operator |
| 105 |
assert complex.__lt__(1+1j, None) is NotImplemented |
| 106 |
assert complex.__eq__(1+1j, 2+2j) is False |
| 107 |
assert complex.__eq__(1+1j, 1+1j) is True |
| 108 |
assert complex.__ne__(1+1j, 1+1j) is False |
| 109 |
assert complex.__ne__(1+1j, 2+2j) is True |
| 110 |
assert complex.__lt__(1+1j, 2+2j) is NotImplemented |
| 111 |
assert complex.__le__(1+1j, 2+2j) is NotImplemented |
| 112 |
assert complex.__gt__(1+1j, 2+2j) is NotImplemented |
| 113 |
assert complex.__ge__(1+1j, 2+2j) is NotImplemented |
| 114 |
raises(TypeError, operator.lt, 1+1j, 2+2j) |
| 115 |
raises(TypeError, operator.le, 1+1j, 2+2j) |
| 116 |
raises(TypeError, operator.gt, 1+1j, 2+2j) |
| 117 |
raises(TypeError, operator.ge, 1+1j, 2+2j) |
| 118 |
large = 1 << 10000 |
| 119 |
assert not (5+0j) == large |
| 120 |
assert not large == (5+0j) |
| 121 |
assert (5+0j) != large |
| 122 |
assert large != (5+0j) |
| 123 |
|
| 124 |
def test_richcompare_numbers(): |
| 125 |
for n in 8, 0.01: |
| 126 |
assert complex.__eq__(n+0j, n) |
| 127 |
assert not complex.__ne__(n+0j, n) |
| 128 |
assert not complex.__eq__(complex(n, n), n) |
| 129 |
assert complex.__ne__(complex(n, n), n) |
| 130 |
assert complex.__lt__(n+0j, n) is NotImplemented |
| 131 |
|
| 132 |
def test_richcompare_boundaries(): |
| 133 |
z = 9007199254740992+0j |
| 134 |
i = 9007199254740993 |
| 135 |
assert not complex.__eq__(z, i) |
| 136 |
assert complex.__ne__(z, i) |
| 137 |
|
| 138 |
def test_mod(): |
| 139 |
a = 3.33+4.43j |
| 140 |
with raises(TypeError): |
| 141 |
a % a |
| 142 |
|
| 143 |
def test_divmod(): |
| 144 |
raises(TypeError, divmod, 1+1j, 0+0j) |
| 145 |
|
| 146 |
def test_pow(): |
| 147 |
assert almost_equal(pow(1+1j, 0+0j), 1.0) |
| 148 |
assert almost_equal(pow(0+0j, 2+0j), 0.0) |
| 149 |
raises(ZeroDivisionError, pow, 0+0j, 1j) |
| 150 |
assert almost_equal(pow(1j, -1), 1/1j) |
| 151 |
assert almost_equal(pow(1j, 200), 1) |
| 152 |
raises(ValueError, pow, 1+1j, 1+1j, 1+1j) |
| 153 |
|
| 154 |
a = 3.33+4.43j |
| 155 |
assert a ** 0j == 1 |
| 156 |
assert a ** 0.+0.j == 1 |
| 157 |
|
| 158 |
assert 3j ** 0j == 1 |
| 159 |
assert 3j ** 0 == 1 |
| 160 |
|
| 161 |
with raises(ZeroDivisionError): |
| 162 |
0j ** a |
| 163 |
with raises(ZeroDivisionError): |
| 164 |
0j ** (3-2j) |
| 165 |
|
| 166 |
|
| 167 |
assert a ** 105 == a ** 105 |
| 168 |
assert a ** -105 == a ** -105 |
| 169 |
assert a ** -30 == a ** -30 |
| 170 |
assert a ** 2 == a * a |
| 171 |
|
| 172 |
assert 0.0j ** 0 == 1 |
| 173 |
|
| 174 |
b = 5.1+2.3j |
| 175 |
raises(ValueError, pow, a, b, 0) |
| 176 |
|
| 177 |
raises(ZeroDivisionError, pow, complex(float('inf'), 0.0), complex(10., 3.)) |
| 178 |
|
| 179 |
|
| 180 |
b = pow(1, -1.j) |
| 181 |
assert repr(b.imag) == "-0.0" |
| 182 |
|
| 183 |
|
| 184 |
def test_boolcontext(): |
| 185 |
for i in range(100): |
| 186 |
assert complex(random() + 1e-6, random() + 1e-6) |
| 187 |
assert not complex(0.0, 0.0) |
| 188 |
|
| 189 |
def test_conjugate(): |
| 190 |
assert close(complex(5.3, 9.8).conjugate(), 5.3-9.8j) |
| 191 |
|
| 192 |
def test_constructor(): |
| 193 |
class NS(object): |
| 194 |
def __init__(self, value): |
| 195 |
self.value = value |
| 196 |
def __complex__(self): |
| 197 |
return self.value |
| 198 |
assert complex(NS(1+10j)) == 1+10j |
| 199 |
assert complex(NS(1+10j), 5) == 1+15j |
| 200 |
assert complex(NS(1+10j), 5j) == -4+10j |
| 201 |
raises(TypeError, complex, NS(2.0)) |
| 202 |
raises(TypeError, complex, NS(2)) |
| 203 |
raises(TypeError, complex, NS(None)) |
| 204 |
raises(TypeError, complex, b'10') |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
class F(object): |
| 211 |
def __float__(self): |
| 212 |
return 2.0 |
| 213 |
assert complex(NS(1+10j), F()) == 1+12j |
| 214 |
|
| 215 |
assert almost_equal(complex("1+10j"), 1+10j) |
| 216 |
assert almost_equal(complex(10), 10+0j) |
| 217 |
assert almost_equal(complex(10.0), 10+0j) |
| 218 |
assert almost_equal(complex(10+0j), 10+0j) |
| 219 |
assert almost_equal(complex(1,10), 1+10j) |
| 220 |
assert almost_equal(complex(1,10.0), 1+10j) |
| 221 |
assert almost_equal(complex(1.0,10), 1+10j) |
| 222 |
assert almost_equal(complex(1.0,10.0), 1+10j) |
| 223 |
assert almost_equal(complex(3.14+0j), 3.14+0j) |
| 224 |
assert almost_equal(complex(3.14), 3.14+0j) |
| 225 |
assert almost_equal(complex(314), 314.0+0j) |
| 226 |
assert almost_equal(complex(3.14+0j, 0j), 3.14+0j) |
| 227 |
assert almost_equal(complex(3.14, 0.0), 3.14+0j) |
| 228 |
assert almost_equal(complex(314, 0), 314.0+0j) |
| 229 |
assert almost_equal(complex(0j, 3.14j), -3.14+0j) |
| 230 |
assert almost_equal(complex(0.0, 3.14j), -3.14+0j) |
| 231 |
assert almost_equal(complex(0j, 3.14), 3.14j) |
| 232 |
assert almost_equal(complex(0.0, 3.14), 3.14j) |
| 233 |
assert almost_equal(complex("1"), 1+0j) |
| 234 |
assert almost_equal(complex("1j"), 1j) |
| 235 |
assert almost_equal(complex(), 0) |
| 236 |
assert almost_equal(complex("-1"), -1) |
| 237 |
assert almost_equal(complex("+1"), +1) |
| 238 |
assert almost_equal(complex(" ( +3.14-6J ) "), 3.14-6j) |
| 239 |
exc = raises(ValueError, complex, " ( +3.14- 6J ) ") |
| 240 |
assert str(exc.value) == "complex() arg is a malformed string" |
| 241 |
|
| 242 |
class complex2(complex): |
| 243 |
pass |
| 244 |
assert almost_equal(complex(complex2(1+1j)), 1+1j) |
| 245 |
assert almost_equal(complex(real=17, imag=23), 17+23j) |
| 246 |
assert almost_equal(complex(real=17+23j), 17+23j) |
| 247 |
assert almost_equal(complex(real=17+23j, imag=23), 17+46j) |
| 248 |
assert almost_equal(complex(real=1+2j, imag=3+4j), -3+5j) |
| 249 |
|
| 250 |
c = 3.14 + 1j |
| 251 |
assert complex(c) is c |
| 252 |
del c |
| 253 |
|
| 254 |
raises(TypeError, complex, "1", "1") |
| 255 |
raises(TypeError, complex, 1, "1") |
| 256 |
|
| 257 |
assert complex(" 3.14+J ") == 3.14+1j |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
with raises(ValueError): |
| 263 |
complex("1+1j\0j") |
| 264 |
|
| 265 |
raises(TypeError, int, 5+3j) |
| 266 |
raises(TypeError, float, 5+3j) |
| 267 |
raises(ValueError, complex, "") |
| 268 |
raises(TypeError, complex, None) |
| 269 |
raises(ValueError, complex, "\0") |
| 270 |
raises(TypeError, complex, "1", "2") |
| 271 |
raises(TypeError, complex, "1", 42) |
| 272 |
raises(TypeError, complex, 1, "2") |
| 273 |
raises(ValueError, complex, "1+") |
| 274 |
raises(ValueError, complex, "1+1j+1j") |
| 275 |
raises(ValueError, complex, "--") |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
class EvilExc(Exception): |
| 281 |
pass |
| 282 |
|
| 283 |
class evilcomplex: |
| 284 |
def __complex__(self): |
| 285 |
raise EvilExc |
| 286 |
|
| 287 |
raises(EvilExc, complex, evilcomplex()) |
| 288 |
|
| 289 |
class float2: |
| 290 |
def __init__(self, value): |
| 291 |
self.value = value |
| 292 |
def __float__(self): |
| 293 |
return self.value |
| 294 |
|
| 295 |
assert almost_equal(complex(float2(42.)), 42) |
| 296 |
assert almost_equal(complex(real=float2(17.), imag=float2(23.)), 17+23j) |
| 297 |
raises(TypeError, complex, float2(None)) |
| 298 |
|
| 299 |
def test_complex_string_underscores(): |
| 300 |
valid = [ |
| 301 |
'1_00_00j', |
| 302 |
'1_00_00.5j', |
| 303 |
'1_00_00e5_1j', |
| 304 |
'.1_4j', |
| 305 |
'(1_2.5+3_3j)', |
| 306 |
'(.5_6j)', |
| 307 |
] |
| 308 |
for s in valid: |
| 309 |
assert complex(s) == complex(s.replace("_", "")) |
| 310 |
assert eval(s) == eval(s.replace("_", "")) |
| 311 |
|
| 312 |
invalid = [ |
| 313 |
|
| 314 |
'1.4j_', |
| 315 |
|
| 316 |
'0.1__4j', |
| 317 |
'1e1__0j', |
| 318 |
|
| 319 |
'1_.4j', |
| 320 |
|
| 321 |
'1._4j', |
| 322 |
'._5j', |
| 323 |
|
| 324 |
'1.0e+_1j', |
| 325 |
|
| 326 |
'1.4e5_j', |
| 327 |
|
| 328 |
'1.4_e1j', |
| 329 |
|
| 330 |
'1.4e_1j', |
| 331 |
|
| 332 |
'(1+1.5_j_)', |
| 333 |
'(1+1.5_j)', |
| 334 |
] |
| 335 |
for s in invalid: |
| 336 |
raises(ValueError, complex, s) |
| 337 |
raises(SyntaxError, eval, s) |
| 338 |
|
| 339 |
def test_constructor_bad_error_message(): |
| 340 |
err = raises(TypeError, complex, {}).value |
| 341 |
assert "float" not in str(err) |
| 342 |
assert str(err) == "complex() first argument must be a string or a number, not 'dict'" |
| 343 |
err = raises(TypeError, complex, 1, {}).value |
| 344 |
assert "float" not in str(err) |
| 345 |
assert str(err) == "complex() second argument must be a number, not 'dict'" |
| 346 |
|
| 347 |
def test_error_messages(): |
| 348 |
with raises(ZeroDivisionError) as err: |
| 349 |
1+1j / 0 |
| 350 |
assert str(err.value) == "complex division by zero" |
| 351 |
with raises(TypeError) as err: |
| 352 |
1+1j // 0 |
| 353 |
assert str(err.value) == "unsupported operand type(s) for //: 'complex' and 'int'" |
| 354 |
|
| 355 |
def test_hash(): |
| 356 |
for x in range(-30, 30): |
| 357 |
assert hash(x) == hash(complex(x, 0)) |
| 358 |
x /= 3.0 |
| 359 |
assert hash(x) == hash(complex(x, 0.)) |
| 360 |
|
| 361 |
def test_abs(): |
| 362 |
nums = [complex(x/3., y/7.) for x in range(-9,9) for y in range(-9,9)] |
| 363 |
for num in nums: |
| 364 |
assert almost_equal((num.real**2 + num.imag**2) ** 0.5, abs(num)) |
| 365 |
|
| 366 |
def test_complex_subclass_ctr(): |
| 367 |
class j(complex): |
| 368 |
pass |
| 369 |
assert j(100 + 0j) == 100 + 0j |
| 370 |
assert isinstance(j(100), j) |
| 371 |
assert j("100+0j") == 100 + 0j |
| 372 |
exc = raises(ValueError, j, "100 + 0j") |
| 373 |
assert str(exc.value) == "complex() arg is a malformed string" |
| 374 |
x = j(1+0j) |
| 375 |
x.foo = 42 |
| 376 |
assert x.foo == 42 |
| 377 |
assert type(complex(x)) == complex |
| 378 |
|
| 379 |
def test_infinity(): |
| 380 |
inf = 1e200*1e200 |
| 381 |
assert complex("1"*500) == complex(inf) |
| 382 |
assert complex("-inf") == complex(-inf) |
| 383 |
|
| 384 |
def test_repr(): |
| 385 |
assert repr(1+6j) == '(1+6j)' |
| 386 |
assert repr(1-6j) == '(1-6j)' |
| 387 |
|
| 388 |
assert repr(-(1+0j)) == '(-1-0j)' |
| 389 |
assert repr(complex( 0.0, 0.0)) == '0j' |
| 390 |
assert repr(complex( 0.0, -0.0)) == '-0j' |
| 391 |
assert repr(complex(-0.0, 0.0)) == '(-0+0j)' |
| 392 |
assert repr(complex(-0.0, -0.0)) == '(-0-0j)' |
| 393 |
assert repr(complex(1e45)) == "(" + repr(1e45) + "+0j)" |
| 394 |
assert repr(complex(1e200*1e200)) == '(inf+0j)' |
| 395 |
assert repr(complex(1,-float("nan"))) == '(1+nanj)' |
| 396 |
|
| 397 |
def test_repr_roundtrip(): |
| 398 |
|
| 399 |
INF = float("inf") |
| 400 |
NAN = float("nan") |
| 401 |
vals = [0.0, 1e-500, 1e-315, 1e-200, 0.0123, 3.1415, 1e50, INF, NAN] |
| 402 |
vals += [-v for v in vals] |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
for x in vals: |
| 407 |
for y in vals: |
| 408 |
z = complex(x, y) |
| 409 |
roundtrip = complex(repr(z)) |
| 410 |
floats_identical(z.real, roundtrip.real) |
| 411 |
floats_identical(z.imag, roundtrip.imag) |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
inf, nan = float('inf'), float('nan') |
| 416 |
infj, nanj = complex(0.0, inf), complex(0.0, nan) |
| 417 |
for x in vals: |
| 418 |
for y in vals: |
| 419 |
z = complex(x, y) |
| 420 |
roundtrip = eval(repr(z)) |
| 421 |
|
| 422 |
floats_identical(0.0 + z.real, 0.0 + roundtrip.real) |
| 423 |
floats_identical(0.0 + z.imag, 0.0 + roundtrip.imag) |
| 424 |
|
| 425 |
def test_neg(): |
| 426 |
assert -(1+6j) == -1-6j |
| 427 |
|
| 428 |
def test_file(): |
| 429 |
import os |
| 430 |
import tempfile |
| 431 |
|
| 432 |
a = 3.33+4.43j |
| 433 |
b = 5.1+2.3j |
| 434 |
|
| 435 |
fo = None |
| 436 |
try: |
| 437 |
pth = tempfile.mktemp() |
| 438 |
fo = open(pth, "w") |
| 439 |
print(a, b, file=fo) |
| 440 |
fo.close() |
| 441 |
fo = open(pth, "r") |
| 442 |
res = fo.read() |
| 443 |
assert res == "%s %s\n" % (a, b) |
| 444 |
finally: |
| 445 |
if (fo is not None) and (not fo.closed): |
| 446 |
fo.close() |
| 447 |
try: |
| 448 |
os.remove(pth) |
| 449 |
except (OSError, IOError): |
| 450 |
pass |
| 451 |
|
| 452 |
def test_convert(): |
| 453 |
import warnings |
| 454 |
raises(TypeError, int, 1+1j) |
| 455 |
raises(TypeError, float, 1+1j) |
| 456 |
|
| 457 |
class complex0(complex): |
| 458 |
"""Test usage of __complex__() when inheriting from 'complex'""" |
| 459 |
def __complex__(self): |
| 460 |
return 42j |
| 461 |
assert complex(complex0(1j)) == 42j |
| 462 |
|
| 463 |
class complex1(complex): |
| 464 |
"""Test usage of __complex__() with a __new__() method""" |
| 465 |
def __new__(self, value=0j): |
| 466 |
return complex.__new__(self, 2*value) |
| 467 |
def __complex__(self): |
| 468 |
return self |
| 469 |
with warnings.catch_warnings(record=True) as log: |
| 470 |
warnings.simplefilter("always", DeprecationWarning) |
| 471 |
assert complex(complex1(1j)) == 2j |
| 472 |
assert len(log) == 1 |
| 473 |
assert log[0].category == DeprecationWarning |
| 474 |
|
| 475 |
class complex1b(complex): |
| 476 |
"""Test usage of a complex subclass without __complex__() method""" |
| 477 |
def __new__(self, value=0j): |
| 478 |
return complex.__new__(self, 2*value) |
| 479 |
with warnings.catch_warnings(record=True) as log: |
| 480 |
warnings.simplefilter("always", DeprecationWarning) |
| 481 |
assert complex(complex1b(1j)) == 2j |
| 482 |
assert len(log) == 0 |
| 483 |
|
| 484 |
class complex1_proxy: |
| 485 |
"""Test usage of __complex__() without subclassing complex""" |
| 486 |
def __init__(self, value=0j): |
| 487 |
self.value = value |
| 488 |
def __complex__(self): |
| 489 |
return complex1(self.value) |
| 490 |
with warnings.catch_warnings(record=True) as log: |
| 491 |
warnings.simplefilter("always", DeprecationWarning) |
| 492 |
assert complex(complex1_proxy(1j)) == 2j |
| 493 |
assert len(log) == 1 |
| 494 |
assert log[0].category == DeprecationWarning |
| 495 |
|
| 496 |
class complex2(complex): |
| 497 |
"""Make sure that __complex__() calls fail if anything other than a |
| 498 |
complex is returned""" |
| 499 |
def __complex__(self): |
| 500 |
return None |
| 501 |
raises(TypeError, complex, complex2(1j)) |
| 502 |
|
| 503 |
def test_getnewargs(): |
| 504 |
assert (1+2j).__getnewargs__() == (1.0, 2.0) |
| 505 |
|
| 506 |
def test_method_not_found_on_newstyle_instance(): |
| 507 |
class A(object): |
| 508 |
pass |
| 509 |
a = A() |
| 510 |
a.__complex__ = lambda: 5j |
| 511 |
raises(TypeError, complex, a) |
| 512 |
A.__complex__ = lambda self: 42j |
| 513 |
assert complex(a) == 42j |
| 514 |
|
| 515 |
def test_format(): |
| 516 |
|
| 517 |
assert format(1+3j, '') == str(1+3j) |
| 518 |
assert format(1.5+3.5j, '') == str(1.5+3.5j) |
| 519 |
assert format(3j, '') == str(3j) |
| 520 |
assert format(3.2j, '') == str(3.2j) |
| 521 |
assert format(3+0j, '') == str(3+0j) |
| 522 |
assert format(3.2+0j, '') == str(3.2+0j) |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
assert format(3.2, '-') == str(3.2) |
| 528 |
assert format(3.2+0j, '-') == str(3.2+0j) |
| 529 |
assert format(3.2+0j, '<') == str(3.2+0j) |
| 530 |
z = 10/7. - 100j/7. |
| 531 |
assert format(z, '') == str(z) |
| 532 |
assert format(z, '-') == str(z) |
| 533 |
assert format(z, '<') == str(z) |
| 534 |
assert format(z, '10') == str(z) |
| 535 |
z = complex(0.0, 3.0) |
| 536 |
assert format(z, '') == str(z) |
| 537 |
assert format(z, '-') == str(z) |
| 538 |
assert format(z, '<') == str(z) |
| 539 |
assert format(z, '2') == str(z) |
| 540 |
z = complex(-0.0, 2.0) |
| 541 |
assert format(z, '') == str(z) |
| 542 |
assert format(z, '-') == str(z) |
| 543 |
assert format(z, '<') == str(z) |
| 544 |
assert format(z, '3') == str(z) |
| 545 |
|
| 546 |
assert format(1+3j, 'g') == '1+3j' |
| 547 |
assert format(3j, 'g') == '0+3j' |
| 548 |
assert format(1.5+3.5j, 'g') == '1.5+3.5j' |
| 549 |
|
| 550 |
assert format(1.5+3.5j, '+g') == '+1.5+3.5j' |
| 551 |
assert format(1.5-3.5j, '+g') == '+1.5-3.5j' |
| 552 |
assert format(1.5-3.5j, '-g') == '1.5-3.5j' |
| 553 |
assert format(1.5+3.5j, ' g') == ' 1.5+3.5j' |
| 554 |
assert format(1.5-3.5j, ' g') == ' 1.5-3.5j' |
| 555 |
assert format(-1.5+3.5j, ' g') == '-1.5+3.5j' |
| 556 |
assert format(-1.5-3.5j, ' g') == '-1.5-3.5j' |
| 557 |
|
| 558 |
assert format(-1.5-3.5e-20j, 'g') == '-1.5-3.5e-20j' |
| 559 |
assert format(-1.5-3.5j, 'f') == '-1.500000-3.500000j' |
| 560 |
assert format(-1.5-3.5j, 'F') == '-1.500000-3.500000j' |
| 561 |
assert format(-1.5-3.5j, 'e') == '-1.500000e+00-3.500000e+00j' |
| 562 |
assert format(-1.5-3.5j, '.2e') == '-1.50e+00-3.50e+00j' |
| 563 |
assert format(-1.5-3.5j, '.2E') == '-1.50E+00-3.50E+00j' |
| 564 |
assert format(-1.5e10-3.5e5j, '.2G') == '-1.5E+10-3.5E+05j' |
| 565 |
|
| 566 |
assert format(1.5+3j, '<20g') == '1.5+3j ' |
| 567 |
assert format(1.5+3j, '*<20g') == '1.5+3j**************' |
| 568 |
assert format(1.5+3j, '>20g') == ' 1.5+3j' |
| 569 |
assert format(1.5+3j, '^20g') == ' 1.5+3j ' |
| 570 |
assert format(1.5+3j, '<20') == '(1.5+3j) ' |
| 571 |
assert format(1.5+3j, '>20') == ' (1.5+3j)' |
| 572 |
assert format(1.5+3j, '^20') == ' (1.5+3j) ' |
| 573 |
assert format(1.123-3.123j, '^20.2') == ' (1.1-3.1j) ' |
| 574 |
|
| 575 |
assert format(1.5+3j, '20.2f') == ' 1.50+3.00j' |
| 576 |
assert format(1.5+3j, '>20.2f') == ' 1.50+3.00j' |
| 577 |
assert format(1.5+3j, '<20.2f') == '1.50+3.00j ' |
| 578 |
assert format(1.5e20+3j, '<20.2f') == '150000000000000000000.00+3.00j' |
| 579 |
assert format(1.5e20+3j, '>40.2f') == ' 150000000000000000000.00+3.00j' |
| 580 |
assert format(1.5e20+3j, '^40,.2f') == ' 150,000,000,000,000,000,000.00+3.00j ' |
| 581 |
assert format(1.5e21+3j, '^40,.2f') == ' 1,500,000,000,000,000,000,000.00+3.00j ' |
| 582 |
assert format(1.5e21+3000j, ',.2f') == '1,500,000,000,000,000,000,000.00+3,000.00j' |
| 583 |
assert format(1.5+0.5j, '#f') == '1.500000+0.500000j' |
| 584 |
|
| 585 |
|
| 586 |
with raises(ValueError): |
| 587 |
(1.5+0.5j).__format__("010f") |
| 588 |
|
| 589 |
|
| 590 |
with raises(ValueError): |
| 591 |
(1.5+3j).__format__("=20") |
| 592 |
|
| 593 |
|
| 594 |
for t in 'bcdoxX%': |
| 595 |
raises(ValueError, (1.5+0.5j).__format__, t) |
| 596 |
|
| 597 |
|
| 598 |
assert '*{0:.3f}*'.format(3.14159+2.71828j) == '*3.142+2.718j*' |
| 599 |
assert '{:-}'.format(1.5+3.5j) == '(1.5+3.5j)' |
| 600 |
|
| 601 |
INF = float("inf") |
| 602 |
NAN = float("nan") |
| 603 |
|
| 604 |
assert '{0:f}'.format(INF+0j) == 'inf+0.000000j' |
| 605 |
assert '{0:F}'.format(INF+0j) == 'INF+0.000000j' |
| 606 |
assert '{0:f}'.format(-INF+0j) == '-inf+0.000000j' |
| 607 |
assert '{0:F}'.format(-INF+0j) == '-INF+0.000000j' |
| 608 |
assert '{0:f}'.format(complex(INF, INF)) == 'inf+infj' |
| 609 |
assert '{0:F}'.format(complex(INF, INF)) == 'INF+INFj' |
| 610 |
assert '{0:f}'.format(complex(INF, -INF)) == 'inf-infj' |
| 611 |
assert '{0:F}'.format(complex(INF, -INF)) == 'INF-INFj' |
| 612 |
assert '{0:f}'.format(complex(-INF, INF)) == '-inf+infj' |
| 613 |
assert '{0:F}'.format(complex(-INF, INF)) == '-INF+INFj' |
| 614 |
assert '{0:f}'.format(complex(-INF, -INF)) == '-inf-infj' |
| 615 |
assert '{0:F}'.format(complex(-INF, -INF)) == '-INF-INFj' |
| 616 |
|
| 617 |
assert '{0:f}'.format(complex(NAN, 0)) == 'nan+0.000000j' |
| 618 |
assert '{0:F}'.format(complex(NAN, 0)) == 'NAN+0.000000j' |
| 619 |
assert '{0:f}'.format(complex(NAN, NAN)) == 'nan+nanj' |
| 620 |
assert '{0:F}'.format(complex(NAN, NAN)) == 'NAN+NANj' |
| 621 |
|
| 622 |
def test_str_zeros_bug(): |
| 623 |
assert str(1000000000001j) == '1000000000001j' |
| 624 |
assert complex.__str__ is object.__str__ |
| 625 |
|
| 626 |
def test_complex_two_arguments(): |
| 627 |
raises(TypeError, complex, 5, None) |
| 628 |
|
| 629 |
def test_negated_imaginary_literal(): |
| 630 |
def sign(x): |
| 631 |
import math |
| 632 |
return math.copysign(1.0, x) |
| 633 |
z0 = -0j |
| 634 |
z1 = -7j |
| 635 |
z2 = -1e1000j |
| 636 |
|
| 637 |
|
| 638 |
assert sign(z0.real) == -1 |
| 639 |
assert sign(z0.imag) == -1 |
| 640 |
assert sign(z1.real) == -1 |
| 641 |
assert sign(z1.imag) == -1 |
| 642 |
assert sign(z2.real) == -1 |
| 643 |
assert sign(z2.real) == -1 |
| 644 |
|
| 645 |
def test_hash_minus_one(): |
| 646 |
assert hash(-1.0 + 0j) == -2 |
| 647 |
assert (-1.0 + 0j).__hash__() == -2 |
| 648 |
|
| 649 |
def test_int_override(): |
| 650 |
class MyComplex(complex): |
| 651 |
def __int__(self): |
| 652 |
return 42 |
| 653 |
|
| 654 |
c = MyComplex(0.j) |
| 655 |
assert int(c) == 42 |
| 656 |
|
| 657 |
def test_complex_constructor_calls_index(): |
| 658 |
class A: |
| 659 |
def __init__(self, val): |
| 660 |
self.val = val |
| 661 |
def __index__(self): |
| 662 |
return self.val |
| 663 |
assert complex(A(1), A(2)) == (1.0+2.0j) |
| 664 |
|
| 665 |
def test_parse_complex_whitespace_bug(): |
| 666 |
c = complex('\t( -1.23+4.5J )\n\r\v ') |
| 667 |
assert c == -1.23+4.5j |
| 668 |
|
| 669 |
def test_has_complex_special_method(): |
| 670 |
x = 1+1j |
| 671 |
assert x.__complex__() is x |
| 672 |
class C(complex): pass |
| 673 |
x = C(1+1j) |
| 674 |
assert x.__complex__() == x |
| 675 |
assert x.__complex__().__class__ is complex |
| 676 |
|
| 677 |
class C(complex): |
| 678 |
def __complex__(self): |
| 679 |
return -10-10j |
| 680 |
|
| 681 |
assert complex(C(1 + 1j)) == -10-10j |
| 682 |
|
| 683 |
|
| 684 |
def test_text_signature(): |
| 685 |
assert complex.__text_signature__ == '(real=0, imag=0)' |
| 686 |
|