Ignore:
Timestamp:
Oct 21, 2008, 6:15:06 PM (16 years ago)
Author:
ole
Message:

Work on caching allowing for instances.
Progress made, but Okushiri example still doesn't cache properly.

File:
1 edited

Legend:

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

    r5853 r5854  
    3535  return A.value+B.value, A.another+B.another
    3636 
    37  
     37
     38def f_generic(A):
     39  return A
    3840 
    3941def clear_and_create_cache(Dummy, verbose=False):
     
    114116           
    115117
     118           
    116119    def test_caching_of_numeric_arrays(self):
    117120        """test_caching_of_numeric_arrays
     
    190193
    191194            # Retrieve
     195            #T2 = cache(f_object, (A1, B1),
     196            #           compression=comp, verbose=verbose)                       
     197                       
     198            # Retrieve
    192199            T2 = cache(f_object, (A1, B1),
    193200                       compression=comp, test=1, verbose=verbose)
     
    205212           
    206213
    207                        
     214    def test_caching_of_circular_structures(self):
     215        """test_caching_of_circular_structures
     216       
     217        Test that Caching doesn't recurse infinitely in case of
     218        circular or self-referencing structures
     219        """
     220       
     221        verbose = False
     222       
     223        # Create input argument
     224        A = Dummy(5, 7)
     225        B = {'x': 10, 'A': A}
     226        C = [B, 15]
     227        A.value = C # Make it circular
     228       
     229
     230        # Test caching
     231        comprange = 2
     232        for comp in range(comprange):
     233 
     234            # Evaluate and store
     235            T1 = cache(f_generic, A, evaluate=1,
     236                       compression=comp, verbose=verbose)
     237
     238            # Retrieve
     239            T2 = cache(f_generic, A,
     240                       compression=comp, test=1, verbose=verbose)
     241                       
     242            # Check for presence of cached result
     243            msg = 'Cached object was not found'           
     244            assert T2 is not None, msg
     245
     246            # Reference result
     247            T3 = f_generic(A) # Compute without caching
     248
     249           
     250            msg = 'Cached result does not match computed result'
     251            assert str(T1) == str(T2), msg
     252            assert str(T2) == str(T3), msg
     253           
     254                                   
     255           
     256           
     257           
     258
     259    def XXtest_caching_of_simple_circular_structures(self):
     260   
     261        # FIXME (Ole): This one recurses infinitly on
     262        # arg strings.
     263       
     264        """test_caching_of_circular_structures
     265       
     266        Test that Caching doesn't recurse infinitely in case of
     267        circular or self-referencing structures
     268        """
     269       
     270        verbose = True
     271       
     272        # Create input argument
     273        A = {'x': 10, 'B': None}
     274        B = [A, 15]
     275        A['B'] = B # Make it circular
     276       
     277        print A
     278
     279        # Test caching
     280        comprange = 2
     281        for comp in range(comprange):
     282 
     283            # Evaluate and store
     284            T1 = cache(f_generic, A, evaluate=1,
     285                       compression=comp, verbose=verbose)
     286                       
     287            import sys; sys.exit()
     288                       
     289
     290            # Retrieve
     291            T2 = cache(f_generic, A,
     292                       compression=comp, test=1, verbose=verbose)
     293                       
     294            # Check for presence of cached result
     295            msg = 'Cached object was not found'           
     296            assert T2 is not None, msg
     297
     298            # Reference result
     299            T3 = f_generic(A) # Compute without caching
     300
     301
     302            assert T1 == T2, 'Cached result does not match computed result'
     303            assert T2 == T3, 'Cached result does not match computed result'
     304           
     305                                   
    208306           
    209307           
     
    429527      This test shows how instances can't be effectively cached.
    430528      myhash uses hash which uses id which uses the memory address.
     529     
     530      This will be a NIL problem once caching can handle instances with different id's and
     531      identical content.
     532     
     533      The test is disabled.
    431534      """
     535     
     536
     537     
    432538      verbose = True
    433539      #verbose = False
     
    526632         
    527633# Cache created for use with 'test_objects_are_created_memory'
    528 initial_addr = `Dummy_memorytest`
    529 clear_and_create_cache(Dummy_memorytest, verbose=False)
    530  
    531      
    532 
    533 
    534 
    535      
    536        
     634#initial_addr = `Dummy_memorytest`
     635#clear_and_create_cache(Dummy_memorytest, verbose=False)
     636 
    537637
    538638
    539639#-------------------------------------------------------------
    540640if __name__ == "__main__":
    541     suite = unittest.makeSuite(Test_Caching,'test')
     641    suite = unittest.makeSuite(Test_Caching, 'test')
    542642    runner = unittest.TextTestRunner()
    543643    runner.run(suite)
Note: See TracChangeset for help on using the changeset viewer.