From 9e9dd69b096b029e604465c94c30ea62df5e034b Mon Sep 17 00:00:00 2001 From: Yerden Zhumabekov Date: Wed, 2 Sep 2020 00:30:41 +0600 Subject: [PATCH] mem: add test for Write/Seek Signed-off-by: Yerden Zhumabekov --- mem/file_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/mem/file_test.go b/mem/file_test.go index bb54db6..22af970 100644 --- a/mem/file_test.go +++ b/mem/file_test.go @@ -1,6 +1,7 @@ package mem import ( + "bytes" "io" "testing" "time" @@ -205,3 +206,42 @@ func TestFileReadAtSeekOffset(t *testing.T) { t.Fatal(err) } } + +func TestFileWriteAndSeek(t *testing.T) { + fd := CreateFile("foo") + f := NewFileHandle(fd) + + assert := func(expected bool, v ...interface{}) { + if !expected { + t.Helper() + t.Fatal(v...) + } + } + + data4 := []byte{0, 1, 2, 3} + data20 := bytes.Repeat(data4, 5) + var off int64 + + for i := 0; i < 100; i++ { + // write 20 bytes + n, err := f.Write(data20) + assert(err == nil, err) + off += int64(n) + assert(n == len(data20), n) + assert(off == int64((i+1)*len(data20)), off) + + // rewind to start and write 4 bytes there + cur, err := f.Seek(-off, io.SeekCurrent) + assert(err == nil, err) + assert(cur == 0, cur) + + n, err = f.Write(data4) + assert(err == nil, err) + assert(n == len(data4), n) + + // back at the end + cur, err = f.Seek(off-int64(n), io.SeekCurrent) + assert(err == nil, err) + assert(cur == off, cur, off) + } +}