-
Notifications
You must be signed in to change notification settings - Fork 52
/
skiplist_test.go
232 lines (182 loc) · 4.21 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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package skiplist
import (
"fmt"
"sync"
"testing"
"unsafe"
)
var benchList *SkipList
var discard *Element
func init() {
// Initialize a big SkipList for the Get() benchmark
benchList = New()
for i := 0; i <= 10000000; i++ {
benchList.Set(float64(i), [1]byte{})
}
// Display the sizes of our basic structs
var sl SkipList
var el Element
fmt.Printf("Structure sizes: SkipList is %v, Element is %v bytes\n", unsafe.Sizeof(sl), unsafe.Sizeof(el))
}
func checkSanity(list *SkipList, t *testing.T) {
// each level must be correctly ordered
for k, v := range list.next {
//t.Log("Level", k)
if v == nil {
continue
}
if k > len(v.next) {
t.Fatal("first node's level must be no less than current level")
}
next := v
cnt := 1
for next.next[k] != nil {
if !(next.next[k].key >= next.key) {
t.Fatalf("next key value must be greater than prev key value. [next:%v] [prev:%v]", next.next[k].key, next.key)
}
if k > len(next.next) {
t.Fatalf("node's level must be no less than current level. [cur:%v] [node:%v]", k, next.next)
}
next = next.next[k]
cnt++
}
if k == 0 {
if cnt != list.Length {
t.Fatalf("list len must match the level 0 nodes count. [cur:%v] [level0:%v]", cnt, list.Length)
}
}
}
}
func TestBasicIntCRUD(t *testing.T) {
var list *SkipList
list = New()
list.Set(10, 1)
list.Set(60, 2)
list.Set(30, 3)
list.Set(20, 4)
list.Set(90, 5)
checkSanity(list, t)
list.Set(30, 9)
checkSanity(list, t)
list.Remove(0)
list.Remove(20)
checkSanity(list, t)
v1 := list.Get(10)
v2 := list.Get(60)
v3 := list.Get(30)
v4 := list.Get(20)
v5 := list.Get(90)
v6 := list.Get(0)
if v1 == nil || v1.value.(int) != 1 || v1.key != 10 {
t.Fatal(`wrong "10" value (expected "1")`, v1)
}
if v2 == nil || v2.value.(int) != 2 {
t.Fatal(`wrong "60" value (expected "2")`)
}
if v3 == nil || v3.value.(int) != 9 {
t.Fatal(`wrong "30" value (expected "9")`)
}
if v4 != nil {
t.Fatal(`found value for key "20", which should have been deleted`)
}
if v5 == nil || v5.value.(int) != 5 {
t.Fatal(`wrong "90" value`)
}
if v6 != nil {
t.Fatal(`found value for key "0", which should have been deleted`)
}
}
func TestChangeLevel(t *testing.T) {
var i float64
list := New()
if list.maxLevel != DefaultMaxLevel {
t.Fatal("max level must equal default max value")
}
list = NewWithMaxLevel(4)
if list.maxLevel != 4 {
t.Fatal("wrong maxLevel (wanted 4)", list.maxLevel)
}
for i = 1; i <= 201; i++ {
list.Set(i, i*10)
}
checkSanity(list, t)
if list.Length != 201 {
t.Fatal("wrong list length", list.Length)
}
for c := list.Front(); c != nil; c = c.Next() {
if c.key*10 != c.value.(float64) {
t.Fatal("wrong list element value")
}
}
}
func TestMaxLevel(t *testing.T) {
list := NewWithMaxLevel(DefaultMaxLevel + 1)
list.Set(0, struct{}{})
}
func TestChangeProbability(t *testing.T) {
list := New()
if list.probability != DefaultProbability {
t.Fatal("new lists should have P value = DefaultProbability")
}
list.SetProbability(0.5)
if list.probability != 0.5 {
t.Fatal("failed to set new list probability value: expected 0.5, got", list.probability)
}
}
func TestConcurrency(t *testing.T) {
list := New()
wg := &sync.WaitGroup{}
wg.Add(2)
go func() {
for i := 0; i < 100000; i++ {
list.Set(float64(i), i)
}
wg.Done()
}()
go func() {
for i := 0; i < 100000; i++ {
list.Get(float64(i))
}
wg.Done()
}()
wg.Wait()
if list.Length != 100000 {
t.Fail()
}
}
func BenchmarkIncSet(b *testing.B) {
b.ReportAllocs()
list := New()
for i := 0; i < b.N; i++ {
list.Set(float64(i), [1]byte{})
}
b.SetBytes(int64(b.N))
}
func BenchmarkIncGet(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
res := benchList.Get(float64(i))
if res == nil {
b.Fatal("failed to Get an element that should exist")
}
}
b.SetBytes(int64(b.N))
}
func BenchmarkDecSet(b *testing.B) {
b.ReportAllocs()
list := New()
for i := b.N; i > 0; i-- {
list.Set(float64(i), [1]byte{})
}
b.SetBytes(int64(b.N))
}
func BenchmarkDecGet(b *testing.B) {
b.ReportAllocs()
for i := b.N; i > 0; i-- {
res := benchList.Get(float64(i))
if res == nil {
b.Fatal("failed to Get an element that should exist", i)
}
}
b.SetBytes(int64(b.N))
}