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_objects(self):
        """test_caching_of_objects
        
        Test that Objecs can be recognised as input variabelse 
        by caching even if their id's are different
        """
    

        verbose = False
        
        # Make some test input arguments
        A0 = Dummy(5, 7)
        B0 = Dummy(2.2, -5)
        
        A0.new_attribute = 'x'  
        B0.new_attribute = 'x'        
        
        A1 = deepcopy(A0)
        B1 = deepcopy(B0)        
        
        # Check that their ids are different
        assert id(A0) != id(A1)
        assert id(B0) != id(B1)        
        
        
        # Test caching
        comprange = 2
        for comp in range(comprange):
  
            # Evaluate and store
            T1 = cache(f_object, (A0, B0), evaluate=1,
                       compression=comp, verbose=verbose)

            # Retrieve
            T2 = cache(f_object, (A1, B1),
                       compression=comp,
                       test=1,
                       verbose=verbose) 
                       
            # Check for presence of cached result 
            msg = 'Different objects with same attributes were not recognised'
            assert T2 is not None, msg

            # Reference result
            T3 = f_object(A0, B0)  # Compute without caching


            assert T1 == T2, 'Cached result does not match computed result'
            assert T2 == T3, 'Cached result does not match computed result'
Example #3
0
    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
    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(5, 7)
        BB = {'A': AA, 'x': 10}
        CC = [BB, num.array([1.200, 3.000, 5.00, 1.0 / 10])]
        #AA.value = CC  # Make it circular

        assert myhash(A) == myhash(AA)
Example #5
0
    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_objects(self):
        """test_caching_of_objects
        
        Test that objects can be recognised as input variables 
        by caching even if their id's are different
        """

        # Disabled because this is not necessary in ANUGA (Ole).

        verbose = False

        # Make some test input arguments
        A0 = Dummy(5, 7)
        B0 = Dummy(2.2, -5)

        A0.new_attribute = 'x'
        B0.new_attribute = 'x'

        A1 = deepcopy(A0)
        B1 = deepcopy(B0)

        # Check that their ids are different
        assert id(A0) != id(A1)
        assert id(B0) != id(B1)

        # Test caching
        comprange = 2
        for comp in range(comprange):

            # Evaluate and store
            T1 = cache(f_object, (A0, B0),
                       evaluate=1,
                       compression=comp,
                       verbose=verbose)

            # Retrieve
            T2 = cache(f_object, (A1, B1),
                       compression=comp,
                       test=1,
                       verbose=verbose)

            # Check for presence of cached result
            msg = 'Different objects with same attributes were not recognised'
            assert T2 is not None, msg

            # Reference result
            T3 = f_object(A0, B0)  # Compute without caching

            assert T1 == T2, 'Cached result does not match computed result'
            assert T2 == T3, 'Cached result does not match computed result'