Changeset 4333


Ignore:
Timestamp:
Mar 28, 2007, 5:11:05 PM (17 years ago)
Author:
ole
Message:

Added test that revealed issue where identical functions with different memory addresses caused cache to recompute.
Fixed problem.

Location:
anuga_core/source/anuga/caching
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • anuga_core/source/anuga/caching/caching.py

    r4279 r4333  
    14541454    funcname = string.join(tmp)
    14551455
     1456    # Truncate memory address as in
     1457    # class __main__.Dummy at 0x00A915D0
     1458    index = funcname.find('at 0x')
     1459    if index >= 0:
     1460      funcname = funcname[:index+5] # Keep info that there is an address
     1461
    14561462  funcname = nospace(funcname)
    14571463  return(funcname)
  • anuga_core/source/anuga/caching/test_caching.py

    r4272 r4333  
    2121
    2222class Dummy:
    23   def __init__(self,value, another):
     23  def __init__(self, value, another):
    2424    self.value = value
     25
     26
     27class Dummy_memorytest:
     28  def __init__(self, value, another):
     29    self.value = value
     30
     31
     32def clear_and_create_cache(Dummy, verbose=False):
     33  a = cache(Dummy, 'clear', verbose=verbose)
     34       
     35  a = cache(Dummy, args=(9,10),
     36            verbose=verbose)
     37     
     38
     39def retrieve_cache(Dummy, verbose=False):
     40  if verbose: print 'Check that cache is there'
     41  assert cache(Dummy, args=(9,10), test=1,
     42               verbose=verbose)     
     43     
     44
     45   
    2546
    2647class Test_Caching(unittest.TestCase):
    2748    def setUp(self):
    2849        set_option('verbose', 0)  #Why
    29        
     50
    3051        pass
    3152
     
    4566    def test_basic_caching(self):
    4667
    47        
     68        verbose=False
    4869        # Make some test input arguments
    4970        #
     
    6687            #
    6788            T1 = cache(f, (a,b,c,N), {'x':x, 'y':y}, evaluate=1, \
    68                        compression=comp)
     89                       compression=comp, verbose=verbose)
    6990
    7091            # Retrieve
     
    304325      """
    305326      verbose = True
    306       verbose = False
     327      #verbose = False
    307328
    308329      for i in range(2):
    309330        if verbose: print "clear cache"
    310         a = cache(Dummy,'clear')     
     331        a = cache(Dummy, 'clear')
     332       
    311333        if verbose: print "cache for first time"
    312         a = cache(Dummy,args=(9,10) ,verbose=verbose)
     334        a = cache(Dummy, args=(9,10), verbose=verbose)
    313335        hash_value = myhash(a)
     336       
    314337        #print "hash_value",hash_value
    315338        if verbose: print "cache for second time"
    316         a = cache(Dummy,args=(9,10) ,verbose=verbose)
     339        a = cache(Dummy, args=(9,10), verbose=verbose)
     340       
    317341        #print "myhash(a)",myhash(a)
    318342        assert hash_value == myhash(a)
    319343
     344
     345    def test_objects_are_created(self):
     346      """
     347      However, this test shows how instances can be created from cache
     348      as long as input arguments are unchanged.
     349
     350      Such instances will have different id's and cannot be used as input
     351      arguments in subsequent caches. However, this is still useful.
     352
     353      Do it for all combinations of compression
     354
     355      """
     356
     357      verbose = False
     358
     359      for compression_store in [False, True]:
     360        for compression_retrieve in [False, True]:       
     361       
     362          if verbose: print 'clear cache'
     363          a = cache(Dummy, 'clear')
     364       
     365          if verbose: print 'cache for first time'
     366          a = cache(Dummy, args=(9,10),
     367                    compression=compression_store,
     368                    verbose=verbose)
     369         
     370          if verbose: print 'Check that cache is there'
     371          assert cache(Dummy, args=(9,10), test=1,
     372                       compression=compression_retrieve,
     373                       verbose=verbose)
     374
     375
     376
     377
     378    def test_objects_are_created_memory(self):
     379      """
    320380     
     381      This test shows how instances can be created from cache
     382      as long as input arguments are unchanged - even if the class
     383      lives in different memory locations.
     384
     385      This is using cache created in the main program
     386
     387      """
     388
     389      verbose = False
     390
     391      # Redefine class Dummy_memorytest
     392      class Dummy_memorytest:
     393        def __init__(self, value, another):
     394          self.value = value     
     395
     396      retrieve_cache(Dummy_memorytest, verbose=verbose)     
     397         
     398
     399
     400
     401     
    321402       
    322403
     
    324405#-------------------------------------------------------------
    325406if __name__ == "__main__":
     407
     408    # Cache created for test_objects_are_created_memory to make sure it has a different memory address
     409    clear_and_create_cache(Dummy_memorytest, verbose=False)
     410 
    326411    suite = unittest.makeSuite(Test_Caching,'test')
    327412    runner = unittest.TextTestRunner()
Note: See TracChangeset for help on using the changeset viewer.