vile / scheme / port_text_test.v
217 lines · 202 sloc · 8.99 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import io
4import os
5import time
6
7fn text_port_eval(mut vm VM, source string) Value {
8 return vm.eval(source) or { panic('${source}: ${err}') }
9}
10
11fn text_port_error(mut vm VM, source string, fragment string) {
12 if _ := vm.eval(source) {
13 assert false, source
14 } else {
15 assert err.msg().contains(fragment), '${source}: ${err}'
16 }
17}
18
19@[heap]
20struct TextChunkReader {
21 data string
22 chunk int
23mut:
24 position int
25 calls int
26}
27
28fn (mut source TextChunkReader) read(mut bytes []u8) !int {
29 source.calls++
30 gc_collect()
31 if source.position == source.data.len { return io.Eof{} }
32 count := int_min(bytes.len, int_min(source.chunk, source.data.len - source.position))
33 unsafe { vmemcpy(bytes.data, source.data.str + source.position, count) }
34 source.position += count
35 return count
36}
37
38fn test_text_reads_share_one_cursor_across_chunks_line_endings_and_datums() {
39 for chunk in [1, 2, 3, 7, 64] {
40 mut vm := new()
41 mut source := &TextChunkReader{ data: '\r\nλ😀\rnext\n\x00last\r42 abc', chunk: chunk }
42 port := vm.input_port(source, read_size: 3) or { panic(err) }
43 vm.define('p', port) or { panic(err) }
44 assert vm.read_line(port) or { panic(err) }.as_string() or { panic(err) } == ''
45 assert vm.peek_char(port) or { panic(err) }.as_character() or { panic(err) } == `λ`
46 assert vm.read_string(port, 1) or { panic(err) }.as_string() or { panic(err) } == 'λ'
47 assert vm.read_line(port) or { panic(err) }.as_string() or { panic(err) } == '😀'
48 assert vm.read_line(port) or { panic(err) }.as_string() or { panic(err) } == 'next'
49 assert text_port_eval(mut vm, '(read-string 0 p)').as_string() or { panic(err) } == ''
50 assert vm.read_line(port) or { panic(err) }.as_string() or { panic(err) } == '\x00last'
51 assert vm.read(port) or { panic(err) }.as_integer() or { panic(err) } == 42
52 assert vm.read_string(port, 99) or { panic(err) }.as_string() or { panic(err) } == ' abc'
53 calls := source.calls
54 assert vm.read_string(port, 0) or { panic(err) }.as_string() or { panic(err) } == ''
55 assert vm.read_string(port, 1) or { panic(err) }.tag == .eof
56 assert vm.read_line(port) or { panic(err) }.tag == .eof
57 assert vm.read(port) or { panic(err) }.tag == .eof
58 assert source.calls == calls
59 assert !port.input_pointer().busy
60 }
61}
62
63fn test_string_ports_snapshot_inputs_and_outputs_without_aliases() {
64 mut vm := new()
65 text_port_eval(mut vm, r'(define source (string-copy "aλ😀"))
66 (define p (open-input-string source)) (string-set! source 0 #\z)')
67 assert text_port_eval(mut vm, '(char-ready? p)').is_true()
68 assert text_port_eval(mut vm, '(read-string 9 p)').as_string() or { panic(err) } == 'aλ😀'
69 assert text_port_eval(mut vm, '(eof-object? (peek-char p))').is_true()
70 assert text_port_eval(mut vm, '(char-ready? p)').is_true()
71 text_port_eval(mut vm, r'(define out (open-output-string))
72 (display "aλ" out) (define snapshot (get-output-string out))
73 (string-set! snapshot 0 #\z) (write-char #\b out) (newline out)')
74 assert text_port_eval(mut vm, '(get-output-string out)').as_string() or { panic(err) } == 'aλb\n'
75 assert text_port_eval(mut vm, 'snapshot').as_string() or { panic(err) } == 'zλ'
76 text_port_eval(mut vm, '(close-port out)')
77 assert text_port_eval(mut vm, '(get-output-string out)').as_string() or { panic(err) } == 'aλb\n'
78 assert write(text_port_eval(mut vm, '(list (port? out) (textual-port? out) (binary-port? out)
79 (input-port-open? out) (output-port-open? out) (output-port-open? p) (input-port-open? p))')) or { panic(err) } == '(#t #t #f #f #f #f #t)'
80 text_port_error(mut vm, '(display "closed" out)', 'closed')
81 text_port_eval(mut vm, '(close-port p) (close-port p) (close-port out)')
82 assert !text_port_eval(mut vm, '(input-port-open? p)').is_true()
83 assert text_port_eval(mut vm, '(eof-object? (eof-object))').is_true()
84 assert !text_port_eval(mut vm, '(port? (eof-object))').is_true()
85 assert !text_port_eval(mut vm, '(textual-port? 1)').is_true()
86}
87
88fn test_read_string_zero_does_not_touch_the_host_and_large_counts_do_not_preallocate() {
89 mut vm := new()
90 mut source := &TextChunkReader{ data: 'x', chunk: 1 }
91 port := vm.input_port(source) or { panic(err) }
92 assert vm.read_string(port, 0) or { panic(err) }.text() == ''
93 assert source.calls == 0
94 vm.define('p', port) or { panic(err) }
95 vm.define('large', integer(max_int)) or { panic(err) }
96 assert text_port_eval(mut vm, '(read-string large p)').text() == 'x'
97 assert source.calls == 2
98}
99
100fn test_write_string_ranges_use_character_offsets_and_validate_before_output() {
101 mut vm := new()
102 text_port_eval(mut vm, '(define out (open-output-string))')
103 vm.define('text', string_value('aλ\x00😀z')) or { panic(err) }
104 text_port_eval(mut vm, '(write-string text out 1 4) (write-string text out 5 5)
105 (write-string text out 4) (write-string "!" out)')
106 assert text_port_eval(mut vm, '(get-output-string out)').as_string() or { panic(err) } == 'λ\x00😀z!'
107 for source in ['(write-string text out -1)', '(write-string text out 4 3)',
108 '(write-string text out 0 6)', '(write-string text out 1.0)', '(write-string text out #f)',
109 '(write-string #f out)', '(write-string)', '(write-string text out 0 1 2)'] {
110 text_port_error(mut vm, source, '')
111 }
112 assert text_port_eval(mut vm, '(get-output-string out)').as_string() or { panic(err) } == 'λ\x00😀z!'
113 for source in ['(read-string -1)', '(read-string 1.0)', '(read-string 1 out)', '(read-line out)',
114 '(read-line 1)', '(close-port 1)', '(get-output-string (open-input-string ""))',
115 '(get-output-string)', '(open-output-string 1)', '(open-input-string 1)', '(input-port-open? 1)',
116 '(output-port-open? 1)', '(port?)', '(binary-port?)', '(eof-object 1)'] {
117 text_port_error(mut vm, source, '')
118 }
119}
120
121fn test_text_port_host_storage_ownership_gc_and_large_reads() {
122 mut vm := new()
123 mut original := ('λ😀\x00'.repeat(25000) + '\n42').clone()
124 port := vm.open_input_string(original) or { panic(err) }
125 unsafe { original.free() }
126 gc_collect()
127 line := vm.read_line(port) or { panic(err) }
128 assert line.string_pointer().length == 75000
129 assert line.text() == 'λ😀\x00'.repeat(25000)
130 assert vm.read(port) or { panic(err) }.as_integer() or { panic(err) } == 42
131 assert vm.read_line(port) or { panic(err) }.tag == .eof
132 out := vm.open_output_string() or { panic(err) }
133 vm.define('out', out) or { panic(err) }
134 vm.define('line', line) or { panic(err) }
135 text_port_eval(mut vm, '(write-string line out)')
136 snapshot := vm.get_output_string(out) or { panic(err) }
137 vm.close_output_port(out) or { panic(err) }
138 gc_collect()
139 assert snapshot.text() == line.text()
140 assert vm.get_output_string(out) or { panic(err) }.text() == line.text()
141 mut other := new()
142 for value in [port, out] {
143 other.define('foreign', value) or { panic(err) }
144 for source in ['(close-port foreign)', '(input-port-open? foreign)',
145 '(output-port-open? foreign)', '(get-output-string foreign)', '(read-line foreign)',
146 '(flush-output-port foreign)'] {
147 text_port_error(mut other, source, '')
148 }
149 }
150}
151
152fn test_text_reads_report_invalid_utf8_without_poisoning_port_guards() {
153 for bytes in [[u8(0xff)], [u8(0xc2)], [u8(0xe0), 0x80, 0x80], [u8(0xed), 0xa0, 0x80]] {
154 mut vm := new()
155 text := bytes.bytestr()
156 port := vm.open_input_string('\r' + text) or { panic(err) }
157 assert vm.read_line(port) or { panic(err) }.text() == ''
158 for _ in 0 .. 2 {
159 if _ := vm.read_line(port) {
160 assert false
161 } else {
162 assert err.msg().contains('UTF-8')
163 }
164 assert !port.input_pointer().busy
165 }
166 vm.close_input_port(port) or { panic(err) }
167 }
168}
169
170@[heap]
171struct TextFlushWriter {
172mut:
173 bytes []u8
174 flushes int
175 fail bool
176}
177
178fn (mut sink TextFlushWriter) write(bytes []u8) !int {
179 sink.bytes << bytes
180 return bytes.len
181}
182
183fn (mut sink TextFlushWriter) flush() ! {
184 sink.flushes++
185 gc_collect()
186 if sink.fail { return failure('test flush failure') }
187}
188
189fn test_flush_output_port_handles_host_capabilities_failures_and_files() {
190 mut vm := new()
191 mut sink := &TextFlushWriter{}
192 port := vm.output_port(sink) or { panic(err) }
193 vm.set_output_port(port) or { panic(err) }
194 text_port_eval(mut vm, '(display "pending") (flush-output-port)')
195 assert sink.flushes == 1 && sink.bytes.bytestr() == 'pending'
196 sink.fail = true
197 text_port_error(mut vm, '(flush-output-port)', 'test flush failure')
198 assert !port.output_pointer().busy
199 sink.fail = false
200 vm.flush_output_port(port) or { panic(err) }
201 assert sink.flushes == 3
202 vm.close_output_port(port) or { panic(err) }
203 text_port_error(mut vm, '(flush-output-port)', 'closed')
204 root := os.join_path(os.temp_dir(), 'vile-text-ports-${os.getpid()}-${time.sys_mono_now()}')
205 os.mkdir_all(root) or { panic(err) }
206 defer { os.rmdir_all(root) or {} }
207 path := os.join_path(root, 'flushed')
208 out := vm.open_output_file(path) or { panic(err) }
209 vm.set_output_port(out) or { panic(err) }
210 text_port_eval(mut vm, '(write-string "aλ") (flush-output-port)')
211 assert os.read_file(path) or { panic(err) } == 'aλ'
212 assert !out.output_pointer().closed
213 text_port_eval(mut vm, '(write-string "b") (flush-output-port)')
214 assert os.read_file(path) or { panic(err) } == 'aλb'
215 vm.close_output_port(out) or { panic(err) }
216 vm.flush_output_port(vm.open_output_string() or { panic(err) }) or { panic(err) }
217}
218