-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrbtree_test.go
201 lines (168 loc) · 3.12 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
package rbtree
import (
"flag"
"fmt"
"math/rand"
"os"
"strconv"
"testing"
"time"
)
func compare(a, b interface{}) int {
if a.(int) == b.(int) {
return 0
}
if a.(int) < b.(int) {
return -1
}
return 1
}
func TestMain(m *testing.M) {
seed := time.Now().UnixNano()
rand.Seed(seed)
if !flag.Parsed() {
flag.Parse()
}
if testing.Verbose() {
fmt.Printf("seeded to %d\n", seed)
}
os.Exit(m.Run())
}
func TestFuzz(t *testing.T) {
var (
count = 10
more = 30
max = 200
)
sizes := []int{1, 2, 3, 4, 5, 1e5}
if testing.Short() {
count /= 5
more /= 3
max /= 2
}
// Additional random sizes
for i := 0; i < more; i++ {
sizes = append(sizes, rand.Intn(max)+1)
}
for _, size := range sizes {
for i := 0; i < count; i++ {
tree := New(compare)
keys := generateKeys(size)
for _, key := range keys {
if !tree.Insert(key) {
// keys contained duplicates
continue
}
}
// Size
if tree.Size() != size {
t.Fatalf("fuzz %d:%d size incorrect", size, i)
}
// Search
for _, key := range keys {
if tree.Search(key) != key {
t.Fatalf("fuzz %d:%d search bad val",
size, i)
}
}
// Delete
for _, key := range keys {
if tree.Delete(key) != key {
t.Fatalf("fuzz %d:%d delete bad val",
size, i)
}
}
}
}
}
func TestDeleteNonexistent(t *testing.T) {
tree := New(compare)
if tree.Delete(0) != nil {
t.Fatal("deleted nonexistent key")
}
}
func TestInsertDuplicate(t *testing.T) {
tree := New(compare)
if !tree.Insert(0) {
t.Fatal("first insert failed")
}
if tree.Insert(0) {
t.Fatal("second insert succeeded")
}
}
func TestSize(t *testing.T) {
tree := New(compare)
const size int = 100
keys := generateKeys(size)
for _, key := range keys {
tree.Insert(key)
}
if tree.Size() != size {
t.Fatalf("expected %d got %d", size, tree.Size())
}
for _, key := range keys {
tree.Delete(key)
}
if tree.Size() != 0 {
t.Fatalf("expected %d got %d", 0, tree.Size())
}
}
func TestMinMax(t *testing.T) {
tree := New(func(a, b interface{}) int {
if a.(string) == b.(string) {
return 0
}
if a.(string) < b.(string) {
return -1
}
return 1
})
tree.Insert("c")
tree.Insert("e")
tree.Insert("d")
tree.Insert("a")
tree.Insert("b")
if tree.Max() != "e" {
t.Fatal("wrong max")
}
if tree.Min() != "a" {
t.Fatal("wrong min")
}
}
// Benchmark time taken to do one insert+delete with a pre-existing red-black
// tree.
func BenchmarkInsertDeleteGrowth(b *testing.B) {
top := 6
if testing.Short() {
top /= 2
}
sizes := []int{256}
for i := 1; i < top; i++ {
sizes = append(sizes, sizes[i-1]*4)
}
for _, size := range sizes {
b.Run(strconv.Itoa(size), func(sub *testing.B) {
tree := New(compare)
keys := generateKeys(size)
for i := 0; i < size-1; i++ {
tree.Insert(keys[i])
}
sub.ResetTimer()
for i := 0; i < sub.N; i++ {
tree.Insert(keys[size-1])
tree.Delete(keys[size-1])
}
})
}
}
func generateKeys(n int) []int {
ret := make([]int, n)
for i := range ret {
ret[i] = rand.Int()
// Half are negative.
if rand.Intn(2) == 0 {
ret[i] = -ret[i]
}
}
return ret
}