|
| 1 | +# XXX Taken from CPython's release25-maint revision 56608 |
1 | 2 | # Python test set -- math module |
| 3 | +# XXXX Should not do tests around zero only |
2 | 4 |
|
3 | | -from test_support import * |
| 5 | +from test.test_support import TestFailed, verbose |
4 | 6 |
|
5 | 7 | seps='1e-05' |
6 | 8 | eps = eval(seps) |
7 | | -print_test('math (test_math.py)', 1) # 'math module, testing with eps', seps |
| 9 | +print 'math module, testing with eps', seps |
8 | 10 | import math |
9 | 11 |
|
10 | 12 | def testit(name, value, expected): |
11 | | - if abs(value-expected) > eps: |
12 | | - raise TestFailed, '%s returned %f, expected %f'%\ |
13 | | - (name, value, expected) |
| 13 | + if abs(value-expected) > eps: |
| 14 | + raise TestFailed, '%s returned %f, expected %f'%\ |
| 15 | + (name, value, expected) |
14 | 16 |
|
15 | | -print_test('constants: e, pi', 2) |
16 | | -assert abs(math.pi-3.1415926) < eps |
17 | | -assert abs(math.e-2.7182818) < eps |
| 17 | +print 'constants' |
| 18 | +testit('pi', math.pi, 3.1415926) |
| 19 | +testit('e', math.e, 2.7182818) |
18 | 20 |
|
19 | | -print_test('acos') |
20 | | -testit('acos', math.acos(-1), math.pi) |
21 | | -testit('acos', math.acos(0), math.pi/2) |
22 | | -testit('acos', math.acos(1), 0) |
| 21 | +print 'acos' |
| 22 | +testit('acos(-1)', math.acos(-1), math.pi) |
| 23 | +testit('acos(0)', math.acos(0), math.pi/2) |
| 24 | +testit('acos(1)', math.acos(1), 0) |
23 | 25 |
|
24 | | -print_test('asin') |
| 26 | +print 'asin' |
25 | 27 | testit('asin(-1)', math.asin(-1), -math.pi/2) |
26 | 28 | testit('asin(0)', math.asin(0), 0) |
27 | 29 | testit('asin(1)', math.asin(1), math.pi/2) |
28 | 30 |
|
29 | | -print_test('atan') |
| 31 | +print 'atan' |
30 | 32 | testit('atan(-1)', math.atan(-1), -math.pi/4) |
31 | 33 | testit('atan(0)', math.atan(0), 0) |
32 | 34 | testit('atan(1)', math.atan(1), math.pi/4) |
33 | 35 |
|
34 | | -print_test('atan2') |
| 36 | +print 'atan2' |
35 | 37 | testit('atan2(-1, 0)', math.atan2(-1, 0), -math.pi/2) |
36 | 38 | testit('atan2(-1, 1)', math.atan2(-1, 1), -math.pi/4) |
37 | 39 | testit('atan2(0, 1)', math.atan2(0, 1), 0) |
38 | 40 | testit('atan2(1, 1)', math.atan2(1, 1), math.pi/4) |
39 | 41 | testit('atan2(1, 0)', math.atan2(1, 0), math.pi/2) |
40 | 42 |
|
41 | | -print_test('ceil') |
| 43 | +print 'ceil' |
42 | 44 | testit('ceil(0.5)', math.ceil(0.5), 1) |
43 | 45 | testit('ceil(1.0)', math.ceil(1.0), 1) |
44 | 46 | testit('ceil(1.5)', math.ceil(1.5), 2) |
45 | 47 | testit('ceil(-0.5)', math.ceil(-0.5), 0) |
46 | 48 | testit('ceil(-1.0)', math.ceil(-1.0), -1) |
47 | 49 | testit('ceil(-1.5)', math.ceil(-1.5), -1) |
48 | 50 |
|
49 | | -print_test('cos') |
| 51 | +print 'cos' |
50 | 52 | testit('cos(-pi/2)', math.cos(-math.pi/2), 0) |
51 | 53 | testit('cos(0)', math.cos(0), 1) |
52 | 54 | testit('cos(pi/2)', math.cos(math.pi/2), 0) |
53 | 55 | testit('cos(pi)', math.cos(math.pi), -1) |
54 | 56 |
|
55 | | -print_test('cosh') |
| 57 | +print 'cosh' |
56 | 58 | testit('cosh(0)', math.cosh(0), 1) |
57 | 59 | testit('cosh(2)-2*cosh(1)**2', math.cosh(2)-2*math.cosh(1)**2, -1) # Thanks to Lambert |
58 | 60 |
|
59 | | -print_test('exp') |
| 61 | +print 'degrees' |
| 62 | +testit('degrees(pi)', math.degrees(math.pi), 180.0) |
| 63 | +testit('degrees(pi/2)', math.degrees(math.pi/2), 90.0) |
| 64 | +testit('degrees(-pi/4)', math.degrees(-math.pi/4), -45.0) |
| 65 | + |
| 66 | +print 'exp' |
60 | 67 | testit('exp(-1)', math.exp(-1), 1/math.e) |
61 | 68 | testit('exp(0)', math.exp(0), 1) |
62 | 69 | testit('exp(1)', math.exp(1), math.e) |
63 | 70 |
|
64 | | -print_test('fabs') |
| 71 | +print 'fabs' |
65 | 72 | testit('fabs(-1)', math.fabs(-1), 1) |
66 | 73 | testit('fabs(0)', math.fabs(0), 0) |
67 | 74 | testit('fabs(1)', math.fabs(1), 1) |
68 | 75 |
|
69 | | -print_test('floor') |
| 76 | +print 'floor' |
70 | 77 | testit('floor(0.5)', math.floor(0.5), 0) |
71 | 78 | testit('floor(1.0)', math.floor(1.0), 1) |
72 | 79 | testit('floor(1.5)', math.floor(1.5), 1) |
73 | 80 | testit('floor(-0.5)', math.floor(-0.5), -1) |
74 | 81 | testit('floor(-1.0)', math.floor(-1.0), -1) |
75 | 82 | testit('floor(-1.5)', math.floor(-1.5), -2) |
76 | 83 |
|
77 | | -print_test('fmod') |
| 84 | +print 'fmod' |
78 | 85 | testit('fmod(10,1)', math.fmod(10,1), 0) |
79 | 86 | testit('fmod(10,0.5)', math.fmod(10,0.5), 0) |
80 | 87 | testit('fmod(10,1.5)', math.fmod(10,1.5), 1) |
81 | 88 | testit('fmod(-10,1)', math.fmod(-10,1), 0) |
82 | 89 | testit('fmod(-10,0.5)', math.fmod(-10,0.5), 0) |
83 | 90 | testit('fmod(-10,1.5)', math.fmod(-10,1.5), -1) |
84 | 91 |
|
85 | | -print_test('frexp') |
| 92 | +print 'frexp' |
86 | 93 | def testfrexp(name, (mant, exp), (emant, eexp)): |
87 | | - if abs(mant-emant) > eps or exp <> eexp: |
88 | | - raise TestFailed, '%s returned %s, expected %s'%\ |
89 | | - (name, `mant, exp`, `emant,eexp`) |
| 94 | + if abs(mant-emant) > eps or exp != eexp: |
| 95 | + raise TestFailed, '%s returned %r, expected %r'%\ |
| 96 | + (name, (mant, exp), (emant,eexp)) |
90 | 97 |
|
91 | 98 | testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1)) |
92 | 99 | testfrexp('frexp(0)', math.frexp(0), (0, 0)) |
93 | 100 | testfrexp('frexp(1)', math.frexp(1), (0.5, 1)) |
94 | 101 | testfrexp('frexp(2)', math.frexp(2), (0.5, 2)) |
95 | 102 |
|
96 | | -print_test('hypot') |
| 103 | +print 'hypot' |
97 | 104 | testit('hypot(0,0)', math.hypot(0,0), 0) |
98 | 105 | testit('hypot(3,4)', math.hypot(3,4), 5) |
99 | 106 |
|
100 | | -print_test('ldexp') |
| 107 | +print 'ldexp' |
101 | 108 | testit('ldexp(0,1)', math.ldexp(0,1), 0) |
102 | 109 | testit('ldexp(1,1)', math.ldexp(1,1), 2) |
103 | 110 | testit('ldexp(1,-1)', math.ldexp(1,-1), 0.5) |
104 | 111 | testit('ldexp(-1,1)', math.ldexp(-1,1), -2) |
105 | 112 |
|
106 | | -print_test('log') |
| 113 | +print 'log' |
107 | 114 | testit('log(1/e)', math.log(1/math.e), -1) |
108 | 115 | testit('log(1)', math.log(1), 0) |
109 | 116 | testit('log(e)', math.log(math.e), 1) |
| 117 | +testit('log(32,2)', math.log(32,2), 5) |
| 118 | +testit('log(10**40, 10)', math.log(10**40, 10), 40) |
| 119 | +testit('log(10**40, 10**20)', math.log(10**40, 10**20), 2) |
110 | 120 |
|
111 | | -print_test('log10') |
| 121 | +print 'log10' |
112 | 122 | testit('log10(0.1)', math.log10(0.1), -1) |
113 | 123 | testit('log10(1)', math.log10(1), 0) |
114 | 124 | testit('log10(10)', math.log10(10), 1) |
115 | 125 |
|
116 | | -print_test('modf') |
| 126 | +print 'modf' |
117 | 127 | def testmodf(name, (v1, v2), (e1, e2)): |
118 | | - if abs(v1-e1) > eps or abs(v2-e2): |
119 | | - raise TestFailed, '%s returned %s, expected %s'%\ |
120 | | - (name, `v1,v2`, `e1,e2`) |
| 128 | + if abs(v1-e1) > eps or abs(v2-e2): |
| 129 | + raise TestFailed, '%s returned %r, expected %r'%\ |
| 130 | + (name, (v1,v2), (e1,e2)) |
121 | 131 |
|
122 | 132 | testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0)) |
123 | 133 | testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0)) |
124 | 134 |
|
125 | | -print_test('pow') |
| 135 | +print 'pow' |
126 | 136 | testit('pow(0,1)', math.pow(0,1), 0) |
127 | 137 | testit('pow(1,0)', math.pow(1,0), 1) |
128 | 138 | testit('pow(2,1)', math.pow(2,1), 2) |
129 | 139 | testit('pow(2,-1)', math.pow(2,-1), 0.5) |
130 | 140 |
|
131 | | -print_test('sin') |
| 141 | +print 'radians' |
| 142 | +testit('radians(180)', math.radians(180), math.pi) |
| 143 | +testit('radians(90)', math.radians(90), math.pi/2) |
| 144 | +testit('radians(-45)', math.radians(-45), -math.pi/4) |
| 145 | + |
| 146 | +print 'sin' |
132 | 147 | testit('sin(0)', math.sin(0), 0) |
133 | 148 | testit('sin(pi/2)', math.sin(math.pi/2), 1) |
134 | 149 | testit('sin(-pi/2)', math.sin(-math.pi/2), -1) |
135 | 150 |
|
136 | | -print_test('sinh') |
| 151 | +print 'sinh' |
137 | 152 | testit('sinh(0)', math.sinh(0), 0) |
138 | 153 | testit('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1) |
139 | 154 | testit('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0) |
140 | 155 |
|
141 | | -print_test('sqrt') |
| 156 | +print 'sqrt' |
142 | 157 | testit('sqrt(0)', math.sqrt(0), 0) |
143 | 158 | testit('sqrt(1)', math.sqrt(1), 1) |
144 | 159 | testit('sqrt(4)', math.sqrt(4), 2) |
145 | 160 |
|
146 | | -print_test('tan') |
| 161 | +print 'tan' |
147 | 162 | testit('tan(0)', math.tan(0), 0) |
148 | 163 | testit('tan(pi/4)', math.tan(math.pi/4), 1) |
149 | 164 | testit('tan(-pi/4)', math.tan(-math.pi/4), -1) |
150 | 165 |
|
151 | | -print_test('tanh') |
| 166 | +print 'tanh' |
152 | 167 | testit('tanh(0)', math.tanh(0), 0) |
153 | 168 | testit('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0) |
| 169 | + |
| 170 | +# RED_FLAG 16-Oct-2000 Tim |
| 171 | +# While 2.0 is more consistent about exceptions than previous releases, it |
| 172 | +# still fails this part of the test on some platforms. For now, we only |
| 173 | +# *run* test_exceptions() in verbose mode, so that this isn't normally |
| 174 | +# tested. |
| 175 | + |
| 176 | +def test_exceptions(): |
| 177 | + print 'exceptions' |
| 178 | + try: |
| 179 | + x = math.exp(-1000000000) |
| 180 | + except: |
| 181 | + # mathmodule.c is failing to weed out underflows from libm, or |
| 182 | + # we've got an fp format with huge dynamic range |
| 183 | + raise TestFailed("underflowing exp() should not have raised " |
| 184 | + "an exception") |
| 185 | + if x != 0: |
| 186 | + raise TestFailed("underflowing exp() should have returned 0") |
| 187 | + |
| 188 | + # If this fails, probably using a strict IEEE-754 conforming libm, and x |
| 189 | + # is +Inf afterwards. But Python wants overflows detected by default. |
| 190 | + try: |
| 191 | + x = math.exp(1000000000) |
| 192 | + except OverflowError: |
| 193 | + pass |
| 194 | + else: |
| 195 | + raise TestFailed("overflowing exp() didn't trigger OverflowError") |
| 196 | + |
| 197 | + # If this fails, it could be a puzzle. One odd possibility is that |
| 198 | + # mathmodule.c's macros are getting confused while comparing |
| 199 | + # Inf (HUGE_VAL) to a NaN, and artificially setting errno to ERANGE |
| 200 | + # as a result (and so raising OverflowError instead). |
| 201 | + try: |
| 202 | + x = math.sqrt(-1.0) |
| 203 | + except ValueError: |
| 204 | + pass |
| 205 | + else: |
| 206 | + raise TestFailed("sqrt(-1) didn't raise ValueError") |
| 207 | + |
| 208 | +if verbose: |
| 209 | + test_exceptions() |
0 commit comments