-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbplusTree_test.go
102 lines (95 loc) · 2.24 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
package BplusTree
import (
"fmt"
"math/rand"
"strconv"
"testing"
)
func makeInterfaceList(data interface{}) []interface{} {
var res []interface{}
switch data.(type) {
case []int:
for i := 0; i < len(data.([]int)); i++ {
res = append(res, data.([]int)[i])
}
}
return res
}
func TestBinarySearch(t *testing.T) {
type args struct {
keys []interface{}
key interface{}
}
tests := []struct {
name string
args args
want int
}{
{name: "left edge", args: args{keys: makeInterfaceList([]int{1, 2, 3, 4, 5, 6, 7}), key: 0}, want: -1},
{name: "right edge", args: args{keys: makeInterfaceList([]int{1, 2, 3, 4, 5, 6, 7}), key: 8}, want: -8},
{name: "middle ¬ found", args: args{keys: makeInterfaceList([]int{1, 2, 3, 8, 9, 10, 11}), key: 4}, want: -4},
{name: "find", args: args{keys: makeInterfaceList([]int{1, 2, 3, 8, 9, 10, 11}), key: 3}, want: 2},
}
BinarySearch := generateKeyBinarySearchFunc(nil, 0)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := BinarySearch(tt.args.keys, tt.args.key, 7); got != tt.want {
t.Errorf("BinarySearch() = %v, want %v", got, tt.want)
}
})
}
}
func Test_bPlusTree_Insert(t *testing.T) {
order := 5
type Goods struct {
price int
name string
}
m := make(map[int]bool)
tree := InitBPlusTree(order, nil, Goods{}.price)
for i := 0; i < 500; i++ {
num := rand.Int() % 1000
for {
if _, ok := m[num]; ok {
num = rand.Int() % 1000
} else {
m[num] = true
break
}
}
tree.Insert(num, &Goods{num, "test" + strconv.Itoa(num)})
}
for val, _ := range m {
if price := tree.Search(val).(*Goods).price; price != val {
t.Errorf("Search() = %v, want %v", price, val)
}
}
}
func Test_bPlusTree_Delete(t *testing.T) {
order := 5
type Goods struct {
price int
name string
}
m := make(map[int]bool)
tree := InitBPlusTree(order, nil, Goods{}.price)
for i := 0; i < 10; i++ {
num := rand.Int() % 1000
for {
if _, ok := m[num]; ok {
num = rand.Int() % 1000
} else {
m[num] = true
break
}
}
tree.Insert(num, &Goods{num, "test" + strconv.Itoa(num)})
}
tree.PrintSimply()
for val, _ := range m {
fmt.Println("delete: " + strconv.Itoa(val))
tree.Delete(val)
tree.PrintSimply()
fmt.Println("alfter delete ")
}
}