1 # Python test set -- math module
2 # XXXX Should not do tests around zero only
4 from test_support
import *
8 print 'math module, testing with eps', seps
11 def testit(name
, value
, expected
):
12 if abs(value
-expected
) > eps
:
13 raise TestFailed
, '%s returned %f, expected %f'%\
14 (name
, value
, expected
)
17 testit('pi', math
.pi
, 3.1415926)
18 testit('e', math
.e
, 2.7182818)
21 testit('acos(-1)', math
.acos(-1), math
.pi
)
22 testit('acos(0)', math
.acos(0), math
.pi
/2)
23 testit('acos(1)', math
.acos(1), 0)
26 testit('asin(-1)', math
.asin(-1), -math
.pi
/2)
27 testit('asin(0)', math
.asin(0), 0)
28 testit('asin(1)', math
.asin(1), math
.pi
/2)
31 testit('atan(-1)', math
.atan(-1), -math
.pi
/4)
32 testit('atan(0)', math
.atan(0), 0)
33 testit('atan(1)', math
.atan(1), math
.pi
/4)
36 testit('atan2(-1, 0)', math
.atan2(-1, 0), -math
.pi
/2)
37 testit('atan2(-1, 1)', math
.atan2(-1, 1), -math
.pi
/4)
38 testit('atan2(0, 1)', math
.atan2(0, 1), 0)
39 testit('atan2(1, 1)', math
.atan2(1, 1), math
.pi
/4)
40 testit('atan2(1, 0)', math
.atan2(1, 0), math
.pi
/2)
43 testit('ceil(0.5)', math
.ceil(0.5), 1)
44 testit('ceil(1.0)', math
.ceil(1.0), 1)
45 testit('ceil(1.5)', math
.ceil(1.5), 2)
46 testit('ceil(-0.5)', math
.ceil(-0.5), 0)
47 testit('ceil(-1.0)', math
.ceil(-1.0), -1)
48 testit('ceil(-1.5)', math
.ceil(-1.5), -1)
51 testit('cos(-pi/2)', math
.cos(-math
.pi
/2), 0)
52 testit('cos(0)', math
.cos(0), 1)
53 testit('cos(pi/2)', math
.cos(math
.pi
/2), 0)
54 testit('cos(pi)', math
.cos(math
.pi
), -1)
57 testit('cosh(0)', math
.cosh(0), 1)
58 testit('cosh(2)-2*cosh(1)**2', math
.cosh(2)-2*math
.cosh(1)**2, -1) # Thanks to Lambert
61 testit('exp(-1)', math
.exp(-1), 1/math
.e
)
62 testit('exp(0)', math
.exp(0), 1)
63 testit('exp(1)', math
.exp(1), math
.e
)
66 testit('fabs(-1)', math
.fabs(-1), 1)
67 testit('fabs(0)', math
.fabs(0), 0)
68 testit('fabs(1)', math
.fabs(1), 1)
71 testit('floor(0.5)', math
.floor(0.5), 0)
72 testit('floor(1.0)', math
.floor(1.0), 1)
73 testit('floor(1.5)', math
.floor(1.5), 1)
74 testit('floor(-0.5)', math
.floor(-0.5), -1)
75 testit('floor(-1.0)', math
.floor(-1.0), -1)
76 testit('floor(-1.5)', math
.floor(-1.5), -2)
79 testit('fmod(10,1)', math
.fmod(10,1), 0)
80 testit('fmod(10,0.5)', math
.fmod(10,0.5), 0)
81 testit('fmod(10,1.5)', math
.fmod(10,1.5), 1)
82 testit('fmod(-10,1)', math
.fmod(-10,1), 0)
83 testit('fmod(-10,0.5)', math
.fmod(-10,0.5), 0)
84 testit('fmod(-10,1.5)', math
.fmod(-10,1.5), -1)
87 def testfrexp(name
, (mant
, exp
), (emant
, eexp
)):
88 if abs(mant
-emant
) > eps
or exp
<> eexp
:
89 raise TestFailed
, '%s returned %s, expected %s'%\
90 (name
, `mant
, exp`
, `emant
,eexp`
)
92 testfrexp('frexp(-1)', math
.frexp(-1), (-0.5, 1))
93 testfrexp('frexp(0)', math
.frexp(0), (0, 0))
94 testfrexp('frexp(1)', math
.frexp(1), (0.5, 1))
95 testfrexp('frexp(2)', math
.frexp(2), (0.5, 2))
98 testit('hypot(0,0)', math
.hypot(0,0), 0)
99 testit('hypot(3,4)', math
.hypot(3,4), 5)
102 testit('ldexp(0,1)', math
.ldexp(0,1), 0)
103 testit('ldexp(1,1)', math
.ldexp(1,1), 2)
104 testit('ldexp(1,-1)', math
.ldexp(1,-1), 0.5)
105 testit('ldexp(-1,1)', math
.ldexp(-1,1), -2)
108 testit('log(1/e)', math
.log(1/math
.e
), -1)
109 testit('log(1)', math
.log(1), 0)
110 testit('log(e)', math
.log(math
.e
), 1)
113 testit('log10(0.1)', math
.log10(0.1), -1)
114 testit('log10(1)', math
.log10(1), 0)
115 testit('log10(10)', math
.log10(10), 1)
118 def testmodf(name
, (v1
, v2
), (e1
, e2
)):
119 if abs(v1
-e1
) > eps
or abs(v2
-e2
):
120 raise TestFailed
, '%s returned %s, expected %s'%\
121 (name
, `v1
,v2`
, `e1
,e2`
)
123 testmodf('modf(1.5)', math
.modf(1.5), (0.5, 1.0))
124 testmodf('modf(-1.5)', math
.modf(-1.5), (-0.5, -1.0))
127 testit('pow(0,1)', math
.pow(0,1), 0)
128 testit('pow(1,0)', math
.pow(1,0), 1)
129 testit('pow(2,1)', math
.pow(2,1), 2)
130 testit('pow(2,-1)', math
.pow(2,-1), 0.5)
133 testit('sin(0)', math
.sin(0), 0)
134 testit('sin(pi/2)', math
.sin(math
.pi
/2), 1)
135 testit('sin(-pi/2)', math
.sin(-math
.pi
/2), -1)
138 testit('sinh(0)', math
.sinh(0), 0)
139 testit('sinh(1)**2-cosh(1)**2', math
.sinh(1)**2-math
.cosh(1)**2, -1)
140 testit('sinh(1)+sinh(-1)', math
.sinh(1)+math
.sinh(-1), 0)
143 testit('sqrt(0)', math
.sqrt(0), 0)
144 testit('sqrt(1)', math
.sqrt(1), 1)
145 testit('sqrt(4)', math
.sqrt(4), 2)
148 testit('tan(0)', math
.tan(0), 0)
149 testit('tan(pi/4)', math
.tan(math
.pi
/4), 1)
150 testit('tan(-pi/4)', math
.tan(-math
.pi
/4), -1)
153 testit('tanh(0)', math
.tanh(0), 0)
154 testit('tanh(1)+tanh(-1)', math
.tanh(1)+math
.tanh(-1), 0)