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