func TestBlockException(t *testing.T) { defer test.New(t) _, err := New(root, 0, 33) test.NotNil(err) _, err = New("/notperm", 0, 11) test.NotNil(err) f, err := New(root, 0, 10) test.Nil(err) err = f.Delete(false) test.Nil(err) _, err = f.ReadAt(nil, -1) test.Equal(err, ErrInvalidOffset) test.Equal(f.Close(), true) test.Equal(f.Close(), false) test.Equal(f.Delete(false), nil) _, err = f.WriteAt(nil, 1) test.Equal(err, ErrBlockClosed) _, err = f.ReadAt(nil, 1) test.Equal(err, ErrBlockClosed) }
func writeN(n int, b *testing.B) { ins, err := New(root, 0, DefaultBit) test.Nil(err) test.Nil(ins.Delete(false)) w := qio.NewWriter(ins, 0) buf := qrand.RandBytes(benchmarkSize) for i := 0; i < n; i++ { n, err := w.Write(buf) test.Nil(err) if b != nil { b.SetBytes(int64(n)) } } }
func BenchmarkBlockRead(b *testing.B) { defer test.New(b) writeN(b.N, nil) b.ResetTimer() ins, err := New(root, 0, DefaultBit) test.Nil(err) w := qio.NewReader(ins, 0) buf := make([]byte, benchmarkSize) for i := 0; i < b.N; i++ { n, err := w.Read(buf) test.Nil(err) b.SetBytes(int64(n)) } }
func TestByteOrder(t *testing.T) { defer test.New(t) s := new(Superblock) currentByteOrder := s.GetByteOrder() wrongByteOrder := LittleEndian if wrongByteOrder == currentByteOrder { wrongByteOrder = BigEndian } b := make([]byte, s.Size()) currentByteOrder.Binary().PutUint64(b, uint64(currentByteOrder)) currentByteOrder.Binary().PutUint64(b[8:], 1) test.Nil(s.Decode(b)) test.Equal(s.Decode(b[:16]), ErrSbInvalid) test.Equal(s.Decode(b[:15]), ErrSbInvalidLen) binary.LittleEndian.PutUint64(b[8:], 0) test.Equal(s.Decode(b), ErrSbInvalidVersion) binary.LittleEndian.PutUint64(b, uint64(wrongByteOrder)) test.Equal(s.Decode(b), ErrByteOrderNotEqual) LittleEndian.String() BigEndian.String() ByteOrder(-1).String() wrongByteOrder.Binary() }
func TestVolume(t *testing.T) { defer test.New(t) v, err := NewVolume("hello") test.Nil(err) v.Open("hello") _, err = NewVolume("!hello") test.Equal(err, ErrInvalidVolmueName) }
func TestReaderWriter(t *testing.T) { defer test.New(t) var n int f, err := os.Create(root) test.Nil(err) w := NewWriter(f, 1) offset, _ := w.Seek(-1, 1) test.NotEqual(offset, 0) _, err = w.Write([]byte("caoo")) test.Nil(err) defer w.Close() // |caoo _, err = w.Seek(0, 2) test.Equal(err, ErrSeekNotSupport) _, err = w.Seek(1, 0) test.Nil(err) n, err = w.Write([]byte("j")) test.Equals(err, nil, n, 1) // cj|oo r := NewReader(f, 0) defer r.Close() _, err = r.Seek(1, 1) test.Nil(err) // c|joo _, err = r.Seek(0, 2) test.Equal(err, ErrSeekNotSupport) buf := make([]byte, 3) n, err = r.Read(buf) test.Equals(n, 3, err, nil, buf, []byte("joo")) // cjoo| _, err = r.Seek(0, 0) test.Nil(err) }
func TestBlock(t *testing.T) { defer test.New(t) f, err := New(root, 0, 2) test.Nil(err) test.Nil(f.Delete(false)) defer f.Close() buf := bytes.Repeat([]byte("ha"), 1024) n, err := f.WriteAt(buf, 1) test.Nil(err) test.Equal(n, len(buf)) buf2 := make([]byte, len(buf)) n, err = f.ReadAt(buf2, 0) test.Nil(err) test.Equal(n, len(buf2)) test.Equal(buf[:len(buf)-1], buf2[1:]) n, err = f.ReadAt([]byte(" "), 1024*2+1) test.Equals(n, 0, err, io.EOF) n, err = f.ReadAt([]byte(" "), 0) test.Equals(n, 2, err, nil) n, err = f.ReadAt([]byte(" "), 0) test.Equals(n, 2, err, nil) os.RemoveAll(root) n, err = f.ReadAt([]byte(" "), 4) test.Equals(n, 0) test.CheckError(err, test.StrNotSuchFile) n, err = f.WriteAt([]byte(" "), 4) test.Equals(n, 0) test.CheckError(err, test.StrNotSuchFile) }
func TestEncodeDecode(t *testing.T) { defer test.New(t) s := new(Superblock) s.SuperblockV1 = new(SuperblockV1) s.Version = 1 s.CheckPoint = 2 s.BlockBit = 3 s.IndirectAddr[0] = int64(qrand.RandInt(1024)) s.IndirectAddr[BlockSize-1] = 5 target := s.Encode() s2 := new(Superblock) test.Nil(s2.Decode(target)) test.Equal(s.SuperblockV1, s2.SuperblockV1) test.Equal(s.IndirectAddr[0], s2.IndirectAddr[0]) }
func TestBlockWriteRead(t *testing.T) { defer test.New(t) f, err := New(root, 0, 4) test.Nil(err) defer f.Close() buf := []byte("abcdefgh") for i := 0; i < 1024; i += 8 { n, err := f.WriteAt(buf, int64(i)) test.Equals(n, 8, err, nil) } buf2 := make([]byte, len(buf)) for i := 0; i < 1024; { n, err := f.ReadAt(buf2, int64(i)) test.Equals(n, len(buf2), buf2, buf, err, nil) i += n } }