From 07fae5adf6c4e3f533127de0bea2ada8342b1e1f Mon Sep 17 00:00:00 2001 From: Takahiro Yaota Date: Tue, 24 Nov 2020 00:17:39 +0900 Subject: [PATCH] io: add a test for read_all (#6898) --- vlib/io/reader.v | 5 +++++ vlib/io/reader_test.v | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 vlib/io/reader_test.v diff --git a/vlib/io/reader.v b/vlib/io/reader.v index 5ca59b17f0..99ea10b1a2 100644 --- a/vlib/io/reader.v +++ b/vlib/io/reader.v @@ -15,6 +15,11 @@ pub fn make_reader(r Reader) Reader { return r } +pub const ( + eof_code = -1 + eof = error_with_code('EOF', eof_code) +) + const ( read_all_len = 10 * 1024 read_all_grow_len = 1024 diff --git a/vlib/io/reader_test.v b/vlib/io/reader_test.v new file mode 100644 index 0000000000..7667286683 --- /dev/null +++ b/vlib/io/reader_test.v @@ -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() +} +*/