-
Notifications
You must be signed in to change notification settings - Fork 29
/
tree_test.go
148 lines (120 loc) · 2.88 KB
/
tree_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
package bplustree
import (
"fmt"
"testing"
"time"
)
func TestInsert(t *testing.T) {
testCount := 1000000
bt := newBTree()
start := time.Now()
for i := testCount; i > 0; i-- {
bt.Insert(i, "")
}
fmt.Println(time.Now().Sub(start))
verifyTree(bt, testCount, t)
}
func TestSearch(t *testing.T) {
testCount := 1000000
bt := newBTree()
for i := testCount; i > 0; i-- {
bt.Insert(i, fmt.Sprintf("%d", i))
}
start := time.Now()
for i := 1; i < testCount; i++ {
v, ok := bt.Search(i)
if !ok {
t.Errorf("search: want = true, got = false")
}
if v != fmt.Sprintf("%d", i) {
t.Errorf("search: want = %d, got = %s", i, v)
}
}
fmt.Println(time.Now().Sub(start))
}
func verifyTree(b *BTree, count int, t *testing.T) {
verifyRoot(b, t)
for i := 0; i < b.root.count; i++ {
verifyNode(b.root.kcs[i].child, b.root, t)
}
leftMost := findLeftMost(b.root)
if leftMost != b.first {
t.Errorf("bt.first: want = %p, got = %p", b.first, leftMost)
}
verifyLeaf(leftMost, count, t)
}
// min child: 1
// max child: MaxKC
func verifyRoot(b *BTree, t *testing.T) {
if b.root.parent() != nil {
t.Errorf("root.parent: want = nil, got = %p", b.root.parent())
}
if b.root.count < 1 {
t.Errorf("root.min.child: want >=1, got = %d", b.root.count)
}
if b.root.count > MaxKC {
t.Errorf("root.max.child: want <= %d, got = %d", MaxKC, b.root.count)
}
}
func verifyNode(n node, parent *interiorNode, t *testing.T) {
switch nn := n.(type) {
case *interiorNode:
if nn.count < MaxKC/2 {
t.Errorf("interior.min.child: want >= %d, got = %d", MaxKC/2, nn.count)
}
if nn.count > MaxKC {
t.Errorf("interior.max.child: want <= %d, got = %d", MaxKC, nn.count)
}
if nn.parent() != parent {
t.Errorf("interior.parent: want = %p, got = %p", parent, nn.parent())
}
var last int
for i := 0; i < nn.count; i++ {
key := nn.kcs[i].key
if key != 0 && key < last {
t.Errorf("interior.sort.key: want > %d, got = %d", last, key)
}
last = key
verifyNode(nn.kcs[i].child, nn, t)
}
case *leafNode:
if nn.parent() != parent {
t.Errorf("leaf.parent: want = %p, got = %p", parent, nn.parent())
}
if nn.count < MaxKV/2 {
t.Errorf("leaf.min.child: want >= %d, got = %d", MaxKV/2, nn.count)
}
if nn.count > MaxKV {
t.Errorf("leaf.max.child: want <= %d, got = %d", MaxKV, nn.count)
}
}
}
func verifyLeaf(leftMost *leafNode, count int, t *testing.T) {
curr := leftMost
last := 0
c := 0
for curr != nil {
for i := 0; i < curr.count; i++ {
key := curr.kvs[i].key
if key <= last {
t.Errorf("leaf.sort.key: want > %d, got = %d", last, key)
}
last = key
c++
}
curr = curr.next
}
if c != count {
t.Errorf("leaf.count: want = %d, got = %d", count, c)
}
}
func findLeftMost(n node) *leafNode {
switch nn := n.(type) {
case *interiorNode:
return findLeftMost(nn.kcs[0].child)
case *leafNode:
return nn
default:
panic("")
}
}