-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfallocate_test.go
49 lines (43 loc) · 996 Bytes
/
fallocate_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
package fallocate
import (
"io/ioutil"
"os"
"testing"
)
func checkFileSize(f *os.File, size int64) bool {
fs, err := f.Stat()
if err != nil {
return false
}
return fs.Size() == size
}
func fallocateWithNewFile(t *testing.T, size int64) {
f, err := ioutil.TempFile("", "AllocateFileRange.*.txt")
if err != nil {
t.Error(err)
}
defer func() {
defer os.Remove(f.Name())
if err := f.Close(); err != nil {
t.Error(err)
}
}()
_ = Fallocate(f, 0, size)
if !checkFileSize(f, size) {
t.Errorf("Allocate file from %d to %d failed", 0, size)
}
_ = Fallocate(f, size, size)
if !checkFileSize(f, 2*size) {
t.Errorf("Allocate file from %d to %d failed", size, 2*size)
}
_ = Fallocate(f, 2*size-1, size)
if !checkFileSize(f, 2*size-1+size) {
t.Errorf("Allocate file from %d to %d failed", 2*size-1, 2*size-1+size)
}
}
func TestFallbackFallocate(t *testing.T) {
sizes := []int64{7, 3, 2, 1, 66666}
for _, size := range sizes {
fallocateWithNewFile(t, size)
}
}