コード例 #1
0
ファイル: scheme_test.py プロジェクト: anjishnu/Schemer
def run_tests(src_file = 'tests.scm'):
    """Run a read-eval loop that reads from src_file and collects outputs."""
    expected_output = []
    line_number = 0

    def read_lines(src):
        """Creates a generator that returns the lines of src, filtering out
        '; expect' strings and collecting them into expected_output with their
        line numbers.  The variable line_number gives the number of the last
        line returned for diagnostic purposes."""
        nonlocal line_number
        while True:
            line_number += 1
            line = src.readline()
            if line.lstrip().startswith(EXPECT_STRING):
                expected = line.split(EXPECT_STRING, 1)[1][1:-1]
                expected_output.append((expected, line_number))
                continue
            if not line:
                return
            yield line

    sys.stderr = sys.stdout = io.StringIO() # Collect output to stdout and stderr
    try:
        source = read_lines(open(src_file))
        scheme_repl(source, "", create_global_frame(), False)
    except BaseException as exc:
        sys.stderr = sys.__stderr__
        print("Tests terminated due to unhandled exception "
              "after line {0}:\n>>>".format(line_number),
              file=sys.stderr)
        raise
    output = sys.stdout.getvalue().split('\n')
    sys.stdout = sys.__stdout__  # Revert stdout
    summarize(output, expected_output)
コード例 #2
0
ファイル: scheme_grader.py プロジェクト: JianfengYao/pyscheme
def problem_8(grades):
    tests1 = [
        ('(lambda (x y) (+ x y))',
         LambdaProcedure(pairify(['x', 'y']),
                         pairify(['+', 'x', 'y']),
                         create_global_frame()))
    ]
    tests2 = [
        ('(lambda (x) (+ x) (+ x x))',
         LambdaProcedure(pairify(['x']),
                         pairify(['begin', ['+', 'x'], ['+', 'x', 'x']]),
                         create_global_frame())),
        ('(begin (define x (lambda () 2)) x)',
         LambdaProcedure(nil,
                         2,
                         create_global_frame())),
    ]
    if check_func(scheme_eval, tests1, comp=scheme_equal):
        return True
    if check_func(scheme_eval, tests2, comp=scheme_equal):
        return True
コード例 #3
0
ファイル: scheme_grader.py プロジェクト: JianfengYao/pyscheme
def problem_A9(grades):
    tests1 = [
        ('(begin (define (f x y) (+ x y)) f)',
            LambdaProcedure(pairify(['x', 'y']),
                            pairify(['+', 'x', 'y']),
                            create_global_frame())),
        ('(begin (define (f) (+ 2 2)) f)',
            LambdaProcedure(nil,
                            pairify(['+', 2, 2]),
                            create_global_frame())),
        ('(begin (define (f x) (* x x)) f)',
            LambdaProcedure(pairify(['x']),
                            pairify(['*', 'x', 'x']),
                            create_global_frame())),
        ('(begin (define (f x) 1 2) f)',
            LambdaProcedure(pairify(['x']),
                            pairify(['begin', 1, 2]),
                            create_global_frame())),
        ('(define (0 x) (* x x))', 'SchemeError'),
    ]
    if check_func(scheme_eval, tests1, comp=scheme_equal):
        return True
コード例 #4
0
ファイル: scheme_grader.py プロジェクト: JianfengYao/pyscheme
def scheme_eval(snippet):
    """Convert snippet into a single expression and scheme_eval it."""
    # TODO: figure out how to do this more cleanly
    buf = scheme.buffer_lines(snippet.split('\n'), show_prompt=True)
    exprs = []
    try:
        while True:
            exprs.append(scheme.scheme_read(buf))
    except EOFError:
        pass
    env = scheme.create_global_frame()
    try:
        for expr in exprs[:-1]:
            scheme.scheme_eval(expr, env)
        return scheme.scheme_eval(exprs[-1], env)
    except scheme.SchemeError as err:
        return 'SchemeError'
    except BaseException as err:
        return type(err).__name__ + ' ' + str(err)
コード例 #5
0
ファイル: scheme_test.py プロジェクト: ADanilychevJr/Fall2012
def run_tests(src_file = 'tests.scm'):
    """Run a read-eval loop that reads from src_file and collects outputs."""
    sys.stderr = sys.stdout = io.StringIO() # Collect output to stdout and stderr
    try:
        reader = TestReader(open(src_file).readlines())
        src = Buffer(tokenize_lines(reader))
        def next_line():
            src.current()
            return src
        read_eval_print_loop(next_line, create_global_frame())
    except BaseException as exc:
        sys.stderr = sys.__stderr__
        print("Tests terminated due to unhandled exception "
              "after line {0}:\n>>>".format(reader.line_number),
              file=sys.stderr)
        raise
    output = sys.stdout.getvalue().split('\n')
    sys.stdout = sys.__stdout__  # Revert stdout
    summarize(output, reader.expected_output)
コード例 #6
0
ファイル: scheme_grader.py プロジェクト: JianfengYao/pyscheme
def problem_B11(grades):
    # Note: Doesn't check well-formed but unrequired list matching.
    # E.g., (lambda (a . b) 2) and (lambda x 2)
    tests1 = [
        ('(lambda (x y z) x)',
            LambdaProcedure(pairify(['x', 'y', 'z']),
                            'x',
                            create_global_frame())),
        ('(lambda (0 y z) x)', 'SchemeError'),
        ('(lambda (x y nil) x)', 'SchemeError'),
        ('(lambda (x y (and z)) x)', 'SchemeError'),
        ('(lambda (x #t z) x)', 'SchemeError'),
    ]
    tests2 = [
        ("(lambda (h e l l o) 'world)", 'SchemeError'),
        ("(lambda (c s 6 1 a) 'yay)", 'SchemeError'),
        ]

    if check_func(scheme_eval, tests1, comp=scheme_equal):
        return True
    if check_func(scheme_eval, tests2, comp=scheme_equal):
        return True
コード例 #7
0
ファイル: scheme_grader.py プロジェクト: JianfengYao/pyscheme
def problem_10(grades):
    gf = create_global_frame()

    formals, vals = read_line('(a b c)'), read_line('(1 2 3)')
    call_frame = gf.make_call_frame(formals, vals)
    doctest = [
        (set(call_frame.bindings), {'a', 'b', 'c'}),
        (len(call_frame.bindings), 3),
        (call_frame.bindings['a'], 1),
        (call_frame.bindings['b'], 2),
        (call_frame.bindings['c'], 3),
        (call_frame.parent, gf),
        ]
    if check_func(lambda x: x, doctest, comp=scheme_equal):
        return True

    formals = read_line('(a b c)')
    args = read_line('(2 #t a)')
    lf = gf.make_call_frame(formals, args)
    tests1 = [
        (lf.bindings['a'], 2),
        (lf.bindings['b'], True),
        (lf.bindings['c'], 'a'),
        (lf.parent, gf),
    ]
    if check_func(lambda x: x, tests1, comp=scheme_equal):
        return True

    formals = read_line('(a)')
    args = read_line('(seven)')
    lf2 = lf.make_call_frame(formals, args)
    tests2 = [
        (lf.bindings['a'], 2),
        (lf.parent, gf),
    ]
    if check_func(lambda x: x, tests2, comp=scheme_equal):
        return True
コード例 #8
0
def problem_10(grades):
    gf = create_global_frame()

    formals, vals = read_line('(a b c)'), read_line('(1 2 3)')
    call_frame = gf.make_call_frame(formals, vals)
    doctest = [
        (set(call_frame.bindings), {'a', 'b', 'c'}),
        (len(call_frame.bindings), 3),
        (call_frame.bindings['a'], 1),
        (call_frame.bindings['b'], 2),
        (call_frame.bindings['c'], 3),
        (call_frame.parent, gf),
    ]
    if check_func(lambda x: x, doctest, comp=scheme_equal):
        return True

    formals = read_line('(a b c)')
    args = read_line('(2 #t a)')
    lf = gf.make_call_frame(formals, args)
    tests1 = [
        (lf.bindings['a'], 2),
        (lf.bindings['b'], True),
        (lf.bindings['c'], 'a'),
        (lf.parent, gf),
    ]
    if check_func(lambda x: x, tests1, comp=scheme_equal):
        return True

    formals = read_line('(a)')
    args = read_line('(seven)')
    lf2 = lf.make_call_frame(formals, args)
    tests2 = [
        (lf.bindings['a'], 2),
        (lf.parent, gf),
    ]
    if check_func(lambda x: x, tests2, comp=scheme_equal):
        return True