module scheme import io import os @[heap] struct ReadyTestReader { mut: data []u8 position int ended bool invalid int ready_calls int blocking_calls int } fn (mut source ReadyTestReader) read(mut _bytes []u8) !int { source.blocking_calls++ return failure('unexpected blocking read') } fn (mut source ReadyTestReader) read_ready(mut bytes []u8) !int { source.ready_calls++ if source.invalid != 0 { return source.invalid } if source.position == source.data.len { return if source.ended { 0 } else { -1 } } count := int_min(bytes.len, source.data.len - source.position) copy(mut bytes[..count], source.data[source.position..source.position + count]) source.position += count return count } fn test_char_ready_handles_partial_utf8_without_consuming_or_calling_blocking_read() { mut vm := new() mut source := &ReadyTestReader{} input := vm.input_port(source, read_size: 8) or { panic(err) } assert !(vm.char_ready(input) or { panic(err) }) source.data << u8(0xf0) assert !(vm.char_ready(input) or { panic(err) }) source.data << [u8(0x9f), 0x98] assert !(vm.char_ready(input) or { panic(err) }) source.data << u8(0x80) assert vm.char_ready(input) or { panic(err) } calls := source.ready_calls assert vm.char_ready(input) or { panic(err) } assert source.ready_calls == calls assert vm.peek_char(input) or { panic(err) }.as_character() or { panic(err) } == `😀` assert vm.read_char(input) or { panic(err) }.as_character() or { panic(err) } == `😀` assert !(vm.char_ready(input) or { panic(err) }) source.ended = true assert vm.char_ready(input) or { panic(err) } assert vm.read_char(input) or { panic(err) }.tag == .eof assert vm.char_ready(input) or { panic(err) } assert source.blocking_calls == 0 vm.set_input_port(input) or { panic(err) } assert vm.eval('(char-ready?)') or { panic(err) }.whole() == 1 vm.close_input_port(input) or { panic(err) } if _ := vm.char_ready(input) { assert false } if _ := vm.eval('(char-ready? 1)') { assert false } if _ := vm.eval('(char-ready? 1 2)') { assert false } } fn test_ready_reader_bad_counts_do_not_publish_uninitialized_buffer_or_poison_port() { mut vm := new() mut source := &ReadyTestReader{} input := vm.input_port(source, read_size: 4) or { panic(err) } for count in [-2, 5] { source.invalid = count if _ := vm.char_ready(input) { assert false } assert input.input_pointer().buffer.len == 0 assert !input.input_pointer().busy } source.invalid = 0 source.data << u8(`a`) assert vm.char_ready(input) or { panic(err) } assert vm.read_char(input) or { panic(err) }.as_character() or { panic(err) } == `a` } fn test_ready_reports_truncated_or_invalid_utf8_without_waiting_for_more_bytes() { for data in [[u8(0xc0)], [u8(0xff)], [u8(0xf0), 0x9f]] { mut vm := new() mut source := &ReadyTestReader{ data: data, ended: true } input := vm.input_port(source) or { panic(err) } assert vm.char_ready(input) or { panic(err) } if _ := vm.read_char(input) { assert false } assert source.blocking_calls == 0 } } @[heap] struct PlainReadyTestReader { mut: ended bool } fn (mut source PlainReadyTestReader) read(mut bytes []u8) !int { if source.ended { return io.Eof{} } source.ended = true bytes[0] = `a` return 1 } fn test_plain_reader_can_report_buffered_character_and_known_eof_without_ready_protocol() { mut vm := new() mut source := &PlainReadyTestReader{} input := vm.input_port(source) or { panic(err) } if _ := vm.char_ready(input) { assert false } assert vm.peek_char(input) or { panic(err) }.as_character() or { panic(err) } == `a` assert vm.char_ready(input) or { panic(err) } vm.read_char(input) or { panic(err) } if _ := vm.char_ready(input) { assert false } assert vm.read_char(input) or { panic(err) }.tag == .eof assert vm.char_ready(input) or { panic(err) } } fn test_nonblocking_owned_file_protocol_reads_available_pipe_bytes_and_eof() { $if !windows { mut pipe := os.pipe() or { panic(err) } defer { pipe.close() } mut bytes := []u8{len: 16} mut stream := &PortFileStream{ file: os.File{ fd: pipe.read_fd, is_opened: true }, raw: true } assert stream.read_ready(mut bytes) or { panic(err) } == -1 assert pipe.write_string('abc') or { panic(err) } == 3 assert stream.read_ready(mut bytes) or { panic(err) } == 3 assert bytes[..3].bytestr() == 'abc' assert stream.read_ready(mut bytes) or { panic(err) } == -1 os.fd_close(pipe.write_fd) pipe.write_fd = -1 assert stream.read_ready(mut bytes) or { panic(err) } == 0 } }