root / pobysoPythonSage / src / testPobyso.sage @ 117
Historique | Voir | Annoter | Télécharger (6,58 ko)
1 | 116 | storres | """ |
---|---|---|---|
2 | 116 | storres | Test module for pobyso.py functions. |
3 | 116 | storres | |
4 | 116 | storres | AUTHORS: |
5 | 116 | storres | - S.T. (2014-02-26): initial version |
6 | 116 | storres | |
7 | 116 | storres | TODO: |
8 | 116 | storres | - everything! |
9 | 116 | storres | |
10 | 116 | storres | """ |
11 | 116 | storres | |
12 | 116 | storres | print "\ntestPobyso loading..." |
13 | 116 | storres | # |
14 | 116 | storres | import inspect |
15 | 116 | storres | import resource |
16 | 117 | storres | import timeit |
17 | 116 | storres | |
18 | 116 | storres | |
19 | 116 | storres | def memory_usage_resource(): |
20 | 116 | storres | rusage_denom = RR("1024") |
21 | 116 | storres | if sys.platform == 'darwin': |
22 | 116 | storres | # ... it seems that in OSX the output is different units ... |
23 | 116 | storres | rusage_denom = rusage_denom * rusage_denom |
24 | 116 | storres | mem = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / rusage_denom |
25 | 116 | storres | return mem |
26 | 116 | storres | # End memory_usage_resource() |
27 | 116 | storres | |
28 | 117 | storres | def test_pobyso_wrapper(func, *args, **kwargs): |
29 | 117 | storres | def wrapped(): |
30 | 117 | storres | return func(*args, **kwargs) |
31 | 117 | storres | return wrapped |
32 | 117 | storres | |
33 | 116 | storres | # |
34 | 116 | storres | def test_pobyso_absolute_so_so(): |
35 | 116 | storres | for index in xrange(0,1000000): |
36 | 116 | storres | absoluteSo = pobyso_absolute_so_so() |
37 | 116 | storres | sollya_lib_clear_obj(absoluteSo) |
38 | 116 | storres | # End test_absolute_so_so |
39 | 116 | storres | |
40 | 116 | storres | def test_pobyso_autoprint_so_so(iterationsNum=1000): |
41 | 116 | storres | absoluteSo = pobyso_absolute_so_so() |
42 | 116 | storres | for index in xrange(0,iterationsNum): |
43 | 116 | storres | pobyso_autoprint(absoluteSo) |
44 | 116 | storres | sollya_lib_clear_obj(absoluteSo) |
45 | 116 | storres | # End test_pobyso_autoprint |
46 | 116 | storres | |
47 | 117 | storres | def test_pobyso_bounds_to_range_sa_so(repeat=1000, number=10): |
48 | 117 | storres | functionName = inspect.stack()[0][3] |
49 | 117 | storres | print "Running", inspect.stack()[0][3], "..." |
50 | 116 | storres | lowerBoundSa = RR(1) |
51 | 116 | storres | upperBoundSa = RR(2) |
52 | 116 | storres | precSo = pobyso_get_prec_so() |
53 | 117 | storres | #pobyso_autoprint(precSo) |
54 | 117 | storres | # |
55 | 117 | storres | def test(lowerBoundSa, upperBoundSa): |
56 | 116 | storres | rangeSo = pobyso_bounds_to_range_sa_so(lowerBoundSa, upperBoundSa) |
57 | 116 | storres | sollya_lib_clear_obj(rangeSo) |
58 | 117 | storres | # |
59 | 117 | storres | wrapped = test_pobyso_wrapper(test,lowerBoundSa, upperBoundSa) |
60 | 117 | storres | timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
61 | 117 | storres | # |
62 | 116 | storres | precSo = pobyso_get_prec_so() |
63 | 117 | storres | #pobyso_autoprint(precSo) |
64 | 116 | storres | sollya_lib_clear_obj(precSo) |
65 | 117 | storres | print "\t...", functionName, "done." |
66 | 117 | storres | return timing |
67 | 116 | storres | # End test_pobyso_bounds_to_range_sa_so |
68 | 116 | storres | |
69 | 117 | storres | def test_pobyso_get_list_elements_so_so(repeat = 1000, number=10): |
70 | 117 | storres | functionName = inspect.stack()[0][3] |
71 | 117 | storres | print "Running", functionName, "..." |
72 | 117 | storres | def test(): |
73 | 117 | storres | listSo = pobyso_parse_string_sa_so("[|0,1,2,3,4,5,6|];") |
74 | 117 | storres | (listSaSo, elementsNumSa, isEndEllipticSa) = \ |
75 | 117 | storres | pobyso_get_list_elements(listSo) |
76 | 117 | storres | for elementSo in listSaSo: |
77 | 117 | storres | sollya_lib_clear_obj(elementSo) |
78 | 117 | storres | # Notice that we use sollya_lib_clear_obj and not |
79 | 117 | storres | # sollya_lib_clear_object_list. |
80 | 117 | storres | sollya_lib_clear_obj(listSo, None) |
81 | 117 | storres | # |
82 | 117 | storres | wrapped = test_pobyso_wrapper(test) |
83 | 117 | storres | timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
84 | 117 | storres | print "\t...", functionName, "done." |
85 | 117 | storres | return timing |
86 | 117 | storres | # End test_pobyso_get_list_elements |
87 | 117 | storres | |
88 | 116 | storres | def test_pobyso_get_prec_so_sa(iterationsNum=10000): |
89 | 116 | storres | print "Running", inspect.stack()[0][3] |
90 | 116 | storres | for index in xrange(0,iterationsNum): |
91 | 116 | storres | precSa = pobyso_get_prec_so_sa() |
92 | 116 | storres | # End test_pobyso_get_prec_so_sa |
93 | 116 | storres | |
94 | 116 | storres | def test_pobyso_get_prec_of_constant_so_sa(iterationsNum=10000): |
95 | 116 | storres | print "Running", inspect.stack()[0][3] |
96 | 116 | storres | constSo = pobyso_constant_from_int_sa_so(2) |
97 | 116 | storres | for index in xrange(0,iterationsNum): |
98 | 116 | storres | precSa = pobyso_get_prec_of_constant_so_sa(constSo) |
99 | 116 | storres | sollya_lib_clear_obj(constSo) |
100 | 116 | storres | # End test_pobyso_get_prec_of_constant_so_sa |
101 | 116 | storres | |
102 | 116 | storres | def test_pobyso_lib_init(iterationsNum=10000): |
103 | 116 | storres | """ |
104 | 116 | storres | Must be called exactly once. Leaks lots of memory otherwise. |
105 | 116 | storres | """ |
106 | 116 | storres | print "Running", inspect.stack()[0][3] |
107 | 116 | storres | for index in xrange(0,iterationsNum): |
108 | 116 | storres | pobyso_lib_init() |
109 | 116 | storres | #pobyso_lib_close() |
110 | 116 | storres | # End test_pobyso_lib_init |
111 | 116 | storres | |
112 | 116 | storres | def test_pobyso_parse_string_sa_so(iterationsNum=10000): |
113 | 116 | storres | print "Running", inspect.stack()[0][3] |
114 | 116 | storres | for index in xrange(0,iterationsNum): |
115 | 116 | storres | expressionSo = pobyso_parse_string_sa_so('exp(x)+2*x*sin(x)') |
116 | 116 | storres | sollya_lib_clear_obj(expressionSo) |
117 | 116 | storres | # End pobyso_parse_string_sa_so |
118 | 116 | storres | |
119 | 117 | storres | def test_pobyso_taylor_expansion_no_change_var_so_so(repeat=1000, number=10): |
120 | 117 | storres | functionName = inspect.stack()[0][3] |
121 | 117 | storres | print "Running", functionName, "..." |
122 | 116 | storres | functionSo = pobyso_parse_string('exp(x)') |
123 | 116 | storres | degreeSo = pobyso_constant_from_int_sa_so(20) |
124 | 116 | storres | rangeSo = pobyso_bounds_to_range_sa_so(RR(1),RR(2)) |
125 | 116 | storres | errorTypeSo = pobyso_absolute_so_so() |
126 | 117 | storres | def test(functionSo, degreeSo, rangeSo, errorTypeSo): |
127 | 116 | storres | teSo = pobyso_taylor_expansion_no_change_var_so_so(functionSo, |
128 | 116 | storres | degreeSo, |
129 | 116 | storres | rangeSo, |
130 | 116 | storres | errorTypeSo) |
131 | 116 | storres | sollya_lib_clear_obj(teSo[0]) |
132 | 116 | storres | sollya_lib_clear_obj(teSo[1]) |
133 | 116 | storres | sollya_lib_clear_obj(teSo[2]) |
134 | 117 | storres | # End test |
135 | 117 | storres | wrapped = test_pobyso_wrapper(test, |
136 | 117 | storres | functionSo, |
137 | 117 | storres | degreeSo, |
138 | 117 | storres | rangeSo, |
139 | 117 | storres | errorTypeSo) |
140 | 117 | storres | timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
141 | 116 | storres | sollya_lib_clear_obj(functionSo) |
142 | 116 | storres | sollya_lib_clear_obj(degreeSo) |
143 | 116 | storres | sollya_lib_clear_obj(rangeSo) |
144 | 116 | storres | sollya_lib_clear_obj(errorTypeSo) |
145 | 117 | storres | print "\t...", functionName, "done." |
146 | 117 | storres | return timing |
147 | 116 | storres | # End test_pobyso_taylor_expansion_no_change_var_so_so |
148 | 116 | storres | |
149 | 117 | storres | def test_pobyso_taylor_form_so_so_2(repeat=1000, number=10): |
150 | 117 | storres | functionName = inspect.stack()[0][3] |
151 | 117 | storres | print "Running", functionName, "..." |
152 | 117 | storres | functionSo = pobyso_parse_string('exp(x)') |
153 | 117 | storres | degreeSo = pobyso_constant_from_int_sa_so(20) |
154 | 117 | storres | rangeSo = pobyso_bounds_to_range_sa_so(RR(1),RR(2)) |
155 | 117 | storres | intervalCenterSo = sollya_lib_mid(rangeSo, None) |
156 | 117 | storres | errorTypeSo = pobyso_absolute_so_so() |
157 | 117 | storres | |
158 | 117 | storres | def test(functionSo, degreeSo, intervalCenterSo, errorTypeSo): |
159 | 117 | storres | taylorformSo = pobyso_taylorform_so_so(functionSo, |
160 | 117 | storres | degreeSo, |
161 | 117 | storres | intervalCenterSo, |
162 | 117 | storres | errorTypeSo) |
163 | 117 | storres | sollya_lib_clear_obj(taylorformSo) |
164 | 117 | storres | wrapped = test_pobyso_wrapper(test, |
165 | 117 | storres | functionSo, |
166 | 117 | storres | degreeSo, |
167 | 117 | storres | intervalCenterSo, |
168 | 117 | storres | errorTypeSo) |
169 | 117 | storres | timing = min(timeit.repeat(wrapped, repeat=repeat, number=number)) |
170 | 117 | storres | sollya_lib_clear_obj(functionSo,None) |
171 | 117 | storres | sollya_lib_clear_obj(degreeSo,None) |
172 | 117 | storres | sollya_lib_clear_obj(rangeSo,None) |
173 | 117 | storres | sollya_lib_clear_obj(intervalCenterSo,None) |
174 | 117 | storres | sollya_lib_clear_obj(errorTypeSo,None) |
175 | 117 | storres | return timing |
176 | 117 | storres | # End test_pobyso_taylor_so_so_2 |
177 | 117 | storres | |
178 | 117 | storres | def test_pobyso_dummy_test(iterationsNum=1, executionsNum=1): |
179 | 117 | storres | aString = "2+3" |
180 | 117 | storres | def test(): |
181 | 117 | storres | return 2+3 |
182 | 117 | storres | wrapped = test_pobyso_wrapper(test) |
183 | 117 | storres | timing = timeit.timeit(wrapped, number=executionsNum) |
184 | 117 | storres | return timing |
185 | 116 | storres | print "\t...testPobyso loaded" |