def test_uniqueness_of_hash_values(self): """test_uniqueness_of_hash_values(self): Test that Caching can handle a realistic complex structure by hashing it consistently and uniquely. """ verbose = False # Create input argument A = Dummy(5, 7) B = {'x': 10, 'A': A} C = [B, num.array([1.2, 3, 5, 0.1])] A.value = C # Make it circular # Create identical but separate object AA = Dummy(None, None) BB = {'A': AA, 'x': 10} CC = [BB, num.array([1.200, 3.000, 5.00, 1.0 / 10])] AA.value = CC # Make it circular AA.another = 3 + 4 assert myhash(A) == myhash(AA) # Also test caching now that we are at it comprange = 2 for comp in range(comprange): # Evaluate and store using A T1 = cache(f_generic, A, evaluate=1, compression=comp, verbose=verbose) # Retrieve using copy (AA) T2 = cache(f_generic, AA, compression=comp, test=1, verbose=verbose) # Check for presence of cached result msg = 'Cached object was not found' assert T2 is not None, msg # Reference result T3 = f_generic(A) # Compute without caching msg = 'Cached result does not match computed result' assert str(T1) == str(T2), msg assert str(T2) == str(T3), msg
def test_caching_of_circular_structures(self): """test_caching_of_circular_structures Test that Caching doesn't recurse infinitely in case of circular or self-referencing structures """ verbose = False # Create input argument A = Dummy(5, 7) B = {'x': 10, 'A': A} C = [B, 15] A.value = C # Make it circular A.x = [1, 2, C, 5, A] # More circular and self referential AA = deepcopy(A) # Test caching comprange = 2 for comp in range(comprange): # Evaluate and store T1 = cache(f_generic, A, evaluate=1, compression=comp, verbose=verbose) # Retrieve T2 = cache(f_generic, AA, compression=comp, test=1, verbose=verbose) #test=1, verbose=True) # FIXME (Ole): The works in Python 3 but not in Python 2. # I am in a mind to live with that as we are moving # away from Python 2 anyway import sys if sys.version_info[0] > 2: # Check for presence of cached result msg = 'Cached object was not found' assert T2 is not None, msg # Reference result T3 = f_generic(A) # Compute without caching #msg = 'Cached result does not match computed result' #assert str(T1) == str(T2), msg #assert str(T2) == str(T3), msg assert str(T1) == str(T3), msg
def test_caching_of_circular_structures(self): """test_caching_of_circular_structures Test that Caching doesn't recurse infinitely in case of circular or self-referencing structures """ verbose = False # Create input argument A = Dummy(5, 7) B = {'x': 10, 'A': A} C = [B, 15] A.value = C # Make it circular A.x = [1, 2, C, 5, A] # More circular and self referential AA = deepcopy(A) # Test caching comprange = 2 for comp in range(comprange): # Evaluate and store T1 = cache(f_generic, A, evaluate=1, compression=comp, verbose=verbose) # Retrieve T2 = cache(f_generic, AA, compression=comp, test=1, verbose=verbose) # Check for presence of cached result msg = 'Cached object was not found' assert T2 is not None, msg # Reference result T3 = f_generic(A) # Compute without caching msg = 'Cached result does not match computed result' assert str(T1) == str(T2), msg assert str(T2) == str(T3), msg