| 1 | module scheme |
| 2 | |
| 3 | import io |
| 4 | import reader |
| 5 | |
| 6 | @[heap] |
| 7 | struct EdgecaseAuditReader { |
| 8 | text string |
| 9 | chunk int |
| 10 | split int |
| 11 | mut: |
| 12 | position int |
| 13 | } |
| 14 | |
| 15 | fn (mut source EdgecaseAuditReader) read(mut bytes []u8) !int { |
| 16 | if source.position == source.text.len { return io.Eof{} } |
| 17 | mut count := int_min(bytes.len, int_min(source.chunk, source.text.len - source.position)) |
| 18 | if source.position < source.split { count = int_min(count, source.split - source.position) } |
| 19 | unsafe { vmemcpy(bytes.data, source.text.str + source.position, count) } |
| 20 | source.position += count |
| 21 | return count |
| 22 | } |
| 23 | |
| 24 | fn edgecase_audit_input(mut vm VM, text string, chunk int, split int) Value { |
| 25 | source := &EdgecaseAuditReader{ text: text, chunk: chunk, split: split } |
| 26 | return vm.input_port(source, read_size: 17) or { panic(err.msg()) } |
| 27 | } |
| 28 | |
| 29 | fn test_dotted_tail_comments_keep_source_port_parity_at_every_input_split() { |
| 30 | mut vm := new() |
| 31 | for row in [ |
| 32 | ['(a . b #;c)', '(a . b)'], |
| 33 | ['(a . b #;c #;d)', '(a . b)'], |
| 34 | ['(a . b #;#;c d)', '(a . b)'], |
| 35 | ['(a #;discard . #;discard b #;discard)', '(a . b)'], |
| 36 | ['(a . (b . c #;inner) #;outer)', '(a b . c)'], |
| 37 | ['(a . () #;#u8(256))', '(a)'], |
| 38 | ['(a . b #;#1=(ignored . #1#))', '(a . b)'], |
| 39 | ] { |
| 40 | text := row[0] + ' 91 ' |
| 41 | for chunk in [1, 2, 3, 15, 16, 17, 4096] { |
| 42 | for split in 0 .. text.len { |
| 43 | input := edgecase_audit_input(mut vm, text, chunk, split) |
| 44 | assert write(vm.read(input) or { panic('${text}/${chunk}/${split}: ${err.msg()}') }) or { panic(err.msg()) } == row[1] |
| 45 | assert vm.read(input) or { panic(err.msg()) }.as_integer() or { panic(err.msg()) } == 91 |
| 46 | assert vm.read(input) or { panic(err.msg()) }.kind() == .eof |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | fn test_dotted_tail_comment_directives_and_label_scopes_survive_all_port_refills() { |
| 53 | mut vm := new() |
| 54 | for chunk in [1, 2, 3, 15, 16, 17, 4096] { |
| 55 | text := '#1=(HEAD . #1# #;#1=(ignored . #1#) #;#!no-fold-case IGNORED) MiXeD ' |
| 56 | for split in 0 .. text.len { |
| 57 | input := edgecase_audit_input(mut vm, text, chunk, split) |
| 58 | value := vm.read(input) or { panic('${chunk}/${split}: ${err.msg()}') } |
| 59 | assert value.tag == .pair && eqv(value, value.pair_pointer().cdr) |
| 60 | assert value.pair_pointer().car.symbol_name() or { panic(err.msg()) } == 'head' |
| 61 | assert vm.read(input) or { panic(err.msg()) }.symbol_name() or { panic(err.msg()) } == 'MiXeD' |
| 62 | assert vm.read(input) or { panic(err.msg()) }.tag == .eof |
| 63 | } |
| 64 | for bad in ['#1=(a . #1# #;#2=#1#)', '(a . b #;#1=ignored) #1#'] { |
| 65 | input := edgecase_audit_input(mut vm, bad, chunk, 0) |
| 66 | if bad.starts_with('#1=') { |
| 67 | if _ := vm.read(input) { |
| 68 | assert false |
| 69 | } else { |
| 70 | assert err.msg().contains('undefined datum label') |
| 71 | } |
| 72 | } else { |
| 73 | vm.read(input) or { panic(err.msg()) } |
| 74 | if _ := vm.read(input) { |
| 75 | assert false |
| 76 | } else { |
| 77 | assert err.msg().contains('undefined datum label') |
| 78 | } |
| 79 | } |
| 80 | assert !input.input_pointer().busy && input.input_pointer().mark == -1 |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | fn test_malformed_dotted_tail_comments_do_not_consume_the_next_port_datum() { |
| 86 | mut vm := new() |
| 87 | for bad in ['(a . b #;)', '(a . b #;#;c)', '(a . b #;c d)', '(a . b #;#1#)'] { |
| 88 | text := bad + ' 91 ' |
| 89 | for chunk in [1, 2, 3, 15, 16, 17, 4096] { |
| 90 | for split in 0 .. text.len { |
| 91 | input := edgecase_audit_input(mut vm, text, chunk, split) |
| 92 | if _ := vm.read(input) { |
| 93 | assert false, bad |
| 94 | } else { |
| 95 | assert err is reader.ReadError, err.msg() |
| 96 | if err is reader.ReadError { |
| 97 | assert !err.incomplete |
| 98 | } |
| 99 | } |
| 100 | assert !input.input_pointer().busy && input.input_pointer().mark == -1 |
| 101 | assert vm.read(input) or { panic('${text}: ${err.msg()}') }.as_integer() or { panic(err.msg()) } == 91 |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |