io: add a test for read_all (#6898)

pull/6926/head
Takahiro Yaota 2020-11-24 00:17:39 +09:00 committed by GitHub
parent 380e3640e3
commit 07fae5adf6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 0 deletions

View File

@ -15,6 +15,11 @@ pub fn make_reader(r Reader) Reader {
return r return r
} }
pub const (
eof_code = -1
eof = error_with_code('EOF', eof_code)
)
const ( const (
read_all_len = 10 * 1024 read_all_len = 10 * 1024
read_all_grow_len = 1024 read_all_grow_len = 1024

View File

@ -0,0 +1,40 @@
module io
struct Buf {
pub:
bytes []byte
mut:
i int
}
fn (mut b Buf) read(mut buf []byte) ?int {
if !(b.i < b.bytes.len) {
return eof
}
n := copy(buf, b.bytes[b.i..b.bytes.len])
b.i += n
return n
}
fn test_read_all() {
buf := Buf{
bytes: '123'.repeat(10).bytes()
}
res := read_all(buf) or {
assert false
''.bytes()
}
assert res == '123'.repeat(10).bytes()
}
/*
TODO: This test failed by a bug of read_all
fn test_read_all_huge() {
buf := Buf{bytes: "123".repeat(100000).bytes()}
res := read_all(buf) or {
assert false
"".bytes()
}
assert res == "123".repeat(100000).bytes()
}
*/