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

Last change on this file since 6260 was 6260, checked in by rwilson, 15 years ago

Better test of hashing collision.

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