-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.py
301 lines (272 loc) · 9.05 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import math
import subprocess
import os
import sys
Languages = {
"ada": "./test",
"cpp": "./test",
"csharp": ["mono", "test.exe"],
"d": "./test",
"erlang": ["escript", "test.erl"],
"go": "./test",
"haskell": ["runhaskell", "test.hs"],
"java": ["java", "Test"],
"javascript": ["java", "-cp", "js.jar", "org.mozilla.javascript.tools.shell.Main", "test.js"],
"lua": ["lua", "test.lua"],
"pascal": "./test",
"php": ["php", "test.php"],
"perl": ["perl", "test.pl"],
"python": [sys.executable, "test.py"],
"ruby": ["ruby", "test.rb"],
"scala": ["scala", "picomath.jar"],
"scheme": ["guile", "test.scm"],
"tcl": ["tclsh", "test.tcl"],
}
Verbose = False
class ExecutionError:
def __init__(self, name, error):
self.name = name
self.error = error
class Driver:
def __init__(self, lang):
self.lang = lang
self.pipe = subprocess.Popen(Languages[lang], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=lang)
def call(self, name, x):
if Verbose:
sys.stdout.write("%s %e\n" % (name, x))
self.pipe.stdin.write(("%s %e\n" % (name, x)).encode())
self.pipe.stdin.flush()
s = self.pipe.stdout.readline()
if not s:
raise ExecutionError(name, self.pipe.stderr.read())
if Verbose:
sys.stdout.write(s)
try:
return float(s)
except:
raise ExecutionError(name, s + self.pipe.stdout.read())
def close(self):
self.pipe.stdin.close()
self.pipe.wait()
def test_erf(driver, log):
Tests = (
(-3, -0.999977909503),
(-1, -0.842700792950),
(0.0, 0.0),
(0.5, 0.520499877813),
(2.1, 0.997020533344),
)
maxError = 0
for x, y in Tests:
error = abs(y - driver.call("erf", x))
if error > maxError:
maxError = error
log.append("erf: Maximum error: " + str(maxError))
return maxError < 1e-6
def test_expm1(driver, log):
Tests = (
(-1, -0.632120558828558),
(0.0, 0.0),
(1e-5 - 1e-8, 0.000009990049900216168),
(1e-5 + 1e-8, 0.00001001005010021717),
(0.5, 0.6487212707001282),
)
maxError = 0
for x, y in Tests:
error = abs(y - driver.call("expm1", x))
if error > maxError:
maxError = error
log.append("expm1: Maximum error: " + str(maxError))
return maxError < 1e-6
def test_phi(driver, log):
Tests = (
(-3, 0.00134989803163),
(-1, 0.158655253931),
(0.0, 0.5),
(0.5, 0.691462461274),
(2.1, 0.982135579437),
)
maxError = 0
for x, y in Tests:
error = abs(y - driver.call("phi", x))
if error > maxError:
maxError = error
log.append("phi: Maximum error: " + str(maxError))
return maxError < 1e-6
def test_NormalCDFInverse(driver, log):
Tests = (
(0.0000001, -5.199337582187471),
(0.00001, -4.264890793922602),
(0.001, -3.090232306167813),
(0.05, -1.6448536269514729),
(0.15, -1.0364333894937896),
(0.25, -0.6744897501960817),
(0.35, -0.38532046640756773),
(0.45, -0.12566134685507402),
(0.55, 0.12566134685507402),
(0.65, 0.38532046640756773),
(0.75, 0.6744897501960817),
(0.85, 1.0364333894937896),
(0.95, 1.6448536269514729),
(0.999, 3.090232306167813),
(0.99999, 4.264890793922602),
(0.9999999, 5.199337582187471),
)
maxError = 0
for x, y in Tests:
error = abs(y - driver.call("NormalCDFInverse", x))
if error > maxError:
maxError = error
log.append("NormalCDFInverse: Maximum error: " + str(maxError))
return maxError < 1e-3
def test_Gamma(driver, log):
Tests = (
(1e-20, 1e+20),
(2.19824158876e-16, 4.5490905327e+15), # 0.99*DBL_EPSILON
(2.24265050974e-16, 4.45900953205e+15), # 1.01*DBL_EPSILON
(0.00099, 1009.52477271),
(0.00100, 999.423772485),
(0.00101, 989.522792258),
(6.1, 142.451944066),
(11.999, 39819417.4793),
(12, 39916800.0),
(12.001, 40014424.1571),
(15.2, 149037380723.0),
)
worst_absolute_error = 0.0
worst_relative_error = 0.0
worst_absolute_error_case = 0
worst_relative_error_case = 0
for i, (x, y) in enumerate(Tests):
computed = driver.call("Gamma", x)
absolute_error = abs(computed - y)
relative_error = absolute_error / y
if absolute_error > worst_absolute_error:
worst_absolute_error = absolute_error
worst_absolute_error_case = i
if relative_error > worst_relative_error:
worst_relative_error = relative_error
worst_relative_error_case = i
t = worst_absolute_error_case
x, y = Tests[t]
a = driver.call("Gamma", x)
log.append("Gamma: Worst absolute error: " + str(abs(a - y)))
log.append("Gamma(" + str(x) + ") computed as " + str(a) + " but exact value is " + str(y))
t = worst_relative_error_case
x, y = Tests[t]
a = driver.call("Gamma", x)
log.append("Gamma: Worst relative error: " + str(abs(a - y) / y))
log.append("Gamma(" + str(x) + ") computed as " + str(a) + " but exact value is " + str(y))
return worst_relative_error < 1e-6
def test_LogGamma(driver, log):
Tests = (
(1e-12, 27.6310211159),
(0.9999, 5.77297915613e-05),
(1.0001, -5.77133422205e-05),
(3.1, 0.787375083274),
(6.3, 5.30734288962),
(11.9999, 17.5020635801),
(12, 17.5023078459),
(12.0001, 17.5025521125),
(27.4, 62.5755868211),
)
worst_absolute_error = 0.0
worst_relative_error = 0.0
worst_absolute_error_case = 0
worst_relative_error_case = 0
for i, (x, y) in enumerate(Tests):
computed = driver.call("LogGamma", x)
absolute_error = abs(computed - y)
relative_error = absolute_error / y
if absolute_error > worst_absolute_error:
worst_absolute_error = absolute_error
worst_absolute_error_case = i
if relative_error > worst_relative_error:
worst_relative_error = relative_error
worst_relative_error_case = i
t = worst_absolute_error_case
x, y = Tests[t]
a = driver.call("LogGamma", x)
log.append("LogGamma: Worst absolute error: " + str(abs(a - y)))
log.append("LogGamma(" + str(x) + ") computed as " + str(a) + " but exact value is " + str(y))
t = worst_relative_error_case
x, y = Tests[t]
a = driver.call("LogGamma", x)
log.append("LogGamma: Worst relative error: " + str(abs(a - y) / y))
log.append("LogGamma(" + str(x) + ") computed as " + str(a) + " but exact value is " + str(y))
return worst_relative_error < 1e-10
def test_LogFactorial(driver, log):
def factorial(n):
r = 1
while n > 0:
r *= n
n -= 1
return r
maxError = 0
for x in (0, 1, 10, 100, 1000, 10000):
error = abs(math.log(factorial(x)) - driver.call("LogFactorial", x))
if error > maxError:
maxError = error
log.append("LogFactorial: Worst absolute error: " + str(maxError))
return maxError < 1e-6
TestFunctions = (
test_erf,
test_expm1,
test_phi,
test_NormalCDFInverse,
test_Gamma,
test_LogGamma,
test_LogFactorial,
)
def tests():
global Verbose
langs = []
a = 1
while a < len(sys.argv):
if sys.argv[a].startswith("-"):
if sys.argv[a] == "-v":
Verbose = True
else:
sys.stderr.write("Unknown option: " + sys.argv[a] + "\n")
sys.exit(1)
else:
langs.append(sys.argv[a])
a += 1
anyfail = False
if not langs:
langs = sorted(Languages)
for lang in langs:
sys.stdout.write("Checking " + lang + "... ")
sys.stdout.flush()
try:
driver = Driver(lang)
except:
sys.stdout.write("UNAVAILABLE\n")
sys.stdout.write(str(sys.exc_info()[1]) + "\n")
continue
logs = []
allok = True
for tf in TestFunctions:
log = []
try:
if not tf(driver, log):
allok = False
logs.extend(log)
except ExecutionError:
allok = False
e = sys.exc_info()[1]
logs.append("Error while executing: " + e.name)
logs.extend(e.error.split("\n"))
break
if allok:
sys.stdout.write("ok\n")
else:
anyfail = True
sys.stdout.write("FAIL\n")
for s in logs:
sys.stdout.write(s + "\n")
driver.close()
if anyfail:
sys.exit(1)
if __name__ == "__main__":
tests()