1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/io/io_test.v

42 lines
562 B
V
Raw Normal View History

2021-04-25 21:22:33 +03:00
import io
struct Buf {
pub:
2022-04-15 15:35:35 +03:00
bytes []u8
2021-04-25 21:22:33 +03:00
mut:
i int
}
struct Writ {
pub mut:
2022-04-15 15:35:35 +03:00
bytes []u8
2021-04-25 21:22:33 +03:00
}
fn (mut b Buf) read(mut buf []u8) !int {
2021-04-25 21:22:33 +03:00
if !(b.i < b.bytes.len) {
return IError(io.Eof{})
2021-04-25 21:22:33 +03:00
}
n := copy(mut buf, b.bytes[b.i..])
2021-04-25 21:22:33 +03:00
b.i += n
return n
}
fn (mut w Writ) write(buf []u8) !int {
2021-04-25 21:22:33 +03:00
if buf.len <= 0 {
return error('none')
2021-04-25 21:22:33 +03:00
}
w.bytes << buf
return buf.len
}
fn test_copy() {
mut src := Buf{
2021-04-25 21:22:33 +03:00
bytes: 'abcdefghij'.repeat(10).bytes()
}
mut dst := Writ{
2022-04-15 15:35:35 +03:00
bytes: []u8{}
2021-04-25 21:22:33 +03:00
}
io.cp(mut src, mut dst) or { assert false }
2021-04-25 21:22:33 +03:00
assert dst.bytes == src.bytes
}