-
Notifications
You must be signed in to change notification settings - Fork 10
/
mmap_test.go
47 lines (40 loc) · 1.08 KB
/
mmap_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
package util
import (
"os"
"testing"
"syscall"
"gotest.tools/assert"
)
func TestMmap(t *testing.T) {
fileName := "/tmp/test_util.txt"
os.Remove(fileName)
f, err := os.OpenFile(fileName, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
assert.Assert(t, err == nil)
defer func() {
f.Close()
os.Remove(fileName)
}()
size := 64000
n, err := f.Write(make([]byte, size))
assert.Assert(t, n == size && err == nil)
bytes, err := Mmap(f, true, int64(size))
assert.Assert(t, err == nil && len(bytes) == size)
bytes[0] = 1
assert.Assert(t, MSync(bytes, 1, syscall.MS_SYNC) == nil)
assert.Assert(t, MSync(bytes, 1, syscall.MS_SYNC) == nil) // call twice is ok
assert.Assert(t, MLock(bytes, 1) == nil)
assert.Assert(t, MUnlock(bytes, 1) == nil)
err = Madvise(bytes, true)
assert.Assert(t, err == nil)
err = Madvise(bytes, false)
assert.Assert(t, err == nil)
err = Munmap(bytes)
assert.Assert(t, err == nil)
err = Madvise(bytes, false)
assert.Assert(t, err != nil)
err = Munmap(bytes)
assert.Assert(t, err != nil)
bytes = make([]byte, 10)
err = Munmap(bytes)
assert.Assert(t, err != nil)
}