-
Notifications
You must be signed in to change notification settings - Fork 0
/
rbtree_test.go
218 lines (200 loc) · 3.97 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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package rbtree
import (
"fmt"
"math/rand"
"reflect"
"testing"
"time"
)
func init() {
seed := time.Now().Unix()
rand.Seed(seed)
}
// perm returns a random permutation of n Int items in the range [0, n).
func perm(n int) (out []Item) {
out = make([]Item, 0, n)
for _, v := range rand.Perm(n) {
out = append(out, Int(v))
}
return
}
// rang returns an ordered list of Int items in the range [0, n).
func rang(n int) (out []Item) {
for i := 0; i < n; i++ {
out = append(out, Int(i))
}
return
}
func TestRBtree(t *testing.T) {
tr := New()
const treeSize = 10000
for i := 0; i < 10; i++ {
for _, item := range perm(treeSize) {
tr.Insert(item)
}
if tr.Len() != treeSize {
t.Fatal("insert failed", treeSize, tr.Len())
}
for _, item := range perm(treeSize) {
if tr.Search(item) == nil {
t.Fatal("has did not find item", item)
}
}
for _, item := range perm(treeSize) {
tr.Insert(item)
}
it := tr.NewAscendIterator()
if min, want := it.Value(), Item(Int(0)); min != want {
t.Fatalf("min: want %+v, got %+v", want, min)
}
for _, item := range perm(treeSize) {
if tr.Delete(item) == nil {
t.Fatalf("didn't find %v", item)
}
}
}
}
func ExampleRBTree() {
tr := New()
for i := Int(0); i < 10; i++ {
tr.Insert(i)
}
fmt.Println("len: ", tr.Len())
fmt.Println("search3: ", tr.Search(Int(3)))
fmt.Println("search100: ", tr.Search(Int(100)))
fmt.Println("del4: ", tr.Delete(Int(4)))
fmt.Println("del100: ", tr.Delete(Int(100)))
tr.Insert(Int(5))
tr.Insert(Int(100))
fmt.Println("len: ", tr.Len())
fmt.Printf("for: ")
for it := tr.NewAscendIterator(); it.Valid(); it.Next() {
fmt.Print(it.Value().(Int))
fmt.Print(" ")
}
fmt.Println()
// Output:
// len: 10
// search3: 3
// search100: <nil>
// del4: 4
// del100: <nil>
// len: 10
// for: 0 1 2 3 5 6 7 8 9 100
}
func TestAscendIterator(t *testing.T) {
tr := New()
for _, v := range perm(100) {
tr.Insert(v)
}
var got = make([]Item, 0, 100)
for it := tr.NewAscendIterator(); it.Valid(); it.Next() {
got = append(got, it.Value())
}
if want := rang(100); !reflect.DeepEqual(got, want) {
t.Fatalf("got %v, want %v", got, want)
}
}
const benchmarkTreeSize = 10000
func BenchmarkInsert(b *testing.B) {
b.StopTimer()
insertP := perm(benchmarkTreeSize)
b.StartTimer()
i := 0
for i < b.N {
tr := New()
for _, item := range insertP {
tr.Insert(item)
i++
if i >= b.N {
return
}
}
}
}
func BenchmarkSearch(b *testing.B) {
b.StopTimer()
insertP := perm(benchmarkTreeSize)
searchP := perm(benchmarkTreeSize)
b.StartTimer()
i := 0
for i < b.N {
b.StopTimer()
tr := New()
for _, v := range insertP {
tr.Insert(v)
}
b.StartTimer()
for _, item := range searchP {
tr.Search(item)
i++
if i >= b.N {
return
}
}
}
}
func BenchmarkDeleteInsert(b *testing.B) {
b.StopTimer()
insertP := perm(benchmarkTreeSize)
tr := New()
for _, item := range insertP {
tr.Insert(item)
}
b.StartTimer()
for i := 0; i < b.N; i++ {
tr.Delete(insertP[i%benchmarkTreeSize])
tr.Insert(insertP[i%benchmarkTreeSize])
}
}
func BenchmarkDelete(b *testing.B) {
b.StopTimer()
insertP := perm(benchmarkTreeSize)
removeP := perm(benchmarkTreeSize)
b.StartTimer()
i := 0
for i < b.N {
b.StopTimer()
tr := New()
for _, v := range insertP {
tr.Insert(v)
}
b.StartTimer()
for _, item := range removeP {
tr.Delete(item)
i++
if i >= b.N {
return
}
}
if tr.Len() > 0 {
panic(tr.Len())
}
}
}
func BenchmarkAscend(b *testing.B) {
tr := New()
for _, item := range perm(benchmarkTreeSize) {
tr.Insert(item)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
tr.Ascend(func(i Item) {
})
}
}
func BenchmarkAscendIterater(b *testing.B) {
tr := New()
for _, item := range perm(benchmarkTreeSize) {
tr.Insert(item)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
it := Iterator{
t: tr,
x: tr.minimum(tr.root),
}
for ; it.Valid(); it.Next() {
}
}
}