root / pobysoPythonSage / src / testPobyso.sage @ 192
Historique | Voir | Annoter | Télécharger (12,8 ko)
1 |
""" |
---|---|
2 |
Test module for pobyso.py functions. |
3 |
|
4 |
AUTHORS: |
5 |
- S.T. (2014-02-26): initial version |
6 |
|
7 |
TODO: |
8 |
- almost everything! |
9 |
|
10 |
""" |
11 |
|
12 |
print "\ntestPobyso loading..." |
13 |
# |
14 |
import inspect |
15 |
import resource |
16 |
import timeit |
17 |
|
18 |
|
19 |
def memory_usage_resource(): |
20 |
rusage_denom = RR("1024") |
21 |
if sys.platform == 'darwin': |
22 |
# ... it seems that in OSX the output is different units ... |
23 |
rusage_denom = rusage_denom^2 |
24 |
mem = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / rusage_denom |
25 |
return mem |
26 |
# End memory_usage_resource() |
27 |
|
28 |
def test_pobyso_wrapper(func, *args, **kwargs): |
29 |
def wrapped(): |
30 |
return func(*args, **kwargs) |
31 |
return wrapped |
32 |
|
33 |
# |
34 |
def test_pobyso_absolute_so_so(): |
35 |
for index in xrange(0,1000000): |
36 |
absoluteSo = pobyso_absolute_so_so() |
37 |
sollya_lib_clear_obj(absoluteSo) |
38 |
# End test_absolute_so_so |
39 |
|
40 |
def test_pobyso_autoprint_so_so(iterationsNum=1000): |
41 |
absoluteSo = pobyso_absolute_so_so() |
42 |
for index in xrange(0,iterationsNum): |
43 |
pobyso_autoprint(absoluteSo) |
44 |
sollya_lib_clear_obj(absoluteSo) |
45 |
# End test_pobyso_autoprint |
46 |
|
47 |
def test_pobyso_bounds_to_range_sa_so(repeat=1000, number=10): |
48 |
functionName = inspect.stack()[0][3] |
49 |
print "Running", inspect.stack()[0][3], "..." |
50 |
lowerBoundSa = RR(1) |
51 |
upperBoundSa = RR(2) |
52 |
precSo = pobyso_get_prec_so() |
53 |
#pobyso_autoprint(precSo) |
54 |
# |
55 |
def test(lowerBoundSa, upperBoundSa): |
56 |
rangeSo = pobyso_bounds_to_range_sa_so(lowerBoundSa, upperBoundSa) |
57 |
sollya_lib_clear_obj(rangeSo) |
58 |
# |
59 |
wrapped = test_pobyso_wrapper(test,lowerBoundSa, upperBoundSa) |
60 |
timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
61 |
# |
62 |
precSo = pobyso_get_prec_so() |
63 |
#pobyso_autoprint(precSo) |
64 |
sollya_lib_clear_obj(precSo) |
65 |
print "\t...", functionName, "done." |
66 |
return timing |
67 |
# End test_pobyso_bounds_to_range_sa_so |
68 |
|
69 |
def test_pobyso_clear_taylorform_sa_so(repeat=1000, number=10): |
70 |
functionName = inspect.stack()[0][3] |
71 |
print "Running", inspect.stack()[0][3], "..." |
72 |
intervalSa = RIF(1,2) |
73 |
functionSo = pobyso_parse_string("exp(x)") |
74 |
degreeSo = pobyso_constant_from_int_sa_so(4) |
75 |
#RealIntervalField |
76 |
pointSo = pobyso_constant_sa_so(RR("1.5")) |
77 |
intervalSo = pobyso_interval_to_range_sa_so(intervalSa) |
78 |
# |
79 |
def test(functionSo, degreeSo, pointSo, intervalSo): |
80 |
taylorformSo = pobyso_taylorform_so_so(functionSo, |
81 |
degreeSo, |
82 |
pointSo, |
83 |
intervalSo) |
84 |
pobyso_clear_taylorform_sa_so(taylorformSo) |
85 |
# |
86 |
wrapped = test_pobyso_wrapper(test, |
87 |
functionSo, |
88 |
degreeSo, |
89 |
pointSo, |
90 |
intervalSo) |
91 |
timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
92 |
# |
93 |
sollya_lib_clear_obj(functionSo) |
94 |
sollya_lib_clear_obj(degreeSo) |
95 |
sollya_lib_clear_obj(pointSo) |
96 |
sollya_lib_clear_obj(intervalSo) |
97 |
print "\t...", functionName, "done." |
98 |
return timing |
99 |
# End test_pobyso_bounds_to_range_sa_so |
100 |
|
101 |
def test_pobyso_error_so(repeat=1000, number=10): |
102 |
functionName = inspect.stack()[0][3] |
103 |
print "Running", inspect.stack()[0][3], "..." |
104 |
# |
105 |
def test(): |
106 |
errorSo = pobyso_error_so() |
107 |
sollya_lib_clear_obj(errorSo) |
108 |
# |
109 |
wrapped = test_pobyso_wrapper(test) |
110 |
timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
111 |
print "\t...", functionName, "done." |
112 |
return timing |
113 |
# End test_pobyso_error_so |
114 |
|
115 |
def test_pobyso_get_list_elements_so_so(repeat = 1000, number=10): |
116 |
functionName = inspect.stack()[0][3] |
117 |
print "Running", functionName, "..." |
118 |
def test(): |
119 |
listSo = pobyso_parse_string_sa_so("[|0,1,2,3,4,5,6|];") |
120 |
(listSaSo, elementsNumSa, isEndEllipticSa) = \ |
121 |
pobyso_get_list_elements(listSo) |
122 |
for elementSo in listSaSo: |
123 |
sollya_lib_clear_obj(elementSo) |
124 |
# Notice that we use sollya_lib_clear_obj and not |
125 |
# sollya_lib_clear_object_list. |
126 |
sollya_lib_clear_obj(listSo, None) |
127 |
# |
128 |
wrapped = test_pobyso_wrapper(test) |
129 |
timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
130 |
print "\t...", functionName, "done." |
131 |
return timing |
132 |
# End test_pobyso_get_list_elements |
133 |
|
134 |
def test_pobyso_get_prec_so_sa(repeat=1000, number=10): |
135 |
functionName = inspect.stack()[0][3] |
136 |
print "Running", functionName, "..." |
137 |
def test(): |
138 |
precSa = pobyso_get_prec_so_sa() |
139 |
wrapped = test_pobyso_wrapper(test) |
140 |
timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
141 |
print "\t...", functionName, "done." |
142 |
return timing |
143 |
# End test_pobyso_get_prec_so_sa |
144 |
|
145 |
def test_pobyso_get_prec_of_constant_so_sa(repeat=1000, number=10): |
146 |
functionName = inspect.stack()[0][3] |
147 |
print "Running", functionName, "..." |
148 |
constSo = pobyso_constant_1_sa_so() |
149 |
def test(): |
150 |
precSa = pobyso_get_prec_of_constant_so_sa(constSo) |
151 |
wrapped = test_pobyso_wrapper(test) |
152 |
timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
153 |
print "\t...", functionName, "done." |
154 |
sollya_lib_clear_obj(constSo,None) |
155 |
return timing |
156 |
# End test_pobyso_get_prec_of_constant_so_sa |
157 |
|
158 |
def test_pobyso_guess_degree_sa_sa(repeat=10, number=10): |
159 |
functionName = inspect.stack()[0][3] |
160 |
print "Running", functionName, "..." |
161 |
funcExpSa(x) = exp(x) |
162 |
RRIF = RealIntervalField(113) |
163 |
intervalSa = RRIF(3/8 - 2^-68, 3/8 + 2^-68) |
164 |
print "Interval:", intervalSa.str(style='brackets') |
165 |
errorSa = RR(2^-227) |
166 |
weightSa(x) = 1 |
167 |
degreeBoundSa = 150 |
168 |
# Function definition |
169 |
def test(): |
170 |
pobyso_guess_degree_sa_sa(funcExpSa, |
171 |
intervalSa, |
172 |
errorSa, |
173 |
weightSa, |
174 |
degreeBoundSa) |
175 |
wrapped = test_pobyso_wrapper(test) |
176 |
timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
177 |
print "\t...", functionName, "done." |
178 |
return timing |
179 |
# End test_pobyso_guess_degree_sa_sa |
180 |
|
181 |
def test_pobyso_guess_degree_so_sa(repeat=1000, number=10): |
182 |
functionName = inspect.stack()[0][3] |
183 |
print "Running", functionName, "..." |
184 |
funcExpSo = pobyso_parse_string_sa_so("exp(x)") |
185 |
RRIF = RealIntervalField(113) |
186 |
intervalSa = RRIF(3/8 - 2^-68, 3/8 + 2^-68) |
187 |
print "Interval:", intervalSa.str(style='brackets') |
188 |
intervalSo = pobyso_interval_to_range_sa_so(intervalSa) |
189 |
errorSo = pobyso_constant_sa_so(RR(2^-227)) |
190 |
weightSo=pobyso_parse_string_sa_so("1") |
191 |
degreeBoundSo = pobyso_constant_from_int_sa_so(150) |
192 |
# Function definition |
193 |
def test(): |
194 |
pobyso_guess_degree_so_sa(funcExpSo, |
195 |
intervalSo, |
196 |
errorSo, |
197 |
weightSo, |
198 |
degreeBoundSo) |
199 |
""" |
200 |
pobyso_guess_degree_so_sa(funcExpSo, |
201 |
intervalSo, |
202 |
errorSo, |
203 |
None, |
204 |
None) |
205 |
""" |
206 |
wrapped = test_pobyso_wrapper(test) |
207 |
timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
208 |
print "\t...", functionName, "done." |
209 |
sollya_lib_clear_obj(funcExpSo) |
210 |
sollya_lib_clear_obj(intervalSo) |
211 |
sollya_lib_clear_obj(errorSo) |
212 |
sollya_lib_clear_obj(weightSo) |
213 |
sollya_lib_clear_obj(degreeBoundSo) |
214 |
return timing |
215 |
# End test_pobyso_guess_degree_so_sa |
216 |
|
217 |
def test_pobyso_is_error_so_sa(repeat=1000, number=10): |
218 |
functionName = inspect.stack()[0][3] |
219 |
print "Running", inspect.stack()[0][3], "..." |
220 |
# |
221 |
def test(): |
222 |
errorSo = pobyso_error_so() |
223 |
if not pobyso_is_error_so_sa(errorSo): |
224 |
print "Error!" |
225 |
sollya_lib_clear_obj(errorSo) |
226 |
# |
227 |
wrapped = test_pobyso_wrapper(test) |
228 |
timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
229 |
print "\t...", functionName, "done." |
230 |
return timing |
231 |
# End test_pobyso_error_so |
232 |
|
233 |
def test_pobyso_lib_init(iterationsNum=10000): |
234 |
""" |
235 |
Must be called exactly once. Leaks lots of memory otherwise. |
236 |
""" |
237 |
print "Running", inspect.stack()[0][3] |
238 |
for index in xrange(0,iterationsNum): |
239 |
pobyso_lib_init() |
240 |
#pobyso_lib_close() |
241 |
# End test_pobyso_lib_init |
242 |
|
243 |
def test_pobyso_parse_string_sa_so(iterationsNum=10000): |
244 |
print "Running", inspect.stack()[0][3] |
245 |
for index in xrange(0,iterationsNum): |
246 |
expressionSo = pobyso_parse_string_sa_so('exp(x)+2*x*sin(x)') |
247 |
sollya_lib_clear_obj(expressionSo) |
248 |
# End pobyso_parse_string_sa_so |
249 |
|
250 |
def test_pobyso_taylor_expansion_no_change_var_so_so(repeat=1000, number=10): |
251 |
functionName = inspect.stack()[0][3] |
252 |
print "Running", functionName, "..." |
253 |
functionSo = pobyso_parse_string('exp(x)') |
254 |
degreeSo = pobyso_constant_from_int_sa_so(20) |
255 |
rangeSo = pobyso_bounds_to_range_sa_so(RR(1),RR(2)) |
256 |
errorTypeSo = pobyso_absolute_so_so() |
257 |
def test(functionSo, degreeSo, rangeSo, errorTypeSo): |
258 |
teSo = pobyso_taylor_expansion_no_change_var_so_so(functionSo, |
259 |
degreeSo, |
260 |
rangeSo, |
261 |
errorTypeSo) |
262 |
sollya_lib_clear_obj(teSo[0]) |
263 |
sollya_lib_clear_obj(teSo[1]) |
264 |
sollya_lib_clear_obj(teSo[2]) |
265 |
# End test |
266 |
wrapped = test_pobyso_wrapper(test, |
267 |
functionSo, |
268 |
degreeSo, |
269 |
rangeSo, |
270 |
errorTypeSo) |
271 |
timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
272 |
sollya_lib_clear_obj(functionSo) |
273 |
sollya_lib_clear_obj(degreeSo) |
274 |
sollya_lib_clear_obj(rangeSo) |
275 |
sollya_lib_clear_obj(errorTypeSo) |
276 |
print "\t...", functionName, "done." |
277 |
return timing |
278 |
# End test_pobyso_taylor_expansion_no_change_var_so_so |
279 |
|
280 |
def test_pobyso_taylor_expansion_with_change_var_so_so(repeat=1000, number=10): |
281 |
functionName = inspect.stack()[0][3] |
282 |
print "Running", functionName, "..." |
283 |
functionSo = pobyso_parse_string('exp(x)') |
284 |
degreeSo = pobyso_constant_from_int_sa_so(20) |
285 |
rangeSo = pobyso_bounds_to_range_sa_so(RR(1),RR(2)) |
286 |
errorTypeSo = pobyso_absolute_so_so() |
287 |
def test(functionSo, degreeSo, rangeSo, errorTypeSo): |
288 |
teSo = pobyso_taylor_expansion_with_change_var_so_so(functionSo, |
289 |
degreeSo, |
290 |
rangeSo, |
291 |
errorTypeSo) |
292 |
sollya_lib_clear_obj(teSo[0]) |
293 |
sollya_lib_clear_obj(teSo[1]) |
294 |
sollya_lib_clear_obj(teSo[2]) |
295 |
# End test |
296 |
wrapped = test_pobyso_wrapper(test, |
297 |
functionSo, |
298 |
degreeSo, |
299 |
rangeSo, |
300 |
errorTypeSo) |
301 |
timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
302 |
sollya_lib_clear_obj(functionSo) |
303 |
sollya_lib_clear_obj(degreeSo) |
304 |
sollya_lib_clear_obj(rangeSo) |
305 |
sollya_lib_clear_obj(errorTypeSo) |
306 |
print "\t...", functionName, "done." |
307 |
return timing |
308 |
# End test_pobyso_taylor_expansion_with_change_var_so_so |
309 |
|
310 |
def test_pobyso_taylorform_so_so_2(repeat=1000, number=10): |
311 |
functionName = inspect.stack()[0][3] |
312 |
print "Running", functionName, "..." |
313 |
functionSo = pobyso_parse_string('exp(x)') |
314 |
degreeSo = pobyso_constant_from_int_sa_so(20) |
315 |
rangeSo = pobyso_bounds_to_range_sa_so(RR(1),RR(2)) |
316 |
intervalCenterSo = sollya_lib_mid(rangeSo, None) |
317 |
errorTypeSo = pobyso_absolute_so_so() |
318 |
|
319 |
def test(functionSo, degreeSo, intervalCenterSo, errorTypeSo): |
320 |
taylorformSo = pobyso_taylorform_so_so(functionSo, |
321 |
degreeSo, |
322 |
intervalCenterSo, |
323 |
errorTypeSo) |
324 |
sollya_lib_clear_obj(taylorformSo) |
325 |
|
326 |
wrapped = test_pobyso_wrapper(test, |
327 |
functionSo, |
328 |
degreeSo, |
329 |
intervalCenterSo, |
330 |
errorTypeSo) |
331 |
timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
332 |
sollya_lib_clear_obj(functionSo, None) |
333 |
sollya_lib_clear_obj(degreeSo, None) |
334 |
sollya_lib_clear_obj(rangeSo, None) |
335 |
sollya_lib_clear_obj(intervalCenterSo, None) |
336 |
sollya_lib_clear_obj(errorTypeSo, None) |
337 |
return timing |
338 |
# End test_pobyso_taylor_so_so_2 |
339 |
|
340 |
def test_pobyso_dummy_test(iterationsNum=1, executionsNum=1): |
341 |
aString = "2+3" |
342 |
def test(): |
343 |
return 2+3 |
344 |
wrapped = test_pobyso_wrapper(test) |
345 |
timing = timeit.timeit(wrapped, number=executionsNum) |
346 |
return timing |
347 |
print "\t...testPobyso loaded" |
348 |
|