示例#1
0
def test__def_lookup_failure():
    # with pytest.raises(BaseException) as excinfo:
    #     run = Interpreter().run
    #     run("(asdf 1 2 3 4)")
    # assert "Symbol 'asdf' is not defined!" == str(excinfo.value)
    run = Interpreter().run
    assert run("(asdf 1 2 3 4)") == "Symbol 'asdf' is not defined!"
示例#2
0
def test_strings_equalitf():
    run = Interpreter().run
    assert run('(= "" "")') == "true"
    assert run('(= "abc" "abc")') == "true"
    assert run('(= "abc" "")') == "false"
    assert run('(= "" "abc")') == "false"
    assert run('(= "abc" "def")') == "false"
    assert run('(= "abc" "ABC")') == "false"
    assert run('(= (list) "")') == "false"
    assert run('(= "" (list))') == "false"

    assert run('(if "" 7 8)') == '7'

    assert run('(not false)') == 'true'
    assert run('(not nil)') == 'true'
    assert run('(not true)') == 'false'
    assert run('(not "a")') == 'false'
    assert run('(not 0)') == 'false'

    assert run('""') == '""'
    assert run('"abc"') == '"abc"'
    assert run('"abc  def"') == '"abc  def"'
    assert run('"\\""') == '"\\""'
    assert run('"abc\\ndef\\nghi"') == '"abc\\ndef\\nghi"'
    assert run('"abc\\\\def\\\\ghi"') == '"abc\\\\def\\\\ghi"'
    assert run('"\\\\n"') == '"\\\\n"'
示例#3
0
def test__def():
    run = Interpreter().run
    assert run("(def! x 3)") == "3"
    assert run("x") == "3"
    assert run("(def! x 4)") == "4"
    assert run("x") == "4"
    assert run("(def! y (+ 1 7))") == "8"
    assert run("y") == "8"
    assert run("(+ x y)") == "12"
示例#4
0
def test_list_functions():
    run = Interpreter().run
    assert run("(list)") == "()"
    assert run("(list? (list))") == "true"
    assert run("(empty? (list))") == "true"
    assert run("(empty? (list 1))") == "false"
    assert run("(list 1 2 3)") == "(1 2 3)"
    assert run("(count (list 1 2 3))") == "3"
    assert run("(count (list))") == "0"
    assert run("(count nil)") == "0"
    assert run("(if (> (count (list 1 2 3)) 3) 89 78)") == "78"
    assert run("(if (>= (count (list 1 2 3)) 3) 89 78)") == "89"
示例#5
0
def test_tail_calls():
    run = Interpreter().run

    run("(def! sum2 (fn* (n acc) (if (= n 0) acc (sum2 (- n 1) (+ n acc)))))")
    assert run('(sum2 10 0)') == '55'
    assert run('(def! res2 nil)') == 'nil'
    assert run("(def! res2 (sum2 10000 0))") == '50005000'
    assert run('res2') == '50005000'

    run("(def! foo (fn* (n) (if (= n 0) 0 (bar (- n 1)))))")
    run("(def! bar (fn* (n) (if (= n 0) 0 (foo (- n 1)))))")
    assert run('(foo 10000)') == '0'
示例#6
0
def test__if():
    run = Interpreter().run
    assert run("(if true 7 8)") == "7"
    assert run("(if false 7 8)") == "8"
    assert run("(if false 7 false)") == "false"
    assert run("(if true (+ 1 7) (+ 1 8))") == "8"
    assert run("(if false (+ 1 7) (+ 1 8))") == "9"
    assert run("(if nil 7 8)") == "8"
    assert run("(if 0 7 8)") == "7"
    assert run("(if (list) 7 8)") == "7"
    assert run("(if (list 1 2 3) 7 8)") == "7"
    assert run("(= (list) nil)") == "false"
示例#7
0
def test__do():
    run = Interpreter().run
    assert run("(do (def! a 6) 7 (+ a 8))") == "14"
    assert run("a") == "6"

    assert run("(do (prn 101))") == "nil"
    assert run("(do (prn 102) 7)") == "7"
    assert run("(do (prn 101) (prn 102) (+ 1 2))") == "3"

    assert run("(do (def! a 6) 7 (+ a 8))") == "14"
    assert run("a") == "6"

    # Testing special form case-sensitivity
    run("(def! DO (fn* (a) 7))")
    assert run("(DO 3)") == "7"
示例#8
0
def test__fn():
    run = Interpreter().run
    assert run("(+ 1 2)") == "3"
    assert run("( (fn* (a b) (+ b a)) 3 4)") == "7"
    assert run("( (fn* () 4) )") == "4"
    assert run("( (fn* (f x) (f x)) (fn* (a) (+ 1 a)) 7)") == "8"

    # Testing closures
    assert run("( ( (fn* (a) (fn* (b) (+ a b))) 5) 7)") == "12"
    run("(def! gen-plus5 (fn* () (fn* (b) (+ 5 b))))")
    run("(def! plus5 (gen-plus5))")
    assert run("(plus5 7)") == "12"

    run("(def! gen-plusX (fn* (x) (fn* (b) (+ x b))))")
    run("(def! plus7 (gen-plusX 7))")
    assert run("(plus7 8)") == "15"
示例#9
0
def test__let():
    run = Interpreter().run
    #basic tests
    assert run("(def! x 4)") == "4"
    assert run("(let* (z 9) z)") == "9"
    assert run("(let* (x 9) x)") == "9"
    assert run("x") == "4"
    assert run("(let* (z (+ 2 3)) (+ 1 z))") == "6"
    assert run("(let* (p (+ 2 3) q (+ 2 p)) (+ p q))") == "12"
    assert run("(def! y (let* (z 7) z))") == "7"
    assert run("y") == "7"
    #outer environment tests
    assert run("(def! a 4)") == "4"
    assert run("(let* (q 9) q)") == "9"
    assert run("(let* (q 9) a)") == "4"
    assert run("(let* (z 2) (let* (q 9) a))") == "4"
示例#10
0
def test_list_functions():
    run = Interpreter().run

    # Testing recursive sumdown function
    run("(def! sumdown (fn* (N) (if (> N 0) (+ N (sumdown  (- N 1))) 0)))")
    assert run("(sumdown 1)") == "1"
    assert run("(sumdown 2)") == "3"
    assert run("(sumdown 6)") == "21"

    # Testing recursive fibonacci function
    run("(def! fib (fn* (N) (if (= N 0) 1 (if (= N 1) 1 (+ (fib (- N 1)) (fib (- N 2)))))))")
    assert run("(fib 1)") == "1"
    assert run("(fib 2)") == "2"
    assert run("(fib 4)") == "5"

    # Testing recursive function in environment.
    assert run("(let* (cst (fn* (n) (if (= n 0) nil (cst (- n 1))))) (cst 1))") == "nil"
    assert run("(let* (f (fn* (n) (if (= n 0) 0 (g (- n 1)))) g (fn* (n) (f n))) (f 2))") == "0"
示例#11
0
def test_operators():
    run = Interpreter().run
    assert run("(= 2 1)") == "false"
    assert run("(= 1 1)") == "true"
    assert run("(= 1 2)") == "false"
    assert run("(= 1 (+ 1 1))") == "false"
    assert run("(= 2 (+ 1 1))") == "true"
    assert run("(= nil 1)") == "false"
    assert run("(= nil nil)") == "true"

    assert run("(> 2 1)") == "true"
    assert run("(> 1 1)") == "false"
    assert run("(> 1 2)") == "false"

    assert run("(>= 2 1)") == "true"
    assert run("(>= 1 1)") == "true"
    assert run("(>= 1 2)") == "false"

    assert run("(< 2 1)") == "false"
    assert run("(< 1 1)") == "false"
    assert run("(< 1 2)") == "true"

    assert run("(<= 2 1)") == "false"
    assert run("(<= 1 1)") == "true"
    assert run("(<= 1 2)") == "true"


    assert run("(= 1 1)") == "true"
    assert run("(= 0 0)") == "true"
    assert run("(= 1 0)") == "false"
    assert run("(= true true)") == "true"
    assert run("(= false false)") == "true"
    assert run("(= nil nil)") == "true"

    assert run("(= (list) (list))") == "true"
    assert run("(= (list 1 2) (list 1 2))") == "true"
    assert run("(= (list 1) (list))") == "false"
    assert run("(= (list) (list 1))") == "false"
    assert run("(= 0 (list))") == "false"
    assert run("(= (list) 0)") == "false"
    assert run("(= (list nil) (list))") == "false"
示例#12
0
def test__def_error_abort():
    run = Interpreter().run
    assert run("(asdf 1 2 3 4)") == "Symbol 'asdf' is not defined!"
    assert run("(def! w 44)") == "44"
    assert run("(def! w (asdf))") == "Symbol 'asdf' is not defined!"
    assert run("w") == "44"
示例#13
0
def test__if_oneway():
    run = Interpreter().run
    assert run("(if false (+ 1 7))") == "nil"
    assert run("(if nil 8)") == "nil"
    assert run("(if nil 8 7)") == "7"
    assert run("(if true (+ 1 7))") == "8"
示例#14
0
def test_list_functions():
    run = Interpreter().run
    assert run("(quote (1 2 3))") == "(1 2 3)"
    assert run("(quote (+ 1 3))") == "(+ 1 3)"

    # Testing cons function
    assert run('(cons 1 (list))') == '(1)'
    assert run('(cons 1 (list 2))') == '(1 2)'
    assert run('(cons 1 (list 2 3))') == '(1 2 3)'
    assert run('(cons (list 1) (list 2 3))') == '((1) 2 3)'

    run("(def! a (list 2 3))")
    assert run('(cons 1 a)') == '(1 2 3)'
    assert run('a') == '(2 3)'

    # Testing concat function
    assert run('(concat)') == '()'
    assert run('(concat (list 1 2))') == '(1 2)'
    assert run('(concat (list 1 2) (list 3 4))') == '(1 2 3 4)'
    assert run('(concat (list 1 2) (list 3 4) (list 5 6))') == '(1 2 3 4 5 6)'
    assert run('(concat (concat))') == '()'
    assert run('(concat (list) (list))') == '()'

    run("(def! a (list 1 2)))")
    run("(def! b (list 3 4)))")
    assert run('(concat a b (list 5 6))') == '(1 2 3 4 5 6)'
    assert run('a') == '(1 2)'
    assert run('b') == '(3 4)'

    # Testing regular quote
    assert run('(quote 7)') == '7'
    assert run('(quote (1 2 3))') == '(1 2 3)'
    assert run('(quote (1 2 (3 4)))') == '(1 2 (3 4))'

    # Testing simple quasiquote
    assert run('(quasiquote 7)') == '7'
    assert run('(quasiquote (1 2 3))') == '(1 2 3)'
    assert run('(quasiquote (1 2 (3 4)))') == '(1 2 (3 4))'
    assert run('(quasiquote (nil))') == '(nil)'

    # Testing unquote
    assert run('(quasiquote (unquote 7))') == '7'
    assert run('(def! a 8)') == '8'
    assert run('(quasiquote a)') == 'a'
    assert run('(quasiquote (unquote a))') == '8'
    assert run('(quasiquote (1 a 3))') == '(1 a 3)'
    assert run('(quasiquote (1 (unquote a) 3))') == '(1 8 3)'
    assert run('(def! b (quote (1 "b" "d")))') == '(1 "b" "d")'
    assert run('(quasiquote (1 b 3))') == '(1 b 3)'
    assert run('(quasiquote (1 (unquote b) 3))') == '(1 (1 "b" "d") 3)'
    assert run('(quasiquote ((unquote 1) (unquote 2)))') == '(1 2)'

    # Testing splice-unquote
    assert run('(def! c (quote (1 "b" "d")))') == '(1 "b" "d")'
    assert run('(quasiquote (1 c 3))') == '(1 c 3)'
    assert run('(quasiquote (1 (splice-unquote c) 3))') == '(1 1 "b" "d" 3)'

    # Testing symbol equality
    assert run('(= (quote abc) (quote abc))') == 'true'
    assert run('(= (quote abc) (quote abcd))') == 'false'
    assert run('(= (quote abc) "abc")') == 'false'
    assert run('(= "abc" (quote abc))') == 'false'
    assert run('(= (quote abc) nil)') == 'false'
    assert run('(= nil (quote abc))') == 'false'