Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Public Attributes
test.test_cmath.CMathTests Class Reference

List of all members.

Public Member Functions

def setUp
def tearDown
def assertFloatIdentical
def assertComplexIdentical
def rAssertAlmostEqual
def test_constants
def test_user_object
def test_input_type
def test_cmath_matches_math
def test_specific_values
def assertCISEqual
def test_polar
def test_phase
def test_abs
def test_abs_overflows
def assertCEqual
def test_rect
def test_isfinite
def test_isnan
def test_isinf
def testTanhSign
def testAtanSign
def testAtanhSign

Public Attributes

 test_values
 value

Static Public Attributes

list test_functions

Detailed Description

Definition at line 49 of file test_cmath.py.


Member Function Documentation

def test.test_cmath.CMathTests.assertCEqual (   self,
  a,
  b 
)

Definition at line 467 of file test_cmath.py.

00467 
00468     def assertCEqual(self, a, b):
00469         eps = 1E-7
00470         if abs(a.real - b[0]) > eps or abs(a.imag - b[1]) > eps:
00471             self.fail((a ,b))

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_cmath.CMathTests.assertCISEqual (   self,
  a,
  b 
)

Definition at line 384 of file test_cmath.py.

00384 
00385     def assertCISEqual(self, a, b):
00386         eps = 1E-7
00387         if abs(a[0] - b[0]) > eps or abs(a[1] - b[1]) > eps:
00388             self.fail((a ,b))

Here is the call graph for this function:

Here is the caller graph for this function:

Fail unless complex numbers x and y have equal values and signs.

In particular, if x and y both have real (or imaginary) part
zero, but the zeros have different signs, this test will fail.

Definition at line 88 of file test_cmath.py.

00088 
00089     def assertComplexIdentical(self, x, y):
00090         """Fail unless complex numbers x and y have equal values and signs.
00091 
00092         In particular, if x and y both have real (or imaginary) part
00093         zero, but the zeros have different signs, this test will fail.
00094 
00095         """
00096         self.assertFloatIdentical(x.real, y.real)
00097         self.assertFloatIdentical(x.imag, y.imag)

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_cmath.CMathTests.assertFloatIdentical (   self,
  x,
  y 
)
Fail unless floats x and y are identical, in the sense that:
(1) both x and y are nans, or
(2) both x and y are infinities, with the same sign, or
(3) both x and y are zeros, with the same sign, or
(4) x and y are both finite and nonzero, and x == y

Definition at line 65 of file test_cmath.py.

00065 
00066     def assertFloatIdentical(self, x, y):
00067         """Fail unless floats x and y are identical, in the sense that:
00068         (1) both x and y are nans, or
00069         (2) both x and y are infinities, with the same sign, or
00070         (3) both x and y are zeros, with the same sign, or
00071         (4) x and y are both finite and nonzero, and x == y
00072 
00073         """
00074         msg = 'floats {!r} and {!r} are not identical'
00075 
00076         if math.isnan(x) or math.isnan(y):
00077             if math.isnan(x) and math.isnan(y):
00078                 return
00079         elif x == y:
00080             if x != 0.0:
00081                 return
00082             # both zero; check that signs match
00083             elif math.copysign(1.0, x) == math.copysign(1.0, y):
00084                 return
00085             else:
00086                 msg += ': zeros have different signs'
00087         self.fail(msg.format(x, y))

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_cmath.CMathTests.rAssertAlmostEqual (   self,
  a,
  b,
  rel_err = 2e-15,
  abs_err = 5e-323,
  msg = None 
)
Fail if the two floating-point numbers are not almost equal.

Determine whether floating-point values a and b are equal to within
a (small) rounding error.  The default values for rel_err and
abs_err are chosen to be suitable for platforms where a float is
represented by an IEEE 754 double.  They allow an error of between
9 and 19 ulps.

Definition at line 99 of file test_cmath.py.

00099 
00100                            msg=None):
00101         """Fail if the two floating-point numbers are not almost equal.
00102 
00103         Determine whether floating-point values a and b are equal to within
00104         a (small) rounding error.  The default values for rel_err and
00105         abs_err are chosen to be suitable for platforms where a float is
00106         represented by an IEEE 754 double.  They allow an error of between
00107         9 and 19 ulps.
00108         """
00109 
00110         # special values testing
00111         if math.isnan(a):
00112             if math.isnan(b):
00113                 return
00114             self.fail(msg or '{!r} should be nan'.format(b))
00115 
00116         if math.isinf(a):
00117             if a == b:
00118                 return
00119             self.fail(msg or 'finite result where infinity expected: '
00120                       'expected {!r}, got {!r}'.format(a, b))
00121 
00122         # if both a and b are zero, check whether they have the same sign
00123         # (in theory there are examples where it would be legitimate for a
00124         # and b to have opposite signs; in practice these hardly ever
00125         # occur).
00126         if not a and not b:
00127             if math.copysign(1., a) != math.copysign(1., b):
00128                 self.fail(msg or 'zero has wrong sign: expected {!r}, '
00129                           'got {!r}'.format(a, b))
00130 
00131         # if a-b overflows, or b is infinite, return False.  Again, in
00132         # theory there are examples where a is within a few ulps of the
00133         # max representable float, and then b could legitimately be
00134         # infinite.  In practice these examples are rare.
00135         try:
00136             absolute_error = abs(b-a)
00137         except OverflowError:
00138             pass
00139         else:
00140             # test passes if either the absolute error or the relative
00141             # error is sufficiently small.  The defaults amount to an
00142             # error of between 9 ulps and 19 ulps on an IEEE-754 compliant
00143             # machine.
00144             if absolute_error <= max(abs_err, rel_err * abs(a)):
00145                 return
00146         self.fail(msg or
00147                   '{!r} and {!r} are not sufficiently close'.format(a, b))

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 59 of file test_cmath.py.

00059 
00060     def setUp(self):
00061         self.test_values = open(test_file)

Here is the caller graph for this function:

Definition at line 62 of file test_cmath.py.

00062 
00063     def tearDown(self):
00064         self.test_values.close()

Here is the caller graph for this function:

Definition at line 437 of file test_cmath.py.

00437 
00438     def test_abs(self):
00439         # zeros
00440         for z in complex_zeros:
00441             self.assertEqual(abs(z), 0.0)
00442 
00443         # infinities
00444         for z in complex_infinities:
00445             self.assertEqual(abs(z), INF)
00446 
00447         # real or imaginary part NaN
00448         self.assertEqual(abs(complex(NAN, -INF)), INF)
00449         self.assertTrue(math.isnan(abs(complex(NAN, -2.3))))
00450         self.assertTrue(math.isnan(abs(complex(NAN, -0.0))))
00451         self.assertTrue(math.isnan(abs(complex(NAN, 0.0))))
00452         self.assertTrue(math.isnan(abs(complex(NAN, 2.3))))
00453         self.assertEqual(abs(complex(NAN, INF)), INF)
00454         self.assertEqual(abs(complex(-INF, NAN)), INF)
00455         self.assertTrue(math.isnan(abs(complex(-2.3, NAN))))
00456         self.assertTrue(math.isnan(abs(complex(-0.0, NAN))))
00457         self.assertTrue(math.isnan(abs(complex(0.0, NAN))))
00458         self.assertTrue(math.isnan(abs(complex(2.3, NAN))))
00459         self.assertEqual(abs(complex(INF, NAN)), INF)
00460         self.assertTrue(math.isnan(abs(complex(NAN, NAN))))
00461 

Here is the call graph for this function:

Definition at line 463 of file test_cmath.py.

00463 
00464     def test_abs_overflows(self):
00465         # result overflows
00466         self.assertRaises(OverflowError, abs, complex(1.4e308, 1.4e308))

Here is the call graph for this function:

Definition at line 267 of file test_cmath.py.

00267 
00268     def test_cmath_matches_math(self):
00269         # check that corresponding cmath and math functions are equal
00270         # for floats in the appropriate range
00271 
00272         # test_values in (0, 1)
00273         test_values = [0.01, 0.1, 0.2, 0.5, 0.9, 0.99]
00274 
00275         # test_values for functions defined on [-1., 1.]
00276         unit_interval = test_values + [-x for x in test_values] + \
00277             [0., 1., -1.]
00278 
00279         # test_values for log, log10, sqrt
00280         positive = test_values + [1.] + [1./x for x in test_values]
00281         nonnegative = [0.] + positive
00282 
00283         # test_values for functions defined on the whole real line
00284         real_line = [0.] + positive + [-x for x in positive]
00285 
00286         test_functions = {
00287             'acos' : unit_interval,
00288             'asin' : unit_interval,
00289             'atan' : real_line,
00290             'cos' : real_line,
00291             'cosh' : real_line,
00292             'exp' : real_line,
00293             'log' : positive,
00294             'log10' : positive,
00295             'sin' : real_line,
00296             'sinh' : real_line,
00297             'sqrt' : nonnegative,
00298             'tan' : real_line,
00299             'tanh' : real_line}
00300 
00301         for fn, values in test_functions.items():
00302             float_fn = getattr(math, fn)
00303             complex_fn = getattr(cmath, fn)
00304             for v in values:
00305                 z = complex_fn(v)
00306                 self.rAssertAlmostEqual(float_fn(v), z.real)
00307                 self.assertEqual(0., z.imag)
00308 
00309         # test two-argument version of log with various bases
00310         for base in [0.5, 2., 10.]:
00311             for v in positive:
00312                 z = cmath.log(v, base)
00313                 self.rAssertAlmostEqual(math.log(v, base), z.real)
00314                 self.assertEqual(0., z.imag)

Here is the call graph for this function:

Definition at line 148 of file test_cmath.py.

00148 
00149     def test_constants(self):
00150         e_expected = 2.71828182845904523536
00151         pi_expected = 3.14159265358979323846
00152         self.assertAlmostEqual(cmath.pi, pi_expected, places=9,
00153             msg="cmath.pi is {}; should be {}".format(cmath.pi, pi_expected))
00154         self.assertAlmostEqual(cmath.e, e_expected, places=9,
00155             msg="cmath.e is {}; should be {}".format(cmath.e, e_expected))

Here is the call graph for this function:

Definition at line 255 of file test_cmath.py.

00255 
00256     def test_input_type(self):
00257         # ints and longs should be acceptable inputs to all cmath
00258         # functions, by virtue of providing a __float__ method
00259         for f in self.test_functions:
00260             for arg in [2, 2.]:
00261                 self.assertEqual(f(arg), f(arg.__float__()))
00262 
00263         # but strings should give a TypeError
00264         for f in self.test_functions:
00265             for arg in ["a", "long_string", "0", "1j", ""]:
00266                 self.assertRaises(TypeError, f, arg)

Here is the call graph for this function:

Definition at line 479 of file test_cmath.py.

00479 
00480     def test_isfinite(self):
00481         real_vals = [float('-inf'), -2.3, -0.0,
00482                      0.0, 2.3, float('inf'), float('nan')]
00483         for x in real_vals:
00484             for y in real_vals:
00485                 z = complex(x, y)
00486                 self.assertEqual(cmath.isfinite(z),
00487                                   math.isfinite(x) and math.isfinite(y))

Here is the call graph for this function:

Definition at line 499 of file test_cmath.py.

00499 
00500     def test_isinf(self):
00501         self.assertFalse(cmath.isinf(1))
00502         self.assertFalse(cmath.isinf(1j))
00503         self.assertFalse(cmath.isinf(NAN))
00504         self.assertTrue(cmath.isinf(INF))
00505         self.assertTrue(cmath.isinf(complex(INF, 0)))
00506         self.assertTrue(cmath.isinf(complex(0, INF)))
00507         self.assertTrue(cmath.isinf(complex(INF, INF)))
00508         self.assertTrue(cmath.isinf(complex(NAN, INF)))
00509         self.assertTrue(cmath.isinf(complex(INF, NAN)))

Here is the call graph for this function:

Definition at line 488 of file test_cmath.py.

00488 
00489     def test_isnan(self):
00490         self.assertFalse(cmath.isnan(1))
00491         self.assertFalse(cmath.isnan(1j))
00492         self.assertFalse(cmath.isnan(INF))
00493         self.assertTrue(cmath.isnan(NAN))
00494         self.assertTrue(cmath.isnan(complex(NAN, 0)))
00495         self.assertTrue(cmath.isnan(complex(0, NAN)))
00496         self.assertTrue(cmath.isnan(complex(NAN, NAN)))
00497         self.assertTrue(cmath.isnan(complex(NAN, INF)))
00498         self.assertTrue(cmath.isnan(complex(INF, NAN)))

Here is the call graph for this function:

Definition at line 396 of file test_cmath.py.

00396 
00397     def test_phase(self):
00398         self.assertAlmostEqual(phase(0), 0.)
00399         self.assertAlmostEqual(phase(1.), 0.)
00400         self.assertAlmostEqual(phase(-1.), pi)
00401         self.assertAlmostEqual(phase(-1.+1E-300j), pi)
00402         self.assertAlmostEqual(phase(-1.-1E-300j), -pi)
00403         self.assertAlmostEqual(phase(1j), pi/2)
00404         self.assertAlmostEqual(phase(-1j), -pi/2)
00405 
00406         # zeros
00407         self.assertEqual(phase(complex(0.0, 0.0)), 0.0)
00408         self.assertEqual(phase(complex(0.0, -0.0)), -0.0)
00409         self.assertEqual(phase(complex(-0.0, 0.0)), pi)
00410         self.assertEqual(phase(complex(-0.0, -0.0)), -pi)
00411 
00412         # infinities
00413         self.assertAlmostEqual(phase(complex(-INF, -0.0)), -pi)
00414         self.assertAlmostEqual(phase(complex(-INF, -2.3)), -pi)
00415         self.assertAlmostEqual(phase(complex(-INF, -INF)), -0.75*pi)
00416         self.assertAlmostEqual(phase(complex(-2.3, -INF)), -pi/2)
00417         self.assertAlmostEqual(phase(complex(-0.0, -INF)), -pi/2)
00418         self.assertAlmostEqual(phase(complex(0.0, -INF)), -pi/2)
00419         self.assertAlmostEqual(phase(complex(2.3, -INF)), -pi/2)
00420         self.assertAlmostEqual(phase(complex(INF, -INF)), -pi/4)
00421         self.assertEqual(phase(complex(INF, -2.3)), -0.0)
00422         self.assertEqual(phase(complex(INF, -0.0)), -0.0)
00423         self.assertEqual(phase(complex(INF, 0.0)), 0.0)
00424         self.assertEqual(phase(complex(INF, 2.3)), 0.0)
00425         self.assertAlmostEqual(phase(complex(INF, INF)), pi/4)
00426         self.assertAlmostEqual(phase(complex(2.3, INF)), pi/2)
00427         self.assertAlmostEqual(phase(complex(0.0, INF)), pi/2)
00428         self.assertAlmostEqual(phase(complex(-0.0, INF)), pi/2)
00429         self.assertAlmostEqual(phase(complex(-2.3, INF)), pi/2)
00430         self.assertAlmostEqual(phase(complex(-INF, INF)), 0.75*pi)
00431         self.assertAlmostEqual(phase(complex(-INF, 2.3)), pi)
00432         self.assertAlmostEqual(phase(complex(-INF, 0.0)), pi)
00433 
00434         # real or imaginary part NaN
00435         for z in complex_nans:
00436             self.assertTrue(math.isnan(phase(z)))

Here is the call graph for this function:

Definition at line 389 of file test_cmath.py.

00389 
00390     def test_polar(self):
00391         self.assertCISEqual(polar(0), (0., 0.))
00392         self.assertCISEqual(polar(1.), (1., 0.))
00393         self.assertCISEqual(polar(-1.), (1., pi))
00394         self.assertCISEqual(polar(1j), (1., pi/2))
00395         self.assertCISEqual(polar(-1j), (1., -pi/2))

Here is the call graph for this function:

Definition at line 472 of file test_cmath.py.

00472 
00473     def test_rect(self):
00474         self.assertCEqual(rect(0, 0), (0, 0))
00475         self.assertCEqual(rect(1, 0), (1., 0))
00476         self.assertCEqual(rect(1, -pi), (-1., 0))
00477         self.assertCEqual(rect(1, pi/2), (0, 1.))
00478         self.assertCEqual(rect(1, -pi/2), (0, -1.))

Here is the call graph for this function:

Definition at line 316 of file test_cmath.py.

00316 
00317     def test_specific_values(self):
00318         def rect_complex(z):
00319             """Wrapped version of rect that accepts a complex number instead of
00320             two float arguments."""
00321             return cmath.rect(z.real, z.imag)
00322 
00323         def polar_complex(z):
00324             """Wrapped version of polar that returns a complex number instead of
00325             two floats."""
00326             return complex(*polar(z))
00327 
00328         for id, fn, ar, ai, er, ei, flags in parse_testfile(test_file):
00329             arg = complex(ar, ai)
00330             expected = complex(er, ei)
00331             if fn == 'rect':
00332                 function = rect_complex
00333             elif fn == 'polar':
00334                 function = polar_complex
00335             else:
00336                 function = getattr(cmath, fn)
00337             if 'divide-by-zero' in flags or 'invalid' in flags:
00338                 try:
00339                     actual = function(arg)
00340                 except ValueError:
00341                     continue
00342                 else:
00343                     self.fail('ValueError not raised in test '
00344                           '{}: {}(complex({!r}, {!r}))'.format(id, fn, ar, ai))
00345 
00346             if 'overflow' in flags:
00347                 try:
00348                     actual = function(arg)
00349                 except OverflowError:
00350                     continue
00351                 else:
00352                     self.fail('OverflowError not raised in test '
00353                           '{}: {}(complex({!r}, {!r}))'.format(id, fn, ar, ai))
00354 
00355             actual = function(arg)
00356 
00357             if 'ignore-real-sign' in flags:
00358                 actual = complex(abs(actual.real), actual.imag)
00359                 expected = complex(abs(expected.real), expected.imag)
00360             if 'ignore-imag-sign' in flags:
00361                 actual = complex(actual.real, abs(actual.imag))
00362                 expected = complex(expected.real, abs(expected.imag))
00363 
00364             # for the real part of the log function, we allow an
00365             # absolute error of up to 2e-15.
00366             if fn in ('log', 'log10'):
00367                 real_abs_err = 2e-15
00368             else:
00369                 real_abs_err = 5e-323
00370 
00371             error_message = (
00372                 '{}: {}(complex({!r}, {!r}))\n'
00373                 'Expected: complex({!r}, {!r})\n'
00374                 'Received: complex({!r}, {!r})\n'
00375                 'Received value insufficiently close to expected value.'
00376                 ).format(id, fn, ar, ai,
00377                      expected.real, expected.imag,
00378                      actual.real, actual.imag)
00379             self.rAssertAlmostEqual(expected.real, actual.real,
00380                                         abs_err=real_abs_err,
00381                                         msg=error_message)
00382             self.rAssertAlmostEqual(expected.imag, actual.imag,
00383                                         msg=error_message)

Here is the call graph for this function:

Definition at line 156 of file test_cmath.py.

00156 
00157     def test_user_object(self):
00158         # Test automatic calling of __complex__ and __float__ by cmath
00159         # functions
00160 
00161         # some random values to use as test values; we avoid values
00162         # for which any of the functions in cmath is undefined
00163         # (i.e. 0., 1., -1., 1j, -1j) or would cause overflow
00164         cx_arg = 4.419414439 + 1.497100113j
00165         flt_arg = -6.131677725
00166 
00167         # a variety of non-complex numbers, used to check that
00168         # non-complex return values from __complex__ give an error
00169         non_complexes = ["not complex", 1, 5, 2., None,
00170                          object(), NotImplemented]
00171 
00172         # Now we introduce a variety of classes whose instances might
00173         # end up being passed to the cmath functions
00174 
00175         # usual case: new-style class implementing __complex__
00176         class MyComplex(object):
00177             def __init__(self, value):
00178                 self.value = value
00179             def __complex__(self):
00180                 return self.value
00181 
00182         # old-style class implementing __complex__
00183         class MyComplexOS:
00184             def __init__(self, value):
00185                 self.value = value
00186             def __complex__(self):
00187                 return self.value
00188 
00189         # classes for which __complex__ raises an exception
00190         class SomeException(Exception):
00191             pass
00192         class MyComplexException(object):
00193             def __complex__(self):
00194                 raise SomeException
00195         class MyComplexExceptionOS:
00196             def __complex__(self):
00197                 raise SomeException
00198 
00199         # some classes not providing __float__ or __complex__
00200         class NeitherComplexNorFloat(object):
00201             pass
00202         class NeitherComplexNorFloatOS:
00203             pass
00204         class MyInt(object):
00205             def __int__(self): return 2
00206             def __index__(self): return 2
00207         class MyIntOS:
00208             def __int__(self): return 2
00209             def __index__(self): return 2
00210 
00211         # other possible combinations of __float__ and __complex__
00212         # that should work
00213         class FloatAndComplex(object):
00214             def __float__(self):
00215                 return flt_arg
00216             def __complex__(self):
00217                 return cx_arg
00218         class FloatAndComplexOS:
00219             def __float__(self):
00220                 return flt_arg
00221             def __complex__(self):
00222                 return cx_arg
00223         class JustFloat(object):
00224             def __float__(self):
00225                 return flt_arg
00226         class JustFloatOS:
00227             def __float__(self):
00228                 return flt_arg
00229 
00230         for f in self.test_functions:
00231             # usual usage
00232             self.assertEqual(f(MyComplex(cx_arg)), f(cx_arg))
00233             self.assertEqual(f(MyComplexOS(cx_arg)), f(cx_arg))
00234             # other combinations of __float__ and __complex__
00235             self.assertEqual(f(FloatAndComplex()), f(cx_arg))
00236             self.assertEqual(f(FloatAndComplexOS()), f(cx_arg))
00237             self.assertEqual(f(JustFloat()), f(flt_arg))
00238             self.assertEqual(f(JustFloatOS()), f(flt_arg))
00239             # TypeError should be raised for classes not providing
00240             # either __complex__ or __float__, even if they provide
00241             # __int__ or __index__.  An old-style class
00242             # currently raises AttributeError instead of a TypeError;
00243             # this could be considered a bug.
00244             self.assertRaises(TypeError, f, NeitherComplexNorFloat())
00245             self.assertRaises(TypeError, f, MyInt())
00246             self.assertRaises(Exception, f, NeitherComplexNorFloatOS())
00247             self.assertRaises(Exception, f, MyIntOS())
00248             # non-complex return value from __complex__ -> TypeError
00249             for bad_complex in non_complexes:
00250                 self.assertRaises(TypeError, f, MyComplex(bad_complex))
00251                 self.assertRaises(TypeError, f, MyComplexOS(bad_complex))
00252             # exceptions in __complex__ should be propagated correctly
00253             self.assertRaises(SomeException, f, MyComplexException())
00254             self.assertRaises(SomeException, f, MyComplexExceptionOS())

Here is the call graph for this function:

Definition at line 531 of file test_cmath.py.

00531 
00532     def testAtanhSign(self):
00533         for z in complex_zeros:
00534             self.assertComplexIdentical(cmath.atanh(z), z)
00535 

Here is the call graph for this function:

Definition at line 524 of file test_cmath.py.

00524 
00525     def testAtanSign(self):
00526         for z in complex_zeros:
00527             self.assertComplexIdentical(cmath.atan(z), z)

Here is the call graph for this function:

Definition at line 513 of file test_cmath.py.

00513 
00514     def testTanhSign(self):
00515         for z in complex_zeros:
00516             self.assertComplexIdentical(cmath.tanh(z), z)

Here is the call graph for this function:


Member Data Documentation

Initial value:
[getattr(cmath, fname) for fname in [
            'acos', 'acosh', 'asin', 'asinh', 'atan', 'atanh',
            'cos', 'cosh', 'exp', 'log', 'log10', 'sin', 'sinh',
            'sqrt', 'tan', 'tanh']]

Definition at line 51 of file test_cmath.py.

Definition at line 60 of file test_cmath.py.

Definition at line 177 of file test_cmath.py.


The documentation for this class was generated from the following file: