示例#1
0
文件: test.py 项目: PlumpMath/lispy-1
def test_macro():
    e = Env()
    p = parse('(def t (macro (x) (+ x x)))')
    c = parse('(t (+ 1 2))')
    eval_lisp(p, e)
    assert eval_lisp(c, e) == 6

    print('macro passed')
示例#2
0
文件: test.py 项目: PlumpMath/lispy-1
def input_test():
    e = Env()

    inp1 = parse('(1 2)')
    assert show(inp1) == '(1 2 ())'

    inp2 = parse('(+ 1 2)')
    assert show(inp2) == '(+ 1 2 ())'
    assert eval_lisp(inp2, e) == 3

    inp3 = parse('(1 2 3 4)')
    assert show(inp3) == '(1 2 3 4 ())'

    inp4 = parse('(+ (+ 3 2) 3)')
    assert eval_lisp(inp4, e) == 8
    assert show(inp4) == '(+ (+ 3 2 ()) 3 ())'

    inp5 = parse('(1 (4 5))')
    assert show(inp5) == '(1 (4 5 ()) ())'

    eval_lisp(parse('(def a 10)'), e)
    assert eval_lisp(parse('(+ a 3)'), e) == 13

    eval_lisp(parse('(def b (lambda (x) (* x x)))'), e)
    assert eval_lisp(parse('(b 3)'), e) == 9

    print('input tests passed')
示例#3
0
文件: test.py 项目: PlumpMath/lispy-1
def test_fib():
    e = Env()
    fib = parse('(def fib (lambda (n) ('
                'cond ((== n 1) 1)'
                ' ((== n 0) 0)'
                ' (else (+ (fib (- n 1)) (fib (- n 2)))))'
                '))')
    assert show(
        fib) == '(def fib (lambda (n ()) (cond ((== n 1 ()) (1 ())) ((== n 0 ()) (0 ())) (else (+ (fib (- n 1 ()) ()) (fib (- n 2 ()) ()) ()) ()) ()) ()) ())'
    eval_lisp(fib, e)
    exec_fib = parse('(fib 10)')
    assert eval_lisp(exec_fib, e) == 55
示例#4
0
文件: test.py 项目: PlumpMath/lispy-1
def test_quote():
    e = Env()
    p = parse('(defn a (x)'
              ' ((defn g (z) (+ z x)) (` g))'
              ')')
    eval_lisp(p, e)
    c = parse('((a 7) 3)')
    assert eval_lisp(c, e) == 10

    e = Env()
    p = parse('(defn a (x)'
              ' ((defn g (z) (+ z x)) `g)'
              ')')
    eval_lisp(p, e)
    c = parse('((a 7) 3)')
    assert eval_lisp(c, e) == 10

    print('quote passed')
示例#5
0
文件: test.py 项目: PlumpMath/lispy-1
def test_high_order_func():
    e = Env()
    p = parse('(defn a (x)'
              ' ((defn g (z) (+ z x)) (` g))'
              ')')
    eval_lisp(p, e)
    c = parse('((a 7) 3)')
    assert eval_lisp(c, e) == 10

    e = Env()
    p = parse('(defn a (x)'
              ' ((defn g () (+ 3 x)) (` g))'
              ')')
    eval_lisp(p, e)
    c = parse('((a 1))')
    assert eval_lisp(c, e) == 4

    print('high order passed')
示例#6
0
文件: test.py 项目: PlumpMath/lispy-1
def test_simple_coms():
    e = Env()

    emp = parse('(defn empty? (x) (and (== (car x) None) (== (cdr x) None)))')
    f = parse('(empty? (` (1 2)))')
    t = parse('(empty? ())')
    qua = parse(
        '(defn qua (x)('
        'if (empty? x)'
        '()'
        '(cons (* 2 (car x)) (qua (cdr x)))'
        '))'
    )
    q = parse('(qua (qua (` (1 3))))')
    eval_lisp(emp, e)
    eval_lisp(qua, e)
    assert not eval_lisp(f, e)
    assert eval_lisp(t, e)
    assert show(eval_lisp(q, e)) == '(4 12 ())'

    print('simple tests passed')
示例#7
0
文件: test.py 项目: PlumpMath/lispy-1
def test_cond():
    e = Env()
    p = parse('(cond ((< 3 1) 1) ((< 2 3) 2))')
    assert eval_lisp(p, e) == 2

    print('cond test passed')
示例#8
0
文件: test.py 项目: PlumpMath/lispy-1
def test_if():
    assert eval_lisp(parse('(if (< 3 5) (12) (22))'), Env()) == 12