-
Notifications
You must be signed in to change notification settings - Fork 85
/
btree_test.go
162 lines (148 loc) · 3.09 KB
/
btree_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
package zoekt
import (
"fmt"
"testing"
)
func TestBTree_sorted(t *testing.T) {
bt := newBtree(btreeOpts{bucketSize: 2, v: 2})
insertMany(t, bt, []ngram{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
// inner nodes only
//
// [3,5,7]
// / / \ \
// [2] [4] [6] [8,9]
//
want := "{bucketSize:2 v:2}[3,5,7][2][4][6][8,9]"
if s := bt.String(); s != want {
t.Fatalf("\nwant:%s\ngot: %s", want, s)
}
}
func TestFindBucket(t *testing.T) {
bt := newBtree(btreeOpts{bucketSize: 4, v: 2})
insertMany(t, bt, []ngram{1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
buckets := 0
offset := 0
bt.visit(func(no node) {
switch n := no.(type) {
case *leaf:
n.bucketIndex = buckets
buckets++
n.postingIndexOffset = offset
offset += n.bucketSize
case *innerNode:
return
}
})
cases := []struct {
ng ngram
wantBucketIndex int
wantPostingIndexOffset int
}{
{
ng: 7,
wantBucketIndex: 3,
wantPostingIndexOffset: 6,
},
}
for _, tt := range cases {
t.Run(fmt.Sprintf("ngram: %d", tt.ng), func(t *testing.T) {
haveBucketIndex, havePostingIndexOffset := bt.find(tt.ng)
if tt.wantBucketIndex != haveBucketIndex {
t.Fatalf("bucketIndex: want %d, got %d", tt.wantBucketIndex, haveBucketIndex)
}
if tt.wantPostingIndexOffset != havePostingIndexOffset {
t.Fatalf("postingIndexOffset: want %d, got %d", tt.wantPostingIndexOffset, havePostingIndexOffset)
}
})
}
}
func TestGetBucket(t *testing.T) {
var off uint32 = 13
bucketSize := 4
cases := []struct {
nNgrams int
bucketIndex int
wantOff uint32
wantSz uint32
}{
// tiny B-tree with just 1 bucket.
{
nNgrams: 1,
bucketIndex: 0,
wantOff: off,
wantSz: 8,
},
{
nNgrams: 2,
bucketIndex: 0,
wantOff: off,
wantSz: 16,
},
{
nNgrams: 3,
bucketIndex: 0,
wantOff: off,
wantSz: 24,
},
// B-tree with 10 ngrams, think 1,2,3,4,5,6,7,8,9,10
{
nNgrams: 10,
bucketIndex: 0,
wantOff: off,
wantSz: 16,
},
{
nNgrams: 10,
bucketIndex: 1,
wantOff: off + 16,
wantSz: 16,
},
{
nNgrams: 10,
bucketIndex: 2,
wantOff: off + 32,
wantSz: 16,
},
{
nNgrams: 10,
bucketIndex: 3,
wantOff: off + 48,
wantSz: 32,
},
{
nNgrams: 9,
bucketIndex: 3,
wantOff: off + 48,
wantSz: 24,
},
}
for _, tt := range cases {
t.Run("", func(t *testing.T) {
bi := btreeIndex{
ngramSec: simpleSection{off: off, sz: uint32(tt.nNgrams * ngramEncoding)},
}
bt := newBtree(btreeOpts{
bucketSize: bucketSize,
v: 2,
})
for i := 0; i < tt.nNgrams; i++ {
bt.insert(ngram(i + 1))
}
bt.freeze()
bi.bt = bt
off, sz := bi.getBucket(tt.bucketIndex)
if off != tt.wantOff {
t.Fatalf("off: want %d, got %d", tt.wantOff, off)
}
if sz != tt.wantSz {
t.Fatalf("sz: want %d, got %d", tt.wantSz, sz)
}
})
}
}
func insertMany(t *testing.T, bt *btree, ngrams []ngram) {
t.Helper()
for _, ng := range ngrams {
bt.insert(ng)
}
}