-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdir_test.go
57 lines (49 loc) · 1.5 KB
/
dir_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
package dir
import (
"testing"
"github.com/goose-lang/primitive/disk"
"github.com/stretchr/testify/assert"
)
func makeBlock(x byte) disk.Block {
b := make(disk.Block, disk.BlockSize)
b[0] = x
return b
}
func TestDirAppendRead(t *testing.T) {
assert := assert.New(t)
theDisk := disk.NewMemDisk(NumInodes + 100)
dir := Open(theDisk, theDisk.Size())
assert.Equal(uint64(0), dir.Size(1))
dir.Append(1, makeBlock(1))
dir.Append(1, makeBlock(2))
dir.Append(2, makeBlock(3))
assert.Equal(uint64(2), dir.Size(1))
assert.Equal(makeBlock(2), dir.Read(1, 1))
assert.Equal(makeBlock(3), dir.Read(2, 0))
assert.Equal(uint64(0), dir.Size(0))
}
func TestDirRecover(t *testing.T) {
assert := assert.New(t)
theDisk := disk.NewMemDisk(NumInodes + 3)
dir := Open(theDisk, theDisk.Size())
ok := dir.Append(1, makeBlock(1))
assert.True(ok, "append should succeed")
assert.True(dir.Append(1, makeBlock(2)),
"append should succeed")
dir = Open(theDisk, theDisk.Size())
assert.True(dir.Append(2, makeBlock(3)),
"append of last block should succeed")
assert.Equal(makeBlock(1), dir.Read(1, 0))
assert.Equal(makeBlock(2), dir.Read(1, 1))
assert.Equal(makeBlock(3), dir.Read(2, 0))
}
func TestDirRecoverFull(t *testing.T) {
assert := assert.New(t)
theDisk := disk.NewMemDisk(NumInodes + 2)
dir := Open(theDisk, theDisk.Size())
dir.Append(1, makeBlock(1))
dir.Append(1, makeBlock(2))
dir = Open(theDisk, theDisk.Size())
ok := dir.Append(2, makeBlock(3))
assert.False(ok, "should be no space to add more blocks")
}