-
Notifications
You must be signed in to change notification settings - Fork 0
/
ranklist_test.go
359 lines (299 loc) · 7.42 KB
/
ranklist_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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package ranklist
import (
"math/rand/v2"
"strconv"
"testing"
)
func TestNew(t *testing.T) {
sl := New[int, int]()
if sl.length != 0 {
t.Errorf("New skiplist should have length 0, got %d", sl.length)
}
if sl.level != 1 {
t.Errorf("New skiplist should have initial level 1, got %d", sl.level)
}
}
func TestSet(t *testing.T) {
sl := New[string, int]()
for k := 0; k < 10000; k++ {
sl.Set(strconv.Itoa(k), k)
}
}
func TestSetAndGet(t *testing.T) {
sl := New[string, int]()
sl.Set("a", 1)
sl.Set("b", 2)
sl.Set("c", 3)
testCases := []struct {
key string
expected int
exists bool
}{
{"a", 1, true},
{"b", 2, true},
{"c", 3, true},
{"d", 0, false},
}
for _, tc := range testCases {
value, exists := sl.Get(tc.key)
if exists != tc.exists {
t.Errorf("Key %s: unexpected exists status, got %v", tc.key, exists)
}
if exists && value != tc.expected {
t.Errorf("Key %s: expected value %d, got %d", tc.key, tc.expected, value)
}
}
}
func TestUpdate(t *testing.T) {
sl := New[int, int]()
sl.Set(1, 100)
sl.Set(1, 200)
value, exists := sl.Get(1)
if !exists {
t.Fatalf("Key should exist after update")
}
if value != 200 {
t.Errorf("Value should be updated to 200, got %d", value)
}
}
func TestDel(t *testing.T) {
sl := New[int, int]()
sl.Set(1, 100)
sl.Set(2, 200)
sl.Set(2, 300)
if !sl.Del(1) {
t.Fatalf("Delete should return true for existing key")
}
if _, exists := sl.Get(1); exists {
t.Fatalf("Key should not exist after deletion")
}
if sl.Del(3) {
t.Fatalf("Delete should return false for non-existent key")
}
}
func TestRank(t *testing.T) {
sl := New[string, int]()
testData := []struct {
key string
value int
rank int
}{
{"a", 1, 1},
{"b", 2, 2},
{"c", 3, 3},
{"d", 3, 4},
{"e", 4, 5},
{"f", 5, 6},
}
for _, data := range testData {
sl.Set(data.key, data.value)
}
for _, data := range testData {
rank, exists := sl.Rank(data.key)
if !exists {
t.Fatalf("Key %s should exist", data.key)
}
if rank != data.rank {
t.Errorf("Key %s: expected rank %d, got %d", data.key, data.rank, rank)
}
}
rank, exists := sl.Rank("x")
if exists {
t.Fatalf("Rank should return false for non-existent key")
}
if rank != 0 {
t.Errorf("Rank should be 0 for non-existent key, got %d", rank)
}
}
func TestRankAfterDel(t *testing.T) {
sl := New[string, int]()
testData := []struct {
key string
value int
rank int
}{
{"a", 1, 1},
{"b", 2, 2},
{"d", 3, 3},
{"e", 4, 4},
{"g", 6, 5},
{"h", 7, 6},
}
for _, data := range testData {
sl.Set(data.key, data.value)
}
sl.Set("c", 3)
sl.Del("c")
sl.Set("f", 5)
sl.Del("f")
for _, data := range testData {
rank, exists := sl.Rank(data.key)
if !exists {
t.Fatalf("Key %s should exist", data.key)
}
if rank != data.rank {
t.Errorf("Key %s: expected rank %d, got %d", data.key, data.rank, rank)
}
}
}
func TestMassiveRank(t *testing.T) {
sl := New[string, int64]()
for k := 0; k < 10000; k++ {
sl.Set(strconv.Itoa(k), rand.Int64N(10000))
}
for k := 0; k < 10000; k++ {
_, exists := sl.Rank(strconv.Itoa(k))
if !exists {
t.Errorf("Key %s should exist", strconv.Itoa(k))
}
}
}
func TestRankKeyNotExist(t *testing.T) {
sl := New[string, int]()
sl.dict["x"] = 0
rank, exists := sl.Rank("x")
if exists {
t.Fatalf("Rank should return false for non-existent key")
}
if rank != 0 {
t.Errorf("Rank should be 0 for non-existent key, got %d", rank)
}
}
func TestEdgeCases(t *testing.T) {
sl := New[int, int]()
if _, exists := sl.Get(1); exists {
t.Fatalf("Get should return false for empty skip list")
}
if sl.Del(1) {
t.Fatalf("Delete should return false for empty skip list")
}
for i := 0; i < 1000; i++ {
sl.Set(i, i)
}
if sl.length != 1000 {
t.Errorf("Length should be 1000 after insertions, got %d", sl.length)
}
for i := 0; i < 1000; i++ {
if !sl.Del(i) {
t.Errorf("Failed to delete key %d", i)
}
}
if sl.length != 0 {
t.Errorf("Length should be 0 after all deletions, got %d", sl.length)
}
}
func TestLength(t *testing.T) {
sl := New[int, int]()
for i := 0; i < 1000; i++ {
sl.Set(i, i)
}
if sl.Length() != 1000 {
t.Errorf("Length should be 1000 after insertions, got %d", sl.length)
}
}
func TestRankList_Range_SimpleRange(t *testing.T) {
rankList := New[int, int]()
rankList.Set(1, 100)
rankList.Set(2, 120)
rankList.Set(3, 150)
rankList.Set(4, 180)
rankList.Set(5, 200)
start := 1
end := 3
expected := []Entry[int, int]{
{Key: 1, Value: 100},
{Key: 2, Value: 120},
}
result := rankList.Range(start, end)
if len(result) != len(expected) {
t.Errorf("expected %d entries, got %d", len(expected), len(result))
}
for i, entry := range result {
if entry.Key != expected[i].Key || entry.Value != expected[i].Value {
t.Errorf("at index %d: expected (%d, %d), got (%d, %d)",
i, expected[i].Key, expected[i].Value, entry.Key, entry.Value)
}
}
}
func TestRankList_Range_FullRange(t *testing.T) {
rankList := New[int, int]()
rankList.Set(1, 100)
rankList.Set(2, 120)
rankList.Set(3, 150)
rankList.Set(4, 180)
rankList.Set(5, 200)
start := 0
end := 5
expected := []Entry[int, int]{
{Key: 1, Value: 100},
{Key: 2, Value: 120},
{Key: 3, Value: 150},
{Key: 4, Value: 180},
{Key: 5, Value: 200},
}
result := rankList.Range(start, end)
if len(result) != len(expected) {
t.Errorf("expected %d entries, got %d", len(expected), len(result))
}
for i, entry := range result {
if entry.Key != expected[i].Key || entry.Value != expected[i].Value {
t.Errorf("at index %d: expected (%d, %d), got (%d, %d)",
i, expected[i].Key, expected[i].Value, entry.Key, entry.Value)
}
}
}
func TestRankList_Range_EmptyRange(t *testing.T) {
rankList := New[int, int]()
rankList.Set(1, 100)
rankList.Set(2, 120)
rankList.Set(3, 150)
rankList.Set(4, 180)
rankList.Set(5, 200)
start := 10
end := 10
expected := []Entry[int, int]{}
result := rankList.Range(start, end)
if len(result) != len(expected) {
t.Errorf("expected %d entries, got %d", len(expected), len(result))
}
}
func TestRankList_Range_InvalidRange(t *testing.T) {
rankList := New[int, int]()
rankList.Set(1, 100)
rankList.Set(2, 120)
rankList.Set(3, 150)
rankList.Set(4, 180)
rankList.Set(5, 200)
start := 3
end := 1
expected := []Entry[int, int]{}
result := rankList.Range(start, end)
if len(result) != len(expected) {
t.Errorf("expected %d entries, got %d", len(expected), len(result))
}
}
func TestRankList_Range_OutOfBoundsEnd(t *testing.T) {
rankList := New[int, int]()
rankList.Set(1, 100)
rankList.Set(2, 120)
rankList.Set(3, 150)
rankList.Set(4, 180)
rankList.Set(5, 200)
start := 3
end := 10
expected := []Entry[int, int]{
{Key: 3, Value: 150},
{Key: 4, Value: 180},
{Key: 5, Value: 200},
}
result := rankList.Range(start, end)
if len(result) != len(expected) {
t.Errorf("expected %d entries, got %d", len(expected), len(result))
}
for i, entry := range result {
if entry.Key != expected[i].Key || entry.Value != expected[i].Value {
t.Errorf("at index %d: expected (%d, %d), got (%d, %d)",
i, expected[i].Key, expected[i].Value, entry.Key, entry.Value)
}
}
}