Lichen

pyparser/test/unittest_samples.py

716:1f9bb1de08f5
2017-03-12 Paul Boddie Merged changes from the default branch. return-value-definition
     1 """test module for CPython / PyPy nested tuples comparison"""     2      3 import os, os.path as osp     4 import sys     5 from pyparser.pythonutil import python_parse, pypy_parse     6 from pprint import pprint     7 from pyparser import grammar     8 grammar.DEBUG = False     9 from symbol import sym_name    10     11     12 def name(elt):    13     return "%s[%s]"% (sym_name.get(elt,elt),elt)    14     15 def read_samples_dir():    16     return [osp.join('samples', fname) for fname in os.listdir('samples') if fname.endswith('.py')]    17     18 def print_sym_tuple(nested, level=0, limit=15, names=False, trace=()):    19     buf = []    20     if level <= limit:    21         buf.append("%s(" % (" "*level))    22     else:    23         buf.append("(")    24     for index, elt in enumerate(nested):    25         # Test if debugging and if on last element of error path    26         if trace and not trace[1:] and index == trace[0]:    27             buf.append('\n----> ')    28         if type(elt) is int:    29             if names:    30                 buf.append(name(elt))    31             else:    32                 buf.append(str(elt))    33             buf.append(', ')    34         elif type(elt) is str:    35             buf.append(repr(elt))    36         else:    37             if level < limit:    38                 buf.append('\n')    39             buf.extend(print_sym_tuple(elt, level+1, limit,    40                                        names, trace[1:]))    41     buf.append(')')    42     return buf    43     44 def assert_tuples_equal(tup1, tup2, curpos = ()):    45     for index, (elt1, elt2) in enumerate(zip(tup1, tup2)):    46         if elt1 != elt2:    47             if type(elt1) is tuple and type(elt2) is tuple:    48                 assert_tuples_equal(elt1, elt2, curpos + (index,))    49             raise AssertionError('Found difference at %s : %s != %s' %    50                                  (curpos, name(elt1), name(elt2) ), curpos)    51     52 from time import time, clock    53 def test_samples( samples ):    54     time_reports = {}    55     for sample in samples:    56         print "testing", sample    57         tstart1, cstart1 = time(), clock()    58         pypy_tuples = pypy_parse(sample)    59         tstart2, cstart2 = time(), clock()    60         python_tuples = python_parse(sample)    61         time_reports[sample] = (time() - tstart2, tstart2-tstart1, clock() - cstart2, cstart2-cstart1 )    62         #print "-"*10, "PyPy parse results", "-"*10    63         #print ''.join(print_sym_tuple(pypy_tuples, names=True))    64         #print "-"*10, "CPython parse results", "-"*10    65         #print ''.join(print_sym_tuple(python_tuples, names=True))    66         print    67         try:    68             assert_tuples_equal(pypy_tuples, python_tuples)    69         except AssertionError as e:    70             error_path = e.args[-1]    71             print "ERROR PATH =", error_path    72             print "="*80    73             print file(sample).read()    74             print "="*80    75             print "-"*10, "PyPy parse results", "-"*10    76             print ''.join(print_sym_tuple(pypy_tuples, names=True, trace=error_path))    77             print "-"*10, "CPython parse results", "-"*10    78             print ''.join(print_sym_tuple(python_tuples, names=True, trace=error_path))    79             print "Failed on (%s)" % sample    80             # raise    81     pprint(time_reports)    82     83 if __name__=="__main__":    84     import getopt    85     opts, args = getopt.getopt( sys.argv[1:], "d:", [] )    86     for opt, val in opts:    87         if opt == "-d":    88             pass    89 #            set_debug(int(val))    90     if args:    91         samples = args    92     else:    93         samples = read_samples_dir()    94     95     test_samples( samples )