vile / scheme / port_transcoder_allocation_test.v
98 lines · 90 sloc · 3.06 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import arena
4import io
5
6@[heap]
7struct AttachedAllocationSource {
8 bytes []u8
9mut:
10 position int
11}
12
13fn (mut source AttachedAllocationSource) read(mut bytes []u8) !int {
14 if source.position == source.bytes.len { return io.Eof{} }
15 count := int_min(bytes.len, source.bytes.len - source.position)
16 unsafe { vmemcpy(bytes.data, &u8(source.bytes.data) + source.position, count) }
17 source.position += count
18 return count
19}
20
21@[heap]
22struct AttachedAllocationSink {
23mut:
24 count int
25 calls int
26}
27
28fn (mut sink AttachedAllocationSink) write(bytes []u8) !int {
29 sink.count += bytes.len
30 sink.calls++
31 return bytes.len
32}
33
34fn test_attached_prepared_utf16_output_uses_stack_batches_without_heap_work() {
35 mut region := arena.new(chunk_size: 1048576)
36 region.push()
37 defer {
38 region.pop()
39 region.free()
40 }
41 mut vm := new()
42 mut sink := &AttachedAllocationSink{}
43 binary := vm.output_port(sink, binary: true) or { panic(err.msg()) }
44 output := vm.transcoded_port(binary, transcoder_value(.utf16, .crlf, .raise)) or { panic(err.msg()) }
45 args := [output, string_value('aλ😀\n'.repeat(1024))]
46 port_put_string(mut vm, args) or { panic(err.msg()) }
47 before := region.used()
48 bytes := sink.count
49 calls := sink.calls
50 for _ in 0 .. 100 { port_put_string(mut vm, args) or { panic(err.msg()) } }
51 vm.flush_output_port(output) or { panic(err.msg()) }
52 assert sink.count - bytes == 100 * 1024 * 12
53 assert sink.calls - calls <= 400
54 assert region.used() == before, 'prepared output allocated ${region.used() - before} bytes'
55}
56
57fn test_attached_prepared_unicode_input_reuses_both_port_buffers() {
58 mut region := arena.new(chunk_size: 1048576)
59 region.push()
60 defer {
61 region.pop()
62 region.free()
63 }
64 mut vm := new()
65 binary := vm.open_input_bytevector(bytevector('λ'.repeat(32768).bytes())) or { panic(err.msg()) }
66 input := vm.transcoded_port(binary, transcoder_value(.utf8, .none, .raise)) or { panic(err.msg()) }
67 // Warm a full facade buffer and its first lazy diagnostic checkpoint.
68 for _ in 0 .. 4096 { vm.read_char(input) or { panic(err.msg()) } }
69 before := region.used()
70 for _ in 4096 .. 32768 {
71 assert (vm.read_char(input) or { panic(err.msg()) }).whole() == `λ`
72 }
73 assert region.used() == before, 'prepared input allocated ${region.used() - before} bytes'
74}
75
76fn test_attached_tiny_input_buffers_drain_partial_scalars_without_heap_work() {
77 for size in [1, 2, 3, 5, 7] {
78 mut region := arena.new(chunk_size: 1048576)
79 region.push()
80 mut vm := new()
81 bytes := 'Aλ€😀'.repeat(1024).bytes()
82 binary := vm.input_port(&AttachedAllocationSource{ bytes: bytes },
83 binary: true
84 read_size: size
85 ) or { panic(err.msg()) }
86 input := vm.transcoded_port(binary, transcoder_value(.utf8, .none, .raise)) or { panic(err.msg()) }
87 for _ in 0 .. 16 { vm.read_char(input) or { panic(err.msg()) } }
88 before := region.used()
89 for _ in 4 .. 1024 {
90 for expected in [`A`, `λ`, `€`, `😀`] {
91 assert (vm.read_char(input) or { panic(err.msg()) }).whole() == expected
92 }
93 }
94 assert region.used() == before, 'read_size ${size} allocated ${region.used() - before} bytes'
95 region.pop()
96 region.free()
97 }
98}
99