vile / scheme / loop_continue_allocation_test.v
39 lines · 35 sloc · 1.37 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import arena
4
5fn test_continue_direct_iterations_allocate_only_fixed_entry_storage() {
6 continue_allocation_is_fixed('(define (run n) (do ((i 0 (+ i 1))) ((= i n) 42) (continue) missing))', false)
7}
8
9fn test_continue_vile_scopes_and_temporary_locals_allocate_only_fixed_entry_storage() {
10 continue_allocation_is_fixed('run(n):={for(i:=0,i<n,i=i+1){continue}\n42}', true)
11 continue_allocation_is_fixed('run(n):={for(i:=0,i<n,i=i+1){x:=i\nif x>=0 {continue}\nmissing}\n42}', true)
12}
13
14fn continue_allocation_is_fixed(source string, alternate bool) {
15 mut region := arena.new()
16 region.push()
17 defer {
18 region.pop()
19 region.free()
20 }
21 mut vm := new_with_options(Options{ max_steps: 0, max_frames: 4 }) or { panic(err.msg()) }
22 if alternate {
23 vm.eval_alt(source) or { panic(err.msg()) }
24 } else {
25 vm.eval(source) or { panic(err.msg()) }
26 }
27 run := vm.lookup('run') or { panic(err.msg()) }
28 mut arguments := [integer(1)]
29 vm.call(run, arguments) or { panic(err.msg()) }
30 mut fixed := 0
31 for count in [1, 101, 10001] {
32 container_store_validated(mut arguments, 0, integer(count))
33 before := region.used()
34 assert vm.call(run, arguments) or { panic(err.msg()) }.as_integer() or { panic(err.msg()) } == 42
35 allocated := region.used() - before
36 if fixed == 0 { fixed = allocated }
37 assert allocated == fixed, '${count} iterations: ${allocated} bytes, expected fixed ${fixed}'
38 }
39}
40