| 1 | module scheme |
| 2 | |
| 3 | import arena |
| 4 | import os |
| 5 | import reader |
| 6 | |
| 7 | fn test_textual_file_positions_restore_absolute_errors_and_reader_directives() { |
| 8 | path := os.join_path(os.temp_dir(), 'vile-position-${os.getpid()}.scm') |
| 9 | defer { os.rm(path) or {} } |
| 10 | os.write_file(path, "0\n #'") or { panic(err.msg()) } |
| 11 | mut vm := new() |
| 12 | input := vm.open_input_file(path) or { panic(err.msg()) } |
| 13 | defer { vm.close_input_port(input) or {} } |
| 14 | assert (vm.read(input) or { panic(err.msg()) }).whole() == 0 |
| 15 | saved := vm.port_position(input) or { panic(err.msg()) } |
| 16 | for _ in 0 .. 2 { |
| 17 | if _ := vm.read(input) { |
| 18 | assert false |
| 19 | } else { |
| 20 | assert err is reader.ReadError |
| 21 | if err is reader.ReadError { |
| 22 | assert err.offset == 3 && err.line == 2 && err.column == 2 |
| 23 | } |
| 24 | } |
| 25 | vm.set_port_position(input, saved) or { panic(err.msg()) } |
| 26 | } |
| 27 | case_input := vm.open_input_string('a #!no-fold-case A B #!fold-case C') or { panic(err.msg()) } |
| 28 | assert (vm.read(case_input) or { panic(err.msg()) }).text() == 'a' |
| 29 | assert (vm.read(case_input) or { panic(err.msg()) }).text() == 'A' |
| 30 | case_saved := vm.port_position(case_input) or { panic(err.msg()) } |
| 31 | assert (vm.read(case_input) or { panic(err.msg()) }).text() == 'B' |
| 32 | assert (vm.read(case_input) or { panic(err.msg()) }).text() == 'c' |
| 33 | vm.set_port_position(case_input, case_saved) or { panic(err.msg()) } |
| 34 | assert (vm.read(case_input) or { panic(err.msg()) }).text() == 'B' |
| 35 | } |
| 36 | |
| 37 | fn test_large_memory_input_keeps_one_seekable_snapshot_after_full_reads() { |
| 38 | mut vm := new() |
| 39 | text := 'λ😀'.repeat(40000) |
| 40 | input := vm.open_input_string(text) or { panic(err.msg()) } |
| 41 | backing := input.input_pointer().buffer.data |
| 42 | start := vm.port_position(input) or { panic(err.msg()) } |
| 43 | value := vm.read_string(input, 80000) or { panic(err.msg()) } |
| 44 | assert value.text() == text |
| 45 | assert input.input_pointer().buffer.data == backing |
| 46 | assert input.input_pointer().buffer.len == text.len |
| 47 | assert (vm.read_char(input) or { panic(err.msg()) }).tag == .eof |
| 48 | vm.set_port_position(input, start) or { panic(err.msg()) } |
| 49 | assert (vm.read_char(input) or { panic(err.msg()) }).whole() == `λ` |
| 50 | assert input.input_pointer().buffer.data == backing |
| 51 | } |
| 52 | |
| 53 | fn test_positioned_string_output_replaces_characters_without_copying_snapshots_back() { |
| 54 | mut vm := new() |
| 55 | output := vm.open_output_string() or { panic(err.msg()) } |
| 56 | vm.define('out', output) or { panic(err.msg()) } |
| 57 | vm.eval('(display "A" out)') or { panic(err.msg()) } |
| 58 | after_a := vm.port_position(output) or { panic(err.msg()) } |
| 59 | vm.eval('(display "😀λZ" out)') or { panic(err.msg()) } |
| 60 | original := vm.get_output_string(output) or { panic(err.msg()) } |
| 61 | vm.set_port_position(output, after_a) or { panic(err.msg()) } |
| 62 | vm.eval('(display "xy" out)') or { panic(err.msg()) } |
| 63 | assert (vm.get_output_string(output) or { panic(err.msg()) }).text() == 'AxyZ' |
| 64 | vm.set_port_position(output, after_a) or { panic(err.msg()) } |
| 65 | vm.eval('(display "😀λ" out)') or { panic(err.msg()) } |
| 66 | assert (vm.get_output_string(output) or { panic(err.msg()) }).text() == 'A😀λZ' |
| 67 | assert original.text() == 'A😀λZ' |
| 68 | } |
| 69 | |
| 70 | fn test_binary_memory_position_queries_seeks_and_append_writes_allocate_nothing() { |
| 71 | mut vm := new() |
| 72 | input := vm.open_input_bytevector(bytevector([u8(1), 2, 3])) or { panic(err.msg()) } |
| 73 | output := vm.open_output_bytevector() or { panic(err.msg()) } |
| 74 | output.output_pointer().memory_output.bytes.ensure_cap(4096) |
| 75 | mut region := arena.new(chunk_size: 65536) |
| 76 | region.push() |
| 77 | defer { |
| 78 | region.pop() |
| 79 | region.free() |
| 80 | } |
| 81 | before := region.used() |
| 82 | for _ in 0 .. 1001 { |
| 83 | assert vm.port_has_position(input) or { panic(err.msg()) } |
| 84 | assert vm.port_has_set_position(output) or { panic(err.msg()) } |
| 85 | vm.set_port_position(input, integer(0)) or { panic(err.msg()) } |
| 86 | assert (vm.read_u8(input) or { panic(err.msg()) }).whole() == 1 |
| 87 | assert (vm.port_position(input) or { panic(err.msg()) }).whole() == 1 |
| 88 | vm.write_u8(output, 42) or { panic(err.msg()) } |
| 89 | } |
| 90 | assert region.used() == before |
| 91 | assert (vm.port_position(output) or { panic(err.msg()) }).whole() == 1001 |
| 92 | vm.set_port_position(output, integer(1000000000)) or { panic(err.msg()) } |
| 93 | assert output.output_pointer().memory_output.bytes.len == 1001 |
| 94 | assert (vm.port_position(output) or { panic(err.msg()) }).whole() == 1000000000 |
| 95 | assert region.used() == before |
| 96 | } |
| 97 | |
| 98 | fn test_raw_nonseekable_position_capability_queries_allocate_no_discarded_errors() { |
| 99 | $if !windows { |
| 100 | mut pipe := os.pipe() or { panic(err.msg()) } |
| 101 | defer { pipe.close() } |
| 102 | mut vm := new() |
| 103 | stream := &PortFileStream{ file: os.File{ fd: pipe.read_fd, is_opened: true }, raw: true } |
| 104 | input := vm.input_port(stream, binary: true) or { panic(err.msg()) } |
| 105 | mut port := input.input_pointer() |
| 106 | port.stream = stream |
| 107 | mut region := arena.new(chunk_size: 65536) |
| 108 | region.push() |
| 109 | defer { |
| 110 | region.pop() |
| 111 | region.free() |
| 112 | } |
| 113 | before := region.used() |
| 114 | for _ in 0 .. 1001 { |
| 115 | assert !(vm.port_has_position(input) or { panic(err.msg()) }) |
| 116 | assert !(vm.port_has_set_position(input) or { panic(err.msg()) }) |
| 117 | } |
| 118 | assert region.used() == before |
| 119 | assert pipe.write_string('q') or { panic(err.msg()) } == 1 |
| 120 | assert (vm.read_u8(input) or { panic(err.msg()) }).whole() == `q` |
| 121 | vm.close_input_port(input) or { panic(err.msg()) } |
| 122 | } |
| 123 | } |
| 124 | |