vile / scheme / custom_raw_symbolic_test.v
96 lines · 89 sloc · 4.28 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import arena
4
5fn crs_check(condition bool, message string) {
6 if !condition { panic(message) }
7}
8
9fn test_raw_initial_cohorts_use_anchor_identity_even_when_contents_are_equal() {
10 mut a := &CustomRawAnchor{}
11 mut b := &CustomRawAnchor{}
12 x := custom_initial_raw_point(a, 0) or { panic(err.msg()) }
13 y := custom_initial_raw_point(b, 0) or { panic(err.msg()) }
14 crs_check(!x.same_origin(y), 'distinct unresolved initial anchors aliased')
15 a.resolve(integer(100)) or { panic(err.msg()) }
16 b.resolve(integer(100)) or { panic(err.msg()) }
17 crs_check(!x.same_origin(y), 'distinct resolved initial anchors aliased')
18 crs_check(x.same(x) && y.same(y), 'same anchor identity lost')
19}
20
21fn test_raw_symbolic_coordinates_prove_relative_identity_without_inventing_absolute_positions() {
22 mut cohorts := CustomRawCohorts{}
23 first := cohorts.next()
24 second := CustomRawPoint{ ...first, offset: 2 }
25 third := cohorts.next()
26 crs_check(second.same_origin(first) && second.delta(first) == 2, 'same-publication offsets')
27 crs_check(!third.same_origin(first) && !third.same(first), 'different publications aliased')
28 retreated := second.retreat(1) or { panic(err.msg()) }
29 crs_check(retreated.delta(first) == 1, 'symbolic retreat')
30 for point in [first, second, third, retreated] {
31 if _ := point.absolute() { panic('cohort ID became an absolute source position') }
32 }
33 mut anchor := cohorts.anchor
34 if _ := anchor.resolve(integer(0)) { panic('symbolic origin was resolved as position zero') }
35 if _ := custom_initial_raw_point(anchor, 0) {
36 panic('symbolic anchor accepted as initial resolvable origin')
37 }
38 mut ordinary := &CustomRawAnchor{}
39 if _ := custom_symbolic_raw_point(ordinary, 1, 0) {
40 panic('ordinary initial anchor accepted as symbolic')
41 }
42 ordinary.resolve(integer(100)) or { panic(err.msg()) }
43 resolved := custom_initial_raw_point(ordinary, 2) or { panic(err.msg()) }
44 crs_check((resolved.absolute() or { panic(err.msg()) }).whole() == 102, 'ordinary origin resolution changed')
45}
46
47fn test_raw_symbolic_cohort_counter_rotation_keeps_old_captures_distinct() {
48 mut cohorts := CustomRawCohorts{}
49 old := cohorts.next()
50 cohorts.serial = u64(max_i64) - 1
51 last := cohorts.next()
52 crs_check(last.origin.whole() == max_i64 && voidptr(last.initial) == voidptr(old.initial), 'last inline ID')
53 rotated := cohorts.next()
54 crs_check(rotated.origin.whole() == 1 && voidptr(rotated.initial) != voidptr(old.initial), 'cohort anchor did not rotate')
55 crs_check(!rotated.same_origin(old), 'old captured ID aliased after rotation')
56 crs_check(old.same(old) && !old.same(last), 'older captures changed')
57 if _ := rotated.absolute() { panic('rotated symbolic origin materialized') }
58}
59
60fn test_raw_symbolic_map_distinguishes_unread_and_unobserved_ignored_tail_from_physical_cursor() {
61 mut cohorts := CustomRawCohorts{}
62 publication := cohorts.next()
63 physical := CustomRawPoint{ ...publication, offset: 2 }
64 mut history := TranscodedBoundaries[CustomRawPoint]{}
65 history.reset(publication)
66 history.append(1, CustomRawPoint{ ...publication, offset: 1 })
67 visible := history.at(1) or { panic(err.msg()) }
68 crs_check(!visible.same(physical), 'unread raw suffix lost')
69 if _ := visible.absolute() { panic('unknown absolute rewind target invented') }
70 history.append(1, physical)
71 crs_check((history.at(2) or { panic(err.msg()) }).same(physical), 'consumed input cannot prove no rewind needed')
72 // Ignored suffix advances the decoder physically, but does not move the
73 // last visible endpoint until an error or EOF is actually observed.
74 history.reset(publication)
75 history.append(1, CustomRawPoint{ ...publication, offset: 1 })
76 crs_check(!(history.at(1) or { panic(err.msg()) }).same(physical), 'ignored suffix committed before observation')
77 history.commit(physical)
78 crs_check((history.at(1) or { panic(err.msg()) }).same(physical), 'observed EOF did not commit symbolic frontier')
79}
80
81fn test_raw_symbolic_publications_do_not_allocate_per_callback_after_warmup() {
82 mut region := arena.new()
83 region.push()
84 defer {
85 region.pop()
86 region.free()
87 }
88 mut cohorts := CustomRawCohorts{}
89 first := cohorts.next()
90 before := region.used()
91 for _ in 0 .. 10000 {
92 point := cohorts.next()
93 crs_check(!point.same_origin(first) && voidptr(point.initial) == voidptr(first.initial), 'symbolic publication identity')
94 }
95 crs_check(region.used() == before, 'per-callback symbolic allocation')
96}
97