Back to index

radiance  4R0+20100331
Classes | Functions
py_tests.unit_tools.lcompare Namespace Reference

Classes

class  error

Functions

def _icompare
def _fcompare
def _typify_token
def lcompare
def llcompare
def split_headers
def split_rad
def split_radfile

Detailed Description

Text comparison functions for Radiance unit testing.

This allows differences in whitespace, which is why the text
corpora are split into tokens first.
Tokens are then converted into an appropriate data type, so
that floating point items will still be considered correct
even if they are slightly different, eg. as a consequence of
binary rounding errors.

Class Documentation

class py_tests::unit_tools::lcompare::error

Definition at line 14 of file lcompare.py.


Function Documentation

def py_tests.unit_tools.lcompare._fcompare (   ftest,
  fref 
) [private]
compare floats (not public)

Definition at line 25 of file lcompare.py.

00025 
00026 def _fcompare(ftest, fref):
00027        '''compare floats (not public)'''
00028        FUZZ = 0.0000001 # XXX heuristically determined
00029        if type(ftest) == str:
00030               fftest = float(ftest)
00031        else: fftest = ftest
00032        if (fftest < (fref + FUZZ)) and (fftest > (fref - FUZZ)):
00033               return 1
00034        return 0

Here is the caller graph for this function:

def py_tests.unit_tools.lcompare._icompare (   itest,
  iref 
) [private]
compare ints (not public)

Definition at line 17 of file lcompare.py.

00017 
00018 def _icompare(itest, iref):
00019        '''compare ints (not public)'''
00020        if type(itest) == str:
00021               iftest = int(itest)
00022        else: iftest = itest
00023        if iftest == iref: return 1
00024        return 0

Here is the caller graph for this function:

return the token as int resp. float if possible (not public)

Definition at line 35 of file lcompare.py.

00035 
00036 def _typify_token(t):
00037        '''return the token as int resp. float if possible (not public)'''
00038        try: return int(t)
00039        except ValueError: pass
00040        try: return float(t)
00041        except ValueError: pass
00042        return t
00043 
00044 
00045 # public comparison functions

def py_tests.unit_tools.lcompare.lcompare (   ltest,
  lref 
)
compare a list of tokens
       raise an error if there are intolerable differences
       the reference tokens in lref should already be of the correct type.

Definition at line 46 of file lcompare.py.

00046 
00047 def lcompare(ltest, lref):
00048        '''compare a list of tokens
00049               raise an error if there are intolerable differences
00050               the reference tokens in lref should already be of the correct type.
00051        '''
00052        if len(ltest) != len(lref):
00053               raise error, ('List comparision failed: Different number of tokens'
00054                      ' (%d, %d)' % (len(ltest), len(lref)))
00055        for i in range(len(lref)):
00056               tref = lref[i]
00057               ttest = ltest[i]
00058               if type(tref) == str and tref != ttest:
00059                      raise error, 'Token comparison failed: "%s" != "%s"' % (ttest, tref)
00060               elif type(tref) == int and not _icompare(ttest, tref):
00061                      raise error, 'Token comparison failed: %s != %s' % (ttest, tref)
00062               elif type(tref) == float and not _fcompare(ttest, tref):
00063                      raise error, 'Token comparison failed: %s != %s' % (ttest, tref)

Here is the call graph for this function:

Here is the caller graph for this function:

def py_tests.unit_tools.lcompare.llcompare (   lltest,
  llref,
  ignore_empty = 0,
  recurse = [] 
)
compare a list of lists of tokens recursively
       raise an error if there are intolerable differences
       the reference tokens in lref should already be of the correct type.
       if ignore_empty is true, empty lines are not included in the comparison
       the recurse argument is only used internally

Definition at line 64 of file lcompare.py.

00064 
00065 def llcompare(lltest, llref, ignore_empty=0, recurse=[]):
00066        '''compare a list of lists of tokens recursively
00067               raise an error if there are intolerable differences
00068               the reference tokens in lref should already be of the correct type.
00069               if ignore_empty is true, empty lines are not included in the comparison
00070               the recurse argument is only used internally
00071        '''
00072        if ignore_empty:
00073               lltest = filter(None, lltest)
00074               llref = filter(None, llref)
00075        if len(lltest) != len(llref):
00076               raise error, 'Comparision failed: Different number of lines (%d,%d)' %(
00077                      len(lltest), len(llref))
00078        for i in range(len(llref)):
00079               if llref[i]:
00080                      rtype = type(llref[i][0])
00081                      if rtype == list or rtype == tuple:
00082                             return llcompare(lltest[i], llref[i],
00083                                           recurse=recurse.append(i), ignore_empty=ignore_empty)
00084               try: lcompare(lltest[i], llref[i])
00085               except error, e:
00086                      if recurse:
00087                             raise error, '%s (line %s)' % (str(e), recurse.append(i + 1))
00088                      else: raise error, '%s (line %d)' % (str(e), i + 1)
       

Here is the call graph for this function:

split Radiance file headers
       return a list of lists of tokens suitable for llcompare()
       this is useful to check the output of getinfo

Definition at line 89 of file lcompare.py.

00089 
00090 def split_headers(s):
00091        '''split Radiance file headers
00092               return a list of lists of tokens suitable for llcompare()
00093               this is useful to check the output of getinfo'''
00094        ll = map(string.strip,string.split(s, '\n'))
00095        nll = []
00096        for l in ll:
00097               parts = string.split(l, '=', 1)
00098               if len(parts) == 2:
00099                      left = map(_typify_token, string.split(parts[0]))
00100                      right = map(_typify_token, string.split(parts[1]))
00101                      nll.append(left + ['='] + right)
00102               else: nll.append(map(_typify_token, string.split(l)))
00103        return nll

Split the contents of a scene description string
       return a list of list of tokens suitable for llcompare()

Definition at line 104 of file lcompare.py.

00104 
00105 def split_rad(s):
00106        '''Split the contents of a scene description string
00107               return a list of list of tokens suitable for llcompare()'''
00108        ll = map(string.strip,string.split(s, '\n'))
00109        nll = []
00110        for l in ll:
00111               nll.append(map(_typify_token, string.split(l)))
00112        return nll

Here is the caller graph for this function:

Split the contents of a file object containing a scene description
       return a list of list of tokens suitable for llcompare()

Definition at line 113 of file lcompare.py.

00113 
00114 def split_radfile(fn):
00115        '''Split the contents of a file object containing a scene description
00116               return a list of list of tokens suitable for llcompare()'''
00117        f = open(fn, 'r')
00118        ll = split_rad(f.read())
00119        f.close()
00120        return ll
00121        

Here is the call graph for this function: