vile / scheme / port_location_test.v
142 lines · 134 sloc · 5.24 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import io
4import lexer
5import reader
6
7@[heap]
8struct LocatedPortReader {
9 data []u8
10 chunk int
11mut:
12 position int
13}
14
15fn (mut source LocatedPortReader) read(mut destination []u8) !int {
16 if source.position == source.data.len { return io.Eof{} }
17 count := int_min(destination.len, int_min(source.chunk, source.data.len - source.position))
18 unsafe { vmemcpy(destination.data, &u8(source.data.data) + source.position, count) }
19 source.position += count
20 return count
21}
22
23fn test_port_errors_keep_absolute_byte_locations_after_compaction_and_mixed_operations() {
24 mut vm := new()
25 prefix := 'α\r\u0085;ignored\u2028 42\r\n '
26 for chunk in [1, 2, 3, 15, 16, 17, 4096] {
27 mut source := &LocatedPortReader{ data: (prefix + '#u8(1 256)').bytes(), chunk: chunk }
28 input := vm.input_port(source, read_size: 17) or { panic(err) }
29 assert vm.read_char(input) or { panic(err) }.as_character() or { panic(err) } == `α`
30 assert vm.read(input) or { panic(err) }.whole() == 42
31 if _ := vm.read(input) {
32 assert false
33 } else {
34 assert err is EvalError
35 assert err.offset == prefix.len + 6, '${chunk}: ${err}'
36 assert err.line == 4 && err.column == 9, '${chunk}: ${err}'
37 }
38 assert !input.input_pointer().busy && input.input_pointer().mark == -1
39 }
40 // read-string and read-line advance the same cursor without eagerly storing
41 // location metadata; subsequent refill must retain their consumed prefixes.
42 for chunk in [1, 2, 3, 15, 16, 17, 4096] {
43 prefix2 := 'λx\r\nrest\n '
44 mut source := &LocatedPortReader{ data: (prefix2 + '|bad\\x;|').bytes(), chunk: chunk }
45 input := vm.input_port(source, read_size: 5) or { panic(err) }
46 assert vm.read_string(input, 2) or { panic(err) }.as_string() or { panic(err) } == 'λx'
47 assert vm.read_line(input) or { panic(err) }.as_string() or { panic(err) } == ''
48 assert vm.read_line(input) or { panic(err) }.as_string() or { panic(err) } == 'rest'
49 if _ := vm.read(input) {
50 assert false
51 } else {
52 assert err is lexer.LexError
53 assert err.offset == prefix2.len + 4 && err.line == 3 && err.column == 8, '${chunk}: ${err}'
54 }
55 }
56}
57
58fn test_port_unterminated_trivia_errors_retain_the_original_opening_after_many_refills() {
59 mut vm := new()
60 for suffix in ['#|' + ('ignored λ\r\n').repeat(20000), '#!unknown'] {
61 mut source := &LocatedPortReader{ data: ('a\r\n ' + suffix).bytes(), chunk: 19 }
62 input := vm.input_port(source, read_size: 23) or { panic(err) }
63 assert vm.read_char(input) or { panic(err) }.as_character() or { panic(err) } == `a`
64 if _ := vm.read(input) {
65 assert false
66 } else {
67 assert err is lexer.LexError
68 assert err.offset == 5 && err.line == 2 && err.column == 3, '${err}'
69 assert err.incomplete == suffix.starts_with('#|')
70 }
71 assert input.input_pointer().buffer.cap < 4096
72 }
73}
74
75fn test_reader_and_lexer_errors_are_absolute_after_large_frame_buffer_release() {
76 mut vm := new()
77 prefix := '"' + ('a\r\n').repeat(30000) + '"\r\n '
78 for bad in ['(a .)', '"bad\\q"'] {
79 mut source := &LocatedPortReader{ data: (prefix + bad).bytes(), chunk: 997 }
80 input := vm.input_port(source, read_size: 4096) or { panic(err) }
81 vm.read(input) or { panic(err) }
82 assert input.input_pointer().buffer.cap <= 65536
83 if _ := vm.read(input) {
84 assert false
85 } else {
86 if err is reader.ReadError {
87 assert err.offset == prefix.len + 4 && err.line == 30002 && err.column == 7, '${err}'
88 } else if err is lexer.LexError {
89 assert err.offset == prefix.len + 4 && err.line == 30002 && err.column == 7, '${err}'
90 } else {
91 assert false, '${err}'
92 }
93 }
94 }
95}
96
97fn test_invalid_utf8_inside_quoted_frames_uses_the_failed_sequence_absolute_location() {
98 mut vm := new()
99 for bytes in [[u8(255)], [u8(0xc2), 0x20], [u8(0xe0), 0x80, 0x80], [u8(0xed), 0xa0, 0x80],
100 [u8(0xf4), 0x90, 0x80, 0x80]] {
101 for chunk in [1, 2, 3, 15, 16, 17, 4096] {
102 mut data := 'x\r\n "ok'.bytes()
103 data << bytes
104 data << `"`
105 mut source := &LocatedPortReader{ data: data, chunk: chunk }
106 input := vm.input_port(source, read_size: 5) or { panic(err) }
107 assert vm.read(input) or { panic(err) }.symbol_name() or { panic(err) } == 'x'
108 if _ := vm.read(input) {
109 assert false
110 } else {
111 assert err is lexer.LexError
112 assert err.offset == 8 && err.line == 2 && err.column == 6, '${chunk}: ${err}'
113 }
114 }
115 }
116}
117
118fn test_port_line_reading_uses_the_same_unicode_endings_without_consuming_next_line_data() {
119 mut vm := new()
120 for ending in ['\n', '\r', '\r\n', '\r\u0085', '\u0085', '\u2028'] {
121 for chunk in [1, 2, 3, 15, 16, 17, 4096] {
122 mut source := &LocatedPortReader{ data: ('a' + ending + 'b' + ending + ' 1/0 ').bytes(), chunk: chunk }
123 input := vm.input_port(source, read_size: 3) or { panic(err) }
124 assert vm.read_line(input) or { panic(err) }.as_string() or { panic(err) } == 'a'
125 assert vm.read_line(input) or { panic(err) }.as_string() or { panic(err) } == 'b'
126 if _ := vm.read(input) {
127 assert false
128 } else {
129 assert err is EvalError
130 assert err.line == 3 && err.column == 3 && err.offset == 4 + 2 * ending.len, '${ending.bytes()}: ${err}'
131 }
132 }
133 }
134 input := vm.open_input_string('42\n (a 1/0)') or { panic(err) }
135 assert vm.read(input) or { panic(err) }.whole() == 42
136 if _ := vm.read(input) {
137 assert false
138 } else {
139 assert err is EvalError
140 assert err.line == 2 && err.column == 6 && err.offset == 8, '${err}'
141 }
142}
143