forked from rjkroege/edwood
-
Notifications
You must be signed in to change notification settings - Fork 0
/
disk_test.go
163 lines (136 loc) · 4.24 KB
/
disk_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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"bytes"
"strings"
"testing"
)
func TestNewBlock(t *testing.T) {
disk := NewDisk()
defer disk.Close()
if len(disk.free) != 33 {
t.Errorf("disk.free isn't big enough or I don't understand the code anymore.")
}
b := disk.NewBlock(255)
if got, want := b.addr, uint(0); got != want {
t.Errorf("got b.addr %d, want %d", got, want)
}
if got, want := b.n, uint(255); got != want {
t.Errorf("got b.n %d, want %d", got, want)
}
if got, want := disk.addr, uint(256); got != want {
t.Errorf("got disk.addr %d, want %d", got, want)
}
if b == disk.blist {
t.Errorf("b should not be at the head of the pre-allocated list.")
}
if b.next != disk.blist {
// This property exists because we don't have union types.
// TODO(rjk): Fragile under a more idiomatic implementation.
t.Errorf("b.next should point at the pre-allocated list.")
}
disk.Release(b)
if b != disk.free[1] {
t.Errorf("b didn't get put in the first element in appropriate free list")
}
b2 := disk.NewBlock(251)
if b2 != b {
t.Errorf("we failed to recycle b to b2")
}
if got, want := b2.n, uint(251); got != want {
t.Errorf("got b2.n %d, want %d", got, want)
}
}
// writereadtestcore provides the core write a rune array, read it back and compare for
// equality.
func writereadtestcore(t *testing.T, testname, inputstring string, oblock *Block, disk *Disk) *Block {
inputrunes := bytes.Runes([]byte(inputstring))
inputlen := len(inputrunes)
nblock := oblock
disk.Write(&nblock, inputrunes, uint(inputlen))
// In this case, we are not changing the length.
outputrunes := make([]rune, inputlen)
disk.Read(nblock, outputrunes, uint(inputlen))
var b strings.Builder
for _, r := range outputrunes {
b.WriteRune(r)
}
if got, want := b.String(), inputstring; got != want {
t.Errorf("%s got %s, want %s", testname, got, want)
}
return nblock
}
func TestReadWriteSmall(t *testing.T) {
disk := NewDisk()
defer disk.Close()
oblock := disk.NewBlock(uint(4))
nblock := writereadtestcore(t, "small write-read test", "a日本b", oblock, disk)
if oblock != nblock {
t.Errorf("without resizing, nblock should equal oblock")
}
}
func TestReadWriteBig(t *testing.T) {
disk := NewDisk()
defer disk.Close()
// Roundtrip a bigger unicode string
// Make a larger string.
var b strings.Builder
for i := 0; i < 100; i++ {
b.WriteString("a日本b")
}
bigstring := b.String()
originalLargeBlk := disk.NewBlock(uint(4 * 100))
newLargeBlk := writereadtestcore(t, "big write-read test", bigstring, originalLargeBlk, disk)
if originalLargeBlk != newLargeBlk {
t.Errorf("without resizing, newLargeBlk should equal originalLargeBlk")
}
// Resize it with a little string.
newSmallBlk := writereadtestcore(t, "small size-changing write-read test", "c日本d", newLargeBlk, disk)
if newSmallBlk == originalLargeBlk {
t.Errorf("with resizing, newSmallBlk should not equal originalLargeBlk")
}
if originalLargeBlk != disk.free[2] {
t.Errorf("with resizing, originalLargeBlk should be in the free-bucket for re-use")
}
if originalLargeBlk.next != nil {
t.Errorf("Release failed to make originalLargeBlk.next point to nothing")
}
// Resize with a big string, make sure that we reuse the block
b.Reset()
for i := 0; i < 98; i++ {
b.WriteString("eö本f")
}
bigstring = b.String()
differentLargeBlk := writereadtestcore(t, "small to large size-changing write-read test", bigstring, newSmallBlk, disk)
if newSmallBlk == differentLargeBlk {
t.Errorf("with resizing, from small to large, differentLargeBlk should not equal newSmallBlk")
}
if differentLargeBlk != originalLargeBlk {
t.Errorf("with resizing to a previously-used large block, should have reused originalLargeBlk")
}
if disk.free[2] != nil {
t.Errorf("reusing block from bucket should have removed the block")
}
}
func TestNtosize(t *testing.T) {
testvector := []struct {
n uint
sz uint
ip uint
}{
{MaxBlock, MaxBlock, 32},
{255, 256, 1},
{1, 256, 1},
{256, 256, 1},
{257, 512, 2},
{0, 0, 0},
}
for _, tv := range testvector {
sz, ip := ntosize(tv.n)
if got, want := sz, tv.sz; got != want {
t.Errorf("for %d, got sz %d, want sz %d", tv.n, sz, tv.sz)
}
if got, want := ip, tv.ip; got != want {
t.Errorf("for %d, got ip %d, want ip %d", tv.n, ip, tv.ip)
}
}
}