-
Notifications
You must be signed in to change notification settings - Fork 0
/
skiplist_test.go
93 lines (76 loc) · 1.54 KB
/
skiplist_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
package skiplist
import (
"math/rand"
"testing"
)
const HEIGHT = 16
func benchPut(size int, b *testing.B) {
list := New(HEIGHT)
for n := 0; n < b.N; n++ {
for i := 0; i < size; i++ {
list.Put(n, struct{}{})
}
}
}
func BenchmarkPut100(b *testing.B) {
benchPut(100, b)
}
func BenchmarkPut1000(b *testing.B) {
benchPut(1000, b)
}
func BenchmarkPut10000(b *testing.B) {
benchPut(10000, b)
}
func benchPutRemoveAll(size int, b *testing.B) {
for n := 0; n < b.N; n++ {
ns := []*Node{}
list := New(HEIGHT)
for i := 0; i < size; i++ {
ns = append(ns, list.Put(i, struct{}{}))
}
for _, n := range ns {
list.Remove(n)
}
}
}
func BenchmarkPutRemoveAll100(b *testing.B) {
benchPutRemoveAll(100, b)
}
func BenchmarkPutRemoveAll1000(b *testing.B) {
benchPutRemoveAll(1000, b)
}
func BenchmarkPutRemoveAll10000(b *testing.B) {
benchPutRemoveAll(10000, b)
}
func BenchmarkPutRemove(b *testing.B) {
b.StopTimer()
list := New(HEIGHT)
for i := 0; i < 1000; i++ {
list.Put(rand.Intn(10000), struct{}{})
}
b.StartTimer()
for n := 0; n < b.N; n++ {
node := list.Put(n, struct{}{})
list.Remove(node)
}
}
func TestPutRemoveAll(t *testing.T) {
size := 100
ns := []*Node{}
list := New(HEIGHT)
for i := 0; i < size; i++ {
ns = append(ns, list.Put(rand.Intn(size*size), struct{}{}))
}
for _, n := range ns {
_, ok := list.Get(n.Key)
if !ok {
t.Fatalf("Failed to find node just inserted: %d", n)
}
}
for _, n := range ns {
ok := list.Remove(n)
if !ok {
t.Fatalf("Failed to remove node just inserted: %d", n)
}
}
}