Statistiques
| Révision :

root / pobysoPythonSage / src / testPobyso.sage @ 120

Historique | Voir | Annoter | Télécharger (8 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 120 storres
def test_pobyso_clear_taylorform_sa_so(repeat=1000, number=10):
70 120 storres
    functionName =  inspect.stack()[0][3]
71 120 storres
    print "Running", inspect.stack()[0][3], "..."
72 120 storres
    intervalSa = RIF(1,2)
73 120 storres
    functionSo = pobyso_parse_string("exp(x)")
74 120 storres
    degreeSo   = pobyso_
75 120 storres
    #RealIntervalField
76 120 storres
    pointSo    = pobyso_constant_sa_so(RR("1.5"))
77 120 storres
    intervalSo = pobyso_interval_to_range_sa_so(intervalSa)
78 120 storres
    #
79 120 storres
    def test(lowerBoundSa, upperBoundSa):
80 120 storres
        taylorformSo = pobyso_taylorform_so_so(functionSo,
81 120 storres
                                               degreeSo,
82 120 storres
                                               pointSo,
83 120 storres
                                               intervalSo)
84 120 storres
        sollya_lib_clear_obj(rangeSo)
85 120 storres
    #
86 120 storres
    wrapped = test_pobyso_wrapper(test,lowerBoundSa, upperBoundSa)
87 120 storres
    timing = min(timeit.repeat(wrapped, repeat=repeat, number=number))
88 120 storres
    #
89 120 storres
    precSo = pobyso_get_prec_so()
90 120 storres
    #pobyso_autoprint(precSo)
91 120 storres
    sollya_lib_clear_obj(precSo)
92 120 storres
    print "\t...", functionName, "done."
93 120 storres
    return timing
94 120 storres
# End test_pobyso_bounds_to_range_sa_so
95 120 storres
96 117 storres
def test_pobyso_get_list_elements_so_so(repeat = 1000, number=10):
97 117 storres
    functionName = inspect.stack()[0][3]
98 117 storres
    print "Running", functionName, "..."
99 117 storres
    def test():
100 117 storres
        listSo = pobyso_parse_string_sa_so("[|0,1,2,3,4,5,6|];")
101 117 storres
        (listSaSo, elementsNumSa, isEndEllipticSa) = \
102 117 storres
            pobyso_get_list_elements(listSo)
103 117 storres
        for elementSo in listSaSo:
104 117 storres
            sollya_lib_clear_obj(elementSo)
105 117 storres
        # Notice that we use sollya_lib_clear_obj and not
106 117 storres
        # sollya_lib_clear_object_list.
107 117 storres
        sollya_lib_clear_obj(listSo, None)
108 117 storres
    #
109 117 storres
    wrapped = test_pobyso_wrapper(test)
110 117 storres
    timing = min(timeit.repeat(wrapped, repeat=repeat, number=number))
111 117 storres
    print "\t...", functionName, "done."
112 117 storres
    return timing
113 117 storres
# End test_pobyso_get_list_elements
114 117 storres
115 120 storres
def test_pobyso_get_prec_so_sa(repeat=1000, number=10):
116 120 storres
    functionName = inspect.stack()[0][3]
117 120 storres
    print "Running", functionName, "..."
118 120 storres
    def test():
119 116 storres
        precSa = pobyso_get_prec_so_sa()
120 120 storres
    wrapped = test_pobyso_wrapper(test)
121 120 storres
    timing = min(timeit.repeat(wrapped, repeat=repeat, number=number))
122 120 storres
    print "\t...", functionName, "done."
123 120 storres
    return timing
124 116 storres
# End test_pobyso_get_prec_so_sa
125 116 storres
126 120 storres
def test_pobyso_get_prec_of_constant_so_sa(repeat=1000, number=10):
127 120 storres
    functionName = inspect.stack()[0][3]
128 120 storres
    print "Running", functionName, "..."
129 120 storres
    constSo = pobyso_constant_1_sa_so()
130 120 storres
    def test():
131 116 storres
        precSa = pobyso_get_prec_of_constant_so_sa(constSo)
132 120 storres
    wrapped = test_pobyso_wrapper(test)
133 120 storres
    timing = min(timeit.repeat(wrapped, repeat=repeat, number=number))
134 120 storres
    sollya_lib_clear_obj(constSo,None)
135 120 storres
    print "\t...", functionName, "done."
136 120 storres
    return timing
137 116 storres
    sollya_lib_clear_obj(constSo)
138 116 storres
# End test_pobyso_get_prec_of_constant_so_sa
139 116 storres
140 116 storres
def test_pobyso_lib_init(iterationsNum=10000):
141 116 storres
    """
142 116 storres
    Must be called exactly once. Leaks lots of memory otherwise.
143 116 storres
    """
144 116 storres
    print "Running", inspect.stack()[0][3]
145 116 storres
    for index in xrange(0,iterationsNum):
146 116 storres
        pobyso_lib_init()
147 116 storres
        #pobyso_lib_close()
148 116 storres
# End test_pobyso_lib_init
149 116 storres
150 116 storres
def test_pobyso_parse_string_sa_so(iterationsNum=10000):
151 116 storres
    print "Running", inspect.stack()[0][3]
152 116 storres
    for index in xrange(0,iterationsNum):
153 116 storres
        expressionSo = pobyso_parse_string_sa_so('exp(x)+2*x*sin(x)')
154 116 storres
        sollya_lib_clear_obj(expressionSo)
155 116 storres
# End pobyso_parse_string_sa_so
156 116 storres
157 117 storres
def test_pobyso_taylor_expansion_no_change_var_so_so(repeat=1000, number=10):
158 117 storres
    functionName = inspect.stack()[0][3]
159 117 storres
    print "Running", functionName, "..."
160 116 storres
    functionSo  = pobyso_parse_string('exp(x)')
161 116 storres
    degreeSo    = pobyso_constant_from_int_sa_so(20)
162 116 storres
    rangeSo     = pobyso_bounds_to_range_sa_so(RR(1),RR(2))
163 116 storres
    errorTypeSo = pobyso_absolute_so_so()
164 117 storres
    def test(functionSo, degreeSo, rangeSo, errorTypeSo):
165 116 storres
        teSo = pobyso_taylor_expansion_no_change_var_so_so(functionSo,
166 116 storres
                                                           degreeSo,
167 116 storres
                                                           rangeSo,
168 116 storres
                                                           errorTypeSo)
169 116 storres
        sollya_lib_clear_obj(teSo[0])
170 116 storres
        sollya_lib_clear_obj(teSo[1])
171 116 storres
        sollya_lib_clear_obj(teSo[2])
172 117 storres
    # End test
173 117 storres
    wrapped = test_pobyso_wrapper(test,
174 117 storres
                                  functionSo,
175 117 storres
                                  degreeSo,
176 117 storres
                                  rangeSo,
177 117 storres
                                  errorTypeSo)
178 117 storres
    timing = min(timeit.repeat(wrapped, repeat=repeat, number=number))
179 116 storres
    sollya_lib_clear_obj(functionSo)
180 116 storres
    sollya_lib_clear_obj(degreeSo)
181 116 storres
    sollya_lib_clear_obj(rangeSo)
182 116 storres
    sollya_lib_clear_obj(errorTypeSo)
183 117 storres
    print "\t...", functionName, "done."
184 117 storres
    return timing
185 116 storres
# End test_pobyso_taylor_expansion_no_change_var_so_so
186 116 storres
187 120 storres
def test_pobyso_taylorform_so_so_2(repeat=1000, number=10):
188 117 storres
    functionName = inspect.stack()[0][3]
189 117 storres
    print "Running", functionName, "..."
190 117 storres
    functionSo  = pobyso_parse_string('exp(x)')
191 117 storres
    degreeSo    = pobyso_constant_from_int_sa_so(20)
192 117 storres
    rangeSo     = pobyso_bounds_to_range_sa_so(RR(1),RR(2))
193 117 storres
    intervalCenterSo = sollya_lib_mid(rangeSo, None)
194 117 storres
    errorTypeSo = pobyso_absolute_so_so()
195 117 storres
196 117 storres
    def test(functionSo, degreeSo, intervalCenterSo, errorTypeSo):
197 117 storres
        taylorformSo = pobyso_taylorform_so_so(functionSo,
198 117 storres
                                               degreeSo,
199 117 storres
                                               intervalCenterSo,
200 117 storres
                                               errorTypeSo)
201 117 storres
        sollya_lib_clear_obj(taylorformSo)
202 117 storres
    wrapped = test_pobyso_wrapper(test,
203 117 storres
                                  functionSo,
204 117 storres
                                  degreeSo,
205 117 storres
                                  intervalCenterSo,
206 117 storres
                                  errorTypeSo)
207 117 storres
    timing = min(timeit.repeat(wrapped, repeat=repeat, number=number))
208 117 storres
    sollya_lib_clear_obj(functionSo,None)
209 117 storres
    sollya_lib_clear_obj(degreeSo,None)
210 117 storres
    sollya_lib_clear_obj(rangeSo,None)
211 117 storres
    sollya_lib_clear_obj(intervalCenterSo,None)
212 117 storres
    sollya_lib_clear_obj(errorTypeSo,None)
213 117 storres
    return timing
214 117 storres
# End   test_pobyso_taylor_so_so_2
215 117 storres
216 117 storres
def test_pobyso_dummy_test(iterationsNum=1, executionsNum=1):
217 117 storres
    aString = "2+3"
218 117 storres
    def test():
219 117 storres
        return 2+3
220 117 storres
    wrapped = test_pobyso_wrapper(test)
221 117 storres
    timing = timeit.timeit(wrapped, number=executionsNum)
222 117 storres
    return timing
223 120 storres
print "\t...testPobyso loaded"
224 120 storres
print