source: anuga_core/source/anuga/caching/test_caching.py @ 7197

Last change on this file since 7197 was 6406, checked in by ole, 16 years ago

Changed reference hash values to reflect 2009 upgrade of GA 64 bit clusters

File size: 26.5 KB
Line 
1
2import unittest
3
4from copy import deepcopy
5       
6from anuga.caching import *
7from anuga.caching.dummy_classes_for_testing import Dummy, Dummy_memorytest
8
9import Numeric as num
10
11
12# Define a test function to be cached
13#
14def f(a,b,c,N,x=0,y='abcdefg'):
15  """f(a,b,c,N)
16     Do something time consuming and produce a complex result.
17  """
18
19  import string
20
21  B = []
22  for n in range(N):
23    s = str(n+2.0/(n + 4.0))+'.a'*10
24    B.append((a,b,c,s,n,x,y))
25  return(B)
26 
27def f_numeric(A, B):
28  """Operation on Numeric arrays
29  """
30 
31  return 3.1*A + B + 1
32 
33 
34def f_object(A, B):
35  """Operation of objecs of class Dummy
36  """
37 
38  return A.value+B.value, A.another+B.another
39 
40
41def f_generic(A):
42  return A
43 
44def clear_and_create_cache(Dummy, verbose=False):
45
46  a = cache(Dummy, 'clear', verbose=verbose)
47  a = cache(Dummy, args=(9,10),
48            verbose=verbose)
49     
50
51def retrieve_cache(Dummy, verbose=False):
52  if verbose: print 'Check that cache is there'
53 
54  X = cache(Dummy, args=(9,10), test=1,
55            verbose=verbose)     
56           
57  msg = 'Cached value was not found'
58  assert X is not None, msg
59
60     
61
62   
63
64class Test_Caching(unittest.TestCase):
65    def setUp(self):
66        set_option('verbose', 0)  #Why
67
68        pass
69
70    def tearDown(self):
71        pass
72
73    def test_simple(self):
74        """Check set_option (and switch stats off)
75        """
76
77        set_option('savestat', 0)
78        assert options['savestat'] == 0
79        set_option('verbose', 0)
80        assert options['verbose'] == 0       
81       
82
83    def test_basic_caching(self):
84
85        verbose=False
86        # Make some test input arguments
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        # Test caching
97        #
98
99        comprange = 2
100
101        for comp in range(comprange):
102 
103            # Evaluate and store
104            #
105            T1 = cache(f, (a,b,c,N), {'x':x, 'y':y}, evaluate=1, \
106                       compression=comp, verbose=verbose)
107
108            # Retrieve
109            #                           
110            T2 = cache(f, (a,b,c,N), {'x':x, 'y':y}, compression=comp) 
111
112            # Reference result
113            #   
114            T3 = f(a,b,c,N,x=x,y=y)  # Compute without caching
115
116
117            assert T1 == T2, 'Cached result does not match computed result'
118            assert T2 == T3, 'Cached result does not match computed result'
119           
120
121           
122    def test_caching_of_numeric_arrays(self):
123        """test_caching_of_numeric_arrays
124       
125        Test that Numeric arrays can be recognised by caching even if their id's are different
126        """
127       
128        verbose = False
129       
130        # Make some test input arguments
131        A0 = num.arange(5)
132        B0 = num.array([1.1, 2.2, 0.0, -5, -5])
133       
134        A1 = A0.copy()
135        B1 = B0.copy()
136       
137        # Check that their ids are different
138        assert id(A0) != id(A1)
139        assert id(B0) != id(B1)       
140       
141       
142        # Test caching
143        comprange = 2
144        for comp in range(comprange):
145 
146            # Evaluate and store
147            T1 = cache(f_numeric, (A0, B0), evaluate=1,
148                       compression=comp, verbose=verbose)
149
150            # Retrieve
151            T2 = cache(f_numeric, (A1, B1), 
152                       compression=comp, test=1, verbose=verbose) 
153                       
154            # Check for presence of cached result
155            msg = 'Different array objects with same contents were not recognised'           
156            assert T2 is not None, msg
157
158            # Reference result
159            T3 = f_numeric(A0, B0) # Compute without caching
160
161
162            assert T1 == T2, 'Cached result does not match computed result'
163            assert T2 == T3, 'Cached result does not match computed result'
164           
165
166    def test_hash_collision(self):
167        """test_hash_collision(self):
168       
169        Test that hash collisons are dealt with correctly
170        """
171       
172        verbose = False
173       
174        # Make test input arguments
175        A0 = num.arange(5)*1.0
176        B = ('x', 15)
177       
178        # Create different A that hashes to the same address (having the same average)
179        A1 = num.array([2.0, 2.0, 2.0, 2.0, 2.0])       
180       
181        assert myhash(A0) == myhash(A1)
182           
183           
184        # Test caching
185        comprange = 2
186        for comp in range(comprange):
187       
188            # Clear
189            cache(f_numeric, (A0, A0), clear=1,
190                  compression=comp, verbose=verbose)       
191            cache(f_numeric, (A1, A1), clear=1,
192                  compression=comp, verbose=verbose)                         
193 
194 
195            # Evaluate and store
196            T1 = cache(f_numeric, (A0, A0), evaluate=1,
197                       compression=comp, verbose=verbose)
198
199           
200            # Check that A1 doesn't trigger retrieval of the previous result
201            # even though it hashes to the same address
202            T2 = cache(f_numeric, (A1, A1), 
203                       compression=comp, verbose=verbose) 
204           
205            T1_ref = f_numeric(A0, A0)
206            T2_ref = f_numeric(A1, A1)
207
208            assert num.alltrue(T1 == T1_ref)
209            assert num.alltrue(T2 == T2_ref)
210
211
212    def test_caching_of_dictionaries(self):
213        """test_caching_of_dictionaries
214       
215        Real example from ANUGA that caused some
216        hashing problems
217        """
218   
219
220        verbose = False #True
221       
222        D = {'point_attributes': None, 
223             'use_cache': True, 
224             'vertex_coordinates': None, 
225             'verbose': True, 
226             'max_read_lines': 500, 
227             'acceptable_overshoot': 1.01, 
228             'mesh': None, 
229             'data_origin': None, 
230             'alpha': 0.02, 
231             'mesh_origin': None, 
232             'attribute_name': None, 
233             'triangles': None}         
234       
235        DD = deepcopy(D) # Mangles the dictionary ordering
236       
237        assert myhash(DD) == myhash(D)
238
239        # Also test caching now that we are at it
240        comprange = 2
241        for comp in range(comprange):
242 
243            # Evaluate and store using D
244            T1 = cache(f_generic, D, evaluate=1,
245                       compression=comp, verbose=verbose)
246
247            # Retrieve using copy (DD)
248            T2 = cache(f_generic, DD, 
249                       compression=comp, test=1, verbose=verbose) 
250                       
251            # Check for presence of cached result
252            msg = 'Cached object was not found'           
253            assert T2 is not None, msg
254
255            # Reference result
256            T3 = f_generic(D) # Compute without caching
257
258           
259            msg = 'Cached result does not match computed result' 
260           
261            # Compare dictionaries
262            for key in T1:
263                assert T1[key] == T2[key]
264                assert T2[key] == T3[key]               
265               
266           
267           
268           
269
270    def test_caching_of_objects(self):
271        """test_caching_of_objects
272       
273        Test that Objecs can be recognised as input variabelse
274        by caching even if their id's are different
275        """
276   
277
278        verbose = False
279       
280        # Make some test input arguments
281        A0 = Dummy(5, 7)
282        B0 = Dummy(2.2, -5)
283       
284        A0.new_attribute = 'x' 
285        B0.new_attribute = 'x'       
286       
287        A1 = deepcopy(A0)
288        B1 = deepcopy(B0)       
289       
290        # Check that their ids are different
291        assert id(A0) != id(A1)
292        assert id(B0) != id(B1)       
293       
294       
295        # Test caching
296        comprange = 2
297        for comp in range(comprange):
298 
299            # Evaluate and store
300            T1 = cache(f_object, (A0, B0), evaluate=1,
301                       compression=comp, verbose=verbose)
302
303            # Retrieve
304            T2 = cache(f_object, (A1, B1), 
305                       compression=comp, 
306                       test=1, 
307                       verbose=verbose) 
308                       
309            # Check for presence of cached result
310            msg = 'Different objects with same attributes were not recognised'
311            assert T2 is not None, msg
312
313            # Reference result
314            T3 = f_object(A0, B0) # Compute without caching
315
316
317            assert T1 == T2, 'Cached result does not match computed result'
318            assert T2 == T3, 'Cached result does not match computed result'
319           
320
321    def test_caching_of_circular_structures(self):
322        """test_caching_of_circular_structures
323       
324        Test that Caching doesn't recurse infinitely in case of
325        circular or self-referencing structures
326        """
327       
328        verbose = False
329       
330        # Create input argument
331        A = Dummy(5, 7)
332        B = {'x': 10, 'A': A}
333        C = [B, 15]
334        A.value = C # Make it circular
335        A.x = [1,2,C,5,A] # More circular and self referential
336       
337        AA = deepcopy(A)
338
339        # Test caching
340        comprange = 2
341        for comp in range(comprange):
342 
343            # Evaluate and store
344            T1 = cache(f_generic, A, 
345                       evaluate=1,
346                       compression=comp, verbose=verbose)
347
348            # Retrieve
349            T2 = cache(f_generic, AA, 
350                       compression=comp, 
351                       test=1, verbose=verbose) 
352                       
353            # Check for presence of cached result
354            msg = 'Cached object was not found'           
355            assert T2 is not None, msg
356
357            # Reference result
358            T3 = f_generic(A) # Compute without caching
359
360           
361            msg = 'Cached result does not match computed result' 
362            assert str(T1) == str(T2), msg
363            assert str(T2) == str(T3), msg
364                                   
365           
366    def test_caching_of_callable_objects(self):
367        """test_caching_of_callable_objects(self)
368       
369        Test that caching will discern between calls of
370        two different instances of a callable object with the same input.
371       
372        This cause problems with add_quantity in ANUGA in changeset:6225
373        where a previous instance of Polygon_function was picked up.
374        """
375
376        class call:
377       
378          def __init__(self, a, b):
379            self.a = a
380            self.b = b
381           
382          def __call__(self, x):
383            return self.a*x + self.b
384
385           
386        f1 = call(2, 3)
387        f2 = call(5, 7)
388
389        #print myhash(f1)
390        #print myhash(f2)     
391
392        # Check that hash value of callable objects don't change
393        # FIXME (Ole): The hash values do appear to change when OS
394        # and/or dependencies are upgraded
395        if os.name == 'posix' and os.uname()[4] in ['x86_64', 'ia64']:
396          # 64 bit hash values
397          f1hash = 7079146893884768701
398          f2hash = -6995306676314913340
399
400          # Prior to cluster upgrades Feb 2009
401          #f1hash = 1914027059797211698
402          #f2hash = 1914027059807087171
403        else:
404          # 32 bit hash values
405          f1hash = -758136387
406          f2hash = -11221564     
407         
408        assert myhash(f1) == f1hash
409        assert myhash(f2) == f2hash
410       
411        bc1 = get_bytecode(f1)
412        bc2 = get_bytecode(f2)
413       
414        #print 'bc1', bc1
415        #print 'bc2', bc2
416       
417        msg = 'Byte code should be different'
418        assert bc1 != bc2, msg
419
420       
421        x = num.arange(10).astype(num.Float)
422       
423        ref1 = f1(x)
424        ref2 = f2(x)
425
426        # Clear cache for f1(x) and f2(x) and verify that all is clear
427        cache(f1, x, clear=True, verbose=False)
428        flag = cache(f1, x, test=True, verbose=False)       
429        assert flag is None
430       
431        cache(f2, x, clear=True, verbose=False)
432        flag = cache(f2, x, test=True, verbose=False)       
433        assert flag is None       
434       
435        # Run f1(x) and cache
436        res1 = cache(f1, x, verbose=False)
437        assert num.allclose(res1, ref1)       
438       
439        # Test that f1(x) has been cached correctly
440        res1 = cache(f1, x, test=True, verbose=False)               
441        assert num.allclose(res1, ref1)               
442       
443        # Test that f2(x) is still clear
444        cache(f2, x, clear=True, verbose=False)
445        flag = cache(f2, x, test=True, verbose=False)       
446        assert flag is None               
447       
448        # Run f2(x) and test result
449        res2 = cache(f2, x, verbose=False)
450        msg = 'Wrong result for f2(x)'
451        assert num.allclose(res2, ref2), msg
452       
453
454       
455       
456    def test_uniqueness_of_hash_values(self):
457        """test_uniqueness_of_hash_values(self):
458       
459        Test that Caching can handle a realistic
460        complex structure by hashing it consistently and
461        uniquely.
462        """
463       
464        verbose = False
465       
466        # Create input argument
467        A = Dummy(5, 7)
468        B = {'x': 10, 'A': A}
469        C = [B, num.array([1.2, 3, 5, 0.1])]
470        A.value = C # Make it circular
471
472        # Create identical but separate object   
473        AA = Dummy(None, None)
474        BB = {'A': AA, 'x': 10}
475        CC = [BB, num.array([1.200, 3.000, 5.00, 1.0/10])]
476        AA.value = CC # Make it circular
477        AA.another = 3+4       
478       
479       
480        assert myhash(A) == myhash(AA)     
481           
482           
483       
484        # Also test caching now that we are at it
485        comprange = 2
486        for comp in range(comprange):
487 
488            # Evaluate and store using A
489            T1 = cache(f_generic, A, evaluate=1,
490                       compression=comp, verbose=verbose)
491
492            # Retrieve using copy (AA)
493            T2 = cache(f_generic, AA, 
494                       compression=comp, test=1, verbose=verbose) 
495                       
496            # Check for presence of cached result
497            msg = 'Cached object was not found'           
498            assert T2 is not None, msg
499
500            # Reference result
501            T3 = f_generic(A) # Compute without caching
502
503           
504            msg = 'Cached result does not match computed result' 
505            assert str(T1) == str(T2), msg
506            assert str(T2) == str(T3), msg
507           
508           
509
510    def test_caching_of_simple_circular_dictionaries(self):
511        """test_caching_of_circular_structures
512       
513        Test that Caching doesn't recurse infinitely in case of
514        circular or self-referencing structures
515        """
516       
517        verbose = False #True
518       
519        # Create input argument
520        A = {'x': 10, 'B': None}
521        B = [A, 15]
522        A['B'] = B # Make it circular
523       
524        # Test caching
525        comprange = 2
526        for comp in range(comprange):
527 
528            # Evaluate and store
529            T1 = cache(f_generic, A, evaluate=1,
530                       compression=comp, verbose=verbose)
531                       
532
533            # Retrieve
534            T2 = cache(f_generic, A, 
535                       compression=comp, test=1, verbose=verbose) 
536                       
537            # Check for presence of cached result
538            msg = 'Cached object was not found'           
539            assert T2 is not None, msg
540
541            # Reference result
542            T3 = f_generic(A) # Compute without caching
543
544
545            msg = 'Cached result does not match computed result'
546            assert str(T1) == str(T2), msg
547            assert str(T2) == str(T3), msg
548
549           
550           
551    def test_cachefiles(self):
552        """Test existence of cachefiles
553        """       
554        N = 5000  #Make N fairly small here
555
556        a = [1,2]
557        b = ('Thou shalt count the number three',4)
558        c = {'Five is right out': 6, (7,8): 9}
559        x = 3
560        y = 'holy hand granate'
561
562       
563        FN = cache(f,(a,b,c,N), {'x':x, 'y':y}, verbose=0, \
564                  return_filename = 1)
565
566
567        assert FN[:2] == 'f['
568
569        CD = checkdir(cachedir)
570        compression = 1
571
572        (datafile,compressed0) = myopen(CD+FN+'_'+file_types[0],"rb",compression)
573        (argsfile,compressed1) = myopen(CD+FN+'_'+file_types[1],"rb",compression)
574        (admfile,compressed2) =  myopen(CD+FN+'_'+file_types[2],"rb",compression)
575
576        datafile.close()
577        argsfile.close()
578        admfile.close()
579
580    def test_test(self):       
581        """Test 'test' function when cache is present
582        """
583       
584        verbose = False
585       
586        N = 5 
587
588        a = [1,2]
589        b = ('Thou shalt count the number three',4)
590        c = {'Five is right out': 6, (7,8): 9}
591        x = 3
592        y = 'holy hand granate'
593       
594
595        T1 = cache(f, (a,b,c,N), {'x':x, 'y':y}, 
596                   evaluate=1, 
597                   verbose=verbose)
598       
599        T2 = cache(f, (a,b,c,N), {'x':x, 'y':y}, 
600                   test=1,
601                   verbose=verbose)
602                   
603                   
604        # Check for presence of cached result
605        msg = 'Different objects with same attributes were not recognised'
606        assert T2 is not None, msg                   
607                   
608        assert T1 == T2, "Option 'test' when cache file present failed"     
609
610
611    def test_clear(self):       
612        """Test that 'clear' works
613        """
614
615        N = 5000  #Make N fairly small here
616
617        a = [1,2]
618        b = ('Thou shalt count the number three',4)
619        c = {'Five is right out': 6, (7,8): 9}
620        x = 3
621        y = 'holy hand granate'
622       
623
624        T1 = cache(f,(a,b,c,N), {'x':x, 'y':y}, evaluate = 1)
625       
626       
627        cache(f, (a,b,c,N), {'x':x, 'y':y}, clear = 1)   
628
629 
630        # Test 'test' function when cache is absent
631       
632       
633        T4 = cache(f, (a,b,c,N), {'x':x, 'y':y}, test=1)
634        #print 'T4', T4
635        assert T4 is None, "Option 'test' when cache absent failed"
636
637
638    def test_dependencies(self):
639       
640        # Make a dependency file
641        CD = checkdir(cachedir)       
642
643        DepFN = CD + 'testfile.tmp'
644        DepFN_wildcard = CD + 'test*.tmp'
645        Depfile = open(DepFN,'w')
646        Depfile.write('We are the knights who say NI!')
647        Depfile.close()
648
649       
650        # Test
651        #
652
653        N = 5000  #Make N fairly small here
654
655        a = [1,2]
656        b = ('Thou shalt count the number three',4)
657        c = {'Five is right out': 6, (7,8): 9}
658        x = 3
659        y = 'holy hand granate'
660       
661        T1 = cache(f,(a,b,c,N), {'x':x, 'y':y}, dependencies=DepFN) 
662        T2 = cache(f,(a,b,c,N), {'x':x, 'y':y}, dependencies=DepFN)                     
663                       
664        assert T1 == T2, 'Dependencies do not work'
665
666
667        # Test basic wildcard dependency
668        T3 = cache(f,(a,b,c,N), {'x':x, 'y':y}, dependencies=DepFN_wildcard)                     
669   
670        assert T1 == T3, 'Dependencies with wildcards do not work'
671
672
673        # Test that changed timestamp in dependencies triggers recomputation
674 
675        # Modify dependency file
676        Depfile = open(DepFN,'a')
677        Depfile.write('You must cut down the mightiest tree in the forest with a Herring')
678        Depfile.close()
679 
680        T3 = cache(f,(a,b,c,N), {'x':x, 'y':y}, dependencies=DepFN, test = 1)
681       
682        assert T3 is None, 'Changed dependencies not recognised'
683 
684        # Test recomputation when dependencies have changed
685        #
686        T3 = cache(f,(a,b,c,N), {'x':x, 'y':y}, dependencies=DepFN)                       
687        assert T1 == T3, 'Recomputed value with changed dependencies failed'
688
689    #def test_performance(self):       
690    #    """Performance test (with statistics)
691    #    Don't really rely on this as it will depend on specific computer.
692    #    """
693    #
694    #    import time
695    #    set_option('savestat', 1)
696    #
697    #    N = 300000   #Should be large on fast computers...
698    #    a = [1,2]
699    #    b = ('Thou shalt count the number three',4)
700    #    c = {'Five is right out': 6, (7,8): 9}
701    #    x = 3
702    #    y = 'holy hand granate'
703    #     
704    #   
705    #    tt = time.time()
706    #    T1 = cache(f,(a,b,c,N), {'x':x, 'y':y})
707    #    t1 = time.time() - tt
708    #
709    #    tt = time.time()
710    #    T2 = cache(f,(a,b,c,N), {'x':x, 'y':y})
711    #    t2 = time.time() - tt
712     
713    #    assert T1 == T2
714    #     assert t1 > t2, 'WARNING: Performance a bit low - this could be specific to current platform. Try to increase N in this test'
715    #    #test_OK('Performance test: relative time saved = %s pct' \
716    #    #        %str(round((t1-t2)*100/t1,2)))
717
718
719    def test_statsfile(self):                   
720        """Test presence of statistics file
721        """
722        import os, string
723        statsfile  = '.cache_stat'  # Basefilename for cached statistics.
724       
725        CD = checkdir(cachedir)               
726        DIRLIST = os.listdir(CD)
727        SF = []
728        for FN in DIRLIST:
729            if string.find(FN,statsfile) >= 0:
730                try:
731                    fid = open(CD+FN,'r')
732                    fid.close()
733                except:
734                    raise 'Statistics files cannot be opened'         
735 
736         
737
738    # def test_network_cachedir(self):
739
740#         #set_option('cachedir', 'H:\\.python_cache\\')
741#         set_option('cachedir', 'V:\\2\\cit\\.python_cache\\')
742#         set_option('verbose', 1)
743
744       
745#         # Make some test input arguments
746#         #
747#         N = 5000  #Make N fairly small here
748
749#         a = [1,2]
750#         b = ('Thou shalt count the number three',4)
751#         c = {'Five is right out': 6, (7,8): 9}
752#         x = 3
753#         y = 'holy hand granate'
754
755#         # Test caching
756#         #
757
758#         comprange = 2
759
760#         for comp in range(comprange):
761 
762#             # Evaluate and store
763#             #
764#             T1 = cache(f, (a,b,c,N), {'x':x, 'y':y}, evaluate=1, \
765#                        compression=comp)
766
767#             # Retrieve
768#             #                           
769#             T2 = cache(f, (a,b,c,N), {'x':x, 'y':y}, compression=comp)
770
771#             # Reference result
772#             #   
773#             T3 = f(a,b,c,N,x=x,y=y)  # Compute without caching
774
775
776#             assert T1 == T2, 'Cached result does not match computed result'
777#             assert T2 == T3, 'Cached result does not match computed result'
778             
779
780
781    def Will_fail_test_objects(self):
782      """
783      This test shows how instances can't be effectively cached.
784      myhash uses hash which uses id which uses the memory address.
785     
786      This will be a NIL problem once caching can handle instances with different id's and
787      identical content.
788     
789      The test is disabled.
790      """
791     
792
793     
794      verbose = True
795      #verbose = False
796
797      for i in range(2):
798        if verbose: print "clear cache"
799        a = cache(Dummy, 'clear')
800       
801        if verbose: print "cache for first time"
802        a = cache(Dummy, args=(9,10), verbose=verbose)
803        hash_value = myhash(a)
804       
805        #print "hash_value",hash_value
806        if verbose: print "cache for second time"
807        a = cache(Dummy, args=(9,10), verbose=verbose)
808       
809        #print "myhash(a)",myhash(a)
810        assert hash_value == myhash(a)
811
812
813    # This test works in the caching dir and in anuga_core, but not in the
814    # anuga_core/source/anuga dir
815    # This has to do with pickle (see e.g. http://telin.ugent.be/~slippens/drupal/pickleproblem)
816    # The error message is
817    # PicklingError: Can't pickle test_caching.Dummy: it's not the same object as test_caching.Dummy
818    #
819    # This problem was fixed by moving the class into the separate module
820   
821    def test_objects_are_created(self):
822      """
823      This test shows how instances can be created from cache
824      as long as input arguments are unchanged.
825
826      Such instances will have different id's and cannot be currently be used as input
827      arguments in subsequent caches. However, this is still useful.
828
829      Do it for all combinations of compression
830
831      """
832
833      verbose = False
834     
835      for compression_store in [False, True]:
836        for compression_retrieve in [False, True]:       
837       
838          if verbose: print 'clear cache'
839          a = cache(Dummy, 'clear')
840       
841          if verbose: print 'cache for first time'
842          a_ref = cache(Dummy, args=(9,10),
843                        compression=compression_store,
844                        verbose=verbose)
845         
846          if verbose: print 'Check that cache is there'
847          assert cache(Dummy, args=(9,10), test=1,
848                       compression=compression_retrieve,
849                       verbose=verbose)
850                       
851          if verbose: print 'Check cached result'
852          a = cache(Dummy, args=(9,10),
853                    compression=compression_store,
854                    verbose=verbose)                       
855          assert a.__dict__ == a_ref.__dict__
856
857
858
859    # NOTE (Ole): This test has been commented out because,
860    #             although the test will pass (not anymore!)
861    #             inside the caching dir and also at the anuga_core level,
862    #             it won't pass at the anuga_core/source/anuga level.
863    # It may have to do with the comments above.
864    #
865    # But this is a very nice test to run occasionally within the caching
866    # area
867    def Xtest_objects_are_created_memory(self):
868      """
869     
870      This test shows how instances can be created from cache
871      as long as input arguments are unchanged - even if the class
872      lives in different memory locations.
873
874      This is using cache created in the main program below
875      """
876
877      verbose = True #False
878
879      # Redefine class Dummy_memorytest
880      class Dummy_memorytest:
881        def __init__(self, value, another):
882          self.value = value     
883
884      # Make sure that class has been redefined to another address
885      print
886      print 'Initial_addr  ', initial_addr
887      print 'Redefined addr', `Dummy_memorytest`
888      msg = 'Redefined class ended up at same memory location as '
889      msg += 'original class making this test irrelevant. Try to run '
890      msg += 'it again and see if this error goes away.'
891      msg += 'If it persists contact Ole.Nielsen@ga.gov.au'
892      assert initial_addr != `Dummy_memorytest`, msg   
893
894     
895      retrieve_cache(Dummy_memorytest, verbose=verbose)     
896         
897# Cache created for use with 'test_objects_are_created_memory'
898#initial_addr = `Dummy_memorytest`
899#clear_and_create_cache(Dummy_memorytest, verbose=False)
900 
901
902
903#-------------------------------------------------------------
904if __name__ == "__main__":
905    #suite = unittest.makeSuite(Test_Caching, 'test_caching_of_callable_objects')
906    suite = unittest.makeSuite(Test_Caching, 'test')
907    runner = unittest.TextTestRunner()
908    runner.run(suite)
Note: See TracBrowser for help on using the repository browser.