def test_testfile(self, math_module=math, ulps_err=None): # Rigorous variant of MathTests.test_testfile requiring accuracy in ulps. fail_fmt = "{}:{}({!r}): expected {!r}, got {!r}" failures = [] for id, fn, ar, ai, er, ei, flags in parse_testfile(test_file): # Skip if either the input or result is complex, or if # flags is nonempty if ai != 0. or ei != 0. or flags: continue if fn in ['rect', 'polar']: # no real versions of rect, polar continue if ulps_err is not None: fn_ulps_err = ulps_err else: # java.Math mostly promises 1 ulp, except for: if fn in ['atan2']: fn_ulps_err = 2 elif fn in ['cosh', 'sinh', 'tanh']: fn_ulps_err = 2.5 else: fn_ulps_err = 1 func = getattr(math_module, fn) arg = ar expected = er if 'invalid' in flags or 'divide-by-zero' in flags: expected = 'ValueError' elif 'overflow' in flags: expected = 'OverflowError' try: got = float(func(arg)) except ValueError: got = 'ValueError' except OverflowError: got = 'OverflowError' accuracy_failure = None if isinstance(got, float) and isinstance(expected, float): if math.isnan(expected) and math.isnan(got): continue accuracy_failure = ulps_check(expected, got, fn_ulps_err) if accuracy_failure is None: continue if isinstance(got, str) and isinstance(expected, str): if got == expected: continue fail_msg = fail_fmt.format(id, fn, arg, expected, got) if accuracy_failure is not None: fail_msg += ' ({})'.format(accuracy_failure) failures.append(fail_msg) if failures: self.fail('Failures in test_testfile:\n ' + '\n '.join(failures))
def test_specific_values(self): if not float.__getformat__("double").startswith("IEEE"): return def rect_complex(z): """Wrapped version of rect that accepts a complex number instead of two float arguments.""" return cmath.rect(z.real, z.imag) def polar_complex(z): """Wrapped version of polar that returns a complex number instead of two floats.""" return complex(*polar(z)) for id, fn, ar, ai, er, ei, flags in parse_testfile(test_file): arg = complex(ar, ai) expected = complex(er, ei) if fn == 'rect': function = rect_complex elif fn == 'polar': function = polar_complex else: function = getattr(cmath, fn) if 'divide-by-zero' in flags or 'invalid' in flags: try: try: actual = function(arg) except ValueError: continue else: self.fail('ValueError not raised in test ' '{}: {}(complex({!r}, {!r}))'.format( id, fn, ar, ai)) except AssertionError, ex: print "Got", function, ex except BaseException, ex: print "Got", function, ex
def test_specific_values(self): if not float.__getformat__("double").startswith("IEEE"): return def rect_complex(z): """Wrapped version of rect that accepts a complex number instead of two float arguments.""" return cmath.rect(z.real, z.imag) def polar_complex(z): """Wrapped version of polar that returns a complex number instead of two floats.""" return complex(*polar(z)) for id, fn, ar, ai, er, ei, flags in parse_testfile(test_file): arg = complex(ar, ai) expected = complex(er, ei) if fn == 'rect': function = rect_complex elif fn == 'polar': function = polar_complex else: function = getattr(cmath, fn) if 'divide-by-zero' in flags or 'invalid' in flags: try: try: actual = function(arg) except ValueError: continue else: self.fail('ValueError not raised in test ' '{}: {}(complex({!r}, {!r}))'.format(id, fn, ar, ai)) except AssertionError, ex: print "Got", function, ex except BaseException, ex: print "Got", function, ex
def test_specific_values(self): if not float.__getformat__("double").startswith("IEEE"): return def rect_complex(z): """Wrapped version of rect that accepts a complex number instead of two float arguments.""" return cmath.rect(z.real, z.imag) def polar_complex(z): """Wrapped version of polar that returns a complex number instead of two floats.""" return complex(*polar(z)) for id, fn, ar, ai, er, ei, flags in parse_testfile(test_file): arg = complex(ar, ai) expected = complex(er, ei) if fn == 'rect': function = rect_complex elif fn == 'polar': function = polar_complex else: function = getattr(cmath, fn) if 'divide-by-zero' in flags or 'invalid' in flags: try: actual = function(arg) except ValueError: continue else: self.fail('ValueError not raised in test ' '{}: {}(complex({!r}, {!r}))'.format(id, fn, ar, ai)) if 'overflow' in flags: try: actual = function(arg) except OverflowError: continue else: self.fail('OverflowError not raised in test ' '{}: {}(complex({!r}, {!r}))'.format(id, fn, ar, ai)) actual = function(arg) if 'ignore-real-sign' in flags: actual = complex(abs(actual.real), actual.imag) expected = complex(abs(expected.real), expected.imag) if 'ignore-imag-sign' in flags: actual = complex(actual.real, abs(actual.imag)) expected = complex(expected.real, abs(expected.imag)) # for the real part of the log function, we allow an # absolute error of up to 2e-15. if fn in ('log', 'log10'): real_abs_err = 2e-15 else: real_abs_err = 5e-323 error_message = ( '{}: {}(complex({!r}, {!r}))\n' 'Expected: complex({!r}, {!r})\n' 'Received: complex({!r}, {!r})\n' 'Received value insufficiently close to expected value.' ).format(id, fn, ar, ai, expected.real, expected.imag, actual.real, actual.imag) self.rAssertAlmostEqual(expected.real, actual.real, abs_err=real_abs_err, msg=error_message) self.rAssertAlmostEqual(expected.imag, actual.imag, msg=error_message)
def test_specific_values(self): if not float.__getformat__("double").startswith("IEEE"): self.skipTest('needs IEEE double') def rect_complex(z): """Wrapped version of rect that accepts a complex number instead of two float arguments.""" return cmath.rect(z.real, z.imag) def polar_complex(z): """Wrapped version of polar that returns a complex number instead of two floats.""" return complex(*polar(z)) raised_fmt = '\n' \ '{}: {}(complex({!r}, {!r}))\n' \ 'Raised: {!r}\n' \ 'Expected: complex({!r}, {!r})' not_raised_fmt = '\n' \ '{} not raised in test {}: {}(complex({!r}, {!r}))\n' \ 'Received: complex({!r}, {!r})' value_fmt = '\n' \ '{}: {}(complex({!r}, {!r}))\n' \ 'Expected: complex({!r}, {!r})\n' \ 'Received: complex({!r}, {!r})\n' \ 'Received value insufficiently close to expected value.' failures = 0 for id, fn, ar, ai, er, ei, flags in parse_testfile(test_file): arg = complex(ar, ai) if fn == 'rect': function = rect_complex elif fn == 'polar': function = polar_complex else: function = getattr(cmath, fn) try: # Catch and count failing test cases locally if 'divide-by-zero' in flags or 'invalid' in flags: try: actual = function(arg) except ValueError: pass else: failures += 1 self.fail(not_raised_fmt.format('ValueError', id, fn, ar, ai, actual.real, actual.imag)) elif 'overflow' in flags: try: actual = function(arg) except OverflowError: pass else: failures += 1 self.fail(not_raised_fmt.format('OverflowError', id, fn, ar, ai, actual.real, actual.imag)) else : actual = function(arg) # Make sign of expected conform to actual, where ignored. exr, exi = er, ei if 'ignore-real-sign' in flags: exr = math.copysign(er, actual.real) if 'ignore-imag-sign' in flags: exi = math.copysign(ei, actual.imag) # for the real part of the log function, we allow an # absolute error of up to 2e-15. if fn in ('log', 'log10'): real_abs_err = 2e-15 else: real_abs_err = 5e-323 error_message = value_fmt.format(id, fn, ar, ai, er, ei, actual.real, actual.imag) self.rAssertAlmostEqual(exr, actual.real, abs_err=real_abs_err, msg=error_message) self.rAssertAlmostEqual(exi, actual.imag, msg=error_message) except AssertionError as ex: failures += 1 if verbose : print(ex) except BaseException as ex: failures += 1 if verbose : print(raised_fmt.format(id, fn, ar, ai, ex, er, ei)) if failures : self.fail('{} discrepancies'.format(failures))
def test_specific_values(self): # Some tests need to be skipped on ancient OS X versions. # See issue #27953. SKIP_ON_TIGER = {'tan0064'} osx_version = None if sys.platform == 'darwin': version_txt = platform.mac_ver()[0] try: osx_version = tuple(map(int, version_txt.split('.'))) except ValueError: pass def rect_complex(z): """Wrapped version of rect that accepts a complex number instead of two float arguments.""" return cmath.rect(z.real, z.imag) def polar_complex(z): """Wrapped version of polar that returns a complex number instead of two floats.""" return complex(*polar(z)) for id, fn, ar, ai, er, ei, flags in parse_testfile(test_file): arg = complex(ar, ai) expected = complex(er, ei) # Skip certain tests on OS X 10.4. if osx_version is not None and osx_version < (10, 5): if id in SKIP_ON_TIGER: continue if fn == 'rect': function = rect_complex elif fn == 'polar': function = polar_complex else: function = getattr(cmath, fn) if 'divide-by-zero' in flags or 'invalid' in flags: try: actual = function(arg) except ValueError: continue else: self.fail('ValueError not raised in test ' '{}: {}(complex({!r}, {!r}))'.format( id, fn, ar, ai)) if 'overflow' in flags: try: actual = function(arg) except OverflowError: continue else: self.fail('OverflowError not raised in test ' '{}: {}(complex({!r}, {!r}))'.format( id, fn, ar, ai)) actual = function(arg) if 'ignore-real-sign' in flags: actual = complex(abs(actual.real), actual.imag) expected = complex(abs(expected.real), expected.imag) if 'ignore-imag-sign' in flags: actual = complex(actual.real, abs(actual.imag)) expected = complex(expected.real, abs(expected.imag)) # for the real part of the log function, we allow an # absolute error of up to 2e-15. if fn in ('log', 'log10'): real_abs_err = 2e-15 else: real_abs_err = 5e-323 error_message = ( '{}: {}(complex({!r}, {!r}))\n' 'Expected: complex({!r}, {!r})\n' 'Received: complex({!r}, {!r})\n' 'Received value insufficiently close to expected value.' ).format(id, fn, ar, ai, expected.real, expected.imag, actual.real, actual.imag) self.rAssertAlmostEqual(expected.real, actual.real, abs_err=real_abs_err, msg=error_message) self.rAssertAlmostEqual(expected.imag, actual.imag, msg=error_message)
def test_specific_values(self): if not float.__getformat__("double").startswith("IEEE"): self.skipTest('needs IEEE double') def rect_complex(z): """Wrapped version of rect that accepts a complex number instead of two float arguments.""" return cmath.rect(z.real, z.imag) def polar_complex(z): """Wrapped version of polar that returns a complex number instead of two floats.""" return complex(*polar(z)) raised_fmt = '\n' \ '{}: {}(complex({!r}, {!r}))\n' \ 'Raised: {!r}\n' \ 'Expected: complex({!r}, {!r})' not_raised_fmt = '\n' \ '{} not raised in test {}: {}(complex({!r}, {!r}))\n' \ 'Received: complex({!r}, {!r})' value_fmt = '\n' \ '{}: {}(complex({!r}, {!r}))\n' \ 'Expected: complex({!r}, {!r})\n' \ 'Received: complex({!r}, {!r})\n' \ 'Received value insufficiently close to expected value.' failures = 0 for id, fn, ar, ai, er, ei, flags in parse_testfile(test_file): arg = complex(ar, ai) if fn == 'rect': function = rect_complex elif fn == 'polar': function = polar_complex else: function = getattr(cmath, fn) try: # Catch and count failing test cases locally if 'divide-by-zero' in flags or 'invalid' in flags: try: actual = function(arg) except ValueError: pass else: failures += 1 self.fail( not_raised_fmt.format('ValueError', id, fn, ar, ai, actual.real, actual.imag)) elif 'overflow' in flags: try: actual = function(arg) except OverflowError: pass else: failures += 1 self.fail( not_raised_fmt.format('OverflowError', id, fn, ar, ai, actual.real, actual.imag)) else: actual = function(arg) # Make sign of expected conform to actual, where ignored. exr, exi = er, ei if 'ignore-real-sign' in flags: exr = math.copysign(er, actual.real) if 'ignore-imag-sign' in flags: exi = math.copysign(ei, actual.imag) # for the real part of the log function, we allow an # absolute error of up to 2e-15. if fn in ('log', 'log10'): real_abs_err = 2e-15 else: real_abs_err = 5e-323 error_message = value_fmt.format(id, fn, ar, ai, er, ei, actual.real, actual.imag) self.rAssertAlmostEqual(exr, actual.real, abs_err=real_abs_err, msg=error_message) self.rAssertAlmostEqual(exi, actual.imag, msg=error_message) except AssertionError as ex: failures += 1 if verbose: print(ex) except BaseException as ex: failures += 1 if verbose: print((raised_fmt.format(id, fn, ar, ai, ex, er, ei))) if failures: self.fail('{} discrepancies'.format(failures))
def test_testfile(self, math_module=math, ulps_err=None): # Rigorous variant of MathTests.test_testfile requiring accuracy in ulps. fail_fmt = "{}:{}({!r}): expected {!r}, got {!r}" failures = [] for id, fn, ar, ai, er, ei, flags in parse_testfile(test_file): # Skip if either the input or result is complex, or if # flags is nonempty if ai != 0. or ei != 0. or flags: continue if fn in ['rect', 'polar']: # no real versions of rect, polar continue if ulps_err is not None : fn_ulps_err = ulps_err else : # java.Math mostly promises 1 ulp, except for: if fn in ['atan2'] : fn_ulps_err = 2 elif fn in ['cosh', 'sinh', 'tanh'] : fn_ulps_err = 2.5 else : fn_ulps_err = 1 func = getattr(math_module, fn) arg = ar expected = er if 'invalid' in flags or 'divide-by-zero' in flags: expected = 'ValueError' elif 'overflow' in flags: expected = 'OverflowError' try: got = float(func(arg)) except ValueError: got = 'ValueError' except OverflowError: got = 'OverflowError' accuracy_failure = None if isinstance(got, float) and isinstance(expected, float): if math.isnan(expected) and math.isnan(got): continue accuracy_failure = ulps_check(expected, got, fn_ulps_err) if accuracy_failure is None: continue if isinstance(got, str) and isinstance(expected, str): if got == expected: continue fail_msg = fail_fmt.format(id, fn, arg, expected, got) if accuracy_failure is not None: fail_msg += ' ({})'.format(accuracy_failure) failures.append(fail_msg) if failures: self.fail('Failures in test_testfile:\n ' + '\n '.join(failures))
def test_specific_values(self): # Some tests need to be skipped on ancient OS X versions. # See issue #27953. SKIP_ON_TIGER = {'tan0064'} osx_version = None if sys.platform == 'darwin': version_txt = platform.mac_ver()[0] try: osx_version = tuple(map(int, version_txt.split('.'))) except ValueError: pass def rect_complex(z): """Wrapped version of rect that accepts a complex number instead of two float arguments.""" return cmath.rect(z.real, z.imag) def polar_complex(z): """Wrapped version of polar that returns a complex number instead of two floats.""" return complex(*polar(z)) for id, fn, ar, ai, er, ei, flags in parse_testfile(test_file): arg = complex(ar, ai) expected = complex(er, ei) # Skip certain tests on OS X 10.4. if osx_version is not None and osx_version < (10, 5): if id in SKIP_ON_TIGER: continue if fn == 'rect': function = rect_complex elif fn == 'polar': function = polar_complex else: function = getattr(cmath, fn) if 'divide-by-zero' in flags or 'invalid' in flags: try: actual = function(arg) except ValueError: continue else: self.fail('ValueError not raised in test ' '{}: {}(complex({!r}, {!r}))'.format(id, fn, ar, ai)) if 'overflow' in flags: try: actual = function(arg) except OverflowError: continue else: self.fail('OverflowError not raised in test ' '{}: {}(complex({!r}, {!r}))'.format(id, fn, ar, ai)) actual = function(arg) if 'ignore-real-sign' in flags: actual = complex(abs(actual.real), actual.imag) expected = complex(abs(expected.real), expected.imag) if 'ignore-imag-sign' in flags: actual = complex(actual.real, abs(actual.imag)) expected = complex(expected.real, abs(expected.imag)) # for the real part of the log function, we allow an # absolute error of up to 2e-15. if fn in ('log', 'log10'): real_abs_err = 2e-15 else: real_abs_err = 5e-323 error_message = ( '{}: {}(complex({!r}, {!r}))\n' 'Expected: complex({!r}, {!r})\n' 'Received: complex({!r}, {!r})\n' 'Received value insufficiently close to expected value.' ).format(id, fn, ar, ai, expected.real, expected.imag, actual.real, actual.imag) self.rAssertAlmostEqual(expected.real, actual.real, abs_err=real_abs_err, msg=error_message) self.rAssertAlmostEqual(expected.imag, actual.imag, msg=error_message)