-
Notifications
You must be signed in to change notification settings - Fork 19
/
skipset_test.go
505 lines (470 loc) · 9.09 KB
/
skipset_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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
package skipset
import (
"fmt"
"math"
"sort"
"strconv"
"sync"
"sync/atomic"
"testing"
"github.com/zhangyunhao116/fastrand"
)
func TestOrdered(t *testing.T) {
testIntSet(t, func() anyskipset[int] {
return New[int]()
})
testIntSetDesc(t, func() anyskipset[int] {
return NewDesc[int]()
})
testStringSet(t, func() anyskipset[string] {
return New[string]()
})
}
func TestFunc(t *testing.T) {
x := NewFunc(func(a, b float64) bool {
return a < b || (math.IsNaN(a) && !math.IsNaN(b))
})
x.Add(math.NaN())
x.Add(3)
x.Add(1)
x.Add(math.NaN())
x.Add(2)
x.Add(math.NaN())
if x.Len() != 4 {
t.Fatal(x.Len())
}
res := []float64{math.NaN(), 1, 2, 3}
var i int
x.Range(func(value float64) bool {
if i == 0 && !math.IsNaN(value) {
t.Fatal()
}
if i >= 1 && value != res[i] {
t.Fatal()
}
i++
return true
})
testIntSet(t, func() anyskipset[int] {
return NewFunc(func(a, b int) bool {
return a < b
})
})
testIntSetDesc(t, func() anyskipset[int] {
return NewFunc(func(a, b int) bool {
return a > b
})
})
testStringSet(t, func() anyskipset[string] {
return NewFunc(func(a, b string) bool {
return a < b
})
})
}
func TestTypes(t *testing.T) {
testIntSet(t, func() anyskipset[int] {
return NewInt()
})
testIntSetDesc(t, func() anyskipset[int] {
return NewIntDesc()
})
testStringSet(t, func() anyskipset[string] {
return NewString()
})
}
type anyskipset[T any] interface {
Add(v T) bool
Remove(v T) bool
Contains(v T) bool
Range(f func(v T) bool)
RangeFrom(start T, f func(v T) bool)
Len() int
}
// Test suites.
func testIntSet(t *testing.T, newset func() anyskipset[int]) {
// Correctness.
l := newset()
if l.Len() != 0 {
t.Fatal("invalid length")
}
if l.Contains(0) {
t.Fatal("invalid contains")
}
if !l.Add(0) || l.Len() != 1 {
t.Fatal("invalid add")
}
if !l.Contains(0) {
t.Fatal("invalid contains")
}
if !l.Remove(0) || l.Len() != 0 {
t.Fatal("invalid remove")
}
if !l.Add(20) || l.Len() != 1 {
t.Fatal("invalid add")
}
if !l.Add(22) || l.Len() != 2 {
t.Fatal("invalid add")
}
if !l.Add(21) || l.Len() != 3 {
t.Fatal("invalid add")
}
var i int
l.Range(func(score int) bool {
if i == 0 && score != 20 {
t.Fatal("invalid range")
}
if i == 1 && score != 21 {
t.Fatal("invalid range")
}
if i == 2 && score != 22 {
t.Fatal("invalid range")
}
i++
return true
})
if !l.Remove(21) || l.Len() != 2 {
t.Fatal("invalid remove")
}
i = 0
l.Range(func(score int) bool {
if i == 0 && score != 20 {
t.Fatal("invalid range")
}
if i == 1 && score != 22 {
t.Fatal("invalid range")
}
i++
return true
})
const num = math.MaxInt16
// Make rand shuffle array.
// The testArray contains [1,num]
testArray := make([]int, num)
testArray[0] = num + 1
for i := 1; i < num; i++ {
// We left 0, because it is the default score for head and tail.
// If we check the skipset contains 0, there must be something wrong.
testArray[i] = int(i)
}
for i := len(testArray) - 1; i > 0; i-- { // Fisher–Yates shuffle
j := fastrand.Uint32n(uint32(i + 1))
testArray[i], testArray[j] = testArray[j], testArray[i]
}
// Concurrent add.
var wg sync.WaitGroup
for i := 0; i < num; i++ {
i := i
wg.Add(1)
go func() {
l.Add(testArray[i])
wg.Done()
}()
}
wg.Wait()
if l.Len() != int(num) {
t.Fatalf("invalid length expected %d, got %d", num, l.Len())
}
// Don't contains 0 after concurrent addion.
if l.Contains(0) {
t.Fatal("contains 0 after concurrent addion")
}
// Concurrent contains.
for i := 0; i < num; i++ {
i := i
wg.Add(1)
go func() {
if !l.Contains(testArray[i]) {
wg.Done()
panic(fmt.Sprintf("add doesn't contains %d", i))
}
wg.Done()
}()
}
wg.Wait()
// Concurrent remove.
for i := 0; i < num; i++ {
i := i
wg.Add(1)
go func() {
if !l.Remove(testArray[i]) {
wg.Done()
panic(fmt.Sprintf("can't remove %d", i))
}
wg.Done()
}()
}
wg.Wait()
if l.Len() != 0 {
t.Fatalf("invalid length expected %d, got %d", 0, l.Len())
}
// Test all methods.
const smallRndN = 1 << 8
for i := 0; i < 1<<16; i++ {
wg.Add(1)
go func() {
r := fastrand.Uint32n(num)
if r < 333 {
l.Add(int(fastrand.Uint32n(smallRndN)) + 1)
} else if r < 666 {
l.Contains(int(fastrand.Uint32n(smallRndN)) + 1)
} else if r != 999 {
l.Remove(int(fastrand.Uint32n(smallRndN)) + 1)
} else {
var pre int
l.Range(func(score int) bool {
if score <= pre { // 0 is the default value for header and tail score
panic("invalid content")
}
pre = score
return true
})
}
wg.Done()
}()
}
wg.Wait()
// Correctness 2.
var (
x = newset()
y = newset()
count = 10000
)
for i := 0; i < count; i++ {
x.Add(i)
}
for i := 0; i < 16; i++ {
wg.Add(1)
go func() {
x.Range(func(score int) bool {
if x.Remove(score) {
if !y.Add(score) {
panic("invalid add")
}
}
return true
})
wg.Done()
}()
}
wg.Wait()
if x.Len() != 0 || y.Len() != count {
t.Fatal("invalid length")
}
// Concurrent Add and Remove in small zone.
x = newset()
var (
addcount uint64 = 0
removecount uint64 = 0
)
for i := 0; i < 16; i++ {
wg.Add(1)
go func() {
for i := 0; i < 1000; i++ {
if fastrand.Uint32n(2) == 0 {
if x.Remove(int(fastrand.Uint32n(10))) {
atomic.AddUint64(&removecount, 1)
}
} else {
if x.Add(int(fastrand.Uint32n(10))) {
atomic.AddUint64(&addcount, 1)
}
}
}
wg.Done()
}()
}
wg.Wait()
if addcount < removecount {
panic("invalid count")
}
if addcount-removecount != uint64(x.Len()) {
panic("invalid count")
}
pre := -1
x.Range(func(score int) bool {
if score <= pre {
panic("invalid content")
}
pre = score
return true
})
// Correctness 3.
s1 := newset()
var s2 sync.Map
var counter uint64
for i := 0; i <= 10000; i++ {
wg.Add(1)
go func() {
if fastrand.Uint32n(2) == 0 {
r := fastrand.Uint32()
s1.Add(int(r))
s2.Store(int(r), nil)
} else {
r := atomic.AddUint64(&counter, 1)
s1.Add(int(r))
s2.Store(int(r), nil)
}
wg.Done()
}()
}
wg.Wait()
s1.Range(func(value int) bool {
_, ok := s2.Load(value)
if !ok {
t.Fatal(value)
}
return true
})
s2.Range(func(key, value interface{}) bool {
k := key.(int)
if !s1.Contains(k) {
t.Fatal(value)
}
return true
})
}
func testIntSetDesc(t *testing.T, newsetdesc func() anyskipset[int]) {
s := newsetdesc()
nums := []int{-1, 0, 5, 12}
for _, v := range nums {
s.Add(v)
}
i := len(nums) - 1
s.Range(func(value int) bool {
if nums[i] != value {
t.Fatal("error")
}
i--
return true
})
}
func testStringSet(t *testing.T, newset func() anyskipset[string]) {
x := newset()
if !x.Add("111") || x.Len() != 1 {
t.Fatal("invalid")
}
if !x.Add("222") || x.Len() != 2 {
t.Fatal("invalid")
}
if x.Add("111") || x.Len() != 2 {
t.Fatal("invalid")
}
if !x.Contains("111") || !x.Contains("222") {
t.Fatal("invalid")
}
if !x.Remove("111") || x.Len() != 1 {
t.Fatal("invalid")
}
if !x.Remove("222") || x.Len() != 0 {
t.Fatal("invalid")
}
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
i := i
go func() {
if !x.Add(strconv.Itoa(i)) {
panic("invalid")
}
wg.Done()
}()
}
wg.Wait()
tmp := make([]int, 0, 100)
x.Range(func(val string) bool {
res, _ := strconv.Atoi(val)
tmp = append(tmp, res)
return true
})
sort.Ints(tmp)
for i := 0; i < 100; i++ {
if i != tmp[i] {
t.Fatal("invalid")
}
}
}
func TestFloatMap(t *testing.T) {
cases := []float64{
math.NaN(),
0.04,
math.NaN(),
0.05,
math.Inf(1),
0.04,
math.NaN(),
0.05,
math.Inf(-1),
math.Inf(1),
math.Inf(-1),
}
m := NewFloat64()
md := NewFloat64Desc()
m32 := NewFloat32()
m32d := NewFloat32Desc()
for _, k := range cases {
m.Add(k)
md.Add(k)
m32.Add(float32(k))
m32d.Add(float32(k))
}
var (
mr, mdr []float64
m32r, m32dr []float32
)
m.Range(func(key float64) bool {
mr = append(mr, key)
return true
})
md.Range(func(key float64) bool {
mdr = append(mdr, key)
return true
})
m32.Range(func(key float32) bool {
m32r = append(m32r, key)
return true
})
m32d.Range(func(key float32) bool {
m32dr = append(m32dr, key)
return true
})
var (
asc = []float64{
math.NaN(), math.Inf(-1), 0.04, 0.05, math.Inf(1),
}
desc = []float64{
math.NaN(), math.Inf(1), 0.05, 0.04, math.Inf(-1),
}
asc32 = []float32{
float32(math.NaN()), float32(math.Inf(-1)), 0.04, 0.05, float32(math.Inf(1)),
}
desc32 = []float32{
float32(math.NaN()), float32(math.Inf(1)), 0.05, 0.04, float32(math.Inf(-1)),
}
)
checkEqual := func(a, b []float64) {
l := len(a)
if len(b) != l {
t.Fatal("invalid length", l)
}
for i := 0; i < l; i++ {
if a[i] != b[i] && !(math.IsNaN(a[i])) {
t.Fatal("not equal", i, a[i], b[i])
}
}
}
checkEqual32 := func(a, b []float32) {
l := len(a)
if len(b) != l {
t.Fatal("invalid length", l)
}
for i := 0; i < l; i++ {
if a[i] != b[i] && !(isNaNf32(a[i])) {
t.Fatal("not equal", i, a[i], b[i])
}
}
}
checkEqual(mr, asc)
checkEqual(mdr, desc)
checkEqual32(m32r, asc32)
checkEqual32(m32dr, desc32)
}