-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnode_test.go
272 lines (258 loc) · 6.07 KB
/
node_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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package btree
import (
"testing"
)
func TestNode_findRecord(t *testing.T) {
leafNode := makeTestLeafNode([]string{"Mozart", "Singh", "Srinivasan", "Wu"},
[]string{"Mozart_value", "Singh_value", "Srinivasan_value", "Wu_value"})
v, ok := leafNode.findRecord(key("Singh"))
if !ok {
t.Fatalf("should find Singh_value")
}
value, ok := v.(*entry)
if !ok {
t.Fatalf("shuold be *entry")
}
if value.toValue() != "Singh_value" {
t.Fatalf("find Singh_value, but not correct")
}
_, ok = leafNode.findRecord(key("trump"))
if ok {
t.Fatalf("shuold be *entry")
}
}
func TestNode_updateRecord(t *testing.T) {
leafNode := makeTestLeafNode([]string{"key1", "key2", "key4", "key5"},
[]string{"key1_value", "key2_value", "key4_value", "key5_value"})
targetKey := key("key2")
et := &entry{
value: "key2222_value",
}
leafNode.updateRecord(targetKey, et)
r, ok := leafNode.pointers[1].(*entry)
if !ok {
t.Fatalf("should be entry")
}
if r.toValue() != "key2222_value" {
t.Fatalf("should be key2222_value, actully value: %v", r.toValue())
}
t.Logf("keys: %v", leafNode.keys)
}
func TestNode_lookupSiblingOne(t *testing.T) {
parentNode := &node{
isLeaf: false,
keys: make([]key, 0, 3),
pointers: make([]interface{}, 0, 3),
maxSize: 3,
}
parentNode.keys = append(parentNode.keys, "c")
leafNode1 := &node{
isLeaf: true,
keys: []key{"a", "b"},
pointers: []interface{}{entry{}, entry{}},
}
leafNode2 := &node{
isLeaf: true,
keys: []key{"c", "d"},
pointers: []interface{}{entry{}, entry{}},
}
leafNode1.lastOrNextNode = leafNode2
leafNode1.parent = parentNode
leafNode2.parent = parentNode
parentNode.pointers = append(parentNode.pointers, leafNode1)
parentNode.lastOrNextNode = leafNode2
sibling, index, ky, isPrev := leafNode1.lookupSibling()
if sibling != leafNode2 {
t.Fatal("should be leafNode2")
}
if index != 0 {
t.Fatal("should be 0")
}
if ky.toString() != "c" {
t.Fatal("should be c")
}
if isPrev {
t.Fatal("should be not prev")
}
sibling, index, ky, isPrev = leafNode2.lookupSibling()
if sibling != leafNode1 {
t.Fatal("should be leafNode1")
}
if index != 0 {
t.Fatal("should be 0")
}
if ky.toString() != "c" {
t.Fatal("should be c")
}
if !isPrev {
t.Fatal("should be prev")
}
}
func TestNode_lookupSiblingTwo(t *testing.T) {
parentNode := &node{
isLeaf: false,
keys: make([]key, 0, 4),
pointers: make([]interface{}, 0, 4),
maxSize: 4,
}
parentNode.keys = append(parentNode.keys, "c", "e", "d")
leafNode1 := &node{
isLeaf: true,
keys: []key{"a", "b"},
pointers: []interface{}{entry{}, entry{}},
}
leafNode2 := &node{
isLeaf: true,
keys: []key{"c", "d"},
pointers: []interface{}{entry{}, entry{}},
}
leafNode3 := &node{
isLeaf: true,
keys: []key{"e", "f", "g"},
pointers: []interface{}{entry{}, entry{}, entry{}},
}
leafNode4 := &node{
isLeaf: true,
keys: []key{"e", "f", "g"},
pointers: []interface{}{entry{}, entry{}, entry{}},
}
leafNode1.lastOrNextNode = leafNode2
leafNode2.lastOrNextNode = leafNode3
leafNode3.lastOrNextNode = leafNode4
leafNode1.parent = parentNode
leafNode2.parent = parentNode
leafNode3.parent = parentNode
leafNode4.parent = parentNode
parentNode.pointers = append(parentNode.pointers, leafNode1)
parentNode.pointers = append(parentNode.pointers, leafNode2)
parentNode.pointers = append(parentNode.pointers, leafNode3)
parentNode.lastOrNextNode = leafNode4
sibling, index, ky, isPrev := leafNode1.lookupSibling()
if sibling != leafNode2 {
t.Fatal("should be leafNode2")
}
if index != 0 {
t.Fatal("should be 0")
}
if ky.toString() != "c" {
t.Fatal("should be c")
}
if isPrev {
t.Fatal("should be not prev")
}
sibling, index, ky, isPrev = leafNode2.lookupSibling()
if sibling != leafNode1 {
t.Fatal("should be leafNode1")
}
if index != 0 {
t.Fatal("should be 0")
}
if ky.toString() != "c" {
t.Fatal("should be e")
}
if !isPrev {
t.Fatal("should be prev")
}
sibling, index, ky, isPrev = leafNode3.lookupSibling()
if sibling != leafNode4 {
t.Fatal("should be leafNode4")
}
if index != 2 {
t.Fatal("should be 2")
}
if ky.toString() != "d" {
t.Fatal("should be e")
}
if isPrev {
t.Fatal("should be prev")
}
sibling, index, ky, isPrev = leafNode4.lookupSibling()
if sibling != leafNode3 {
t.Fatal("should be leafNode3")
}
if index != 2 {
t.Fatal("should be 2")
}
if ky.toString() != "d" {
t.Fatal("should be d")
}
if !isPrev {
t.Fatal("should be prev")
}
}
func TestNode_deleteLeaf(t *testing.T) {
keys := []key{key("a"), key("b"), key("c"), key("d")}
next := new(node)
a := &entry{}
pointers := []interface{}{a, &entry{}, &entry{}, &entry{}}
nd := &node{
isLeaf: true,
keys: keys,
pointers: pointers,
maxSize: 4,
lastOrNextNode: next,
}
nd.delete(key("a"), a)
for i, k := range nd.keys {
if i == 0 && k.toString() == "b" {
continue
}
if i == 1 && k.toString() == "c" {
continue
}
if i == 2 && k.toString() == "d" {
continue
}
t.Fatalf("node delete wrong, node keys:%v\n", nd.keys)
}
if len(nd.pointers) != 3 {
t.Fatalf("node delete wrong, node pointers:%v\n", nd.pointers)
}
}
func TestNode_deleteInternal(t *testing.T) {
keys := []key{key("a"), key("b"), key("c"), key("d")}
next := new(node)
a := &node{}
b := &node{}
c := &node{}
d := &node{}
pointers := []interface{}{a, b, c, d}
nd := &node{
isLeaf: false,
keys: keys,
pointers: pointers,
maxSize: 4,
lastOrNextNode: next,
}
nd.delete(key("d"), next)
for i, k := range nd.keys {
if i == 0 && k.toString() == "a" {
continue
}
if i == 1 && k.toString() == "b" {
continue
}
if i == 2 && k.toString() == "c" {
continue
}
t.Fatalf("node delete wrong, node keys:%v\n", nd.keys)
}
for i, k := range nd.pointers {
if i == 0 && k == a {
continue
}
if i == 1 && k == b {
continue
}
if i == 2 && k == c {
continue
}
if i == 3 && k == d {
continue
}
t.Fatalf("node delete wrong, node pointer:%v\n", nd.pointers)
}
if nd.lastOrNextNode != d {
t.Fatal("node delete wrong lastOrNextNode")
}
}