siddontang.golib/ioutil2/sectionwriter_test.go

57 lines
766 B
Go
Raw Normal View History

2014-09-17 13:11:39 +08:00
package ioutil2
2014-04-26 11:58:30 +08:00
import (
"io/ioutil"
"os"
"testing"
)
func TestSectionWriter(t *testing.T) {
f, err := ioutil.TempFile(".", "test_")
if err != nil {
t.Fatal(err)
}
defer func() {
n := f.Name()
f.Close()
os.Remove(n)
}()
f.Truncate(3)
rw := NewSectionWriter(f, 0, 1)
_, err = rw.Write([]byte{'1'})
if err != nil {
t.Fatal(err)
}
_, err = rw.Write([]byte{'1'})
if err == nil {
t.Fatal("must err")
}
rw = NewSectionWriter(f, 1, 2)
_, err = rw.Write([]byte{'2', '3', '4'})
if err == nil {
t.Fatal("must err")
}
_, err = rw.Write([]byte{'2', '3'})
if err != nil {
t.Fatal(err)
}
buf := make([]byte, 3)
_, err = f.ReadAt(buf, 0)
if err != nil {
t.Fatal(err)
}
if string(buf) != "123" {
t.Fatal(string(buf))
}
}