vile / scheme / goops_public_weak_allocation_prod_test.v
104 lines · 100 sloc · 3.78 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import arena
4
5fn test_public_weak_list_read_allocates_only_its_output_array() {
6 mut storage := arena.new()
7 storage.push()
8 defer {
9 storage.pop()
10 storage.free()
11 }
12 mut vm := new()
13 class := vm.eval('(make-class (quote ()) (quote ()))') or { panic(err) }
14 input := []Value{len: 128, init: class}
15 references := goops_public_references(input)
16 before_control := storage.used()
17 mut control := []Value{}
18 for value in input { control << value }
19 control_growth := storage.used() - before_control
20 before_read := storage.used()
21 actual := goops_public_reference_values(references)
22 read_growth := storage.used() - before_read
23 if actual.len != input.len || control.len != input.len {
24 panic('public weak list lost live entries')
25 }
26 for i, value in actual {
27 if !eqv(value, input[i]) || !eqv(value, control[i]) {
28 panic('public weak list changed an entry')
29 }
30 }
31 // The two allocations can begin on different alignment boundaries.
32 // Allow one output element of padding, but no per-entry metadata.
33 if read_growth > control_growth + int(sizeof(Value)) {
34 panic('weak-list read allocated ${read_growth} bytes; output storage needs ${control_growth}')
35 }
36}
37
38fn test_public_weak_snapshot_allocates_only_the_independent_header_and_entries() {
39 mut owner := arena.new()
40 owner.push()
41 defer {
42 owner.pop()
43 owner.free()
44 }
45 mut vm := new()
46 class := vm.eval('(make-class (quote ()) (quote ()))') or { panic(err) }
47 input := []Value{len: 128, init: class}
48 source := goops_public_references(input)
49 before_control := owner.used()
50 mut control := &GoopsPublicReferences{}
51 goops_public_reserve(mut control, input.len)
52 control_growth := owner.used() - before_control
53 before_copy := owner.used()
54 copied := goops_copy_public_references(source)
55 copy_growth := owner.used() - before_copy
56 if copied.len != input.len || voidptr(copied.values) == voidptr(source.values) || voidptr(copied.values) == voidptr(control.values) {
57 panic('snapshot must have independent storage for all entries')
58 }
59 if copy_growth > control_growth + int(sizeof(Value)) {
60 panic('weak snapshot allocated ${copy_growth} bytes; header and entries need ${control_growth}')
61 }
62 goops_public_remove(source, class)
63 values := goops_public_reference_values(copied)
64 if source.len != 0 || values.len != input.len {
65 panic('mutating the source changed its snapshot')
66 }
67 for value in values {
68 if !eqv(value, class) { panic('snapshot lost its target') }
69 }
70}
71
72fn test_public_weak_snapshot_filters_expired_entries_and_keeps_order_and_duplicates() {
73 mut owner := arena.new()
74 owner.push()
75 defer {
76 owner.pop()
77 owner.free()
78 }
79 mut vm := new()
80 a := vm.eval('(make-class (quote ()) (quote ()))') or { panic(err) }
81 b := vm.eval('(make-class (quote ()) (quote ()))') or { panic(err) }
82 make_class := vm.lookup('make-class') or { panic(err) }
83 mut child := arena.new()
84 child.push()
85 temporary := vm.call(make_class, [nil_value(), nil_value()]) or { panic(err) }
86 child.pop()
87 source := goops_public_references([temporary, b, temporary, a, b, temporary])
88 expired := goops_public_references([temporary, temporary])
89 child.free()
90 if !isnil(goops_copy_public_references(unsafe { nil })) { panic('nil snapshot changed') }
91 empty := goops_copy_public_references(expired)
92 if isnil(empty) || empty.len != 0 || !isnil(empty.values) {
93 panic('expired entries reserved snapshot storage')
94 }
95 copied := goops_copy_public_references(source)
96 if copied.len != 3 || source.len != 3 { panic('expired entries survived compaction') }
97 goops_public_remove(copied, a)
98 actual := goops_public_reference_values(source)
99 other := goops_public_reference_values(copied)
100 if actual.len != 3 || !eqv(actual[0], b) || !eqv(actual[1], a) || !eqv(actual[2], b)
101 || other.len != 2 || !eqv(other[0], b) || !eqv(other[1], b) {
102 panic('snapshot mutation changed ordering, duplicates, or source entries')
103 }
104}
105