v/vlib/io/io_test.v

42 lines
541 B
V
Raw Normal View History

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