| 1 |
|
| 2 |
from __future__ import print_function |
| 3 |
|
| 4 |
import py |
| 5 |
|
| 6 |
from pypy.objspace.std.complexobject import W_ComplexObject, _split_complex |
| 7 |
|
| 8 |
EPS = 1e-9 |
| 9 |
|
| 10 |
class TestW_ComplexObject: |
| 11 |
def test_instantiation(self): |
| 12 |
def _t_complex(r=0.0,i=0.0): |
| 13 |
c = W_ComplexObject(r, i) |
| 14 |
assert c.realval == float(r) and c.imagval == float(i) |
| 15 |
pairs = ( |
| 16 |
(1, 1), |
| 17 |
(1.0, 2.0), |
| 18 |
(2L, 3L), |
| 19 |
) |
| 20 |
for r,i in pairs: |
| 21 |
_t_complex(r,i) |
| 22 |
|
| 23 |
def test_parse_complex(self): |
| 24 |
f = _split_complex |
| 25 |
def test_cparse(cnum, realnum, imagnum): |
| 26 |
result = f(cnum) |
| 27 |
assert len(result) == 2 |
| 28 |
r, i = result |
| 29 |
assert r == realnum |
| 30 |
assert i == imagnum |
| 31 |
|
| 32 |
test_cparse('3', '3', '0.0') |
| 33 |
test_cparse('3+3j', '3', '3') |
| 34 |
test_cparse('3.0+3j', '3.0', '3') |
| 35 |
test_cparse('3L+3j', '3L', '3') |
| 36 |
test_cparse('3j', '0.0', '3') |
| 37 |
test_cparse('.e+5', '.e+5', '0.0') |
| 38 |
test_cparse('(1+2j)', '1', '2') |
| 39 |
test_cparse('(1-6j)', '1', '-6') |
| 40 |
test_cparse(' ( +3.14-6J )', '+3.14', '-6') |
| 41 |
test_cparse(' +J', '0.0', '1.0') |
| 42 |
test_cparse(' -J', '0.0', '-1.0') |
| 43 |
|
| 44 |
def test_unpackcomplex(self): |
| 45 |
space = self.space |
| 46 |
w_z = W_ComplexObject(2.0, 3.5) |
| 47 |
assert space.unpackcomplex(w_z) == (2.0, 3.5) |
| 48 |
space.raises_w(space.w_TypeError, space.unpackcomplex, space.w_None) |
| 49 |
w_f = space.newfloat(42.5) |
| 50 |
assert space.unpackcomplex(w_f) == (42.5, 0.0) |
| 51 |
w_l = space.wrap(-42L) |
| 52 |
assert space.unpackcomplex(w_l) == (-42.0, 0.0) |
| 53 |
|
| 54 |
def test_pow(self): |
| 55 |
def _pow((r1, i1), (r2, i2)): |
| 56 |
w_res = W_ComplexObject(r1, i1).pow(W_ComplexObject(r2, i2)) |
| 57 |
return w_res.realval, w_res.imagval |
| 58 |
assert _pow((0.0,2.0),(0.0,0.0)) == (1.0,0.0) |
| 59 |
assert _pow((0.0,0.0),(2.0,0.0)) == (0.0,0.0) |
| 60 |
rr, ir = _pow((0.0,1.0),(2.0,0.0)) |
| 61 |
assert abs(-1.0 - rr) < EPS |
| 62 |
assert abs(0.0 - ir) < EPS |
| 63 |
|
| 64 |
def _powu((r1, i1), n): |
| 65 |
w_res = W_ComplexObject.pow_positive_int(r1, i1, n) |
| 66 |
return w_res.realval, w_res.imagval |
| 67 |
assert _powu((0.0,2.0),0) == (1.0,0.0) |
| 68 |
assert _powu((0.0,0.0),2) == (0.0,0.0) |
| 69 |
assert _powu((0.0,1.0),2) == (-1.0,0.0) |
| 70 |
assert _powu((0.0,1.0),3) == (-0.0,-1.0) |
| 71 |
expected = [(1.0, 0.0), (0.0, 1.0), (-1.0, 0.0), (-0.0, -1.0)] |
| 72 |
for i in range(100): |
| 73 |
assert _powu((0.0, 1.0), i) == expected[i % 4] |
| 74 |
|
| 75 |
|
| 76 |
def _powi((r1, i1), n): |
| 77 |
w_res = W_ComplexObject(r1, i1).pow_small_int(n) |
| 78 |
return w_res.realval, w_res.imagval |
| 79 |
assert _powi((0.0,2.0),0) == (1.0,0.0) |
| 80 |
assert _powi((0.0,0.0),2) == (0.0,0.0) |
| 81 |
assert _powi((0.0,1.0),2) == (-1.0,0.0) |
| 82 |
c = W_ComplexObject(0.0,1.0) |
| 83 |
p = W_ComplexObject(2.0,0.0) |
| 84 |
r = c.descr_pow(self.space, p, self.space.wrap(None)) |
| 85 |
assert r.realval == -1.0 |
| 86 |
assert r.imagval == 0.0 |