root / pobysoPythonSage / src / testPobyso.sage @ 160
Historique | Voir | Annoter | Télécharger (11,21 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(lowerBoundSa, upperBoundSa): |
80 |
taylorformSo = pobyso_taylorform_so_so(functionSo, |
81 |
degreeSo, |
82 |
pointSo, |
83 |
intervalSo) |
84 |
sollya_lib_clear_obj(rangeSo) |
85 |
# |
86 |
wrapped = test_pobyso_wrapper(test,lowerBoundSa, upperBoundSa) |
87 |
timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
88 |
# |
89 |
precSo = pobyso_get_prec_so() |
90 |
#pobyso_autoprint(precSo) |
91 |
sollya_lib_clear_obj(precSo) |
92 |
print "\t...", functionName, "done." |
93 |
return timing |
94 |
# End test_pobyso_bounds_to_range_sa_so |
95 |
|
96 |
def test_pobyso_error_so(repeat=1000, number=10): |
97 |
functionName = inspect.stack()[0][3] |
98 |
print "Running", inspect.stack()[0][3], "..." |
99 |
# |
100 |
def test(): |
101 |
errorSo = pobyso_error_so() |
102 |
sollya_lib_clear_obj(errorSo) |
103 |
# |
104 |
wrapped = test_pobyso_wrapper(test) |
105 |
timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
106 |
print "\t...", functionName, "done." |
107 |
return timing |
108 |
# End test_pobyso_error_so |
109 |
|
110 |
def test_pobyso_get_list_elements_so_so(repeat = 1000, number=10): |
111 |
functionName = inspect.stack()[0][3] |
112 |
print "Running", functionName, "..." |
113 |
def test(): |
114 |
listSo = pobyso_parse_string_sa_so("[|0,1,2,3,4,5,6|];") |
115 |
(listSaSo, elementsNumSa, isEndEllipticSa) = \ |
116 |
pobyso_get_list_elements(listSo) |
117 |
for elementSo in listSaSo: |
118 |
sollya_lib_clear_obj(elementSo) |
119 |
# Notice that we use sollya_lib_clear_obj and not |
120 |
# sollya_lib_clear_object_list. |
121 |
sollya_lib_clear_obj(listSo, None) |
122 |
# |
123 |
wrapped = test_pobyso_wrapper(test) |
124 |
timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
125 |
print "\t...", functionName, "done." |
126 |
return timing |
127 |
# End test_pobyso_get_list_elements |
128 |
|
129 |
def test_pobyso_get_prec_so_sa(repeat=1000, number=10): |
130 |
functionName = inspect.stack()[0][3] |
131 |
print "Running", functionName, "..." |
132 |
def test(): |
133 |
precSa = pobyso_get_prec_so_sa() |
134 |
wrapped = test_pobyso_wrapper(test) |
135 |
timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
136 |
print "\t...", functionName, "done." |
137 |
return timing |
138 |
# End test_pobyso_get_prec_so_sa |
139 |
|
140 |
def test_pobyso_get_prec_of_constant_so_sa(repeat=1000, number=10): |
141 |
functionName = inspect.stack()[0][3] |
142 |
print "Running", functionName, "..." |
143 |
constSo = pobyso_constant_1_sa_so() |
144 |
def test(): |
145 |
precSa = pobyso_get_prec_of_constant_so_sa(constSo) |
146 |
wrapped = test_pobyso_wrapper(test) |
147 |
timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
148 |
print "\t...", functionName, "done." |
149 |
sollya_lib_clear_obj(constSo,None) |
150 |
return timing |
151 |
# End test_pobyso_get_prec_of_constant_so_sa |
152 |
|
153 |
def test_pobyso_guess_degree_sa_sa(repeat=1000, number=10): |
154 |
functionName = inspect.stack()[0][3] |
155 |
print "Running", functionName, "..." |
156 |
funcExpSa(x) = exp(x) |
157 |
RRIF = RealIntervalField(113) |
158 |
intervalSa = RRIF(3/8 - 2^-68, 3/8 + 2^-68) |
159 |
print "Interval:", intervalSa.str(style='brackets') |
160 |
errorSa = RR(2^-227) |
161 |
weightSa(x) = 1 |
162 |
degreeBoundSa = 150 |
163 |
# Function definition |
164 |
def test(): |
165 |
pobyso_guess_degree_sa_sa(funcExpSa, |
166 |
intervalSa, |
167 |
errorSa, |
168 |
weightSa, |
169 |
degreeBoundSa) |
170 |
wrapped = test_pobyso_wrapper(test) |
171 |
timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
172 |
print "\t...", functionName, "done." |
173 |
return timing |
174 |
# End test_pobyso_guess_degree_sa_sa |
175 |
|
176 |
def test_pobyso_guess_degree_so_sa(repeat=1000, number=10): |
177 |
functionName = inspect.stack()[0][3] |
178 |
print "Running", functionName, "..." |
179 |
funcExpSo = pobyso_parse_string_sa_so("exp(x)") |
180 |
RRIF = RealIntervalField(113) |
181 |
intervalSa = RRIF(3/8 - 2^-68, 3/8 + 2^-68) |
182 |
print "Interval:", intervalSa.str(style='brackets') |
183 |
intervalSo = pobyso_interval_to_range_sa_so(intervalSa) |
184 |
errorSo = pobyso_constant_sa_so(RR(2^-227)) |
185 |
weightSo=pobyso_parse_string_sa_so("1") |
186 |
degreeBoundSo = pobyso_constant_from_int_sa_so(150) |
187 |
# Function definition |
188 |
def test(): |
189 |
pobyso_guess_degree_so_sa(funcExpSo, |
190 |
intervalSo, |
191 |
errorSo, |
192 |
weightSo, |
193 |
degreeBoundSo) |
194 |
""" |
195 |
pobyso_guess_degree_so_sa(funcExpSo, |
196 |
intervalSo, |
197 |
errorSo, |
198 |
None, |
199 |
None) |
200 |
""" |
201 |
wrapped = test_pobyso_wrapper(test) |
202 |
timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
203 |
print "\t...", functionName, "done." |
204 |
sollya_lib_clear_obj(funcExpSo) |
205 |
sollya_lib_clear_obj(intervalSo) |
206 |
sollya_lib_clear_obj(errorSo) |
207 |
sollya_lib_clear_obj(weightSo) |
208 |
sollya_lib_clear_obj(degreeBoundSo) |
209 |
return timing |
210 |
# End test_pobyso_guess_degree_so_sa |
211 |
|
212 |
def test_pobyso_is_error_so_sa(repeat=1000, number=10): |
213 |
functionName = inspect.stack()[0][3] |
214 |
print "Running", inspect.stack()[0][3], "..." |
215 |
# |
216 |
def test(): |
217 |
errorSo = pobyso_error_so() |
218 |
if not pobyso_is_error_so_sa(errorSo): |
219 |
print "Error!" |
220 |
sollya_lib_clear_obj(errorSo) |
221 |
# |
222 |
wrapped = test_pobyso_wrapper(test) |
223 |
timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
224 |
print "\t...", functionName, "done." |
225 |
return timing |
226 |
# End test_pobyso_error_so |
227 |
|
228 |
def test_pobyso_lib_init(iterationsNum=10000): |
229 |
""" |
230 |
Must be called exactly once. Leaks lots of memory otherwise. |
231 |
""" |
232 |
print "Running", inspect.stack()[0][3] |
233 |
for index in xrange(0,iterationsNum): |
234 |
pobyso_lib_init() |
235 |
#pobyso_lib_close() |
236 |
# End test_pobyso_lib_init |
237 |
|
238 |
def test_pobyso_parse_string_sa_so(iterationsNum=10000): |
239 |
print "Running", inspect.stack()[0][3] |
240 |
for index in xrange(0,iterationsNum): |
241 |
expressionSo = pobyso_parse_string_sa_so('exp(x)+2*x*sin(x)') |
242 |
sollya_lib_clear_obj(expressionSo) |
243 |
# End pobyso_parse_string_sa_so |
244 |
|
245 |
def test_pobyso_taylor_expansion_no_change_var_so_so(repeat=1000, number=10): |
246 |
functionName = inspect.stack()[0][3] |
247 |
print "Running", functionName, "..." |
248 |
functionSo = pobyso_parse_string('exp(x)') |
249 |
degreeSo = pobyso_constant_from_int_sa_so(20) |
250 |
rangeSo = pobyso_bounds_to_range_sa_so(RR(1),RR(2)) |
251 |
errorTypeSo = pobyso_absolute_so_so() |
252 |
def test(functionSo, degreeSo, rangeSo, errorTypeSo): |
253 |
teSo = pobyso_taylor_expansion_no_change_var_so_so(functionSo, |
254 |
degreeSo, |
255 |
rangeSo, |
256 |
errorTypeSo) |
257 |
sollya_lib_clear_obj(teSo[0]) |
258 |
sollya_lib_clear_obj(teSo[1]) |
259 |
sollya_lib_clear_obj(teSo[2]) |
260 |
# End test |
261 |
wrapped = test_pobyso_wrapper(test, |
262 |
functionSo, |
263 |
degreeSo, |
264 |
rangeSo, |
265 |
errorTypeSo) |
266 |
timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
267 |
sollya_lib_clear_obj(functionSo) |
268 |
sollya_lib_clear_obj(degreeSo) |
269 |
sollya_lib_clear_obj(rangeSo) |
270 |
sollya_lib_clear_obj(errorTypeSo) |
271 |
print "\t...", functionName, "done." |
272 |
return timing |
273 |
# End test_pobyso_taylor_expansion_no_change_var_so_so |
274 |
|
275 |
def test_pobyso_taylorform_so_so_2(repeat=1000, number=10): |
276 |
functionName = inspect.stack()[0][3] |
277 |
print "Running", functionName, "..." |
278 |
functionSo = pobyso_parse_string('exp(x)') |
279 |
degreeSo = pobyso_constant_from_int_sa_so(20) |
280 |
rangeSo = pobyso_bounds_to_range_sa_so(RR(1),RR(2)) |
281 |
intervalCenterSo = sollya_lib_mid(rangeSo, None) |
282 |
errorTypeSo = pobyso_absolute_so_so() |
283 |
|
284 |
def test(functionSo, degreeSo, intervalCenterSo, errorTypeSo): |
285 |
taylorformSo = pobyso_taylorform_so_so(functionSo, |
286 |
degreeSo, |
287 |
intervalCenterSo, |
288 |
errorTypeSo) |
289 |
sollya_lib_clear_obj(taylorformSo) |
290 |
wrapped = test_pobyso_wrapper(test, |
291 |
functionSo, |
292 |
degreeSo, |
293 |
intervalCenterSo, |
294 |
errorTypeSo) |
295 |
timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
296 |
sollya_lib_clear_obj(functionSo,None) |
297 |
sollya_lib_clear_obj(degreeSo,None) |
298 |
sollya_lib_clear_obj(rangeSo,None) |
299 |
sollya_lib_clear_obj(intervalCenterSo,None) |
300 |
sollya_lib_clear_obj(errorTypeSo,None) |
301 |
return timing |
302 |
# End test_pobyso_taylor_so_so_2 |
303 |
|
304 |
def test_pobyso_dummy_test(iterationsNum=1, executionsNum=1): |
305 |
aString = "2+3" |
306 |
def test(): |
307 |
return 2+3 |
308 |
wrapped = test_pobyso_wrapper(test) |
309 |
timing = timeit.timeit(wrapped, number=executionsNum) |
310 |
return timing |
311 |
print "\t...testPobyso loaded" |
312 |
|