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

Last change on this file since 6232 was 6232, checked in by ole, 15 years ago

Improved test_caching_of_set_quantity - it still needs to be moved to validation suite.

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