0001 # Python test set -- math module 0002 # XXXX Should not do tests around zero only 0003 0004 from test.test_support import TestFailed, verbose 0005 0006 seps='1e-05' 0007 eps = eval(seps) 0008 print 'math module, testing with eps', seps 0009 import math 0010 0011 def testit(name, value, expected): 0012 if abs(value-expected) > eps: 0013 raise TestFailed, '%s returned %f, expected %f'%\ 0014 (name, value, expected) 0015 0016 print 'constants' 0017 testit('pi', math.pi, 3.1415926) 0018 testit('e', math.e, 2.7182818) 0019 0020 print 'acos' 0021 testit('acos(-1)', math.acos(-1), math.pi) 0022 testit('acos(0)', math.acos(0), math.pi/2) 0023 testit('acos(1)', math.acos(1), 0) 0024 0025 print 'asin' 0026 testit('asin(-1)', math.asin(-1), -math.pi/2) 0027 testit('asin(0)', math.asin(0), 0) 0028 testit('asin(1)', math.asin(1), math.pi/2) 0029 0030 print 'atan' 0031 testit('atan(-1)', math.atan(-1), -math.pi/4) 0032 testit('atan(0)', math.atan(0), 0) 0033 testit('atan(1)', math.atan(1), math.pi/4) 0034 0035 print 'atan2' 0036 testit('atan2(-1, 0)', math.atan2(-1, 0), -math.pi/2) 0037 testit('atan2(-1, 1)', math.atan2(-1, 1), -math.pi/4) 0038 testit('atan2(0, 1)', math.atan2(0, 1), 0) 0039 testit('atan2(1, 1)', math.atan2(1, 1), math.pi/4) 0040 testit('atan2(1, 0)', math.atan2(1, 0), math.pi/2) 0041 0042 print 'ceil' 0043 testit('ceil(0.5)', math.ceil(0.5), 1) 0044 testit('ceil(1.0)', math.ceil(1.0), 1) 0045 testit('ceil(1.5)', math.ceil(1.5), 2) 0046 testit('ceil(-0.5)', math.ceil(-0.5), 0) 0047 testit('ceil(-1.0)', math.ceil(-1.0), -1) 0048 testit('ceil(-1.5)', math.ceil(-1.5), -1) 0049 0050 print 'cos' 0051 testit('cos(-pi/2)', math.cos(-math.pi/2), 0) 0052 testit('cos(0)', math.cos(0), 1) 0053 testit('cos(pi/2)', math.cos(math.pi/2), 0) 0054 testit('cos(pi)', math.cos(math.pi), -1) 0055 0056 print 'cosh' 0057 testit('cosh(0)', math.cosh(0), 1) 0058 testit('cosh(2)-2*cosh(1)**2', math.cosh(2)-2*math.cosh(1)**2, -1) # Thanks to Lambert 0059 0060 print 'degrees' 0061 testit('degrees(pi)', math.degrees(math.pi), 180.0) 0062 testit('degrees(pi/2)', math.degrees(math.pi/2), 90.0) 0063 testit('degrees(-pi/4)', math.degrees(-math.pi/4), -45.0) 0064 0065 print 'exp' 0066 testit('exp(-1)', math.exp(-1), 1/math.e) 0067 testit('exp(0)', math.exp(0), 1) 0068 testit('exp(1)', math.exp(1), math.e) 0069 0070 print 'fabs' 0071 testit('fabs(-1)', math.fabs(-1), 1) 0072 testit('fabs(0)', math.fabs(0), 0) 0073 testit('fabs(1)', math.fabs(1), 1) 0074 0075 print 'floor' 0076 testit('floor(0.5)', math.floor(0.5), 0) 0077 testit('floor(1.0)', math.floor(1.0), 1) 0078 testit('floor(1.5)', math.floor(1.5), 1) 0079 testit('floor(-0.5)', math.floor(-0.5), -1) 0080 testit('floor(-1.0)', math.floor(-1.0), -1) 0081 testit('floor(-1.5)', math.floor(-1.5), -2) 0082 0083 print 'fmod' 0084 testit('fmod(10,1)', math.fmod(10,1), 0) 0085 testit('fmod(10,0.5)', math.fmod(10,0.5), 0) 0086 testit('fmod(10,1.5)', math.fmod(10,1.5), 1) 0087 testit('fmod(-10,1)', math.fmod(-10,1), 0) 0088 testit('fmod(-10,0.5)', math.fmod(-10,0.5), 0) 0089 testit('fmod(-10,1.5)', math.fmod(-10,1.5), -1) 0090 0091 print 'frexp' 0092 def testfrexp(name, (mant, exp), (emant, eexp)): 0093 if abs(mant-emant) > eps or exp != eexp: 0094 raise TestFailed, '%s returned %r, expected %r'%\ 0095 (name, (mant, exp), (emant,eexp)) 0096 0097 testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1)) 0098 testfrexp('frexp(0)', math.frexp(0), (0, 0)) 0099 testfrexp('frexp(1)', math.frexp(1), (0.5, 1)) 0100 testfrexp('frexp(2)', math.frexp(2), (0.5, 2)) 0101 0102 print 'hypot' 0103 testit('hypot(0,0)', math.hypot(0,0), 0) 0104 testit('hypot(3,4)', math.hypot(3,4), 5) 0105 0106 print 'ldexp' 0107 testit('ldexp(0,1)', math.ldexp(0,1), 0) 0108 testit('ldexp(1,1)', math.ldexp(1,1), 2) 0109 testit('ldexp(1,-1)', math.ldexp(1,-1), 0.5) 0110 testit('ldexp(-1,1)', math.ldexp(-1,1), -2) 0111 0112 print 'log' 0113 testit('log(1/e)', math.log(1/math.e), -1) 0114 testit('log(1)', math.log(1), 0) 0115 testit('log(e)', math.log(math.e), 1) 0116 testit('log(32,2)', math.log(32,2), 5) 0117 testit('log(10**40, 10)', math.log(10**40, 10), 40) 0118 testit('log(10**40, 10**20)', math.log(10**40, 10**20), 2) 0119 0120 print 'log10' 0121 testit('log10(0.1)', math.log10(0.1), -1) 0122 testit('log10(1)', math.log10(1), 0) 0123 testit('log10(10)', math.log10(10), 1) 0124 0125 print 'modf' 0126 def testmodf(name, (v1, v2), (e1, e2)): 0127 if abs(v1-e1) > eps or abs(v2-e2): 0128 raise TestFailed, '%s returned %r, expected %r'%\ 0129 (name, (v1,v2), (e1,e2)) 0130 0131 testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0)) 0132 testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0)) 0133 0134 print 'pow' 0135 testit('pow(0,1)', math.pow(0,1), 0) 0136 testit('pow(1,0)', math.pow(1,0), 1) 0137 testit('pow(2,1)', math.pow(2,1), 2) 0138 testit('pow(2,-1)', math.pow(2,-1), 0.5) 0139 0140 print 'radians' 0141 testit('radians(180)', math.radians(180), math.pi) 0142 testit('radians(90)', math.radians(90), math.pi/2) 0143 testit('radians(-45)', math.radians(-45), -math.pi/4) 0144 0145 print 'sin' 0146 testit('sin(0)', math.sin(0), 0) 0147 testit('sin(pi/2)', math.sin(math.pi/2), 1) 0148 testit('sin(-pi/2)', math.sin(-math.pi/2), -1) 0149 0150 print 'sinh' 0151 testit('sinh(0)', math.sinh(0), 0) 0152 testit('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1) 0153 testit('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0) 0154 0155 print 'sqrt' 0156 testit('sqrt(0)', math.sqrt(0), 0) 0157 testit('sqrt(1)', math.sqrt(1), 1) 0158 testit('sqrt(4)', math.sqrt(4), 2) 0159 0160 print 'tan' 0161 testit('tan(0)', math.tan(0), 0) 0162 testit('tan(pi/4)', math.tan(math.pi/4), 1) 0163 testit('tan(-pi/4)', math.tan(-math.pi/4), -1) 0164 0165 print 'tanh' 0166 testit('tanh(0)', math.tanh(0), 0) 0167 testit('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0) 0168 0169 # RED_FLAG 16-Oct-2000 Tim 0170 # While 2.0 is more consistent about exceptions than previous releases, it 0171 # still fails this part of the test on some platforms. For now, we only 0172 # *run* test_exceptions() in verbose mode, so that this isn't normally 0173 # tested. 0174 0175 def test_exceptions(): 0176 print 'exceptions' 0177 try: 0178 x = math.exp(-1000000000) 0179 except: 0180 # mathmodule.c is failing to weed out underflows from libm, or 0181 # we've got an fp format with huge dynamic range 0182 raise TestFailed("underflowing exp() should not have raised " 0183 "an exception") 0184 if x != 0: 0185 raise TestFailed("underflowing exp() should have returned 0") 0186 0187 # If this fails, probably using a strict IEEE-754 conforming libm, and x 0188 # is +Inf afterwards. But Python wants overflows detected by default. 0189 try: 0190 x = math.exp(1000000000) 0191 except OverflowError: 0192 pass 0193 else: 0194 raise TestFailed("overflowing exp() didn't trigger OverflowError") 0195 0196 # If this fails, it could be a puzzle. One odd possibility is that 0197 # mathmodule.c's macros are getting confused while comparing 0198 # Inf (HUGE_VAL) to a NaN, and artificially setting errno to ERANGE 0199 # as a result (and so raising OverflowError instead). 0200 try: 0201 x = math.sqrt(-1.0) 0202 except ValueError: 0203 pass 0204 else: 0205 raise TestFailed("sqrt(-1) didn't raise ValueError") 0206 0207 if verbose: 0208 test_exceptions() 0209
Generated by PyXR 0.9.4