vile / scheme / port_hex_case_test.v
180 lines · 170 sloc · 6.55 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import io
4import lexer
5import reader
6
7@[heap]
8struct HexCasePortReader {
9 text string
10 chunk int
11 split int
12mut:
13 position int
14}
15
16fn (mut source HexCasePortReader) read(mut destination []u8) !int {
17 if source.position == source.text.len { return io.Eof{} }
18 mut count := int_min(destination.len, int_min(source.chunk, source.text.len - source.position))
19 if source.position < source.split { count = int_min(count, source.split - source.position) }
20 unsafe { vmemcpy(destination.data, source.text.str + source.position, count) }
21 source.position += count
22 return count
23}
24
25fn hex_case_input(mut vm VM, text string, chunk int, split int, fold bool) !Value {
26 source := &HexCasePortReader{ text: text, chunk: chunk, split: split }
27 return vm.input_port(source, read_size: 17, fold_case: fold)
28}
29
30fn test_port_uppercase_hex_identifier_framing_owns_the_whole_escape_at_every_split() {
31 mut vm := new()
32 for spelling in [r'A\X20;B', r'\X41;B', r'a\X3b;b', r'\X0;Name', r'|A\X20;B|', r'"\X41;\x42;\X0;"',
33 r'#\X1F600', r'#\X'] {
34 text := spelling + ' 91 '
35 for fold in [false, true] {
36 for chunk in [1, 2, 3, 15, 16, 17, 4096] {
37 for split in 0 .. text.len {
38 input := hex_case_input(mut vm, text, chunk, split, fold) or { panic(err) }
39 mut port := input.input_pointer()
40 start, end, initial, present := port.frame_datum() or { panic(err) }
41 assert present && initial == fold
42 assert end - start == spelling.len, '${spelling}, chunk${chunk}, split${split}'
43 framed := unsafe { tos(&u8(port.buffer.data) + start, end - start) }
44 assert framed == spelling
45 port.mark = -1
46 assert vm.read(input) or { panic(err) }.whole() == 91
47 }
48 }
49 }
50 }
51}
52
53fn test_port_uppercase_hex_decoding_matches_fixed_values_in_both_fold_modes_at_all_splits() {
54 mut vm := new()
55 for row in [
56 [r'A\X20;B', 'A B', 'a b'],
57 [r'\X41;B', 'AB', 'ab'],
58 [r'a\X3b;b', 'a;b', 'a;b'],
59 [r'\X0;Name', '\x00Name', '\x00name'],
60 [r'|A\X20;B|', 'A B', 'a b'],
61 [r'|A\X7c;B|', 'A|B', 'a|b'],
62 [r'|\X5c;|', '\\', '\\'],
63 [r'|\X1F600;Name|', '😀Name', '😀name'],
64 ] {
65 text := row[0] + ' 91 '
66 for fold in [false, true] {
67 expected := row[if fold { 2 } else { 1 }]
68 for chunk in [1, 2, 3, 15, 16, 17, 4096] {
69 for split in 0 .. text.len {
70 input := hex_case_input(mut vm, text, chunk, split, fold) or { panic(err) }
71 assert vm.read(input) or { panic('${row[0]}, ${chunk}, ${split}: ${err}') }.symbol_name() or { panic(err) } == expected
72 assert vm.read(input) or { panic(err) }.whole() == 91
73 }
74 }
75 }
76 }
77 for fold in [false, true] {
78 for chunk in [1, 2, 3, 15, 16, 17, 4096] {
79 for row in [[r'"\X41;\x42;\X0;"', 'AB\x00'], [r'"\X1f600;"', '😀']] {
80 text := row[0] + ' 91 '
81 for split in 0 .. text.len {
82 input := hex_case_input(mut vm, text, chunk, split, fold) or { panic(err) }
83 assert vm.read(input) or { panic(err) }.as_string() or { panic(err) } == row[1]
84 assert vm.read(input) or { panic(err) }.whole() == 91
85 }
86 }
87 }
88 }
89}
90
91fn test_port_uppercase_hex_characters_and_literal_x_are_exact_in_both_fold_modes() {
92 mut vm := new()
93 spellings := [r'#\X41 ', r'#\X1F600 ', r'#\X ', r'#\x ']
94 expected := [rune(65), rune(128512), rune(88), rune(120)]
95 for fold in [false, true] {
96 for chunk in [1, 2, 3, 15, 16, 17, 4096] {
97 for i, spelling in spellings {
98 text := spelling + '91 '
99 for split in 0 .. text.len {
100 input := hex_case_input(mut vm, text, chunk, split, fold) or { panic(err) }
101 assert vm.read(input) or { panic(err) }.as_character() or { panic(err) } == expected[i]
102 assert vm.read(input) or { panic(err) }.whole() == 91
103 }
104 }
105 input := hex_case_input(mut vm, r'#\X', chunk, 0, fold) or { panic(err) }
106 assert vm.read(input) or { panic(err) }.as_character() or { panic(err) } == `X`
107 assert vm.read(input) or { panic(err) }.tag == .eof
108 }
109 }
110}
111
112fn test_port_uppercase_hex_errors_and_incomplete_escapes_are_not_eof_and_release_busy_state() {
113 mut vm := new()
114 for text in [r'a\X;', r'a\X110000; ', r'|\XD800;|', r'"\X110000;"', r'#\XD800 ', r'a\X41',
115 r'|a\X41', r'"\X41'] {
116 for fold in [false, true] {
117 for chunk in [1, 2, 3, 15, 16, 17, 4096] {
118 for split in 0 .. text.len {
119 input := hex_case_input(mut vm, text, chunk, split, fold) or { panic(err) }
120 if _ := vm.read(input) {
121 assert false, text
122 } else {
123 if err is lexer.LexError {
124 assert err.incomplete == text.ends_with('41')
125 } else if err is reader.ReadError {
126 assert err.incomplete == text.ends_with('41')
127 } else {
128 assert false, '${text}: ${err}'
129 }
130 }
131 assert !input.input_pointer().busy && input.input_pointer().mark == -1
132 }
133 }
134 }
135 }
136}
137
138fn test_port_uppercase_inline_hex_with_directives_inside_one_frame_decodes_from_initial_mode() {
139 mut vm := new()
140 text := r'(A\X41; #!no-fold-case B\X42; #!fold-case C\X43;) Mixed '
141 for chunk in [1, 2, 3, 15, 16, 17, 4096] {
142 for split in 0 .. text.len {
143 input := hex_case_input(mut vm, text, chunk, split, true) or { panic(err) }
144 value := vm.read(input) or { panic(err) }
145 assert value.pair_pointer().car.symbol_name() or { panic(err) } == 'aa'
146 assert value.pair_pointer().cdr.pair_pointer().car.symbol_name() or { panic(err) } == 'BB'
147 assert value.pair_pointer().cdr.pair_pointer().cdr.pair_pointer().car.symbol_name() or { panic(err) } == 'cc'
148 assert vm.read(input) or { panic(err) }.symbol_name() or { panic(err) } == 'mixed'
149 }
150 }
151}
152
153fn test_port_mixed_case_directives_persist_across_chunks_and_do_not_fold_named_characters_when_disabled() {
154 mut vm := new()
155 text := r'#!NO-FOLD-CASE A\X41; #!fOlD-cAsE B\X42; #!No-FoLd-CaSe |C\X43;| #!R6RS Mixed #!FOLD-CASE #\SPACE '
156 for chunk in [1, 2, 3, 15, 16, 17, 4096] {
157 for split in 0 .. text.len {
158 input := hex_case_input(mut vm, text, chunk, split, true) or { panic(err) }
159 for expected in ['AA', 'bb', 'CC', 'Mixed'] {
160 assert vm.read(input) or { panic(err) }.symbol_name() or { panic(err) } == expected
161 }
162 assert vm.read(input) or { panic(err) }.as_character() or { panic(err) } == ` `
163 assert vm.read(input) or { panic(err) }.tag == .eof
164 }
165 }
166 text2 := r'#!NO-FOLD-CASE #\SPACE #\X #!FOLD-CASE #\SPACE '
167 for chunk in [1, 2, 3, 15, 16, 17, 4096] {
168 for split in 0 .. text2.len {
169 input := hex_case_input(mut vm, text2, chunk, split, true) or { panic(err) }
170 if _ := vm.read(input) {
171 assert false
172 } else {
173 assert err is lexer.LexError
174 }
175 assert !input.input_pointer().busy && input.input_pointer().mark == -1
176 assert vm.read(input) or { panic(err) }.as_character() or { panic(err) } == `X`
177 assert vm.read(input) or { panic(err) }.as_character() or { panic(err) } == ` `
178 }
179 }
180}
181