vile / scheme / integer_tail_entry_test.v
53 lines · 48 sloc · 2.6 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3fn test_entry391_unsupported_closure_operator_has_no_rejected_loop_plan() {
4 mut vm := new_with_options(Options{max_steps:0,max_frames:0})!
5 vm.eval('(define(increment x)(+ x 1))(define(twice f x)(f(f x)))
6 (define(f n a)(if (= n 0) a (f (- n 1)(twice increment a))))')!
7 assert vm.call(vm.lookup('f')!,[integer(10),integer(0)])!.whole() == 20
8 assert isnil(vm.lookup('f')!.procedure_pointer().function.integer_tail)
9}
10
11fn test_entry391_cons_operator_has_no_rejected_loop_plan() {
12 mut vm := new_with_options(Options{max_steps:0,max_frames:0})!
13 vm.eval('(define(f n a)(if (= n 0) a (f (- n 1)(cons n a))))')!
14 assert write(vm.call(vm.lookup('f')!,[integer(3),nil_value()])!)! == '(1 2 3)'
15 assert isnil(vm.lookup('f')!.procedure_pointer().function.integer_tail)
16}
17
18fn test_entry391_custom_comparison_has_no_rejected_loop_plan() {
19 mut vm := new_with_options(Options{max_steps:0,max_frames:0})!
20 vm.eval('(define(same a b)(= a b))(define(f n a)(if (same n 0) a (f (- n 1)(+ a n))))')!
21 assert vm.call(vm.lookup('f')!,[integer(10),integer(0)])!.whole() == 55
22 assert isnil(vm.lookup('f')!.procedure_pointer().function.integer_tail)
23}
24
25fn test_entry391_numeric_work_runs_at_cached_closure_entry_before_fetch() {
26 for tail in [false,true] {
27 mut vm := new_with_options(Options{max_steps:0,max_frames:0})!
28 vm.eval('(define(f n a)(if (= n 0) a (f (- n 1)(+ a n))))')!
29 f := vm.lookup('f')!
30 assert vm.call(f,[integer(10),integer(0)])!.whole() == 55
31 caller := vm.eval('(lambda()0)')!.procedure_pointer().function
32 mut machine := Machine{function:caller,env:&Environment{},ip:7,
33 stack:[f,integer(10),integer(0)],frames:[]CallFrame{cap:1}}
34 assert machine.enter_cached_closure[PlainMachineLoop](vm,2,tail)
35 assert machine.env.slots[0].whole() == 0 && machine.env.slots[1].whole() == 55
36 assert machine.steps == 191 && machine.ip == 0
37 assert machine.frames.len == if tail { 0 } else { 1 }
38 assert machine.stack.len == 0
39 }
40}
41
42fn test_entry391_admission_does_not_specialize_live_operator_bindings() {
43 mut vm := new_with_options(Options{max_steps:0,max_frames:0})!
44 vm.eval('(define old-plus +)(define(f n a)(if (= n 0) a (f (- n 1)(+ a n))))')!
45 assert vm.call(vm.lookup('f')!,[integer(4),integer(1)])!.whole() == 11
46 vm.define('+',vm.lookup('*')!)!
47 assert vm.call(vm.lookup('f')!,[integer(4),integer(1)])!.whole() == 24
48 vm.define('+',vm.lookup('old-plus')!)!
49 vm.eval('(define combine(lambda(a b)7))(define(g n a)(if (= n 0) a (g (- n 1)(combine a n))))')!
50 assert vm.call(vm.lookup('g')!,[integer(3),integer(0)])!.whole() == 7
51 vm.define('combine',vm.lookup('+')!)!
52 assert vm.call(vm.lookup('g')!,[integer(3),integer(0)])!.whole() == 6
53}
54