vile / scheme / port_reader_test.v
266 lines · 251 sloc · 10.16 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import io
4import math
5import os
6import reader
7import strconv
8
9@[heap]
10struct DatumPortReader {
11 data []u8
12 chunk int = 4096
13 forbid_eof bool
14mut:
15 position int
16 calls int
17}
18
19fn (mut source DatumPortReader) read(mut destination []u8) !int {
20 source.calls++
21 if source.position == source.data.len {
22 if source.forbid_eof {
23 return failure('reader was asked for input after the complete datum')
24 }
25 return io.Eof{}
26 }
27 count := int_min(destination.len, int_min(source.chunk, source.data.len - source.position))
28 copy(mut destination[..count], source.data[source.position..source.position + count])
29 source.position += count
30 return count
31}
32
33fn test_read_handles_every_datum_kind_and_streams_arbitrary_input_chunk_boundaries() {
34 for row in [
35 ['42 ', '42'],
36 ['#e1.50 ', '3/2'],
37 ['#xdead ', '57005'],
38 ['#t', '#t'],
39 ['#F', '#f'],
40 ['Hello ', 'hello'],
41 ['"λ😀"', '"λ😀"'],
42 [r'"a\"b\\c"', r'"a\"b\\c"'],
43 [r'#\space ', r'#\space'],
44 [r'#\λ ', r'#\λ'],
45 [r'#\) ', r'#\)'],
46 [r'#\( ', r'#\('],
47 [r'#\; ', r'#\;'],
48 [r'#\" ', r'#\"'],
49 ['()', '()'],
50 ['(1 2 . 3)', '(1 2 . 3)'],
51 ['#()', '#()'],
52 ['#(1 "λ" (a . b))', '#(1 "λ" (a . b))'],
53 ["'(a b)", '(quote (a b))'],
54 ['`(a ,b ,@c)', '(quasiquote (a (unquote b) (unquote-splicing c)))'],
55 ['(a; ignored ) " #t\n b)', '(a b)'],
56 [' ; λ😀 leading comment\r\n (a #t#f)', '(a #t #f)'],
57 [r'(#\) #\; #\")', r'(#\) #\; #\")'],
58 ] {
59 for chunk in [1, 2, 3, 7, 4096] {
60 mut source := &DatumPortReader{ data: row[0].bytes(), chunk: chunk }
61 mut vm := new()
62 input := vm.input_port(source, read_size: 5) or { panic(err) }
63 value := vm.read(input) or { panic('${row} chunk ${chunk}: ${err}') }
64 assert write(value) or { panic(err) } == row[1], '${row} chunk ${chunk}'
65 assert !input.input_pointer().busy && input.input_pointer().mark == -1
66 assert vm.read(input) or { panic(err) }.tag == .eof
67 }
68 }
69}
70
71fn test_read_finishes_self_terminating_data_without_waiting_for_eof() {
72 // Short booleans need lookahead: #t may still become #true.
73 for text in ['#t ', '#F ', '()', '(1 2)', '#(1 2)', '"hello"', "'(a)", '42 ', 'name ', r'#\space '] {
74 mut source := &DatumPortReader{ data: text.bytes(), chunk: 1, forbid_eof: true }
75 mut vm := new()
76 input := vm.input_port(source, read_size: 1) or { panic(err) }
77 value := vm.read(input) or { panic('${text}: ${err}') }
78 assert value.tag != .eof
79 assert source.calls == text.len
80 }
81}
82
83fn test_read_char_and_peek_char_share_the_same_cursor_and_leave_atom_delimiters_unread() {
84 mut source := &DatumPortReader{ data: '12 (a)"x"#t#foo'.bytes(), chunk: 3 }
85 mut vm := new()
86 input := vm.input_port(source, read_size: 5) or { panic(err) }
87 assert vm.read(input) or { panic(err) }.as_integer() or { panic(err) } == 12
88 assert vm.peek_char(input) or { panic(err) }.as_character() or { panic(err) } == ` `
89 assert vm.read_char(input) or { panic(err) }.as_character() or { panic(err) } == ` `
90 assert write(vm.read(input) or { panic(err) }) or { panic(err) } == '(a)'
91 assert vm.peek_char(input) or { panic(err) }.as_character() or { panic(err) } == `"`
92 assert vm.read(input) or { panic(err) }.as_string() or { panic(err) } == 'x'
93 assert vm.read(input) or { panic(err) }.as_boolean() or { panic(err) }
94 assert !(vm.read(input) or { panic(err) }.as_boolean() or { panic(err) })
95 assert vm.read(input) or { panic(err) }.symbol_name() or { panic(err) } == 'oo'
96 assert vm.read_char(input) or { panic(err) }.tag == .eof
97}
98
99fn test_read_results_are_fresh_mutable_data_and_keep_their_storage_after_buffer_reuse() {
100 mut source := &DatumPortReader{ data: '(a "λ😀" #(1 "x") . tail) '.bytes(), chunk: 1 }
101 mut vm := new()
102 input := vm.input_port(source, read_size: 3) or { panic(err) }
103 data := vm.read(input) or { panic(err) }
104 vm.define('data', data) or { panic(err) }
105 vm.eval('(set-car! data 42) (string-set! (cadr data) 0 #\\a) (vector-set! (caddr data) 0 99) (string-set! (vector-ref (caddr data) 1) 0 #\\z)') or { panic(err) }
106 assert write(data) or { panic(err) } == '(42 "a😀" #(99 "z") . tail)'
107 assert vm.read(input) or { panic(err) }.tag == .eof
108 gc_collect()
109 assert write(data) or { panic(err) } == '(42 "a😀" #(99 "z") . tail)'
110 text := 'λ😀'.repeat(40000)
111 mut large := &DatumPortReader{ data: ('"' + text + '" ' + '123 '.repeat(2000)).bytes(), chunk: 997 }
112 large_port := vm.input_port(large, read_size: 4096) or { panic(err) }
113 saved := vm.read(large_port) or { panic(err) }
114 assert large_port.input_pointer().buffer.cap <= 65536
115 for _ in 0 .. 2000 {
116 assert vm.read(large_port) or { panic(err) }.as_integer() or { panic(err) } == 123
117 }
118 assert vm.read(large_port) or { panic(err) }.tag == .eof
119 gc_collect()
120 assert saved.as_string() or { panic(err) } == text
121}
122
123fn test_read_ignores_large_leading_trivia_without_retaining_it_as_datum_storage() {
124 mut source := &DatumPortReader{ data: ('; ' + 'λ😀 '.repeat(20000) + '\n42 ').bytes(), chunk: 193 }
125 mut vm := new()
126 input := vm.input_port(source, read_size: 256) or { panic(err) }
127 assert vm.read(input) or { panic(err) }.as_integer() or { panic(err) } == 42
128 assert input.input_pointer().buffer.cap < 4096
129 mut empty := &DatumPortReader{ data: (' \t; λ😀\r\n'.repeat(10000)).bytes(), chunk: 97 }
130 empty_port := vm.input_port(empty, read_size: 128) or { panic(err) }
131 assert vm.read(empty_port) or { panic(err) }.tag == .eof
132}
133
134fn test_flat_read_lists_and_prefix_sequences_do_not_consume_native_stack_per_element() {
135 mut source := &DatumPortReader{ data: ('(' + "'x ".repeat(20000) + ')').bytes(), chunk: 233 }
136 mut vm := new()
137 input := vm.input_port(source) or { panic(err) }
138 data := vm.read(input) or { panic(err) }
139 assert data.list_length() or { panic(err) } == 20000
140 assert write(data.pair_pointer().car) or { panic(err) } == '(quote x)'
141 assert vm.read(input) or { panic(err) }.tag == .eof
142}
143
144fn test_read_nesting_is_bounded_including_quote_prefixes_but_allows_the_reader_boundary() {
145 mut vm := new()
146 for text in ['('.repeat(reader.max_depth + 1) + ')'.repeat(reader.max_depth + 1),
147 "'".repeat(reader.max_depth) + '()'] {
148 mut source := &DatumPortReader{ data: text.bytes(), chunk: 1 }
149 input := vm.input_port(source, read_size: 1) or { panic(err) }
150 assert vm.read(input) or { panic(err) }.tag == .pair
151 }
152 for text in ['('.repeat(reader.max_depth + 2), "'".repeat(reader.max_depth + 2)] {
153 mut source := &DatumPortReader{ data: text.bytes(), forbid_eof: true }
154 input := vm.input_port(source) or { panic(err) }
155 if _ := vm.read(input) {
156 assert false
157 } else {
158 assert err.msg().contains('nesting limit'), err.msg()
159 }
160 }
161}
162
163fn test_invalid_input_never_becomes_eof_or_an_unfinished_eval_source_error() {
164 for text in ['(', '"unterminated', "'", '#\\', '#', ')', '(1 . 2 3)', '(1 .)', '#(1 . 2)',
165 "a'x ", '1#t ', r'#\a#t ', r'#\(foo ', r'#\ x ', '1/0 ', '"bad\\q"'] {
166 mut vm := new()
167 mut source := &DatumPortReader{ data: text.bytes(), chunk: 1 }
168 input := vm.input_port(source) or { panic(err) }
169 vm.set_input_port(input) or { panic(err) }
170 if _ := vm.eval('(read)') {
171 assert false, text
172 } else {
173 assert err is EvalError, '${text}: ${err}'
174 }
175 assert !input.input_pointer().busy && input.input_pointer().mark == -1
176 }
177}
178
179fn test_read_is_available_through_the_embedding_vm_with_explicit_and_current_ports() {
180 mut vm := new()
181 mut source := &DatumPortReader{ data: '1 2 '.bytes() }
182 input := vm.input_port(source) or { panic(err) }
183 vm.define('input', input) or { panic(err) }
184 vm.set_input_port(input) or { panic(err) }
185 assert write(vm.eval('(list (read input) (read) (eof-object? (read)))') or { panic(err) }) or { panic(err) } == '(1 2 #t)'
186 for code in ['(read 1)', '(read 1 2)'] {
187 if _ := vm.eval(code) {
188 assert false
189 } else {
190 assert err is EvalError
191 }
192 }
193}
194
195fn test_chunked_string_framing_keeps_backslash_parity_across_every_split() {
196 mut vm := new()
197 for left in 0 .. 12 {
198 for right in 0 .. 9 {
199 text := '\\'.repeat(left) + '"λ😀\x00\n' + '\\'.repeat(right)
200 encoded := write(string_value(text)) or { panic(err) }
201 for chunk in [1, 2, 3, 5, 17] {
202 mut source := &DatumPortReader{ data: (encoded + '#t').bytes(), chunk: chunk }
203 input := vm.input_port(source, read_size: 11) or { panic(err) }
204 assert vm.read(input) or { panic(err) }.as_string() or { panic(err) } == text
205 assert vm.read(input) or { panic(err) }.as_boolean() or { panic(err) }
206 assert vm.read(input) or { panic(err) }.tag == .eof
207 }
208 }
209 }
210}
211
212fn test_bounded_framing_scans_find_only_delimiters_inside_the_requested_suffix() {
213 for length in 0 .. 81 {
214 mut bytes := []u8{len: length}
215 for i in 0 .. length { bytes[i] = [u8(0), 255, 128, `a`, `#`, `\\`, `'`, `,`, 0x60][i % 9] }
216 for start in 0 .. length + 1 {
217 assert input_word_end(bytes, start) == length
218 assert input_quote(bytes, start) == length
219 }
220 }
221 for delimiter in [u8(9), 10, 11, 12, 13, 32, 40, 41, 34, 59] {
222 for position in 0 .. 80 {
223 mut bytes := []u8{len: 80, init: `a`}
224 bytes[position] = delimiter
225 for start in 0 .. 81 {
226 expected := if start <= position { position } else { 80 }
227 assert input_word_end(bytes, start) == expected
228 assert input_quote(bytes, start) == if delimiter == `"` { expected } else { 80 }
229 }
230 }
231 }
232}
233
234fn test_numeric_port_reading_matches_independent_rounding_fixtures_without_owned_token_copies() {
235 fixture := os.read_file(@VMODROOT + '/scheme/testdata/numeric_io.tsv') or { panic(err) }
236 mut text := ''
237 mut expected := []u64{}
238 for row in fixture.split_into_lines() {
239 if row.len == 0 || row[0] == `#` { continue }
240 fields := row.split('\t')
241 radix := fields[0].int()
242 mut spelling := fields[1]
243 mut explicit := false
244 for prefix in ['#b', '#B', '#o', '#O', '#d', '#D', '#x', '#X'] {
245 if spelling.contains(prefix) { explicit = true }
246 }
247 if radix != 10 && !explicit {
248 spelling = match radix {
249 2 { '#b' }
250 8 { '#o' }
251 else { '#x' }
252 } + spelling
253 }
254 text += spelling + ' '
255 expected << strconv.parse_uint(fields[2], 16, 64) or { panic(err) }
256 }
257 assert expected.len == 1585
258 mut vm := new()
259 mut source := &DatumPortReader{ data: text.bytes(), chunk: 17 }
260 input := vm.input_port(source, read_size: 31) or { panic(err) }
261 for i, bits in expected {
262 value := vm.read(input) or { panic('fixture ${i}: ${err}') }
263 assert math.f64_bits(value.as_real() or { panic(err) }) == bits, 'fixture ${i}'
264 }
265 assert vm.read(input) or { panic(err) }.tag == .eof
266}
267