vile / scheme / syntax_scratch_failure_test.v
63 lines · 58 sloc · 1.84 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import arena
4
5fn attempt_frozen_scratch(tree &SyntaxTree, mut result BoundedResult) {
6 defer {
7 if message := recover() {
8 result.exhausted = arena.allocation_failure() or { panic(message) }
9 }
10 }
11 tree.require_finite_transformer() or { panic(err) }
12}
13
14fn attempt_frozen_job(storage arena.Arena, mut result BoundedResult) {
15 defer {
16 if message := recover() {
17 result.exhausted = arena.allocation_failure() or { panic(message) }
18 }
19 }
20 mut jobs := []i32{}
21 frozen_syntax_job(mut jobs, storage, 0)
22}
23
24fn test_denied_frozen_transformer_scratch_restores_the_callers_allocator() {
25 tree := &SyntaxTree{ nodes: [SyntaxTreeNode{ kind: .empty_list }] }
26 mut owner := arena.new(limits: arena.ChunkLimits{ max_live_bytes: 0 })
27 defer { owner.free() }
28 mut result := BoundedResult{}
29 owner.push()
30 attempt_frozen_scratch(tree, mut result)
31 restored := owner.is_current()
32 // Release the leaked child when exercising the old implementation so the
33 // failed assertion can report outside the exhausted allocator.
34 if !restored {
35 child := v_arena_current()
36 unsafe {
37 v_arena_pop(child)
38 v_arena_free(child)
39 }
40 }
41 owner.pop()
42 assert restored
43 reason := result.exhausted or { panic('expected scratch allocation denial') }
44 assert reason == .chunk_limit
45 assert owner.used() == 0
46}
47
48fn test_denied_frozen_job_growth_restores_the_callers_allocator() {
49 mut owner := arena.new(limits: arena.ChunkLimits{ max_live_bytes: 0 })
50 defer { owner.free() }
51 owner.push()
52 mut storage := arena.new(chunk_size: 1024)
53 defer { storage.free() }
54 mut result := BoundedResult{}
55 attempt_frozen_job(storage, mut result)
56 restored := owner.is_current()
57 if !restored { unsafe { v_arena_pop(v_arena_current()) } }
58 owner.pop()
59 assert restored
60 reason := result.exhausted or { panic('expected scratch allocation denial') }
61 assert reason == .chunk_limit
62 assert owner.used() == 0
63}
64