source: branches/inundation-numpy-branch/caching/test_caching.py @ 6689

Last change on this file since 6689 was 2263, checked in by ole, 19 years ago

Work towards a test_all.py at the inundation directory level. Most tests pass except a few in least_squares, data_manager and cg_solve...

File size: 8.2 KB
Line 
1
2import unittest
3from caching import *
4
5
6
7# Define a test function to be cached
8#
9def f(a,b,c,N,x=0,y='abcdefg'):
10  """f(a,b,c,N)
11     Do something time consuming and produce a complex result.
12  """
13
14  import string
15
16  B = []
17  for n in range(N):
18    s = str(n+2.0/(n + 4.0))+'.a'*10
19    B.append((a,b,c,s,n,x,y))
20  return(B)
21
22
23
24
25
26
27
28class Test_Caching(unittest.TestCase):
29    def setUp(self):
30        set_option('verbose', 0)  #Why
31       
32        pass
33
34    def tearDown(self):
35        pass
36
37    def test_simple(self):
38        """Check set_option (and switch stats off)
39        """
40
41        set_option('savestat', 0)
42        assert options['savestat'] == 0
43        set_option('verbose', 0)
44        assert options['verbose'] == 0       
45       
46
47    def test_basic_caching(self):
48
49       
50        # Make some test input arguments
51        #
52        N = 5000  #Make N fairly small here
53
54        a = [1,2]
55        b = ('Thou shalt count the number three',4)
56        c = {'Five is right out': 6, (7,8): 9}
57        x = 3
58        y = 'holy hand granate'
59
60        # Test caching
61        #
62
63        comprange = 2
64
65        for comp in range(comprange):
66 
67            # Evaluate and store
68            #
69            T1 = cache(f, (a,b,c,N), {'x':x, 'y':y}, evaluate=1, \
70                       compression=comp)
71
72            # Retrieve
73            #                           
74            T2 = cache(f, (a,b,c,N), {'x':x, 'y':y}, compression=comp) 
75
76            # Reference result
77            #   
78            T3 = f(a,b,c,N,x=x,y=y)  # Compute without caching
79
80
81            assert T1 == T2, 'Cached result does not match computed result'
82            assert T2 == T3, 'Cached result does not match computed result'
83           
84
85    def test_cachefiles(self):
86        """Test existence of cachefiles
87        """       
88        N = 5000  #Make N fairly small here
89
90        a = [1,2]
91        b = ('Thou shalt count the number three',4)
92        c = {'Five is right out': 6, (7,8): 9}
93        x = 3
94        y = 'holy hand granate'
95
96       
97        FN = cache(f,(a,b,c,N), {'x':x, 'y':y}, verbose=0, \
98                  return_filename = 1)
99
100
101        assert FN[:2] == 'f['
102
103        CD = checkdir(cachedir)
104        compression = 1
105
106        (datafile,compressed0) = myopen(CD+FN+'_'+file_types[0],"rb",compression)
107        (argsfile,compressed1) = myopen(CD+FN+'_'+file_types[1],"rb",compression)
108        (admfile,compressed2) =  myopen(CD+FN+'_'+file_types[2],"rb",compression)
109
110        datafile.close()
111        argsfile.close()
112        admfile.close()
113
114    def test_test(self):       
115        """Test 'test' function when cache is present
116        """
117        N = 5000  #Make N fairly small here
118
119        a = [1,2]
120        b = ('Thou shalt count the number three',4)
121        c = {'Five is right out': 6, (7,8): 9}
122        x = 3
123        y = 'holy hand granate'
124       
125
126        T1 = cache(f,(a,b,c,N), {'x':x, 'y':y}, evaluate=1)
127       
128        T4 = cache(f,(a,b,c,N), {'x':x, 'y':y}, test=1)
129        assert T1 == T4, "Option 'test' when cache file present failed"     
130
131
132    def test_clear(self):       
133        """Test that 'clear' works
134        """
135        N = 5000  #Make N fairly small here
136
137        a = [1,2]
138        b = ('Thou shalt count the number three',4)
139        c = {'Five is right out': 6, (7,8): 9}
140        x = 3
141        y = 'holy hand granate'
142       
143
144        T1 = cache(f,(a,b,c,N), {'x':x, 'y':y}, evaluate = 1)
145       
146       
147        cache(f,(a,b,c,N), {'x':x, 'y':y}, clear = 1)   
148
149 
150        # Test 'test' function when cache is absent
151       
152       
153        T4 = cache(f,(a,b,c,N), {'x':x, 'y':y}, test=1)
154        assert T4 is None, "Option 'test' when cache absent failed"
155
156
157    def test_dependencies(self):
158       
159        # Make a dependency file
160        CD = checkdir(cachedir)       
161
162        DepFN = CD + 'testfile.tmp'
163        DepFN_wildcard = CD + 'test*.tmp'
164        Depfile = open(DepFN,'w')
165        Depfile.write('We are the knights who say NI!')
166        Depfile.close()
167
168       
169        # Test
170        #
171
172        N = 5000  #Make N fairly small here
173
174        a = [1,2]
175        b = ('Thou shalt count the number three',4)
176        c = {'Five is right out': 6, (7,8): 9}
177        x = 3
178        y = 'holy hand granate'
179       
180        T1 = cache(f,(a,b,c,N), {'x':x, 'y':y}, dependencies=DepFN) 
181        T2 = cache(f,(a,b,c,N), {'x':x, 'y':y}, dependencies=DepFN)                     
182                       
183        assert T1 == T2, 'Dependencies do not work'
184
185
186        # Test basic wildcard dependency
187        T3 = cache(f,(a,b,c,N), {'x':x, 'y':y}, dependencies=DepFN_wildcard)                     
188   
189        assert T1 == T3, 'Dependencies with wildcards do not work'
190
191
192        # Test that changed timestamp in dependencies triggers recomputation
193 
194        # Modify dependency file
195        Depfile = open(DepFN,'a')
196        Depfile.write('You must cut down the mightiest tree in the forest with a Herring')
197        Depfile.close()
198 
199        T3 = cache(f,(a,b,c,N), {'x':x, 'y':y}, dependencies=DepFN, test = 1)
200       
201        assert T3 is None, 'Changed dependencies not recognised'
202 
203        # Test recomputation when dependencies have changed
204        #
205        T3 = cache(f,(a,b,c,N), {'x':x, 'y':y}, dependencies=DepFN)                       
206        assert T1 == T3, 'Recomputed value with changed dependencies failed'
207
208    #def test_performance(self):       
209    #    """Performance test (with statistics)
210    #    Don't really rely on this as it will depend on specific computer.
211    #    """
212    #
213    #    import time
214    #    set_option('savestat', 1)
215    #
216    #    N = 300000   #Should be large on fast computers...
217    #    a = [1,2]
218    #    b = ('Thou shalt count the number three',4)
219    #    c = {'Five is right out': 6, (7,8): 9}
220    #    x = 3
221    #    y = 'holy hand granate'
222    #     
223    #   
224    #    tt = time.time()
225    #    T1 = cache(f,(a,b,c,N), {'x':x, 'y':y})
226    #    t1 = time.time() - tt
227    #
228    #    tt = time.time()
229    #    T2 = cache(f,(a,b,c,N), {'x':x, 'y':y})
230    #    t2 = time.time() - tt
231     
232    #    assert T1 == T2
233    #     assert t1 > t2, 'WARNING: Performance a bit low - this could be specific to current platform. Try to increase N in this test'
234    #    #test_OK('Performance test: relative time saved = %s pct' \
235    #    #        %str(round((t1-t2)*100/t1,2)))
236
237
238    def test_statsfile(self):                   
239        """Test presence of statistics file
240        """
241        import os, string
242        statsfile  = '.cache_stat'  # Basefilename for cached statistics.
243       
244        CD = checkdir(cachedir)               
245        DIRLIST = os.listdir(CD)
246        SF = []
247        for FN in DIRLIST:
248            if string.find(FN,statsfile) >= 0:
249                try:
250                    fid = open(CD+FN,'r')
251                    fid.close()
252                except:
253                    raise 'Statistics files cannot be opened'         
254 
255         
256
257    # def test_network_cachedir(self):
258
259#         #set_option('cachedir', 'H:\\.python_cache\\')
260#         set_option('cachedir', 'V:\\2\\cit\\.python_cache\\')
261#         set_option('verbose', 1)
262
263       
264#         # Make some test input arguments
265#         #
266#         N = 5000  #Make N fairly small here
267
268#         a = [1,2]
269#         b = ('Thou shalt count the number three',4)
270#         c = {'Five is right out': 6, (7,8): 9}
271#         x = 3
272#         y = 'holy hand granate'
273
274#         # Test caching
275#         #
276
277#         comprange = 2
278
279#         for comp in range(comprange):
280 
281#             # Evaluate and store
282#             #
283#             T1 = cache(f, (a,b,c,N), {'x':x, 'y':y}, evaluate=1, \
284#                        compression=comp)
285
286#             # Retrieve
287#             #                           
288#             T2 = cache(f, (a,b,c,N), {'x':x, 'y':y}, compression=comp)
289
290#             # Reference result
291#             #   
292#             T3 = f(a,b,c,N,x=x,y=y)  # Compute without caching
293
294
295#             assert T1 == T2, 'Cached result does not match computed result'
296#             assert T2 == T3, 'Cached result does not match computed result'
297             
298
299
300
301
302#-------------------------------------------------------------
303if __name__ == "__main__":
304    suite = unittest.makeSuite(Test_Caching,'test')
305    runner = unittest.TextTestRunner()
306    runner.run(suite)
Note: See TracBrowser for help on using the repository browser.