-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrbtree_test.go
82 lines (62 loc) · 1.31 KB
/
rbtree_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
package rbtree
import "testing"
type Thing struct {
Key Int
Val string
}
func (t Thing) Less(item Item) bool {
return t.Key < item.(Thing).Key
}
func (t Thing) More(item Item) bool {
return t.Key > item.(Thing).Key
}
type Int int
func (i Int) Less(item Item) bool {
return i < item.(Thing).Key
}
func (i Int) More(item Item) bool {
return i > item.(Thing).Key
}
func TestInsertAndRetreivalRedBlack(t *testing.T) {
tree := New()
tree.Put(Thing{Int(6), "foo"})
if tree.Size() != 1 {
t.Fatal("Value not inserted correctly")
}
tree.Put(Thing{Int(7), "bar"})
if tree.Size() != 2 {
t.Fatal("Value not inserted correctly", tree.Size())
}
tree.Put(Thing{Int(7), "baz"})
if tree.Size() != 2 {
t.Fatal("Value not inserted correctly")
}
item, ok := tree.Find(Int(6))
if !ok {
t.Fatal("Value not found")
}
if item.(Thing).Val != "foo" {
t.Fatal("Value not retreived correctly")
}
item, ok = tree.Find(Int(7))
if !ok {
t.Fatal("Value not found")
}
if item.(Thing).Val != "baz" {
t.Fatal("Value not retreived correctly")
}
}
func BenchmarkRedBlack(b *testing.B) {
var (
keys = make([]int, 0, b.N)
tree = New()
)
for i := 0; i < b.N*3; i++ {
keys = append(keys, i)
tree.Put(Thing{Int(i), "foo"})
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = tree.Find(Int(keys[i*3]))
}
}