| 1 |
from __future__ import with_statement |
| 2 |
from pypy.module.cmath import interp_cmath |
| 3 |
import os, sys, math |
| 4 |
|
| 5 |
|
| 6 |
def test_special_values(): |
| 7 |
from rpython.rlib.special_value import sqrt_special_values |
| 8 |
assert len(sqrt_special_values) == 7 |
| 9 |
assert len(sqrt_special_values[4]) == 7 |
| 10 |
assert isinstance(sqrt_special_values[5][1], tuple) |
| 11 |
assert sqrt_special_values[5][1][0] == 1e200 * 1e200 |
| 12 |
assert sqrt_special_values[5][1][1] == -0. |
| 13 |
assert math.copysign(1., sqrt_special_values[5][1][1]) == -1. |
| 14 |
|
| 15 |
|
| 16 |
class AppTestCMath: |
| 17 |
spaceconfig = dict(usemodules=['cmath']) |
| 18 |
|
| 19 |
def test_sign(self): |
| 20 |
import math |
| 21 |
z = eval("-0j") |
| 22 |
assert z == -0j |
| 23 |
assert math.copysign(1., z.real) == -1.0 |
| 24 |
assert math.copysign(1., z.imag) == -1.0 |
| 25 |
|
| 26 |
def test_sqrt(self): |
| 27 |
import cmath, math |
| 28 |
assert cmath.sqrt(3+4j) == 2+1j |
| 29 |
z = cmath.sqrt(-0j) |
| 30 |
assert math.copysign(1., z.real) == 1. |
| 31 |
assert math.copysign(1., z.imag) == -1. |
| 32 |
dbl_min = 2.2250738585072014e-308 |
| 33 |
z = cmath.sqrt((dbl_min * 0.00000000000001) + 0j) |
| 34 |
assert abs(z.real - 1.49107189843e-161) < 1e-170 |
| 35 |
assert z.imag == 0.0 |
| 36 |
z = cmath.sqrt(1e200*1e200 - 10j) |
| 37 |
assert math.isinf(z.real) and z.real > 0.0 |
| 38 |
assert z.imag == 0.0 and math.copysign(1., z.imag) == -1. |
| 39 |
|
| 40 |
def test_log(self): |
| 41 |
import cmath, math |
| 42 |
z = cmath.log(100j, 10j) |
| 43 |
assert abs(z - (1.6824165174565446-0.46553647994440367j)) < 1e-10 |
| 44 |
|
| 45 |
def test_pi_tau_e(self): |
| 46 |
import cmath, math |
| 47 |
assert cmath.pi == math.pi |
| 48 |
assert cmath.tau == math.tau |
| 49 |
assert cmath.tau == cmath.pi * 2.0 |
| 50 |
assert cmath.e == math.e |
| 51 |
|
| 52 |
def test_rect(self): |
| 53 |
import cmath |
| 54 |
z = cmath.rect(2.0, cmath.pi/2) |
| 55 |
assert abs(z - 2j) < 1e-10 |
| 56 |
|
| 57 |
def test_polar(self): |
| 58 |
import cmath |
| 59 |
r, phi = cmath.polar(2j) |
| 60 |
assert r == 2 |
| 61 |
assert abs(phi - cmath.pi/2) < 1e-10 |
| 62 |
|
| 63 |
def test_phase(self): |
| 64 |
import cmath |
| 65 |
phi = cmath.phase(2j) |
| 66 |
assert abs(phi - cmath.pi/2) < 1e-10 |
| 67 |
|
| 68 |
def test_valueerror(self): |
| 69 |
import cmath |
| 70 |
raises(ValueError, cmath.log, 0j) |
| 71 |
|
| 72 |
def test_stringarg(self): |
| 73 |
import cmath |
| 74 |
raises(TypeError, cmath.log, "-3j") |
| 75 |
|
| 76 |
def test_isinf(self): |
| 77 |
import cmath |
| 78 |
assert not cmath.isinf(2+3j) |
| 79 |
assert cmath.isinf(float("inf")) |
| 80 |
assert cmath.isinf(-float("inf")) |
| 81 |
assert cmath.isinf(complex("infj")) |
| 82 |
assert cmath.isinf(complex("2-infj")) |
| 83 |
assert cmath.isinf(complex("inf+nanj")) |
| 84 |
assert cmath.isinf(complex("nan+infj")) |
| 85 |
|
| 86 |
def test_isnan(self): |
| 87 |
import cmath |
| 88 |
assert not cmath.isnan(2+3j) |
| 89 |
assert cmath.isnan(float("nan")) |
| 90 |
assert cmath.isnan(complex("nanj")) |
| 91 |
assert cmath.isnan(complex("inf+nanj")) |
| 92 |
assert cmath.isnan(complex("nan+infj")) |
| 93 |
|
| 94 |
def test_isfinite(self): |
| 95 |
import cmath |
| 96 |
import math |
| 97 |
|
| 98 |
real_vals = [ |
| 99 |
float('-inf'), -2.3, -0.0, 0.0, 2.3, float('inf'), float('nan') |
| 100 |
] |
| 101 |
for x in real_vals: |
| 102 |
for y in real_vals: |
| 103 |
z = complex(x, y) |
| 104 |
assert cmath.isfinite(z) == (math.isfinite(x) and math.isfinite(y)) |
| 105 |
|
| 106 |
def test_user_defined_complex(self): |
| 107 |
import cmath |
| 108 |
class Foo(object): |
| 109 |
def __complex__(self): |
| 110 |
return 2j |
| 111 |
r, phi = cmath.polar(Foo()) |
| 112 |
assert r == 2 |
| 113 |
assert abs(phi - cmath.pi/2) < 1e-10 |
| 114 |
|
| 115 |
def test_user_defined_float(self): |
| 116 |
import cmath |
| 117 |
class Foo(object): |
| 118 |
def __float__(self): |
| 119 |
return 2.0 |
| 120 |
assert cmath.polar(Foo()) == (2, 0) |
| 121 |
|
| 122 |
def test_isclose(self): |
| 123 |
import cmath |
| 124 |
raises(ValueError, cmath.isclose, 2, 3, rel_tol=-0.5) |
| 125 |
raises(ValueError, cmath.isclose, 2, 3, abs_tol=-0.5) |
| 126 |
for z in [0.0, 1.0, 1j, |
| 127 |
complex("inf"), complex("infj"), |
| 128 |
complex("-inf"), complex("-infj")]: |
| 129 |
assert cmath.isclose(z, z) |
| 130 |
assert not cmath.isclose(complex("infj"), complex("-infj")) |
| 131 |
assert cmath.isclose(1j, 1j+1e-12) |
| 132 |
assert not cmath.isclose(1j, 1j+1e-12, rel_tol=1e-13) |
| 133 |
assert not cmath.isclose(100000j, 100001j) |
| 134 |
assert cmath.isclose(100000j, 100001j, rel_tol=1e-4) |
| 135 |
assert cmath.isclose(100000j, 100001j, abs_tol=1.5) |
| 136 |
assert not cmath.isclose(100000j, 100001j, abs_tol=0.5) |
| 137 |
|
| 138 |
def test_infinity_and_nan_constants(self): |
| 139 |
import cmath, math |
| 140 |
assert cmath.inf.real == math.inf |
| 141 |
assert cmath.inf.imag == 0.0 |
| 142 |
assert cmath.infj.real == 0.0 |
| 143 |
assert cmath.infj.imag == math.inf |
| 144 |
|
| 145 |
assert math.isnan(cmath.nan.real) |
| 146 |
assert cmath.nan.imag == 0.0 |
| 147 |
assert cmath.nanj.real == 0.0 |
| 148 |
assert math.isnan(cmath.nanj.imag) |
| 149 |
|
| 150 |
|
| 151 |
assert repr(cmath.inf) == "inf" |
| 152 |
assert repr(cmath.infj) == "infj" |
| 153 |
assert repr(cmath.nan) == "nan" |
| 154 |
assert repr(cmath.nanj) == "nanj" |
| 155 |
|
| 156 |
|
| 157 |
def parse_testfile(fname): |
| 158 |
"""Parse a file with test values |
| 159 |
|
| 160 |
Empty lines or lines starting with -- are ignored |
| 161 |
yields id, fn, arg_real, arg_imag, exp_real, exp_imag |
| 162 |
""" |
| 163 |
with open(fname) as fp: |
| 164 |
for line in fp: |
| 165 |
|
| 166 |
if line.startswith('--') or not line.strip(): |
| 167 |
continue |
| 168 |
|
| 169 |
lhs, rhs = line.split('->') |
| 170 |
id, fn, arg_real, arg_imag = lhs.split() |
| 171 |
rhs_pieces = rhs.split() |
| 172 |
exp_real, exp_imag = rhs_pieces[0], rhs_pieces[1] |
| 173 |
flags = rhs_pieces[2:] |
| 174 |
|
| 175 |
yield (id, fn, |
| 176 |
float(arg_real), float(arg_imag), |
| 177 |
float(exp_real), float(exp_imag), |
| 178 |
flags |
| 179 |
) |
| 180 |
|
| 181 |
def rAssertAlmostEqual(a, b, rel_err = 2e-15, abs_err = 5e-323, msg=''): |
| 182 |
"""Fail if the two floating-point numbers are not almost equal. |
| 183 |
|
| 184 |
Determine whether floating-point values a and b are equal to within |
| 185 |
a (small) rounding error. The default values for rel_err and |
| 186 |
abs_err are chosen to be suitable for platforms where a float is |
| 187 |
represented by an IEEE 754 double. They allow an error of between |
| 188 |
9 and 19 ulps. |
| 189 |
""" |
| 190 |
|
| 191 |
|
| 192 |
if math.isnan(a): |
| 193 |
if math.isnan(b): |
| 194 |
return |
| 195 |
raise AssertionError(msg + '%r should be nan' % (b,)) |
| 196 |
|
| 197 |
if math.isinf(a): |
| 198 |
if a == b: |
| 199 |
return |
| 200 |
raise AssertionError(msg + 'finite result where infinity expected: ' |
| 201 |
'expected %r, got %r' % (a, b)) |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
if not a and not b: |
| 208 |
if math.copysign(1., a) != math.copysign(1., b): |
| 209 |
raise AssertionError(msg + 'zero has wrong sign: expected %r, ' |
| 210 |
'got %r' % (a, b)) |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
try: |
| 217 |
absolute_error = abs(b-a) |
| 218 |
except OverflowError: |
| 219 |
pass |
| 220 |
else: |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
if absolute_error <= max(abs_err, rel_err * abs(a)): |
| 226 |
return |
| 227 |
raise AssertionError(msg + '%r and %r are not sufficiently close' % (a, b)) |
| 228 |
|
| 229 |
def test_specific_values(): |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
import rpython |
| 234 |
|
| 235 |
fname = os.path.join(os.path.dirname(rpython.rlib.__file__), 'test', 'rcomplex_testcases.txt') |
| 236 |
for id, fn, ar, ai, er, ei, flags in parse_testfile(fname): |
| 237 |
arg = (ar, ai) |
| 238 |
expected = (er, ei) |
| 239 |
function = getattr(interp_cmath, 'c_' + fn) |
| 240 |
|
| 241 |
if 'divide-by-zero' in flags or 'invalid' in flags: |
| 242 |
try: |
| 243 |
actual = function(*arg) |
| 244 |
except ValueError: |
| 245 |
continue |
| 246 |
else: |
| 247 |
raise AssertionError('ValueError not raised in test ' |
| 248 |
'%s: %s(complex(%r, %r))' % (id, fn, |
| 249 |
ar, ai)) |
| 250 |
if 'overflow' in flags: |
| 251 |
try: |
| 252 |
actual = function(*arg) |
| 253 |
except OverflowError: |
| 254 |
continue |
| 255 |
else: |
| 256 |
raise AssertionError('OverflowError not raised in test ' |
| 257 |
'%s: %s(complex(%r, %r))' % (id, fn, |
| 258 |
ar, ai)) |
| 259 |
actual = function(*arg) |
| 260 |
|
| 261 |
if 'ignore-real-sign' in flags: |
| 262 |
actual = (abs(actual[0]), actual[1]) |
| 263 |
expected = (abs(expected[0]), expected[1]) |
| 264 |
if 'ignore-imag-sign' in flags: |
| 265 |
actual = (actual[0], abs(actual[1])) |
| 266 |
expected = (expected[0], abs(expected[1])) |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
if fn in ('log', 'log10'): |
| 271 |
real_abs_err = 2e-15 |
| 272 |
else: |
| 273 |
real_abs_err = 5e-323 |
| 274 |
|
| 275 |
error_message = ( |
| 276 |
'%s: %s(complex(%r, %r))\n' |
| 277 |
'Expected: complex(%r, %r)\n' |
| 278 |
'Received: complex(%r, %r)\n' |
| 279 |
) % (id, fn, ar, ai, |
| 280 |
expected[0], expected[1], |
| 281 |
actual[0], actual[1]) |
| 282 |
|
| 283 |
rAssertAlmostEqual(expected[0], actual[0], |
| 284 |
abs_err=real_abs_err, |
| 285 |
msg=error_message) |
| 286 |
rAssertAlmostEqual(expected[1], actual[1], |
| 287 |
msg=error_message) |