-
Notifications
You must be signed in to change notification settings - Fork 6
/
preallocate_test.go
66 lines (50 loc) · 1.04 KB
/
preallocate_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
package ox_test
import (
"fmt"
"io"
"io/ioutil"
"os"
"testing"
"github.com/itchio/ox"
"github.com/stretchr/testify/assert"
)
func doTestPreallocate(t *testing.T) {
assert := assert.New(t)
f, err := ioutil.TempFile("", "")
must(err)
defer f.Close()
defer os.Remove(f.Name())
assertSize := func(expected int64) {
s, err := f.Stat()
must(err)
assert.Equal(expected, s.Size())
}
assertSize(0)
must(ox.Preallocate(f, 2048))
assertSize(2048)
_, err = f.Seek(0, io.SeekStart)
must(err)
_, err = f.Write([]byte("hello"))
must(err)
assertSize(2048)
must(ox.Preallocate(f, 4096))
assertSize(4096)
buf := make([]byte, 5)
n, err := f.ReadAt(buf, 0)
must(err)
assert.Equal(5, n)
assert.Equal("hello", string(buf))
}
func Test_Preallocate(t *testing.T) {
t.Logf("With fallocate...")
ox.SIMULATE_FALLOCATE_NOT_SUPPORTED = true
doTestPreallocate(t)
t.Logf("Without fallocate...")
ox.SIMULATE_FALLOCATE_NOT_SUPPORTED = false
doTestPreallocate(t)
}
func must(err error) {
if err != nil {
panic(fmt.Sprintf("%+v", err))
}
}