def test_cons(): run_fix ("(car (cons 1 2))", 1) run_fix ("(cdr (cons 1 2))", 2) with pytest.raises(SchemeException): run("(car 1)", None) with pytest.raises(SchemeException): run("(car 1 2)", None)
def test_cons(): run_fix("(car (cons 1 2))", 1) run_fix("(cdr (cons 1 2))", 2) with pytest.raises(SchemeException): run("(car 1)", None) with pytest.raises(SchemeException): run("(car 1 2)", None)
def test_cons(): run_fix("(car (cons 1 2))", 1) run_fix("(cdr (cons 1 2))", 2) with pytest.raises(Exception): run("(car 1)", None, expect_to_fail=True) with pytest.raises(Exception): run("(car 1 2)", None, expect_to_fail=True)
def test_fib(): run_fix( "(letrec ([fib (lambda (n) (if (< n 2) 1 (+ (fib (- n 1)) (fib (- n 2)))))]) (fib 2))", 2) run_fix( "(letrec ([fib (lambda (n) (if (< n 2) 1 (+ (fib (- n 1)) (fib (- n 2)))))]) (fib 3))", 3)
def test_call_with_values(): run_fix( """ (call-with-values (lambda () (time-apply (lambda () (+ 1 2)) (quote ()))) (lambda (result t r gc) (and (fixnum? t) (fixnum? r) (fixnum? gc) (car result)))) """, 3)
def test_cons(): run_fix ("(car (cons 1 2))", 1) run_fix ("(cdr (cons 1 2))", 2) with pytest.raises(Exception): run("(car 1)", None, expect_to_fail=True) with pytest.raises(Exception): run("(car 1 2)", None, expect_to_fail=True)
def test_fib(): run_fix( "(letrec-values ([(fib) (lambda (n) (if (< n 2) 1 (+ (fib (- n 1)) (fib (- n 2)))))]) (fib 2))", 2) run_fix( "(letrec-values ([(fib) (lambda (n) (if (< n 2) 1 (+ (fib (- n 1)) (fib (- n 2)))))]) (fib 3))", 3)
def test_begin0(): run_fix("(begin0 1 2)", 1) run_fix("(begin0 1)", 1) run_fix("(begin0 1 2 3)", 1) v = run_values("(begin0 (values #t #f) 2 3)") assert v == [w_true, w_false] run_fix("(let ([x 1]) (begin0 x (set! x 2)))", 1) run_fix("(let ([x 10]) (begin0 (set! x 0) (set! x (+ x 1))) x)", 1)
def test_let_values(): run_fix("(let-values ([(a b c) (values 1 2 3)]) (+ a b c))", 6) run_fix( "(let-values ([(a b c) (values 1 2 3)] [(d) 1] [(e f g h) (values 1 2 1 1)]) (+ a b c d e f g h))", 12) run_fix( "(let-values ([(a b c) (values 1 2 3)]) (set! a (+ a 5)) (+ a b c))", 11) run_fix("(let-values ([() (values )]) 1)", 1)
def test_mcons(): run_fix("(mcar (mcons 1 2))", 1) run_fix("(mcdr (mcons 1 2))", 2) run_fix("(unsafe-mcar (mcons 1 2))", 1) run_fix("(unsafe-mcdr (mcons 1 2))", 2) with pytest.raises(SchemeException): run("(mcar 1)", None) with pytest.raises(SchemeException): run("(mcar 1 2)", None)
def test_mcons(): run_fix ("(mcar (mcons 1 2))", 1) run_fix ("(mcdr (mcons 1 2))", 2) run_fix ("(unsafe-mcar (mcons 1 2))", 1) run_fix ("(unsafe-mcdr (mcons 1 2))", 2) with pytest.raises(SchemeException): run("(mcar 1)", None) with pytest.raises(SchemeException): run("(mcar 1 2)", None)
def test_random(): for i in range(100): x = run_flo("(random)") assert 0.0 <= x < 1.0 x = run_fix("(random %s)" % (5 + i)) if pytest.config.new_pycket: assert 0 <= x.value < i + 5 else: assert 0 <= x < i + 5
def test_basic_hl(self): run_fix("(car (cons 1 2))", 1) run_fix("(cdr (cons 1 2))", 2) run_fix("(car (list 1))", 1) run_fix("(car (cdr (list 1 2)))", 2) run("(equal? (cons 1 2) (cons 1 2))", w_true) run("(equal? (cons 1 2) (cons 2 2))", w_false) run("(equal? (cons 1 2) 'barf)", w_false) run("(let ((x (cons 1 2))) (equal? x x))", w_true) run("(equal? (cons 1 (cons 2 3)) (cons 1 (cons 2 3)))", w_true) run("(equal? (cons 1 (cons 2 3)) (cons 1 (cons 3 3)))", w_false) run("(equal? (cons 1 (cons 2 3)) (cons 1 (cons 2 4)))", w_false)
def test_basic_hl(self): run_fix("(car (cons 1 2))", 1) run_fix("(cdr (cons 1 2))", 2) run_fix("(car (list 1))", 1) run_fix("(car (cdr (list 1 2)))", 2) run("(equal? (cons 1 2) (cons 1 2))", w_true) run("(equal? (cons 1 2) (cons 2 2))", w_false) run("(equal? (cons 1 2) (quote barf))", w_false) run("(let-values (((x) (cons 1 2))) (equal? x x))", w_true) run("(equal? (cons 1 (cons 2 3)) (cons 1 (cons 2 3)))", w_true) run("(equal? (cons 1 (cons 2 3)) (cons 1 (cons 3 3)))", w_false) run("(equal? (cons 1 (cons 2 3)) (cons 1 (cons 2 4)))", w_false)
def test_vec_imp(): assert isinstance(run('(impersonate-vector (vector 1) values values)'), W_ImpVector) run('(vector? (chaperone-vector \'#(0 (2 2 2 2) "Anna") values values))', w_true) run_fix("(let ([v (impersonate-vector (vector 1 2 3) values values)]) (vector-length v))", 3) run("(let ([v (impersonate-vector (vector 1 2 3) (lambda (x y z) z) (lambda (x y z) z))]) (vector-set! v 0 0))", w_void) run_fix("(let ([v (impersonate-vector (vector 1 2 3) (lambda (x y z) z) (lambda (x y z) z))]) (vector-set! v 0 0) (vector-length v))", 3) run_fix("(let ([v (impersonate-vector (vector 1 2 3) (lambda (x y z) z) (lambda (x y z) z))]) (vector-set! v 0 0) (vector-ref v 0))", 0)
def test_vec(): assert isinstance(run('(vector 1)'), W_Vector) run('(vector? \'#(0 (2 2 2 2) "Anna"))', w_true) run("(vector? '#())", w_true) run_fix("(let ([v (vector 1 2 3)]) (vector-length v))", 3) run("(let ([v (vector 1 2 3)]) (vector-set! v 0 0))", w_void) run_fix("(let ([v (vector 1 2 3)]) (vector-set! v 0 0) (vector-length v))", 3) run_fix("(let ([v (vector 1 2 3)]) (vector-set! v 0 0) (vector-ref v 0))", 0)
def test_vec(): assert isinstance(run('(vector 1)'), W_Vector) #run('(vector? (quote #(0 (2 2 2 2)) "Anna"))', w_true) #run("(vector? (quote #())", w_true) run_fix("(let-values ([(v) (vector 1 2 3)]) (vector-length v))", 3) run("(let-values ([(v) (vector 1 2 3)]) (vector-set! v 0 0))", w_void) run_fix("(let-values ([(v) (vector 1 2 3)]) (vector-set! v 0 0) (vector-length v))", 3) run_fix("(let-values ([(v) (vector 1 2 3)]) (vector-set! v 0 0) (vector-ref v 0))", 0)
def test_fib(): Y = """ (lambda (f) ((lambda (x) (x x)) (lambda (g) (f (lambda (z) ((g g) z)))))) """ fac = """ (lambda (f) (lambda (x) (if (< x 2) 1 (* x (f (- x 1)))))) """ fib = """ (lambda (f) (lambda (x) (if (< x 2) x (+ (f (- x 1)) (f (- x 2)))))) """ run_fix("((%s %s) 2)"%(Y,fib), 1) run_fix("((%s %s) 2)"%(Y,fac), 2)
def test_fib_ycombinator(): Y = """ (lambda (f) ((lambda (x) (x x)) (lambda (g) (f (lambda (z) ((g g) z)))))) """ fac = """ (lambda (f) (lambda (x) (if (< x 2) 1 (* x (f (- x 1)))))) """ fib = """ (lambda (f) (lambda (x) (if (< x 2) x (+ (f (- x 1)) (f (- x 2)))))) """ run_fix("((%s %s) 2)" % (Y, fib), 1) run_fix("((%s %s) 2)" % (Y, fac), 2)
def test_reclambda(): run_fix("((letrec ([c (lambda (n) (if (< n 0) 1 (c (- n 1))))]) c) 10)", 1) run_fix(""" ((letrec ([c (lambda (n) (let ([ind (lambda (n) (display n) (if (< n 0) 1 (c (- n 1))))]) (ind n)))]) c) 10)""", 1) run_fix(""" (let () (define (nested n) (let countdown ([i n]) (if (< i 0) 1 (countdown (- i 1)))) (if (< n 0) 1 (nested (- n 1)))) (nested 10))""", 1)
def test_reclambda(): run_fix("((letrec-values ([(c) (lambda (n) (if (< n 0) 1 (c (- n 1))))]) c) 10)", 1) run_fix(""" ((letrec-values ([(c) (lambda (n) (let-values ([(ind) (lambda (n) (begin (display n) (if (< n 0) 1 (c (- n 1)))))]) (ind n)))]) c) 10)""", 1) run_fix(""" (letrec-values ([(nested) (lambda (n) (begin (letrec-values ([(countdown) (lambda (i) (if (< i 0) 1 (countdown (- i 1))))]) (countdown n)) (if (< n 0) 1 (nested (- n 1)))))]) (nested 10))""", 1)
def test_reclambda(): run_fix("((letrec ([c (lambda (n) (if (< n 0) 1 (c (- n 1))))]) c) 10)", 1) run_fix( """ ((letrec ([c (lambda (n) (let ([ind (lambda (n) (display n) (if (< n 0) 1 (c (- n 1))))]) (ind n)))]) c) 10)""", 1) run_fix( """ (let () (define (nested n) (let countdown ([i n]) (if (< i 0) 1 (countdown (- i 1)))) (if (< n 0) 1 (nested (- n 1)))) (nested 10))""", 1)
def test_callcc(): run_fix ("(call/cc (lambda (k) 1))", 1) run_fix ("(+ 1 (call/cc (lambda (k) 1)))", 2) run_fix ("(+ 1 (call/cc (lambda (k) (k 1))))", 2) run_fix ("(+ 1 (call/cc (lambda (k) (+ 5 (k 1)))))", 2)
def test_arith_minus_one_arg_bug(): run_fix("(- 1)", -1)
def test_letrec(): run_fix("(letrec ([x 1]) x)", 1) run_fix("(letrec ([x 1] [y 2]) y)", 2) run_fix("(letrec ([x 1] [y 2]) (+ x y))", 3) run_fix("(let ([x 0]) (letrec ([x 1] [y x]) (+ x y)))", 2) run_fix("(letrec ([x (lambda (z) x)]) 2)", 2)
def test_arith(): run_fix("(+ 1 2)", 3) run_fix("(* 1 2)", 2) run_fix("(- 1 2)", -1) run_fix("(* -1 2)", -2)
def test_setbang_recursive_lambda(): run_fix("((letrec ([f (lambda (a) (set! f (lambda (a) 1)) (f a))]) f) 6)", 1)
def test_time(): run_fix("(time 1)", 1)
def run_fix_unsafe(e,v): run_fix(e,v,extra="")
def test_constant(): run_fix("1", v=1)
def test_call(): prog = "((lambda (x) (+ x 1)) 2)" run_fix(prog, 3)
def run_fix_unsafe_expander(e,v): run_fix("(begin (#%%require (quote #%%unsafe)) %s)" % e,v,extra="")
def test_curry(): prog = "(((lambda (y) (lambda (x) (+ x y))) 2) 3)" run_fix(prog, 5)
def test_callwithcurrentcontinuation(): run_fix ("(call-with-current-continuation (lambda (k) 1))", 1) run_fix ("(+ 1 (call-with-current-continuation (lambda (k) 1)))", 2) run_fix ("(+ 1 (call-with-current-continuation (lambda (k) (k 1))))", 2) run_fix ("(+ 1 (call-with-current-continuation (lambda (k) (+ 5 (k 1)))))", 2)
def test_vec_values(): run_fix("(let-values ([(a b c) (vector->values (vector 1 2 3))]) (+ a b c))", 6) run_fix("(let-values ([(b c) (vector->values (vector 1 2 3) 1)]) (+ b c))", 5) run_fix("(let-values ([(b) (vector->values (vector 1 2 3) 1 2)]) (+ b))", 2)
def test_values(): run_fix("(values 1)", 1) run_fix("(let () (values 1 2) (values 3))", 3) prog = "(let () (call/cc (lambda (k) (k 1 2))) 3)" run_fix(prog, 3) v = run_values("(values #t #f)") assert [w_true, w_false] == v run_fix("(call-with-values (lambda () (values 1 2)) (lambda (a b) (+ a b)))", 3) run_fix("(call-with-values (lambda () (values 1 2)) +)", 3) run_fix("(call-with-values (lambda () (values)) (lambda () 0))", 0) run_fix("(call-with-values (lambda () (values 1)) (lambda (x) x))", 1) run_fix("(call-with-values (lambda () (values 1)) values)", 1) run_fix("(call-with-values (lambda () 1) values)", 1) run_fix(""" (call-with-values (lambda () (time-apply (lambda () (+ 1 2)) '())) (lambda (result t r gc) (and (fixnum? t) (fixnum? r) (fixnum? gc) (car result)))) """, 3)
def test_thunk(): prog = "((lambda () 1))" run_fix(prog, 1)
def test_apply(): run_fix("(apply + (list 1 2 3))", 6) run_fix("(apply + 1 2 (list 3))", 6) run_fix("(apply + 1 2 3 (list))", 6)
def test_make_vector(): run_fix("(let ([v (vector)]) (vector-length v))", 0) run_fix("(let ([v (make-vector 5)]) (vector-length v))", 5) vec = run('(make-vector 5)') for i in range(vec.length()): assert vec.ref(i).value == 0
def test_thunk2(): prog = "((lambda () 1 2))" run_fix(prog, 2)
def test_let(): run_fix("(let () 1)", 1) run_fix("(let ([x 1]) x)", 1) run_fix("(let ([x 1] [y 2]) y)", 2) run_fix("(let ([x 1] [y 2]) (+ x y))", 3) run_fix("(let ([x 0]) (let ([x 1] [y x]) (+ x y)))", 1)
def test_let_values(): run_fix("(let-values ([(a b c) (values 1 2 3)]) (+ a b c))", 6) run_fix("(let-values ([(a b c) (values 1 2 3)] [(d) 1] [(e f g h) (values 1 2 1 1)]) (+ a b c d e f g h))", 12) run_fix("(let-values ([(a b c) (values 1 2 3)]) (set! a (+ a 5)) (+ a b c))", 11) run_fix("(let-values ([() (values )]) 1)", 1)
def test_run_pruning_let(): run_fix("(let ([c 7]) (let ([b (+ c 1)]) (let ([a (+ b 1)] [d (- c 5)]) (+ a d))))", 11)
def test_arithmetic(): run_fix("(+ )", 0) run_fix("(+ 1)", 1) run_fix("(+ 2 3)", 5) run_fix("(+ 2 3 4)", 9) with pytest.raises(SchemeException): run_fix("(- )", 0) run_fix("(- 1)", -1) run_fix("(- 2 3)", -1) run_fix("(- 2 3 4)", -5) run_fix("(* )", 1) run_fix("(* 2)", 2) run_fix("(* 2 3)", 6) run_fix("(* 2 3 4)", 24) with pytest.raises(SchemeException): run_flo("(/ )", 0) run_flo("(/ 2.0)", 0.5) run_flo("(/ 2. 3.)", 2. / 3.) run_flo("(/ 2. 3. 4.)", 2. / 3. / 4.)
def test_vararg(): run_fix ("((lambda x (car x)) 1)", 1) run_fix ("((lambda (a . x) a) 1)", 1) run ("((lambda (a . x) x) 1)", w_null)
def test_random(): for i in range(100): x = run_flo("(random)") assert 0.0 <= x < 1.0 x = run_fix("(random %s)" % (5 + i)) assert 0 <= x < i + 5
def test_make_vector_imp(): run_fix("(let ([v (impersonate-vector (vector) (lambda (x y z) z) (lambda (x y z) z))]) (vector-length v))", 0) run_fix("(let ([v (impersonate-vector (make-vector 5) (lambda (x y z) z) (lambda (x y z) z))]) (vector-length v))", 5)