Exemple #1
0
def test_bench_fib():
    from time import time

    start = time()
    res = eval_str(
        "(define (fib n) "
        "   (cond ((< n 2) 1) "
        "         (else (+ (fib (- n 1)) "
        "                  (fib (- n 2))))))"
        "(fib 25)"
    )
    stop = time()
    print("TIME:", stop - start)
    assert res == 121393
Exemple #2
0
def test_application():
    assert eval_str("(+ 1 2)", make_initial_environment()) == 3
Exemple #3
0
def test_cond():
    assert eval_str("(cond (else 0))") == 0
    assert eval_str("(cond (#f 1) (#t 2) (else 3))") == 2
Exemple #4
0
def test_let():
    assert eval_str("(let ((x 1) (y 2)) (+ x y))") == 3
Exemple #5
0
def test_definition():
    assert eval_str("(define x 42)") == symbol("x")
    assert eval_str("(define x 42) x") == 42
    assert is_compound_procedure(eval_str("(define (x) 0) x"))
Exemple #6
0
def test_assignment():
    assert eval_str("(define x 0) (set! x 1)") == symbol("ok")
    assert eval_str("(define x 0) (set! x 1) x") == 1
Exemple #7
0
def test_sequence():
    assert eval_str("(begin 1 2 3)") == 3
    with pytest.raises(SyntaxError):
        eval_str("(begin)")
Exemple #8
0
def test_if():
    assert eval_str("(if #t 2 3)") == 2
    assert eval_str("(if #f 2 3)") == 3
Exemple #9
0
def test_lambda():
    assert is_compound_procedure(eval_str("(lambda (x) x)"))
Exemple #10
0
def test_variable():
    assert eval_str("+") is not None
Exemple #11
0
def test_quotation():
    assert eval_str("'(1 2 3)") == (1, 2, 3)