Changeset 5851


Ignore:
Timestamp:
Oct 21, 2008, 10:30:00 AM (16 years ago)
Author:
ole
Message:

Added two new tests verifying that caching works with Numeric arrays based on
their contents rather than id, but that (as we know) this is not currently
the case for instances.

The latter test fails at the moment.

File:
1 edited

Legend:

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

    r4385 r5851  
    11
    22import unittest
    3 from caching import *
     3from Numeric import arange, array
     4
     5from anuga.caching import *
    46
    57
     
    1921    B.append((a,b,c,s,n,x,y))
    2022  return(B)
    21 
     23 
     24def f_numeric(A, B):
     25  """Operation on Numeric arrays
     26  """
     27 
     28  return 3.1*A + B + 1
     29 
     30 
     31def f_object(A, B):
     32  """Operation of objecs of class Dummy
     33  """
     34 
     35  return A.value+B.value, A.another+B.another
     36 
     37 
     38 
    2239class Dummy:
    2340  def __init__(self, value, another):
    2441    self.value = value
    25 
    26 
    27 #class Dummy_memorytest:
    28 #  def __init__(self, value, another):
    29 #    self.value = value
    30 
     42    self.another = another
     43   
     44  def copy(self):
     45    return Dummy(self.value, self.another)
     46   
    3147
    3248def clear_and_create_cache(Dummy, verbose=False):
     
    102118           
    103119
     120    def test_caching_of_numeric_arrays(self):
     121        """test_caching_of_numeric_arrays
     122       
     123        Test that Numeric arrays can be recognised by caching even if their id's are different
     124        """
     125       
     126        verbose = False
     127       
     128        # Make some test input arguments
     129        A0 = arange(5)
     130        B0 = array([1.1, 2.2, 0.0, -5, -5])
     131       
     132        A1 = A0.copy()
     133        B1 = B0.copy()
     134       
     135        # Check that their ids are different
     136        assert id(A0) != id(A1)
     137        assert id(B0) != id(B1)       
     138       
     139       
     140        # Test caching
     141        comprange = 2
     142        for comp in range(comprange):
     143 
     144            # Evaluate and store
     145            T1 = cache(f_numeric, (A0, B0), evaluate=1,
     146                       compression=comp, verbose=verbose)
     147
     148            # Retrieve
     149            T2 = cache(f_numeric, (A1, B1),
     150                       compression=comp, test=1, verbose=verbose)
     151                       
     152            # Check for presence of cached result
     153            msg = 'Different array objects with same contents were not recognised'           
     154            assert T2 is not None, msg
     155
     156            # Reference result
     157            T3 = f_numeric(A0, B0) # Compute without caching
     158
     159
     160            assert T1 == T2, 'Cached result does not match computed result'
     161            assert T2 == T3, 'Cached result does not match computed result'
     162           
     163
     164
     165    def test_caching_of_objects(self):
     166        """test_caching_of_objects
     167       
     168        Test that Objecs can be recognised as input variabelse
     169        by caching even if their id's are different
     170        """
     171   
     172
     173        verbose = False
     174       
     175        # Make some test input arguments
     176        A0 = Dummy(5, 7)
     177        B0 = Dummy(2.2, -5)
     178       
     179        A1 = A0.copy()
     180        B1 = B0.copy()
     181       
     182        # Check that their ids are different
     183        assert id(A0) != id(A1)
     184        assert id(B0) != id(B1)       
     185       
     186       
     187        # Test caching
     188        comprange = 2
     189        for comp in range(comprange):
     190 
     191            # Evaluate and store
     192            T1 = cache(f_object, (A0, B0), evaluate=1,
     193                       compression=comp, verbose=verbose)
     194
     195            # Retrieve
     196            T2 = cache(f_object, (A1, B1),
     197                       compression=comp, test=1, verbose=verbose)
     198                       
     199            # Check for presence of cached result
     200            msg = 'Different objects with same attributes were not recognised'
     201            assert T2 is not None, msg
     202
     203            # Reference result
     204            T3 = f_object(A0, B0) # Compute without caching
     205
     206
     207            assert T1 == T2, 'Cached result does not match computed result'
     208            assert T2 == T3, 'Cached result does not match computed result'
     209           
     210
     211                       
     212           
     213           
    104214    def test_cachefiles(self):
    105215        """Test existence of cachefiles
     
    343453
    344454
    345     # This test works in the caching dir and in anuga_core, but no in the
     455    # This test works in the caching dir and in anuga_core, but not in the
    346456    # anuga_core/source/anuga dir
    347     def no_test_objects_are_created(self):
     457    # This has to do with pickle (see e.g. http://telin.ugent.be/~slippens/drupal/pickleproblem)
     458    # The error message is
     459    # PicklingError: Can't pickle test_caching.Dummy: it's not the same object as test_caching.Dummy
     460    def Xtest_objects_are_created(self):
    348461      """
    349462      This test shows how instances can be created from cache
     
    377490
    378491
    379 # NOTE (Ole): This test has been commented out because, although the test will pass
    380 #             inside the caching dir and also at the anuga_core level,
    381 #             it won't pass at the anuga_core/source/anuga level.
    382 
    383     def no_test_objects_are_created_memory(self):
     492    # NOTE (Ole): This test has been commented out because, although the test will pass
     493    #             inside the caching dir and also at the anuga_core level,
     494    #             it won't pass at the anuga_core/source/anuga level.
     495    # See  message above.
     496    def Xtest_objects_are_created_memory(self):
    384497      """
    385498     
     
    388501      lives in different memory locations.
    389502
    390       This is using cache created in the main program
    391 
     503      This is using cache created in the main program below
    392504      """
    393505
     
    415527# to make sure it has a different memory address
    416528# to the one defined in test 'test_objects_are_created_memory'
    417 
    418529#class Dummy_memorytest:
    419530#  def __init__(self, value, another):
Note: See TracChangeset for help on using the changeset viewer.