vile / scheme / port_transcoder_boundaries_test.v
66 lines · 62 sloc · 2.14 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import arena
4
5fn test_transcoded_boundary_map_merges_variable_utf8_widths_and_prunes_without_growth() {
6 mut region := arena.new()
7 region.push()
8 defer {
9 region.pop()
10 region.free()
11 }
12 mut history := TranscodedBoundaries[i64]{}
13 history.reset(9)
14 for i in 0 .. 1000 {
15 for width in [1, 2, 3, 4] {
16 history.append(width, history.raw_end + width)
17 assert history.at(history.decoded_end) or { panic(err.msg()) } == 9 + history.decoded_end
18 }
19 assert history.spans.len == 1
20 assert history.at(i * 10 + 1) or { panic(err.msg()) } == 9 + i * 10 + 1
21 assert history.at(i * 10 + 3) or { panic(err.msg()) } == 9 + i * 10 + 3
22 }
23 before := region.used()
24 for _ in 0 .. 1000 {
25 history.prune(history.decoded_end - 4) or { panic(err.msg()) }
26 history.append(4, history.raw_end + 4)
27 }
28 assert history.spans.len == 1
29 assert region.used() == before
30}
31
32fn test_transcoded_boundary_map_committed_error_does_not_stretch_published_prefix() {
33 mut history := TranscodedBoundaries[i64]{}
34 history.reset(2)
35 history.append(100, 102)
36 history.commit(104)
37 history.commit(106)
38 assert history.spans.len == 2
39 assert history.at(99) or { panic(err.msg()) } == 101
40 assert history.at(100) or { panic(err.msg()) } == 106
41 history.append(3, 108)
42 assert history.at(99) or { panic(err.msg()) } == 101
43 assert history.at(100) or { panic(err.msg()) } == 106
44 assert history.at(103) or { panic(err.msg()) } == 108
45 history.prune(100) or { panic(err.msg()) }
46 assert history.at(100) or { panic(err.msg()) } == 106
47 assert history.at(103) or { panic(err.msg()) } == 108
48 if _ := history.at(101) {
49 assert false
50 }
51}
52
53fn test_transcoded_boundary_map_huge_ignored_gap_and_reused_irregular_window() {
54 mut history := TranscodedBoundaries[i64]{}
55 history.reset(0)
56 history.append(1, i64(max_int) + 77)
57 history.append(3, i64(max_int) + 79)
58 assert history.at(0) or { panic(err.msg()) } == 0
59 assert history.at(1) or { panic(err.msg()) } == i64(max_int) + 77
60 assert history.at(4) or { panic(err.msg()) } == i64(max_int) + 79
61 for i in 0 .. 10000 {
62 history.append(1, history.raw_end + 1 + i % 3)
63 history.prune(history.decoded_end - 1) or { panic(err.msg()) }
64 assert history.spans.len <= 4
65 }
66}
67