-
Notifications
You must be signed in to change notification settings - Fork 2
/
skiplist_test.go
173 lines (149 loc) · 2.89 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
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
package skiplist_test
import (
"testing"
. "github.com/kkdai/skiplist"
)
func TestRandomMaxLevel(t *testing.T) {
sp := NewSkipList()
sp.SetMaxLevel(5)
for i := 0; i <= 5; i++ {
ranLevel := sp.RandomLevel()
if ranLevel > 5 || ranLevel < 0 {
t.Errorf(" error found in random: %d", ranLevel)
}
}
}
func TestInsert(t *testing.T) {
sl := NewSkipList()
sl.Insert(30, "string 30")
sl.Insert(50, "string 50")
sl.Insert(40, "string 40")
sl.Insert(20, "string 20")
if val, _ := sl.Search(20); val != "string 20" {
t.Errorf("Expect 20, got %d \n", val)
}
}
func TestInsertBig(t *testing.T) {
sl := NewSkipList()
var i uint32
for i = 0; i < 100000; i++ {
sl.Insert(i, i)
}
val, _ := sl.Search(65536)
ret := val.(uint32)
if ret != 65536 {
t.Error("Cannot search middle ret:", ret, val)
}
}
func TestSearch(t *testing.T) {
sl := NewSkipList()
sl.Insert(30, "string 30")
sl.Insert(50, "string 50")
sl.Insert(40, "string 40")
val, err := sl.Search(40)
if err != nil || val != "string 40" {
t.Errorf("search error, expect string40\n")
}
}
func TestDelete(t *testing.T) {
sl := NewSkipList()
sl.Insert(30, "string 30")
sl.Insert(50, "string 50")
sl.Insert(40, "string 40")
err := sl.Delete(40)
if err != nil {
t.Error("delete error")
}
}
func TestDeleteBig(t *testing.T) {
sl := NewSkipList()
var i uint32
for i = 0; i < 100000; i++ {
sl.Insert(i, i)
}
for i = 0; i < 100000; i++ {
err := sl.Delete(i)
if err != nil {
t.Error("delete big error")
}
}
}
/* Bigger than 25868 times delete might cause error
func BenchmarkDelete(b *testing.B) {
sl := NewSkipList()
var i uint32
for i = 0; i < 100000; i++ {
sl.Insert(i, i)
}
b.ResetTimer()
fmt.Println(b.N)
for i = 0; i < uint32(b.N); i++ {
sl.Delete(i)
}
}
*/
func BenchmarkSkiplistInsert(b *testing.B) {
sl := NewSkipList()
b.ResetTimer()
var i uint32
for i = 0; i < uint32(b.N); i++ {
sl.Insert(i, i)
}
}
func BenchmarkSkiplistSearch(b *testing.B) {
sl := NewSkipList()
var i uint32
for i = 0; i < 100000; i++ {
sl.Insert(i, i)
}
b.ResetTimer()
for i = 0; i < uint32(b.N); i++ {
sl.Search(i)
}
}
func BenchmarkSliceInsert(b *testing.B) {
var sl []uint32
b.ResetTimer()
var i uint32
for i = 0; i < uint32(b.N); i++ {
sl = append(sl, i)
}
}
func BenchmarkSliceSearch(b *testing.B) {
var sl []uint32
var i uint32
for i = 0; i < 100000; i++ {
sl = append(sl, i)
}
var ret int
b.ResetTimer()
for i = 0; i < uint32(b.N); i++ {
for k, v := range sl {
if v == i {
ret = k
ret = ret + 1
}
}
}
}
func BenchmarkMapInsert(b *testing.B) {
sl := make(map[uint32]uint32)
b.ResetTimer()
var i uint32
for i = 0; i < uint32(b.N); i++ {
sl[i] = i
}
}
func BenchmarkMapSearch(b *testing.B) {
sl := make(map[uint32]uint32)
var i uint32
for i = 0; i < 100000; i++ {
sl[i] = i
}
var ret uint32
b.ResetTimer()
for i = 0; i < uint32(b.N); i++ {
ret = sl[i]
ret = ret + 1
}
}