-
Notifications
You must be signed in to change notification settings - Fork 3
/
sparsefile_common_test.go
91 lines (72 loc) · 1.56 KB
/
sparsefile_common_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package spgz
import (
"io"
"testing"
)
type memSparseFileNoSupport struct {
memSparseFile
}
func (s *memSparseFileNoSupport) PunchHole(offset, size int64) error {
return ErrPunchHoleNotSupported
}
func expectRange(buf []byte, offset, size int, value byte, t *testing.T) {
for i := offset; i < offset+size; i++ {
if buf[i] != value {
t.Fatalf("Invalid byte at %d: %d", i, buf[i])
}
}
}
func TestSparseWriterNoHoles(t *testing.T) {
f := &memSparseFileNoSupport{}
_, err := f.Write([]byte("XXXXXXXX"))
if err != nil {
t.Fatal(err)
}
_, err = f.Seek(4, io.SeekStart)
if err != nil {
t.Fatal(err)
}
_, err = f.Write(make([]byte, 8))
if err != nil {
t.Fatal(err)
}
_, err = f.Seek(0, io.SeekStart)
if err != nil {
t.Fatal(err)
}
var b [12]byte
_, err = io.ReadFull(f, b[:])
if err != nil {
t.Fatal(err)
}
if s := string(b[:]); s != "XXXX\x00\x00\x00\x00\x00\x00\x00\x00" {
t.Fatalf("Unexpected value: '%s'", s)
}
off, err := f.Seek(0, io.SeekEnd)
if err != nil {
t.Fatal(err)
}
if off != 12 {
t.Fatalf("Unexpected file size: %d", off)
}
}
func TestNewSparseFileWithFallback(t *testing.T) {
f := &memSparseFileNoSupport{}
sf := &SparseFileWithFallback{SparseFile: f}
buf := make([]byte, 128*1024)
for i := range buf {
buf[i] = 'x'
}
_, err := sf.Write(buf)
if err != nil {
t.Fatal(err)
}
err = sf.PunchHole(3333, 50*1024)
if err != nil {
t.Fatal(err)
}
_ = sf.Close()
expectRange(f.data, 0, 3333, 'x', t)
expectRange(f.data, 3333, 50*1024, 0, t)
expectRange(f.data, 3333+50*1024, 128*1024-(3333+50*1024), 'x', t)
}