-
Notifications
You must be signed in to change notification settings - Fork 0
/
dir_test.go
144 lines (129 loc) · 4.26 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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package bakemono
import (
"math/rand"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDir_MarshalUnmarshalBinary(t *testing.T) {
dir := Dir{}
r := rand.New(rand.NewSource(42))
expectedOffset := uint64(r.Uint32())
expectedBig := uint8(r.Intn(4))
expectedSize := uint8(r.Intn(64))
expectedTag := uint16(r.Intn(4096))
expectedPhase := r.Intn(2) == 1
expectedHead := r.Intn(2) == 1
expectedPinned := r.Intn(2) == 1
expectedNext := uint16(r.Uint32())
expectedApproxSize := uint64(r.Intn(DirMaxDataSize))
//expectedPrev := uint16(r.Uint32())
expectedToken := r.Intn(2) == 1
dir.setOffset(expectedOffset)
dir.setApproxSize(expectedApproxSize)
//dir.setPrev(expectedPrev)
dir.setBigInternal(expectedBig)
dir.setSizeInternal(expectedSize)
dir.setTag(expectedTag)
dir.setPhase(expectedPhase)
dir.setHead(expectedHead)
dir.setPinned(expectedPinned)
dir.setToken(expectedToken)
dir.setNext(expectedNext)
buf, err := dir.MarshalBinary()
if err != nil {
t.Fatal(err)
}
dir2 := Dir{}
err = dir2.UnmarshalBinary(buf)
if err != nil {
t.Fatal(err)
}
if dir2.offset() != expectedOffset {
t.Fatalf("expected offset %d, got %d", expectedOffset, dir2.offset())
}
if dir2.approxSize() != expectedApproxSize {
big := dir2.bigInternal()
size := dir2.sizeInternal()
expectedApproxSize = (SectorSize << (big * 3)) * uint64(size+1)
if dir2.approxSize() != expectedApproxSize {
t.Fatalf("expected approxSize %d, got %d", expectedApproxSize, dir2.approxSize())
}
}
//if dir2.prev() != expectedPrev {
// t.Fatalf("expected prev %d, got %d", expectedPrev, dir2.prev())
//}
if dir2.bigInternal() != expectedBig {
t.Fatalf("expected bigInternal %d, got %d", expectedBig, dir2.bigInternal())
}
if dir2.sizeInternal() != expectedSize {
t.Fatalf("expected sizeInternal %d, got %d", expectedSize, dir2.sizeInternal())
}
if dir2.tag() != expectedTag {
t.Fatalf("expected tag %d, got %d", expectedTag, dir2.tag())
}
if dir2.phase() != expectedPhase {
t.Fatalf("expected phase %v, got %v", expectedPhase, dir2.phase())
}
if dir2.head() != expectedHead {
t.Fatalf("expected head %v, got %v", expectedHead, dir2.head())
}
if dir2.pinned() != expectedPinned {
t.Fatalf("expected pinned %v, got %v", expectedPinned, dir2.pinned())
}
if dir2.token() != expectedToken {
t.Fatalf("expected token %v, got %v", expectedToken, dir2.token())
}
if dir2.next() != expectedNext {
t.Fatalf("expected next %d, got %d", expectedNext, dir2.next())
}
}
func TestDirSetGet(t *testing.T) {
for i := 0; i < 10; i++ {
testDirOnce(t)
}
}
func testDirOnce(t *testing.T) {
convey.Convey("Given a Dir", t, func() {
dir := Dir{}
convey.Convey("When setting various fields", func() {
r := rand.New(rand.NewSource(42))
expectedOffset := uint64(r.Uint32())
expectedBig := uint8(r.Intn(4))
expectedSize := uint8(r.Intn(64))
expectedTag := uint16(r.Intn(4096))
expectedPhase := r.Intn(2) == 1
expectedHead := r.Intn(2) == 1
expectedPinned := r.Intn(2) == 1
expectedToken := r.Intn(2) == 1
setters := []func(){
func() { dir.setOffset(expectedOffset) },
func() { dir.setBigInternal(expectedBig) },
func() { dir.setSizeInternal(expectedSize) },
func() { dir.setTag(expectedTag) },
func() { dir.setPhase(expectedPhase) },
func() { dir.setHead(expectedHead) },
func() { dir.setPinned(expectedPinned) },
func() { dir.setToken(expectedToken) },
}
// Randomly shuffle the order of the setter functions.
for i := range setters {
j := rand.Intn(i + 1)
setters[i], setters[j] = setters[j], setters[i]
}
// Apply the setter functions to the Dir instance in random order.
for _, f := range setters {
f()
}
convey.Convey("Then getting the same fields should return the expected values", func() {
convey.So(dir.offset(), convey.ShouldEqual, expectedOffset)
convey.So(dir.bigInternal(), convey.ShouldEqual, expectedBig)
convey.So(dir.sizeInternal(), convey.ShouldEqual, expectedSize)
convey.So(dir.tag(), convey.ShouldEqual, expectedTag)
convey.So(dir.phase(), convey.ShouldEqual, expectedPhase)
convey.So(dir.head(), convey.ShouldEqual, expectedHead)
convey.So(dir.pinned(), convey.ShouldEqual, expectedPinned)
convey.So(dir.token(), convey.ShouldEqual, expectedToken)
})
})
})
}