| 1 |
from __future__ import with_statement |
| 2 |
|
| 3 |
import rpython.rlib.rcomplex as c |
| 4 |
import os, sys, math, struct |
| 5 |
|
| 6 |
|
| 7 |
def test_add(): |
| 8 |
for c1, c2, result in [ |
| 9 |
((0, 0), (0, 0), (0, 0)), |
| 10 |
((1, 0), (2, 0), (3, 0)), |
| 11 |
((0, 3), (0, 2), (0, 5)), |
| 12 |
((10., -3.), (-5, 7), (5, 4)), |
| 13 |
]: |
| 14 |
assert c.c_add(c1, c2) == result |
| 15 |
|
| 16 |
def test_sub(): |
| 17 |
for c1, c2, result in [ |
| 18 |
((0, 0), (0, 0), (0, 0)), |
| 19 |
((1, 0), (2, 0), (-1, 0)), |
| 20 |
((0, 3), (0, 2), (0, 1)), |
| 21 |
((10, -3), (-5, 7), (15, -10)), |
| 22 |
((42, 0.3), (42, 0.3), (0, 0)) |
| 23 |
]: |
| 24 |
assert c.c_sub(c1, c2) == result |
| 25 |
|
| 26 |
def test_mul(): |
| 27 |
for c1, c2, result in [ |
| 28 |
((0, 0), (0, 0), (0, 0)), |
| 29 |
((1, 0), (2, 0), (2, 0)), |
| 30 |
((0, 3), (0, 2), (-6, 0)), |
| 31 |
((0, -3), (-5, 0), (0, 15)), |
| 32 |
]: |
| 33 |
assert c.c_mul(c1, c2) == result |
| 34 |
|
| 35 |
def test_div(): |
| 36 |
c.c_div((2., 3.), (float('nan'), 0.)) == (float('nan'), float('nan')) |
| 37 |
|
| 38 |
def parse_testfile2(fname): |
| 39 |
"""Parse a file with test values |
| 40 |
|
| 41 |
Empty lines or lines starting with -- are ignored |
| 42 |
yields id, fn, arg1_real, arg1_imag, arg2_real, arg2_imag, |
| 43 |
exp_real, exp_imag where numbers in file may be expressed as floating point or hex |
| 44 |
""" |
| 45 |
fname = os.path.join(os.path.dirname(__file__), fname) |
| 46 |
with open(fname) as fp: |
| 47 |
for line in fp: |
| 48 |
|
| 49 |
if line.startswith('--') or not line.strip(): |
| 50 |
continue |
| 51 |
|
| 52 |
lhs, rhs = line.split('->') |
| 53 |
lhs_pieces = lhs.split() |
| 54 |
rhs_pieces = rhs.split() |
| 55 |
for i in range(2, len(lhs_pieces)): |
| 56 |
if lhs_pieces[i].lower().startswith('0x'): |
| 57 |
lhs_pieces[i] = struct.unpack('d', |
| 58 |
struct.pack('q',int(lhs_pieces[i]))) |
| 59 |
else: |
| 60 |
lhs_pieces[i] = float(lhs_pieces[i]) |
| 61 |
for i in range(2): |
| 62 |
if rhs_pieces[i].lower().startswith('0x'): |
| 63 |
rhs_pieces[i] = struct.unpack('d', |
| 64 |
struct.pack('l',int(rhs_pieces[i]))) |
| 65 |
else: |
| 66 |
rhs_pieces[i] = float(rhs_pieces[i]) |
| 67 |
|
| 68 |
|
| 69 |
flags = rhs_pieces[2:] |
| 70 |
id_f, fn = lhs_pieces[:2] |
| 71 |
if len(lhs_pieces)>4: |
| 72 |
args = (lhs_pieces[2:4], lhs_pieces[4:]) |
| 73 |
else: |
| 74 |
args = lhs_pieces[2:] |
| 75 |
yield id_f, fn, args, rhs_pieces[:2], flags |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
def parse_testfile(fname): |
| 80 |
"""Parse a file with test values |
| 81 |
|
| 82 |
Empty lines or lines starting with -- are ignored |
| 83 |
yields id, fn, arg_real, arg_imag, exp_real, exp_imag |
| 84 |
""" |
| 85 |
fname = os.path.join(os.path.dirname(__file__), fname) |
| 86 |
with open(fname) as fp: |
| 87 |
for line in fp: |
| 88 |
|
| 89 |
if line.startswith('--') or not line.strip(): |
| 90 |
continue |
| 91 |
|
| 92 |
lhs, rhs = line.split('->') |
| 93 |
id, fn, arg_real, arg_imag = lhs.split() |
| 94 |
rhs_pieces = rhs.split() |
| 95 |
exp_real, exp_imag = rhs_pieces[0], rhs_pieces[1] |
| 96 |
flags = rhs_pieces[2:] |
| 97 |
|
| 98 |
yield (id, fn, |
| 99 |
float(arg_real), float(arg_imag), |
| 100 |
float(exp_real), float(exp_imag), |
| 101 |
flags |
| 102 |
) |
| 103 |
|
| 104 |
def args_to_str(args): |
| 105 |
if isinstance(args[0],(list, tuple)): |
| 106 |
return '(complex(%r, %r), complex(%r, %r))' % \ |
| 107 |
(args[0][0], args[0][1], args[1][0], args[1][1]) |
| 108 |
else: |
| 109 |
return '(complex(%r, %r))' % (args[0], args[1]) |
| 110 |
|
| 111 |
def rAssertAlmostEqual(a, b, rel_err = 2e-15, abs_err = 5e-323, msg=''): |
| 112 |
"""Fail if the two floating-point numbers are not almost equal. |
| 113 |
|
| 114 |
Determine whether floating-point values a and b are equal to within |
| 115 |
a (small) rounding error. The default values for rel_err and |
| 116 |
abs_err are chosen to be suitable for platforms where a float is |
| 117 |
represented by an IEEE 754 double. They allow an error of between |
| 118 |
9 and 19 ulps. |
| 119 |
""" |
| 120 |
|
| 121 |
|
| 122 |
if math.isnan(a): |
| 123 |
if math.isnan(b): |
| 124 |
return |
| 125 |
raise AssertionError(msg + '%r should be nan' % (b,)) |
| 126 |
|
| 127 |
if math.isinf(a): |
| 128 |
if a == b: |
| 129 |
return |
| 130 |
raise AssertionError(msg + 'finite result where infinity expected: ' |
| 131 |
'expected %r, got %r' % (a, b)) |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
if not a and not b: |
| 138 |
if math.copysign(1., a) != math.copysign(1., b): |
| 139 |
raise AssertionError(msg + 'zero has wrong sign: expected %r, ' |
| 140 |
'got %r' % (a, b)) |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
try: |
| 147 |
absolute_error = abs(b-a) |
| 148 |
except OverflowError: |
| 149 |
pass |
| 150 |
else: |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
if absolute_error <= max(abs_err, rel_err * abs(a)): |
| 156 |
return |
| 157 |
raise AssertionError(msg + '%r and %r are not sufficiently close' % (a, b)) |
| 158 |
|
| 159 |
def test_specific_values(): |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
for id, fn, arg, expected, flags in parse_testfile2('rcomplex_testcases.txt'): |
| 164 |
function = getattr(c, 'c_' + fn) |
| 165 |
|
| 166 |
if 'divide-by-zero' in flags or 'invalid' in flags: |
| 167 |
try: |
| 168 |
actual = function(*arg) |
| 169 |
except ValueError: |
| 170 |
continue |
| 171 |
else: |
| 172 |
raise AssertionError('ValueError not raised in test ' |
| 173 |
'%s: %s%s' % (id, fn, args_to_str(arg))) |
| 174 |
if 'overflow' in flags: |
| 175 |
try: |
| 176 |
actual = function(*arg) |
| 177 |
except OverflowError: |
| 178 |
continue |
| 179 |
else: |
| 180 |
raise AssertionError('OverflowError not raised in test ' |
| 181 |
'%s: %s%s' % (id, fn, args_to_str(arg))) |
| 182 |
actual = function(*arg) |
| 183 |
|
| 184 |
if 'ignore-real-sign' in flags: |
| 185 |
actual = (abs(actual[0]), actual[1]) |
| 186 |
expected = (abs(expected[0]), expected[1]) |
| 187 |
if 'ignore-imag-sign' in flags: |
| 188 |
actual = (actual[0], abs(actual[1])) |
| 189 |
expected = (expected[0], abs(expected[1])) |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
if fn in ('log', 'log10'): |
| 194 |
real_abs_err = 2e-15 |
| 195 |
else: |
| 196 |
real_abs_err = 5e-323 |
| 197 |
|
| 198 |
error_message = ( |
| 199 |
'%s: %s%s\n' |
| 200 |
'Expected: complex(%r, %r)\n' |
| 201 |
'Received: complex(%r, %r)\n' |
| 202 |
) % (id, fn, args_to_str(arg), |
| 203 |
expected[0], expected[1], |
| 204 |
actual[0], actual[1]) |
| 205 |
|
| 206 |
rAssertAlmostEqual(expected[0], actual[0], |
| 207 |
abs_err=real_abs_err, |
| 208 |
msg=error_message) |
| 209 |
rAssertAlmostEqual(expected[1], actual[1], |
| 210 |
abs_err=real_abs_err, |
| 211 |
msg=error_message) |
| 212 |
|
| 213 |
for id, fn, a, expected, flags in parse_testfile2('rcomplex_testcases2.txt'): |
| 214 |
function = getattr(c, 'c_' + fn) |
| 215 |
|
| 216 |
if 'divide-by-zero' in flags or 'invalid' in flags: |
| 217 |
try: |
| 218 |
actual = function(*a) |
| 219 |
except ValueError: |
| 220 |
continue |
| 221 |
else: |
| 222 |
raise AssertionError('ValueError not raised in test ' |
| 223 |
'%s: %s%s' % (id, fn, args_to_str(a))) |
| 224 |
if 'overflow' in flags: |
| 225 |
try: |
| 226 |
actual = function(*a) |
| 227 |
except OverflowError: |
| 228 |
continue |
| 229 |
else: |
| 230 |
raise AssertionError('OverflowError not raised in test ' |
| 231 |
'%s: %s%s' % (id, fn, args_to_str(a))) |
| 232 |
actual = function(*a) |
| 233 |
|
| 234 |
if 'ignore-real-sign' in flags: |
| 235 |
actual = (abs(actual[0]), actual[1]) |
| 236 |
expected = (abs(expected[0]), expected[1]) |
| 237 |
if 'ignore-imag-sign' in flags: |
| 238 |
actual = (actual[0], abs(actual[1])) |
| 239 |
expected = (expected[0], abs(expected[1])) |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
if fn in ('log', 'log10'): |
| 244 |
real_abs_err = 2e-15 |
| 245 |
else: |
| 246 |
real_abs_err = 5e-323 |
| 247 |
error_message = ( |
| 248 |
'%s: %s%s\n' |
| 249 |
'Expected: complex(%r, %r)\n' |
| 250 |
'Received: complex(%r, %r)\n' |
| 251 |
) % (id, fn, args_to_str(a), |
| 252 |
expected[0], expected[1], |
| 253 |
actual[0], actual[1]) |
| 254 |
|
| 255 |
rAssertAlmostEqual(expected[0], actual[0], |
| 256 |
abs_err=real_abs_err, |
| 257 |
msg=error_message) |
| 258 |
rAssertAlmostEqual(expected[1], actual[1], |
| 259 |
abs_err=real_abs_err, |
| 260 |
msg=error_message) |
| 261 |
|
| 262 |
def test_isnan(): |
| 263 |
assert not c.c_isnan(0, 0) |
| 264 |
assert c.c_isnan(float('nan'), 0) |
| 265 |
assert c.c_isnan(1, float('nan')) |
| 266 |
assert not c.c_isnan(float('inf'), 0) |
| 267 |
|
| 268 |
def test_isinf(): |
| 269 |
assert not c.c_isinf(0, 0) |
| 270 |
assert c.c_isinf(float('inf'), 0) |
| 271 |
assert c.c_isinf(float('-inf'), 0) |
| 272 |
assert c.c_isinf(1, float('inf')) |
| 273 |
assert not c.c_isinf(float('nan'), 0) |
| 274 |
|
| 275 |
def test_isfinite(): |
| 276 |
assert c.c_isfinite(0, 0) |
| 277 |
assert not c.c_isfinite(float('nan'), 0) |
| 278 |
assert not c.c_isfinite(float('-inf'), 0) |
| 279 |
assert not c.c_isfinite(0, float('nan')) |
| 280 |
assert not c.c_isfinite(0, float('-inf')) |
| 281 |
|