-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathbtree_test.go
121 lines (115 loc) · 2.83 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
package btree_test
import (
"../btree"
"strconv"
"testing"
)
func TestInsert(t *testing.T) {
tree := btree.NewBtreeSize(2, 2)
size := 100
for i := 0; i < size; i++ {
if err := tree.Insert([]byte(strconv.Itoa(i)), []byte(strconv.Itoa(i))); err != nil {
t.Fatal("Insert Failed", i, err)
}
}
}
func TestSearch(t *testing.T) {
tree := btree.NewBtreeSize(2, 3)
size := 100
for i := 0; i < size; i++ {
tree.Insert([]byte(strconv.Itoa(i)), []byte(strconv.Itoa(i)))
}
for i := 0; i < size; i++ {
rst, err := tree.Search([]byte(strconv.Itoa(i)))
if string(rst) != strconv.Itoa(i) {
t.Fatal("Find Failed", i, err)
}
}
}
func TestUpdate(t *testing.T) {
tree := btree.NewBtreeSize(3, 2)
size := 100
for i := 0; i < size; i++ {
tree.Insert([]byte(strconv.Itoa(i)), []byte(strconv.Itoa(i)))
}
for i := 0; i < size; i++ {
if err := tree.Update([]byte(strconv.Itoa(i)), []byte(strconv.Itoa(i+1))); err != nil {
t.Fatal("Update Failed", i, err)
}
}
for i := 0; i < size; i++ {
rst, _ := tree.Search([]byte(strconv.Itoa(i)))
if string(rst) != strconv.Itoa(i+1) {
t.Fatal("Find Failed", i)
}
}
}
func TestDelete(t *testing.T) {
tree := btree.NewBtreeSize(3, 3)
size := 8
for i := 0; i < size; i++ {
tree.Insert([]byte(strconv.Itoa(i)), []byte(strconv.Itoa(i)))
}
for i := 0; i < size; i++ {
if err := tree.Delete([]byte(strconv.Itoa(i))); err != nil {
t.Fatal("delete Failed", i)
}
if _, err := tree.Search([]byte(strconv.Itoa(i))); err == nil {
t.Fatal("Find Failed", i)
}
}
}
func BenchmarkInsert(t *testing.B) {
size := 100000
tree := btree.NewBtreeSize(16, 16)
for i := 0; i < size; i++ {
tree.Insert([]byte(strconv.Itoa(i)), []byte(strconv.Itoa(i)))
}
tree.Marshal("treedump")
}
func BenchmarkSearch(t *testing.B) {
tree, err := btree.Unmarshal("treedump")
if err != nil {
t.Fatal("Unmarshal Failed")
}
size := 100000
for i := 0; i < size; i++ {
rst, err := tree.Search([]byte(strconv.Itoa(i)))
if string(rst) != strconv.Itoa(i) {
t.Fatal("Find Failed", i, err)
}
}
}
func BenchmarkUpdate(t *testing.B) {
tree, err := btree.Unmarshal("treedump")
if err != nil {
t.Fatal("Unmarshal Failed")
}
size := 100000
for i := 0; i < size; i++ {
if err := tree.Update([]byte(strconv.Itoa(i)), []byte(strconv.Itoa(i+1))); err != nil {
t.Fatal("Update Failed", i, err)
}
}
for i := 0; i < size; i++ {
rst, _ := tree.Search([]byte(strconv.Itoa(i)))
if string(rst) != strconv.Itoa(i+1) {
t.Fatal("Find Failed", i)
}
}
}
func BenchmarkDelete(t *testing.B) {
tree, err := btree.Unmarshal("treedump")
if err != nil {
t.Fatal("Unmarshal Failed")
}
size := 100000
for i := 0; i < size; i++ {
if err := tree.Delete([]byte(strconv.Itoa(i))); err != nil {
t.Fatal("delete Failed", i)
}
if _, err := tree.Search([]byte(strconv.Itoa(i))); err == nil {
t.Fatal("Find Failed", i)
}
}
}