-
Notifications
You must be signed in to change notification settings - Fork 0
/
bplustree_test.go
159 lines (123 loc) · 4.46 KB
/
bplustree_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
package main
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestIsShapedCorrectlyAfter2(t *testing.T) {
tree := NewBPlusTree(3)
keys := []int{1, 2}
for _, key := range keys {
tree.Insert(key, fmt.Sprintf("Node #%d", key))
}
assert.True(t, len(tree.root.keys) == 2, "root has one key")
assert.True(t, tree.root.keys[0] == 1, "root key is 1")
assert.True(t, tree.root.keys[1] == 2, "root key is 1")
assert.True(t, len(tree.root.children) == 0, "root has no children")
}
func TestIsShapedCorrectlyAfter3(t *testing.T) {
tree := NewBPlusTree(3)
keys := []int{1, 2, 3}
for _, key := range keys {
tree.Insert(key, fmt.Sprintf("Node #%d", key))
}
assert.True(t, len(tree.root.keys) == 1, "root has one key")
assert.True(t, tree.root.keys[0] == 2, "root key is 2")
assert.Equal(t, []int{1}, tree.root.children[0].keys, "1 is the left most child")
assert.Equal(t, []int{2, 3}, tree.root.children[1].keys, "2,3 is the right most children")
}
func TestIsShapedCorrectlyAfter4(t *testing.T) {
tree := NewBPlusTree(3)
keys := []int{1, 2, 3, 4}
for _, key := range keys {
tree.Insert(key, fmt.Sprintf("Node #%d", key))
}
assert.True(t, len(tree.root.keys) == 2, "root has two keys")
assert.True(t, len(tree.root.children) == 3, "root has three children")
assert.Equal(t, []int{2, 3}, tree.root.keys)
assert.Equal(t, []int{1}, tree.root.children[0].keys)
assert.Equal(t, []int{2}, tree.root.children[1].keys)
assert.Equal(t, []int{3, 4}, tree.root.children[2].keys)
}
func TestIsShapedCorrectlyAfter5(t *testing.T) {
tree := NewBPlusTree(3)
keys := []int{1, 2, 3, 4, 5}
for _, key := range keys {
tree.Insert(key, fmt.Sprintf("Node #%d", key))
}
// root
assert.Equal(t, []int{3}, tree.root.keys)
// 2nd layer
assert.Equal(t, []int{2}, tree.root.children[0].keys) // right
assert.Equal(t, []int{4}, tree.root.children[1].keys) // left
// 3rd layer
// left
assert.Equal(t, []int{1}, tree.root.children[0].children[0].keys)
assert.Equal(t, []int{2}, tree.root.children[0].children[1].keys)
// right
assert.Equal(t, []int{3}, tree.root.children[1].children[0].keys)
assert.Equal(t, []int{4, 5}, tree.root.children[1].children[1].keys)
}
func TestIsShapedCorrectlyAfter6(t *testing.T) {
tree := NewBPlusTree(3)
keys := []int{1, 2, 3, 4, 5, 6}
for _, key := range keys {
tree.Insert(key, fmt.Sprintf("Node #%d", key))
}
// root
assert.Equal(t, []int{3}, tree.root.keys)
// 2nd layer
assert.Equal(t, []int{2}, tree.root.children[0].keys) // right
assert.Equal(t, []int{4, 5}, tree.root.children[1].keys) // left
// 3rd layer
// left
assert.Equal(t, []int{1}, tree.root.children[0].children[0].keys)
assert.Equal(t, []int{2}, tree.root.children[0].children[1].keys)
// right
assert.Equal(t, []int{3}, tree.root.children[1].children[0].keys)
assert.Equal(t, []int{4}, tree.root.children[1].children[1].keys)
assert.Equal(t, []int{5, 6}, tree.root.children[1].children[2].keys)
}
func TestIsShapedCorrectlyAfter7(t *testing.T) {
tree := NewBPlusTree(3)
keys := []int{1, 2, 3, 4, 5, 6, 7}
for _, key := range keys {
tree.Insert(key, fmt.Sprintf("Node #%d", key))
}
// root
assert.Equal(t, []int{3, 5}, tree.root.keys)
// 2nd layer
assert.Equal(t, []int{2}, tree.root.children[0].keys) // right
assert.Equal(t, []int{4}, tree.root.children[1].keys) // middle
assert.Equal(t, []int{6}, tree.root.children[2].keys) // left
// 3rd layer
// left
assert.Equal(t, []int{1}, tree.root.children[0].children[0].keys)
assert.Equal(t, []int{2}, tree.root.children[0].children[1].keys)
// middle
assert.Equal(t, []int{3}, tree.root.children[1].children[0].keys)
assert.Equal(t, []int{4}, tree.root.children[1].children[1].keys)
// right
assert.Equal(t, []int{5}, tree.root.children[2].children[0].keys)
assert.Equal(t, []int{6, 7}, tree.root.children[2].children[1].keys)
}
func TestCanHandleOneHundredNodes(t *testing.T) {
tree := NewBPlusTree(3)
for i := 0; i < 100; i++ {
tree.Insert(i, fmt.Sprintf("Node #%d", i))
}
}
// func TestCanFindLeaf(t *testing.T) {
// n1 := &BPlusTreeNode{keys: []int{1, 2, 3}, isLeaf: true}
// n2 := &BPlusTreeNode{keys: []int{4, 5, 6}, isLeaf: true}
// n3 := &BPlusTreeNode{keys: []int{7, 8, 9}, isLeaf: true}
// n4 := &BPlusTreeNode{keys: []int{10, 11, 12}, isLeaf: true}
// root := &BPlusTreeNode{
// keys: []int{4, 7},
// children: []*BPlusTreeNode{n1, n2, n3},
// isLeaf: false,
// }
// root.next = n4 // example of linking nodes sequentially
// tree := NewBPlusTree(3)
// tree.Print()
// }