-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbtree_test.go
101 lines (87 loc) · 2.15 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
package btree
import "testing"
// Benchmarks adding to an unbalanced BTree. (max depth 64)
// The result is the average of Add() operations in indexes [0:63]
func BenchmarkAddU(b *testing.B) {
b.StopTimer()
tree := new(BTree)
for i := 0; i < 64; i++ {
tree.Add(i)
}
b.StartTimer()
for i := 0; i < b.N/64; i++ {
for v := 0; v < 64; v++ {
tree.Add(v)
}
}
}
// Benchmarks adding to a balanced BTree. (max depth 6)
// The result is the average of Add() operations in indexes [0:63]
func BenchmarkAddB(b *testing.B) {
b.StopTimer()
tree := new(BTree)
for i := 0; i < 64; i++ {
tree.Add(i)
}
tree = tree.Balance()
b.StartTimer()
for i := 0; i < b.N/64; i++ {
for v := 0; v < 64; v++ {
tree.Add(v)
}
}
}
// Benchmarks cached length calculations.
func BenchmarkLenC(b *testing.B) {
b.StopTimer()
tree := new(BTree)
for i := 0; i < 64; i++ {
tree.Add(i)
}
tree.Length()
b.StartTimer()
for i := 0; i < b.N; i++ {
tree.Length()
}
}
// Benchmarks explicitly uncached length calculations.
func BenchmarkLenU(b *testing.B) {
b.StopTimer()
tree := new(BTree)
for i := 0; i < 64; i++ {
tree.Add(i)
}
b.StartTimer()
for i := 0; i < b.N; i++ {
tree.uncachedLength()
}
}
func TestAddRemove(t *testing.T) {
t.Logf("Creating new (empty) BTree")
tree := new(BTree)
if have, want := tree.Length(), 0; have != want {
t.Errorf("Length() returned %d but %d was expected.", have, want)
}
vals := []byte{5, 6, 8, 2}
for i := range vals {
t.Logf("Adding value: %d", vals[i])
tree.Add(vals[i])
if have, want := tree.Length(), 1+i; have != want {
t.Errorf("Length() returned %d but %d was expected.", have, want)
}
if have, want := tree.uncachedLength(), 1+i; have != want {
t.Errorf("uncachedLength() returned %d but %d was expected.", have, want)
}
}
vals = []byte{2, 8, 6}
for i := range vals {
t.Logf("Removing value: %d", vals[i])
tree.Remove(vals[i])
if have, want := tree.Length(), 3-i; have != want {
t.Errorf("Length() returned %d but %d was expected.", have, want)
}
if have, want := tree.uncachedLength(), 3-i; have != want {
t.Errorf("uncachedLength() returned %d but %d was expected.", have, want)
}
}
}