vile / scheme / port_ready_test.v
144 lines · 133 sloc · 4.44 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import io
4import os
5
6@[heap]
7struct ReadyTestReader {
8mut:
9 data []u8
10 position int
11 ended bool
12 invalid int
13 ready_calls int
14 blocking_calls int
15}
16
17fn (mut source ReadyTestReader) read(mut _bytes []u8) !int {
18 source.blocking_calls++
19 return failure('unexpected blocking read')
20}
21
22fn (mut source ReadyTestReader) read_ready(mut bytes []u8) !int {
23 source.ready_calls++
24 if source.invalid != 0 { return source.invalid }
25 if source.position == source.data.len { return if source.ended { 0 } else { -1 } }
26 count := int_min(bytes.len, source.data.len - source.position)
27 copy(mut bytes[..count], source.data[source.position..source.position + count])
28 source.position += count
29 return count
30}
31
32fn test_char_ready_handles_partial_utf8_without_consuming_or_calling_blocking_read() {
33 mut vm := new()
34 mut source := &ReadyTestReader{}
35 input := vm.input_port(source, read_size: 8) or { panic(err) }
36 assert !(vm.char_ready(input) or { panic(err) })
37 source.data << u8(0xf0)
38 assert !(vm.char_ready(input) or { panic(err) })
39 source.data << [u8(0x9f), 0x98]
40 assert !(vm.char_ready(input) or { panic(err) })
41 source.data << u8(0x80)
42 assert vm.char_ready(input) or { panic(err) }
43 calls := source.ready_calls
44 assert vm.char_ready(input) or { panic(err) }
45 assert source.ready_calls == calls
46 assert vm.peek_char(input) or { panic(err) }.as_character() or { panic(err) } == `😀`
47 assert vm.read_char(input) or { panic(err) }.as_character() or { panic(err) } == `😀`
48 assert !(vm.char_ready(input) or { panic(err) })
49 source.ended = true
50 assert vm.char_ready(input) or { panic(err) }
51 assert vm.read_char(input) or { panic(err) }.tag == .eof
52 assert vm.char_ready(input) or { panic(err) }
53 assert source.blocking_calls == 0
54 vm.set_input_port(input) or { panic(err) }
55 assert vm.eval('(char-ready?)') or { panic(err) }.whole() == 1
56 vm.close_input_port(input) or { panic(err) }
57 if _ := vm.char_ready(input) {
58 assert false
59 }
60 if _ := vm.eval('(char-ready? 1)') {
61 assert false
62 }
63 if _ := vm.eval('(char-ready? 1 2)') {
64 assert false
65 }
66}
67
68fn test_ready_reader_bad_counts_do_not_publish_uninitialized_buffer_or_poison_port() {
69 mut vm := new()
70 mut source := &ReadyTestReader{}
71 input := vm.input_port(source, read_size: 4) or { panic(err) }
72 for count in [-2, 5] {
73 source.invalid = count
74 if _ := vm.char_ready(input) {
75 assert false
76 }
77 assert input.input_pointer().buffer.len == 0
78 assert !input.input_pointer().busy
79 }
80 source.invalid = 0
81 source.data << u8(`a`)
82 assert vm.char_ready(input) or { panic(err) }
83 assert vm.read_char(input) or { panic(err) }.as_character() or { panic(err) } == `a`
84}
85
86fn test_ready_reports_truncated_or_invalid_utf8_without_waiting_for_more_bytes() {
87 for data in [[u8(0xc0)], [u8(0xff)], [u8(0xf0), 0x9f]] {
88 mut vm := new()
89 mut source := &ReadyTestReader{ data: data, ended: true }
90 input := vm.input_port(source) or { panic(err) }
91 assert vm.char_ready(input) or { panic(err) }
92 if _ := vm.read_char(input) {
93 assert false
94 }
95 assert source.blocking_calls == 0
96 }
97}
98
99@[heap]
100struct PlainReadyTestReader {
101mut:
102 ended bool
103}
104
105fn (mut source PlainReadyTestReader) read(mut bytes []u8) !int {
106 if source.ended { return io.Eof{} }
107 source.ended = true
108 bytes[0] = `a`
109 return 1
110}
111
112fn test_plain_reader_can_report_buffered_character_and_known_eof_without_ready_protocol() {
113 mut vm := new()
114 mut source := &PlainReadyTestReader{}
115 input := vm.input_port(source) or { panic(err) }
116 if _ := vm.char_ready(input) {
117 assert false
118 }
119 assert vm.peek_char(input) or { panic(err) }.as_character() or { panic(err) } == `a`
120 assert vm.char_ready(input) or { panic(err) }
121 vm.read_char(input) or { panic(err) }
122 if _ := vm.char_ready(input) {
123 assert false
124 }
125 assert vm.read_char(input) or { panic(err) }.tag == .eof
126 assert vm.char_ready(input) or { panic(err) }
127}
128
129fn test_nonblocking_owned_file_protocol_reads_available_pipe_bytes_and_eof() {
130 $if !windows {
131 mut pipe := os.pipe() or { panic(err) }
132 defer { pipe.close() }
133 mut bytes := []u8{len: 16}
134 mut stream := &PortFileStream{ file: os.File{ fd: pipe.read_fd, is_opened: true }, raw: true }
135 assert stream.read_ready(mut bytes) or { panic(err) } == -1
136 assert pipe.write_string('abc') or { panic(err) } == 3
137 assert stream.read_ready(mut bytes) or { panic(err) } == 3
138 assert bytes[..3].bytestr() == 'abc'
139 assert stream.read_ready(mut bytes) or { panic(err) } == -1
140 os.fd_close(pipe.write_fd)
141 pipe.write_fd = -1
142 assert stream.read_ready(mut bytes) or { panic(err) } == 0
143 }
144}
145