vile / scheme / port_extensions_test.v
242 lines · 231 sloc · 9.12 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import io
4import lexer
5import reader
6
7@[heap]
8struct ExtensionPortReader {
9 data []u8
10 chunk int = 4096
11 split int = -1
12mut:
13 position int
14}
15
16fn (mut source ExtensionPortReader) read(mut destination []u8) !int {
17 if source.position == source.data.len { return io.Eof{} }
18 mut count := int_min(destination.len, int_min(source.chunk, source.data.len - source.position))
19 if source.position < source.split { count = int_min(count, source.split - source.position) }
20 unsafe { vmemcpy(destination.data, &u8(source.data.data) + source.position, count) }
21 source.position += count
22 return count
23}
24
25fn extension_result(value Value) !string {
26 return match value.tag {
27 .symbol { value.symbol_name()! }
28 .string { value.as_string()! }
29 .character { int(value.as_character()!).str() }
30 else { write(value)! }
31 }
32}
33
34fn test_extended_port_datums_have_independent_expected_values_at_all_chunks_and_splits() {
35 mut vm := new()
36 // Expectations are fixed independently of the source reader. A trailing 91
37 // checks that framing leaves exactly the following datum, including bars,
38 // labels and short boolean forms which do not need a delimiter themselves.
39 for row in [
40 ['#TRUE ', '#t'],
41 ['#False ', '#f'],
42 ['#t', '#t'],
43 ['#u8(0 #x41 #e1.0 255)', '#u8(0 65 1 255)'],
44 ['#VU8(12 #b11)', '#u8(12 3)'],
45 ['|Hello World|', 'hello world'],
46 [r'|a\|b\x5c;c|', 'a|b\\c'],
47 [r'|a\x0;b|', 'a\x00b'],
48 [r'A\x20;B ', 'a b'],
49 ['ΣTRASSE ', 'σtrasse'],
50 ['Straße ', 'strasse'],
51 ['a\u0085', 'a'],
52 ['a\u2003', 'a'],
53 ['a\u2028', 'a'],
54 [r'"a\x0;b\a\b\|"', 'a\x00b\a\b|'],
55 ['"a\r\nb\r\u0085c\u0085d\u2028e"', 'a\nb\nc\nd\ne'],
56 ['"a\\ \t\r\u0085 \tb"', 'ab'],
57 ['#\\alarm ', '7'],
58 ['#\\nul ', '0'],
59 ['#\\x1f600 ', '128512'],
60 ['#\\| ', '124'],
61 ['#| ( " #| nested |# ) |#42 ', '42'],
62 ['; discarded\u008542 ', '42'],
63 ['; discarded\u202842 ', '42'],
64 ['#;#;a b 42 ', '42'],
65 ["#;' #;a b 42 ", '42'],
66 ['(#;#;a b 1 #| ) |# 2)', '(1 2)'],
67 ["' #;a #;b c ", '(quote c)'],
68 ['#;#1=(a . #1#) 42 ', '42'],
69 ['#;(#1# #1=ignored) (#1=kept #1#)', '(kept kept)'],
70 ['#12=42 ', '42'],
71 ['#123456789012345678901234567890=17 ', '17'],
72 ] {
73 text := row[0] + '91 '
74 for chunk in [1, 2, 3, 15, 16, 17, 4096] {
75 mut source := &ExtensionPortReader{ data: text.bytes(), chunk: chunk }
76 input := vm.input_port(source, read_size: 19) or { panic(err) }
77 actual := extension_result(vm.read(input) or { panic('${row[0]} chunk${chunk}: ${err}') }) or { panic(err) }
78 assert actual == row[1], '${row[0]} chunk${chunk}: ${actual}'
79 assert vm.read(input) or { panic(err) }.as_integer() or { panic(err) } == 91
80 assert vm.read(input) or { panic(err) }.tag == .eof
81 }
82 for split in 1 .. text.len {
83 mut source := &ExtensionPortReader{ data: text.bytes(), split: split }
84 input := vm.input_port(source, read_size: text.len) or { panic(err) }
85 assert extension_result(vm.read(input) or { panic('${row[0]} split${split}: ${err}') }) or { panic(err) } == row[1]
86 assert vm.read(input) or { panic(err) }.as_integer() or { panic(err) } == 91
87 }
88 }
89}
90
91fn test_port_directives_persist_and_decode_each_frame_from_its_initial_mode() {
92 mut vm := new()
93 text := '#!no-fold-case A |B| C\\x44; (#;#!fold-case IGNORE E #!no-fold-case F) G #!fold-case H #!r6rs I '
94 for split in 0 .. text.len {
95 chunk := [1, 2, 3, 15, 16, 17, 4096][split % 7]
96 mut source := &ExtensionPortReader{ data: text.bytes(), chunk: chunk, split: split }
97 input := vm.input_port(source, read_size: 17) or { panic(err) }
98 for expected in ['A', 'B', 'CD'] {
99 assert vm.read(input) or { panic(err) }.symbol_name() or { panic(err) } == expected
100 }
101 list := vm.read(input) or { panic(err) }
102 assert list.pair_pointer().car.symbol_name() or { panic(err) } == 'e'
103 assert list.pair_pointer().cdr.pair_pointer().car.symbol_name() or { panic(err) } == 'F'
104 for expected in ['G', 'h', 'i'] {
105 assert vm.read(input) or { panic(err) }.symbol_name() or { panic(err) } == expected
106 }
107 assert vm.read(input) or { panic(err) }.tag == .eof
108 }
109 mut source := &ExtensionPortReader{ data: 'Mixed #!fold-case Mixed '.bytes(), chunk: 1 }
110 input := vm.input_port(source, read_size: 3, fold_case: false) or { panic(err) }
111 assert vm.read(input) or { panic(err) }.symbol_name() or { panic(err) } == 'Mixed'
112 assert vm.read(input) or { panic(err) }.symbol_name() or { panic(err) } == 'mixed'
113 for only in ['#!no-fold-case', '#!fold-case #| hi |# #;x', '#;#;a b'] {
114 mut source_only := &ExtensionPortReader{ data: only.bytes(), chunk: 1 }
115 empty := vm.input_port(source_only, read_size: 2) or { panic(err) }
116 assert vm.read(empty) or { panic(err) }.tag == .eof
117 }
118}
119
120fn test_port_label_scopes_preserve_identity_and_return_mutable_graphs() {
121 mut vm := new()
122 for chunk in [1, 2, 3, 15, 16, 17, 4096] {
123 mut source := &ExtensionPortReader{ data: '(#01=(a . #1#) #1# #2=#(1 #2#) #2# #3="hi" #3# #4=#u8(1 2) #4#) #1# '.bytes(), chunk: chunk }
124 input := vm.input_port(source, read_size: 17) or { panic(err) }
125 data := vm.read(input) or { panic(err) }
126 mut items := [8]Value{}
127 mut cursor := data
128 for i in 0 .. 8 {
129 items[i] = cursor.pair_pointer().car
130 cursor = cursor.pair_pointer().cdr
131 }
132 assert cursor.tag == .nil
133 for i in [0, 2, 4, 6] {
134 assert items[i].pointer() == items[i + 1].pointer()
135 }
136 assert items[0].pair_pointer().cdr.pointer() == items[0].pointer()
137 assert items[2].vector_pointer().items[1].pointer() == items[2].pointer()
138 vm.define('graph', data) or { panic(err) }
139 vm.eval('(set-car! (car graph) 9) (vector-set! (caddr graph) 0 8) (string-set! (list-ref graph 4) 0 #\\H) (bytevector-u8-set! (list-ref graph 6) 0 7)') or { panic(err) }
140 assert items[1].pair_pointer().car.whole() == 9
141 assert items[3].vector_pointer().items[0].whole() == 8
142 assert items[5].as_string() or { panic(err) } == 'Hi'
143 assert items[7].bytevector_pointer().bytes[0] == 7
144 if _ := vm.read(input) {
145 assert false
146 } else {
147 assert err.msg().contains('label'), err.msg()
148 }
149 assert !input.input_pointer().busy && input.input_pointer().mark == -1
150 }
151}
152
153fn test_extended_port_errors_are_not_eof_and_leave_busy_guard_clean_at_all_chunks() {
154 mut vm := new()
155 for text in ['#trueX ', '#false#t ', '#u8(256)', '#vu8(-1)', '#u8(1.0)', '#u8(a)', '#u8(1 . 2)',
156 '#u8(', '#|never closed', '#; ', '#;#;a ', '#;#1# 2 ', '#;(#1=x #1=y) 2 ', '#1# ', '#1=#1# ',
157 '(#1=x #1=y)', '#1=', '|never closed', r'|bad\x;|', r'bad\xD800; ', r'"bad\x110000;"',
158 '"bad\\ \t x"', '#!unknown ', '#!fold-cas ', '#\\xD800 ', '#\\', '#12345'] {
159 for chunk in [1, 2, 3, 15, 16, 17, 4096] {
160 mut source := &ExtensionPortReader{ data: text.bytes(), chunk: chunk }
161 input := vm.input_port(source, read_size: 17) or { panic(err) }
162 vm.set_input_port(input) or { panic(err) }
163 if _ := vm.eval('(read)') {
164 assert false, '${text} chunk${chunk}'
165 } else {
166 assert err is EvalError, '${text}: ${err}'
167 assert err.line >= 1 && err.column >= 1, '${text}: ${err}'
168 }
169 assert !input.input_pointer().busy && input.input_pointer().mark == -1
170 }
171 }
172 // Public host reads preserve incomplete classifications; Scheme read wraps
173 // them as runtime failures so its surrounding source is never replayed.
174 for text in ['#|x', '|x', '#;', '#1=', '#u8('] {
175 input := vm.open_input_string(text) or { panic(err) }
176 if _ := vm.read(input) {
177 assert false
178 } else {
179 if err is lexer.LexError {
180 assert err.incomplete
181 } else if err is reader.ReadError {
182 assert err.incomplete
183 } else {
184 assert false, '${err}'
185 }
186 }
187 }
188}
189
190fn test_extension_scan_kernels_are_bounded_at_all_offsets_and_block_edges() {
191 for length in 0 .. 82 {
192 for event in [u8(9), 10, 13, 32, 40, 41, 34, 59, 35, 124, 92, 128, 255] {
193 for position in 0 .. length {
194 mut bytes := []u8{len: length, init: `a`}
195 bytes[position] = event
196 for start in 0 .. length + 1 {
197 assert input_event_end(bytes, start) == if start <= position {
198 position
199 } else {
200 length
201 }
202 assert input_quote_end(bytes, start, event) == if start <= position {
203 position
204 } else {
205 length
206 }
207 }
208 }
209 }
210 }
211}
212
213fn test_raw_carriage_return_character_leaves_the_other_half_of_a_line_ending_unread() {
214 mut vm := new()
215 for ending in ['\n', '\u0085'] {
216 text := '#\\\r' + ending + '42 '
217 for split in 0 .. text.len {
218 for chunk in [1, 2, 3, 15, 16, 17, 4096] {
219 mut source := &ExtensionPortReader{ data: text.bytes(), chunk: chunk, split: split }
220 input := vm.input_port(source, read_size: 3) or { panic(err) }
221 assert vm.read(input) or { panic(err) }.as_character() or { panic(err) } == `\r`
222 assert vm.peek_char(input) or { panic(err) }.as_character() or { panic(err) } == ending.runes()[0]
223 assert vm.read(input) or { panic(err) }.whole() == 42
224 }
225 }
226 }
227}
228
229fn test_port_fold_directives_consumed_before_failures_persist_for_the_next_read() {
230 mut vm := new()
231 for text in ['(#!no-fold-case a .) Mixed ', '#!no-fold-case #!bad Mixed ',
232 '#u8(#!no-fold-case 256) Mixed '] {
233 for chunk in [1, 2, 3, 15, 16, 17, 4096] {
234 mut source := &ExtensionPortReader{ data: text.bytes(), chunk: chunk }
235 input := vm.input_port(source, read_size: 3) or { panic(err) }
236 if _ := vm.read(input) {
237 assert false, text
238 }
239 assert vm.read(input) or { panic('${text}: ${err}') }.symbol_name() or { panic(err) } == 'Mixed'
240 }
241 }
242}
243