Commit f295d1d0 authored by Georg Brandl's avatar Georg Brandl

Convert test_math to unittest.

parent b3f60cfd
test_math
math module, testing with eps 1e-05
constants
acos
asin
atan
atan2
ceil
cos
cosh
degrees
exp
fabs
floor
fmod
frexp
hypot
ldexp
log
log10
modf
pow
radians
sin
sinh
sqrt
tan
tanh
# Python test set -- math module # Python test set -- math module
# XXXX Should not do tests around zero only # XXXX Should not do tests around zero only
from test.test_support import TestFailed, verbose from test.test_support import run_unittest, verbose
import unittest
import math
seps='1e-05' seps='1e-05'
eps = eval(seps) eps = eval(seps)
print 'math module, testing with eps', seps
import math
def testit(name, value, expected): class MathTests(unittest.TestCase):
if abs(value-expected) > eps:
raise TestFailed, '%s returned %f, expected %f'%\ def ftest(self, name, value, expected):
(name, value, expected) if abs(value-expected) > eps:
self.fail('%s returned %f, expected %f'%\
print 'constants' (name, value, expected))
testit('pi', math.pi, 3.1415926)
testit('e', math.e, 2.7182818) def testConstants(self):
self.ftest('pi', math.pi, 3.1415926)
print 'acos' self.ftest('e', math.e, 2.7182818)
testit('acos(-1)', math.acos(-1), math.pi)
testit('acos(0)', math.acos(0), math.pi/2) def testAcos(self):
testit('acos(1)', math.acos(1), 0) self.ftest('acos(-1)', math.acos(-1), math.pi)
self.ftest('acos(0)', math.acos(0), math.pi/2)
print 'asin' self.ftest('acos(1)', math.acos(1), 0)
testit('asin(-1)', math.asin(-1), -math.pi/2)
testit('asin(0)', math.asin(0), 0) def testAsin(self):
testit('asin(1)', math.asin(1), math.pi/2) self.ftest('asin(-1)', math.asin(-1), -math.pi/2)
self.ftest('asin(0)', math.asin(0), 0)
print 'atan' self.ftest('asin(1)', math.asin(1), math.pi/2)
testit('atan(-1)', math.atan(-1), -math.pi/4)
testit('atan(0)', math.atan(0), 0) def testAtan(self):
testit('atan(1)', math.atan(1), math.pi/4) self.ftest('atan(-1)', math.atan(-1), -math.pi/4)
self.ftest('atan(0)', math.atan(0), 0)
print 'atan2' self.ftest('atan(1)', math.atan(1), math.pi/4)
testit('atan2(-1, 0)', math.atan2(-1, 0), -math.pi/2)
testit('atan2(-1, 1)', math.atan2(-1, 1), -math.pi/4) def testAtan2(self):
testit('atan2(0, 1)', math.atan2(0, 1), 0) self.ftest('atan2(-1, 0)', math.atan2(-1, 0), -math.pi/2)
testit('atan2(1, 1)', math.atan2(1, 1), math.pi/4) self.ftest('atan2(-1, 1)', math.atan2(-1, 1), -math.pi/4)
testit('atan2(1, 0)', math.atan2(1, 0), math.pi/2) self.ftest('atan2(0, 1)', math.atan2(0, 1), 0)
self.ftest('atan2(1, 1)', math.atan2(1, 1), math.pi/4)
print 'ceil' self.ftest('atan2(1, 0)', math.atan2(1, 0), math.pi/2)
testit('ceil(0.5)', math.ceil(0.5), 1)
testit('ceil(1.0)', math.ceil(1.0), 1) def testCeil(self):
testit('ceil(1.5)', math.ceil(1.5), 2) self.ftest('ceil(0.5)', math.ceil(0.5), 1)
testit('ceil(-0.5)', math.ceil(-0.5), 0) self.ftest('ceil(1.0)', math.ceil(1.0), 1)
testit('ceil(-1.0)', math.ceil(-1.0), -1) self.ftest('ceil(1.5)', math.ceil(1.5), 2)
testit('ceil(-1.5)', math.ceil(-1.5), -1) self.ftest('ceil(-0.5)', math.ceil(-0.5), 0)
self.ftest('ceil(-1.0)', math.ceil(-1.0), -1)
print 'cos' self.ftest('ceil(-1.5)', math.ceil(-1.5), -1)
testit('cos(-pi/2)', math.cos(-math.pi/2), 0)
testit('cos(0)', math.cos(0), 1) def testCos(self):
testit('cos(pi/2)', math.cos(math.pi/2), 0) self.ftest('cos(-pi/2)', math.cos(-math.pi/2), 0)
testit('cos(pi)', math.cos(math.pi), -1) self.ftest('cos(0)', math.cos(0), 1)
self.ftest('cos(pi/2)', math.cos(math.pi/2), 0)
print 'cosh' self.ftest('cos(pi)', math.cos(math.pi), -1)
testit('cosh(0)', math.cosh(0), 1)
testit('cosh(2)-2*cosh(1)**2', math.cosh(2)-2*math.cosh(1)**2, -1) # Thanks to Lambert def testCosh(self):
self.ftest('cosh(0)', math.cosh(0), 1)
print 'degrees' self.ftest('cosh(2)-2*cosh(1)**2', math.cosh(2)-2*math.cosh(1)**2, -1) # Thanks to Lambert
testit('degrees(pi)', math.degrees(math.pi), 180.0)
testit('degrees(pi/2)', math.degrees(math.pi/2), 90.0) def testDegrees(self):
testit('degrees(-pi/4)', math.degrees(-math.pi/4), -45.0) self.ftest('degrees(pi)', math.degrees(math.pi), 180.0)
self.ftest('degrees(pi/2)', math.degrees(math.pi/2), 90.0)
print 'exp' self.ftest('degrees(-pi/4)', math.degrees(-math.pi/4), -45.0)
testit('exp(-1)', math.exp(-1), 1/math.e)
testit('exp(0)', math.exp(0), 1) def testExp(self):
testit('exp(1)', math.exp(1), math.e) self.ftest('exp(-1)', math.exp(-1), 1/math.e)
self.ftest('exp(0)', math.exp(0), 1)
print 'fabs' self.ftest('exp(1)', math.exp(1), math.e)
testit('fabs(-1)', math.fabs(-1), 1)
testit('fabs(0)', math.fabs(0), 0) def testFabs(self):
testit('fabs(1)', math.fabs(1), 1) self.ftest('fabs(-1)', math.fabs(-1), 1)
self.ftest('fabs(0)', math.fabs(0), 0)
print 'floor' self.ftest('fabs(1)', math.fabs(1), 1)
testit('floor(0.5)', math.floor(0.5), 0)
testit('floor(1.0)', math.floor(1.0), 1) def testFloor(self):
testit('floor(1.5)', math.floor(1.5), 1) self.ftest('floor(0.5)', math.floor(0.5), 0)
testit('floor(-0.5)', math.floor(-0.5), -1) self.ftest('floor(1.0)', math.floor(1.0), 1)
testit('floor(-1.0)', math.floor(-1.0), -1) self.ftest('floor(1.5)', math.floor(1.5), 1)
testit('floor(-1.5)', math.floor(-1.5), -2) self.ftest('floor(-0.5)', math.floor(-0.5), -1)
self.ftest('floor(-1.0)', math.floor(-1.0), -1)
print 'fmod' self.ftest('floor(-1.5)', math.floor(-1.5), -2)
testit('fmod(10,1)', math.fmod(10,1), 0)
testit('fmod(10,0.5)', math.fmod(10,0.5), 0) def testFmod(self):
testit('fmod(10,1.5)', math.fmod(10,1.5), 1) self.ftest('fmod(10,1)', math.fmod(10,1), 0)
testit('fmod(-10,1)', math.fmod(-10,1), 0) self.ftest('fmod(10,0.5)', math.fmod(10,0.5), 0)
testit('fmod(-10,0.5)', math.fmod(-10,0.5), 0) self.ftest('fmod(10,1.5)', math.fmod(10,1.5), 1)
testit('fmod(-10,1.5)', math.fmod(-10,1.5), -1) self.ftest('fmod(-10,1)', math.fmod(-10,1), 0)
self.ftest('fmod(-10,0.5)', math.fmod(-10,0.5), 0)
print 'frexp' self.ftest('fmod(-10,1.5)', math.fmod(-10,1.5), -1)
def testfrexp(name, (mant, exp), (emant, eexp)):
if abs(mant-emant) > eps or exp != eexp: def testFrexp(self):
raise TestFailed, '%s returned %r, expected %r'%\ def testfrexp(name, (mant, exp), (emant, eexp)):
(name, (mant, exp), (emant,eexp)) if abs(mant-emant) > eps or exp != eexp:
self.fail('%s returned %r, expected %r'%\
testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1)) (name, (mant, exp), (emant,eexp)))
testfrexp('frexp(0)', math.frexp(0), (0, 0))
testfrexp('frexp(1)', math.frexp(1), (0.5, 1)) testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1))
testfrexp('frexp(2)', math.frexp(2), (0.5, 2)) testfrexp('frexp(0)', math.frexp(0), (0, 0))
testfrexp('frexp(1)', math.frexp(1), (0.5, 1))
print 'hypot' testfrexp('frexp(2)', math.frexp(2), (0.5, 2))
testit('hypot(0,0)', math.hypot(0,0), 0)
testit('hypot(3,4)', math.hypot(3,4), 5) def testHypot(self):
self.ftest('hypot(0,0)', math.hypot(0,0), 0)
print 'ldexp' self.ftest('hypot(3,4)', math.hypot(3,4), 5)
testit('ldexp(0,1)', math.ldexp(0,1), 0)
testit('ldexp(1,1)', math.ldexp(1,1), 2) def testLdexp(self):
testit('ldexp(1,-1)', math.ldexp(1,-1), 0.5) self.ftest('ldexp(0,1)', math.ldexp(0,1), 0)
testit('ldexp(-1,1)', math.ldexp(-1,1), -2) self.ftest('ldexp(1,1)', math.ldexp(1,1), 2)
self.ftest('ldexp(1,-1)', math.ldexp(1,-1), 0.5)
print 'log' self.ftest('ldexp(-1,1)', math.ldexp(-1,1), -2)
testit('log(1/e)', math.log(1/math.e), -1)
testit('log(1)', math.log(1), 0) def testLog(self):
testit('log(e)', math.log(math.e), 1) self.ftest('log(1/e)', math.log(1/math.e), -1)
testit('log(32,2)', math.log(32,2), 5) self.ftest('log(1)', math.log(1), 0)
testit('log(10**40, 10)', math.log(10**40, 10), 40) self.ftest('log(e)', math.log(math.e), 1)
testit('log(10**40, 10**20)', math.log(10**40, 10**20), 2) self.ftest('log(32,2)', math.log(32,2), 5)
self.ftest('log(10**40, 10)', math.log(10**40, 10), 40)
print 'log10' self.ftest('log(10**40, 10**20)', math.log(10**40, 10**20), 2)
testit('log10(0.1)', math.log10(0.1), -1)
testit('log10(1)', math.log10(1), 0) def testLog10(self):
testit('log10(10)', math.log10(10), 1) self.ftest('log10(0.1)', math.log10(0.1), -1)
self.ftest('log10(1)', math.log10(1), 0)
print 'modf' self.ftest('log10(10)', math.log10(10), 1)
def testmodf(name, (v1, v2), (e1, e2)):
if abs(v1-e1) > eps or abs(v2-e2): def testModf(self):
raise TestFailed, '%s returned %r, expected %r'%\ def testmodf(name, (v1, v2), (e1, e2)):
(name, (v1,v2), (e1,e2)) if abs(v1-e1) > eps or abs(v2-e2):
self.fail('%s returned %r, expected %r'%\
testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0)) (name, (v1,v2), (e1,e2)))
testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0))
testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0))
print 'pow' testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0))
testit('pow(0,1)', math.pow(0,1), 0)
testit('pow(1,0)', math.pow(1,0), 1) def testPow(self):
testit('pow(2,1)', math.pow(2,1), 2) self.ftest('pow(0,1)', math.pow(0,1), 0)
testit('pow(2,-1)', math.pow(2,-1), 0.5) self.ftest('pow(1,0)', math.pow(1,0), 1)
self.ftest('pow(2,1)', math.pow(2,1), 2)
print 'radians' self.ftest('pow(2,-1)', math.pow(2,-1), 0.5)
testit('radians(180)', math.radians(180), math.pi)
testit('radians(90)', math.radians(90), math.pi/2) def testRadians(self):
testit('radians(-45)', math.radians(-45), -math.pi/4) self.ftest('radians(180)', math.radians(180), math.pi)
self.ftest('radians(90)', math.radians(90), math.pi/2)
print 'sin' self.ftest('radians(-45)', math.radians(-45), -math.pi/4)
testit('sin(0)', math.sin(0), 0)
testit('sin(pi/2)', math.sin(math.pi/2), 1) def testSin(self):
testit('sin(-pi/2)', math.sin(-math.pi/2), -1) self.ftest('sin(0)', math.sin(0), 0)
self.ftest('sin(pi/2)', math.sin(math.pi/2), 1)
print 'sinh' self.ftest('sin(-pi/2)', math.sin(-math.pi/2), -1)
testit('sinh(0)', math.sinh(0), 0)
testit('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1) def testSinh(self):
testit('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0) self.ftest('sinh(0)', math.sinh(0), 0)
self.ftest('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1)
print 'sqrt' self.ftest('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0)
testit('sqrt(0)', math.sqrt(0), 0)
testit('sqrt(1)', math.sqrt(1), 1) def testSqrt(self):
testit('sqrt(4)', math.sqrt(4), 2) self.ftest('sqrt(0)', math.sqrt(0), 0)
self.ftest('sqrt(1)', math.sqrt(1), 1)
print 'tan' self.ftest('sqrt(4)', math.sqrt(4), 2)
testit('tan(0)', math.tan(0), 0)
testit('tan(pi/4)', math.tan(math.pi/4), 1) def testTan(self):
testit('tan(-pi/4)', math.tan(-math.pi/4), -1) self.ftest('tan(0)', math.tan(0), 0)
self.ftest('tan(pi/4)', math.tan(math.pi/4), 1)
print 'tanh' self.ftest('tan(-pi/4)', math.tan(-math.pi/4), -1)
testit('tanh(0)', math.tanh(0), 0)
testit('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0) def testTanh(self):
self.ftest('tanh(0)', math.tanh(0), 0)
# RED_FLAG 16-Oct-2000 Tim self.ftest('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)
# While 2.0 is more consistent about exceptions than previous releases, it
# still fails this part of the test on some platforms. For now, we only # RED_FLAG 16-Oct-2000 Tim
# *run* test_exceptions() in verbose mode, so that this isn't normally # While 2.0 is more consistent about exceptions than previous releases, it
# tested. # still fails this part of the test on some platforms. For now, we only
# *run* test_exceptions() in verbose mode, so that this isn't normally
def test_exceptions(): # tested.
print 'exceptions'
try: if verbose:
x = math.exp(-1000000000) def test_exceptions(self):
except: try:
# mathmodule.c is failing to weed out underflows from libm, or x = math.exp(-1000000000)
# we've got an fp format with huge dynamic range except:
raise TestFailed("underflowing exp() should not have raised " # mathmodule.c is failing to weed out underflows from libm, or
"an exception") # we've got an fp format with huge dynamic range
if x != 0: self.fail("underflowing exp() should not have raised "
raise TestFailed("underflowing exp() should have returned 0") "an exception")
if x != 0:
# If this fails, probably using a strict IEEE-754 conforming libm, and x self.fail("underflowing exp() should have returned 0")
# is +Inf afterwards. But Python wants overflows detected by default.
try: # If this fails, probably using a strict IEEE-754 conforming libm, and x
x = math.exp(1000000000) # is +Inf afterwards. But Python wants overflows detected by default.
except OverflowError: try:
pass x = math.exp(1000000000)
else: except OverflowError:
raise TestFailed("overflowing exp() didn't trigger OverflowError") pass
else:
# If this fails, it could be a puzzle. One odd possibility is that self.fail("overflowing exp() didn't trigger OverflowError")
# mathmodule.c's macros are getting confused while comparing
# Inf (HUGE_VAL) to a NaN, and artificially setting errno to ERANGE # If this fails, it could be a puzzle. One odd possibility is that
# as a result (and so raising OverflowError instead). # mathmodule.c's macros are getting confused while comparing
try: # Inf (HUGE_VAL) to a NaN, and artificially setting errno to ERANGE
x = math.sqrt(-1.0) # as a result (and so raising OverflowError instead).
except ValueError: try:
pass x = math.sqrt(-1.0)
else: except ValueError:
raise TestFailed("sqrt(-1) didn't raise ValueError") pass
else:
if verbose: self.fail("sqrt(-1) didn't raise ValueError")
test_exceptions()
def test_main():
run_unittest(MathTests)
if __name__ == '__main__':
test_main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment