-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree_test.go
More file actions
125 lines (106 loc) · 2.13 KB
/
tree_test.go
File metadata and controls
125 lines (106 loc) · 2.13 KB
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
package btree
import (
"strconv"
"sync"
"testing"
)
var tree *BTree
var ids = [...]int{1, 2, 8, 798, 1560, 10000}
// TestPut ...
func TestIntPut(t *testing.T) {
tree = &BTree{}
for i := 1; i <= ids[len(ids)-1]; i++ {
v := strconv.Itoa(i)
tree.Put(i, v)
}
s := tree.Size()
if s != ids[len(ids)-1] {
t.Errorf("Inserted -%d-, put function not working", s)
}
}
// TestPutConcurrency ...
func TestIntPutConcurrency(t *testing.T) {
var wg sync.WaitGroup
stree := &BTree{}
wg.Add(ids[len(ids)-1])
for i := 1; i <= ids[len(ids)-1]; i++ {
v := strconv.Itoa(i)
go func(i int) {
defer wg.Done()
stree.Put(i, v)
}(i)
}
wg.Wait()
s := tree.Size()
if s != ids[len(ids)-1] {
t.Errorf("Inserted -%d-, put function not working", s)
}
}
// TestFind ...
func TestIntFind(t *testing.T) {
for i := 0; i < len(ids); i++ {
_, found := tree.Find(ids[i])
if !found {
t.Errorf("Could not find key: %d, Find function not working", ids[i])
}
}
}
// TestMax ...
func TestIntMax(t *testing.T) {
r := tree.Max()
if r.Key != ids[len(ids)-1] {
t.Errorf("Invalid Max value")
}
}
// TestMin ...
func TestIntMin(t *testing.T) {
r := tree.Min()
if r.Key != ids[0] {
t.Errorf("Invalid Min value")
}
}
// TestRemove using concurrency
func TestIntRemove(t *testing.T) {
for i := 0; i < len(ids); i++ {
var n *Node
n = tree.Remove(ids[i])
if n == nil {
t.Errorf("Could not remove key: %d, Remove function not working", ids[i])
}
}
// tree.TraverseInOrder(tree.root, func(n *Node) { fmt.Println(n.Key) })
}
// TestIntSave ...
func TestIntSave(t *testing.T) {
err := tree.Commit()
if err != nil {
t.Error(err)
}
}
// TestIntLoad ...
func TestIntLoad(t *testing.T) {
err := tree.Load()
if err != nil {
t.Error(err)
}
}
// TestSize ...
func TestIntSize(t *testing.T) {
s := tree.Size()
if s != (ids[len(ids)-1]) {
t.Errorf("Incorrect size -%d-", s)
}
}
func BenchmarkPut(b *testing.B) {
// run the Fib function b.N times
for n := 0; n < b.N; n++ {
v := strconv.Itoa(n + 1)
tree.Put(n+1, v)
}
}
func BenchmarkRead(b *testing.B) {
// run the Fib function b.N times
for n := 0; n < b.N; n++ {
tree.Find(n + 1)
}
}