mem: add test for Write/Seek

Signed-off-by: Yerden Zhumabekov <yerden.zhumabekov@gmail.com>
This commit is contained in:
Yerden Zhumabekov 2020-09-02 00:30:41 +06:00
parent 64002605e4
commit 9e9dd69b09
1 changed files with 40 additions and 0 deletions

View File

@ -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)
}
}