forked from RoaringBitmap/roaring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runcontainer_test.go
2416 lines (1927 loc) · 58.1 KB
/
runcontainer_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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package roaring
import (
"fmt"
"math/rand"
"sort"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
// trial is used in the randomized testing of runContainers
type trial struct {
n int
percentFill float64
ntrial int
// only in the union test
// only subtract test
percentDelete float64
// only in 067 randomized operations
// we do this + 1 passes
numRandomOpsPass int
// allow sampling range control
// only recent tests respect this.
srang *interval16
}
// canMerge, and mergeInterval16s should do what they say
func TestRleInterval16s(t *testing.T) {
a := newInterval16Range(0, 9)
b := newInterval16Range(0, 1)
report := sliceToString16([]interval16{a, b})
_ = report
c := newInterval16Range(2, 4)
d := newInterval16Range(2, 5)
e := newInterval16Range(0, 4)
f := newInterval16Range(9, 9)
g := newInterval16Range(8, 9)
h := newInterval16Range(5, 6)
i := newInterval16Range(6, 6)
aIb, empty := intersectInterval16s(a, b)
assert.False(t, empty)
assert.EqualValues(t, b, aIb)
assert.True(t, canMerge16(b, c))
assert.True(t, canMerge16(c, b))
assert.True(t, canMerge16(a, h))
assert.True(t, canMerge16(d, e))
assert.True(t, canMerge16(f, g))
assert.True(t, canMerge16(c, h))
assert.False(t, canMerge16(b, h))
assert.False(t, canMerge16(h, b))
assert.False(t, canMerge16(c, i))
assert.EqualValues(t, e, mergeInterval16s(b, c))
assert.EqualValues(t, e, mergeInterval16s(c, b))
assert.EqualValues(t, h, mergeInterval16s(h, i))
assert.EqualValues(t, h, mergeInterval16s(i, h))
////// start
assert.EqualValues(t, newInterval16Range(0, 1), mergeInterval16s(newInterval16Range(0, 0), newInterval16Range(1, 1)))
assert.EqualValues(t, newInterval16Range(0, 1), mergeInterval16s(newInterval16Range(1, 1), newInterval16Range(0, 0)))
assert.EqualValues(t, newInterval16Range(0, 5), mergeInterval16s(newInterval16Range(0, 4), newInterval16Range(3, 5)))
assert.EqualValues(t, newInterval16Range(0, 4), mergeInterval16s(newInterval16Range(0, 4), newInterval16Range(3, 4)))
assert.EqualValues(t, newInterval16Range(0, 8), mergeInterval16s(newInterval16Range(1, 7), newInterval16Range(0, 8)))
assert.EqualValues(t, newInterval16Range(0, 8), mergeInterval16s(newInterval16Range(1, 7), newInterval16Range(0, 8)))
assert.Panics(t, func() { _ = mergeInterval16s(newInterval16Range(0, 0), newInterval16Range(2, 3)) })
}
func TestRunOffset(t *testing.T) {
v := newRunContainer16TakeOwnership([]interval16{newInterval16Range(34, 39)})
offtest := uint16(65500)
l, h := v.addOffset(offtest)
expected := []int{65534, 65535, 65536, 65537, 65538, 65539}
wout := make([]int, len(expected))
var w0card, w1card int
if l != nil {
w0card = l.getCardinality()
for i := 0; i < w0card; i++ {
wout[i] = l.selectInt(uint16(i))
}
}
if h != nil {
w1card = h.getCardinality()
for i := 0; i < w1card; i++ {
wout[i+w0card] = h.selectInt(uint16(i)) + 65536
}
}
assert.Equal(t, v.getCardinality(), w0card+w1card)
for i, x := range wout {
assert.Equal(t, expected[i], x)
}
}
func TestRunArrayUnionToRuns(t *testing.T) {
arrayArg := newArrayContainerRange(0, 10)
runArg := newRunContainer16Range(11, 65535)
intervals, cardMinusOne := runArrayUnionToRuns(runArg, arrayArg)
assert.Equal(t, uint16(65535), cardMinusOne)
assert.Equal(t, []interval16{{start: 0, length: 65535}}, intervals)
}
func TestRleRunIterator16(t *testing.T) {
t.Run("RunIterator16 unit tests for next, hasNext, and peekNext should pass", func(t *testing.T) {
{
rc := newRunContainer16()
msg := rc.String()
_ = msg
assert.EqualValues(t, 0, rc.getCardinality())
it := rc.newRunIterator16()
assert.False(t, it.hasNext())
assert.Panics(t, func() { it.peekNext() })
assert.Panics(t, func() { it.next() })
}
{
rc := newRunContainer16TakeOwnership([]interval16{newInterval16Range(4, 4)})
assert.EqualValues(t, 1, rc.getCardinality())
it := rc.newRunIterator16()
assert.True(t, it.hasNext())
assert.EqualValues(t, uint16(4), it.peekNext())
assert.EqualValues(t, uint16(4), it.next())
}
{
rc := newRunContainer16CopyIv([]interval16{newInterval16Range(4, 9)})
assert.EqualValues(t, 6, rc.getCardinality())
it := rc.newRunIterator16()
assert.True(t, it.hasNext())
for i := 4; i < 10; i++ {
assert.Equal(t, uint16(i), it.next())
}
assert.False(t, it.hasNext())
}
{
// basic nextMany test
rc := newRunContainer16CopyIv([]interval16{newInterval16Range(4, 9)})
assert.EqualValues(t, 6, rc.getCardinality())
it := rc.newManyRunIterator16()
buf := make([]uint32, 10)
n := it.nextMany(0, buf)
assert.Equal(t, 6, n)
expected := []uint32{4, 5, 6, 7, 8, 9, 0, 0, 0, 0}
for i, e := range expected {
assert.Equal(t, e, buf[i])
}
}
{
// nextMany with len(buf) == 0
rc := newRunContainer16CopyIv([]interval16{newInterval16Range(4, 9)})
assert.EqualValues(t, 6, rc.getCardinality())
it := rc.newManyRunIterator16()
var buf []uint32
n := it.nextMany(0, buf)
assert.Equal(t, 0, n)
}
{
// basic nextMany test across ranges
rc := newRunContainer16CopyIv([]interval16{
newInterval16Range(4, 7),
newInterval16Range(11, 13),
newInterval16Range(18, 21),
})
assert.EqualValues(t, 11, rc.getCardinality())
it := rc.newManyRunIterator16()
buf := make([]uint32, 15)
n := it.nextMany(0, buf)
assert.Equal(t, 11, n)
expected := []uint32{4, 5, 6, 7, 11, 12, 13, 18, 19, 20, 21, 0, 0, 0, 0}
for i, e := range expected {
assert.Equal(t, e, buf[i])
}
}
{
// basic nextMany test across ranges with different buffer sizes
rc := newRunContainer16CopyIv([]interval16{
newInterval16Range(4, 7),
newInterval16Range(11, 13),
newInterval16Range(18, 21),
})
expectedCard := 11
expectedVals := []uint32{4, 5, 6, 7, 11, 12, 13, 18, 19, 20, 21}
hs := uint32(1 << 16)
assert.EqualValues(t, expectedCard, rc.getCardinality())
for bufSize := 2; bufSize < 15; bufSize++ {
buf := make([]uint32, bufSize)
seen := 0
it := rc.newManyRunIterator16()
for n := it.nextMany(hs, buf); n != 0; n = it.nextMany(hs, buf) {
// catch runaway iteration
assert.LessOrEqual(t, seen+n, expectedCard)
for i, e := range expectedVals[seen : seen+n] {
assert.Equal(t, e+hs, buf[i])
}
seen += n
// if we have more values to return then we shouldn't leave empty slots in the buffer
if seen < expectedCard {
assert.Equal(t, bufSize, n)
}
}
assert.Equal(t, expectedCard, seen)
}
}
{
// basic nextMany interaction with hasNext
rc := newRunContainer16CopyIv([]interval16{newInterval16Range(4, 4)})
assert.EqualValues(t, 1, rc.getCardinality())
it := rc.newManyRunIterator16()
assert.True(t, it.hasNext())
buf := make([]uint32, 4)
n := it.nextMany(0, buf)
assert.Equal(t, 1, n)
expected := []uint32{4, 0, 0, 0}
for i, e := range expected {
assert.Equal(t, e, buf[i])
}
assert.False(t, it.hasNext())
buf = make([]uint32, 4)
n = it.nextMany(0, buf)
assert.Equal(t, 0, n)
expected = []uint32{0, 0, 0, 0}
for i, e := range expected {
assert.Equal(t, e, buf[i])
}
}
{
rc := newRunContainer16TakeOwnership([]interval16{
newInterval16Range(0, 0),
newInterval16Range(2, 2),
newInterval16Range(4, 4),
})
rc1 := newRunContainer16TakeOwnership([]interval16{
newInterval16Range(6, 7),
newInterval16Range(10, 11),
newInterval16Range(MaxUint16, MaxUint16),
})
rc = rc.union(rc1)
assert.EqualValues(t, 8, rc.getCardinality())
it := rc.newRunIterator16()
assert.EqualValues(t, 0, it.next())
assert.EqualValues(t, 2, it.next())
assert.EqualValues(t, 4, it.next())
assert.EqualValues(t, 6, it.next())
assert.EqualValues(t, 7, it.next())
assert.EqualValues(t, 10, it.next())
assert.EqualValues(t, 11, it.next())
assert.EqualValues(t, MaxUint16, it.next())
assert.False(t, it.hasNext())
newInterval16Range(0, MaxUint16)
rc2 := newRunContainer16TakeOwnership([]interval16{newInterval16Range(0, MaxUint16)})
rc2 = rc2.union(rc)
assert.Equal(t, 1, rc2.numIntervals())
}
})
}
func TestRleRunReverseIterator16(t *testing.T) {
t.Run("RunReverseIterator16 unit tests for next, hasNext, and peekNext should pass", func(t *testing.T) {
{
rc := newRunContainer16()
it := rc.newRunReverseIterator16()
assert.False(t, it.hasNext())
assert.Panics(t, func() { it.next() })
}
{
rc := newRunContainer16TakeOwnership([]interval16{newInterval16Range(0, 0)})
it := rc.newRunReverseIterator16()
assert.True(t, it.hasNext())
assert.EqualValues(t, uint16(0), it.next())
assert.Panics(t, func() { it.next() })
assert.False(t, it.hasNext())
assert.Panics(t, func() { it.next() })
}
{
rc := newRunContainer16TakeOwnership([]interval16{newInterval16Range(4, 4)})
it := rc.newRunReverseIterator16()
assert.True(t, it.hasNext())
assert.EqualValues(t, uint16(4), it.next())
assert.False(t, it.hasNext())
}
{
rc := newRunContainer16TakeOwnership([]interval16{newInterval16Range(MaxUint16, MaxUint16)})
it := rc.newRunReverseIterator16()
assert.True(t, it.hasNext())
assert.EqualValues(t, uint16(MaxUint16), it.next())
assert.False(t, it.hasNext())
}
{
rc := newRunContainer16TakeOwnership([]interval16{newInterval16Range(4, 9)})
it := rc.newRunReverseIterator16()
assert.True(t, it.hasNext())
for i := 9; i >= 4; i-- {
assert.Equal(t, uint16(i), it.next())
if i > 4 {
assert.True(t, it.hasNext())
} else if i == 4 {
assert.False(t, it.hasNext())
}
}
assert.False(t, it.hasNext())
assert.Panics(t, func() { it.next() })
}
{
rc := newRunContainer16TakeOwnership([]interval16{
newInterval16Range(0, 0),
newInterval16Range(2, 2),
newInterval16Range(4, 4),
newInterval16Range(6, 7),
newInterval16Range(10, 12),
newInterval16Range(MaxUint16, MaxUint16),
})
it := rc.newRunReverseIterator16()
assert.Equal(t, uint16(MaxUint16), it.next())
assert.Equal(t, uint16(12), it.next())
assert.Equal(t, uint16(11), it.next())
assert.Equal(t, uint16(10), it.next())
assert.Equal(t, uint16(7), it.next())
assert.Equal(t, uint16(6), it.next())
assert.Equal(t, uint16(4), it.next())
assert.Equal(t, uint16(2), it.next())
assert.Equal(t, uint16(0), it.next())
assert.Equal(t, false, it.hasNext())
assert.Panics(t, func() { it.next() })
}
})
}
func TestRleIntersection16(t *testing.T) {
t.Run("RunContainer16.intersect of two RunContainer16(s) should return their intersection", func(t *testing.T) {
{
vals := []uint16{0, 2, 4, 6, 8, 10, 12, 14, 16, 18, MaxUint16 - 3, MaxUint16 - 1}
a := newRunContainer16FromVals(true, vals[:5]...)
b := newRunContainer16FromVals(true, vals[2:]...)
assert.True(t, haveOverlap16(newInterval16Range(0, 2), newInterval16Range(2, 2)))
assert.False(t, haveOverlap16(newInterval16Range(0, 2), newInterval16Range(3, 3)))
isect := a.intersect(b)
assert.EqualValues(t, 3, isect.getCardinality())
assert.True(t, isect.contains(4))
assert.True(t, isect.contains(6))
assert.True(t, isect.contains(8))
newInterval16Range(0, MaxUint16)
d := newRunContainer16TakeOwnership([]interval16{newInterval16Range(0, MaxUint16)})
isect = isect.intersect(d)
assert.EqualValues(t, 3, isect.getCardinality())
assert.True(t, isect.contains(4))
assert.True(t, isect.contains(6))
assert.True(t, isect.contains(8))
e := newRunContainer16TakeOwnership(
[]interval16{
newInterval16Range(2, 4),
newInterval16Range(8, 9),
newInterval16Range(14, 16),
newInterval16Range(20, 22),
},
)
f := newRunContainer16TakeOwnership(
[]interval16{
newInterval16Range(3, 18),
newInterval16Range(22, 23),
},
)
{
isect = e.intersect(f)
assert.EqualValues(t, 8, isect.getCardinality())
assert.True(t, isect.contains(3))
assert.True(t, isect.contains(4))
assert.True(t, isect.contains(8))
assert.True(t, isect.contains(9))
assert.True(t, isect.contains(14))
assert.True(t, isect.contains(15))
assert.True(t, isect.contains(16))
assert.True(t, isect.contains(22))
}
{
// check for symmetry
isect = f.intersect(e)
assert.EqualValues(t, 8, isect.getCardinality())
assert.True(t, isect.contains(3))
assert.True(t, isect.contains(4))
assert.True(t, isect.contains(8))
assert.True(t, isect.contains(9))
assert.True(t, isect.contains(14))
assert.True(t, isect.contains(15))
assert.True(t, isect.contains(16))
assert.True(t, isect.contains(22))
}
}
})
}
func TestRleRandomIntersection16(t *testing.T) {
t.Run("RunContainer.intersect of two RunContainers should return their intersection, and this should hold over randomized container content when compared to intersection done with hash maps", func(t *testing.T) {
seed := int64(42)
rand.Seed(seed)
trials := []trial{
{n: 100, percentFill: .80, ntrial: 10},
{n: 1000, percentFill: .20, ntrial: 20},
{n: 10000, percentFill: .01, ntrial: 10},
{n: 1000, percentFill: .99, ntrial: 10},
}
tester := func(tr trial) {
for j := 0; j < tr.ntrial; j++ {
ma := make(map[int]bool)
mb := make(map[int]bool)
n := tr.n
a := []uint16{}
b := []uint16{}
var first, second int
draw := int(float64(n) * tr.percentFill)
for i := 0; i < draw; i++ {
r0 := rand.Intn(n)
a = append(a, uint16(r0))
ma[r0] = true
if i == 0 {
first = r0
second = r0 + 1
a = append(a, uint16(second))
ma[second] = true
}
r1 := rand.Intn(n)
b = append(b, uint16(r1))
mb[r1] = true
}
// print a; very likely it has dups
sort.Sort(uint16Slice(a))
stringA := ""
for i := range a {
stringA += fmt.Sprintf("%v, ", a[i])
}
// hash version of intersect:
hashi := make(map[int]bool)
for k := range ma {
if mb[k] {
hashi[k] = true
}
}
// RunContainer's Intersect
brle := newRunContainer16FromVals(false, b...)
// arle := newRunContainer16FromVals(false, a...)
// instead of the above line, create from array
// get better test coverage:
arr := newArrayContainerRange(int(first), int(second))
arle := newRunContainer16FromArray(arr)
arle.set(false, a...)
isect := arle.intersect(brle)
// showHash("hashi", hashi)
for k := range hashi {
assert.True(t, isect.contains(uint16(k)))
}
assert.EqualValues(t, len(hashi), isect.getCardinality())
}
}
for i := range trials {
tester(trials[i])
}
})
}
func TestRleRandomUnion16(t *testing.T) {
t.Run("RunContainer.union of two RunContainers should return their union, and this should hold over randomized container content when compared to union done with hash maps", func(t *testing.T) {
seed := int64(42)
rand.Seed(seed)
trials := []trial{
{n: 100, percentFill: .80, ntrial: 10},
{n: 1000, percentFill: .20, ntrial: 20},
{n: 10000, percentFill: .01, ntrial: 10},
{n: 1000, percentFill: .99, ntrial: 10, percentDelete: .04},
}
tester := func(tr trial) {
for j := 0; j < tr.ntrial; j++ {
ma := make(map[int]bool)
mb := make(map[int]bool)
n := tr.n
a := []uint16{}
b := []uint16{}
draw := int(float64(n) * tr.percentFill)
numDel := int(float64(n) * tr.percentDelete)
for i := 0; i < draw; i++ {
r0 := rand.Intn(n)
a = append(a, uint16(r0))
ma[r0] = true
r1 := rand.Intn(n)
b = append(b, uint16(r1))
mb[r1] = true
}
// hash version of union:
hashu := make(map[int]bool)
for k := range ma {
hashu[k] = true
}
for k := range mb {
hashu[k] = true
}
// showHash("hashu", hashu)
// RunContainer's Union
arle := newRunContainer16()
for i := range a {
arle.Add(a[i])
}
brle := newRunContainer16()
brle.set(false, b...)
union := arle.union(brle)
un := union.AsSlice()
sort.Sort(uint16Slice(un))
for kk, v := range un {
_ = kk
assert.True(t, hashu[int(v)])
}
for k := range hashu {
assert.True(t, union.contains(uint16(k)))
}
assert.EqualValues(t, len(hashu), union.getCardinality())
// do the deletes, exercising the remove functionality
for i := 0; i < numDel; i++ {
r1 := rand.Intn(len(a))
goner := a[r1]
union.removeKey(goner)
delete(hashu, int(goner))
}
// verify the same as in the hashu
assert.EqualValues(t, len(hashu), union.getCardinality())
for k := range hashu {
assert.True(t, union.contains(uint16(k)))
}
}
}
for i := range trials {
tester(trials[i])
}
})
}
func TestRleAndOrXor16(t *testing.T) {
t.Run("RunContainer And, Or, Xor tests", func(t *testing.T) {
{
rc := newRunContainer16TakeOwnership([]interval16{
newInterval16Range(0, 0),
newInterval16Range(2, 2),
newInterval16Range(4, 4),
})
b0 := NewBitmap()
b0.Add(2)
b0.Add(6)
b0.Add(8)
and := rc.And(b0)
or := rc.Or(b0)
xor := rc.Xor(b0)
assert.EqualValues(t, 1, and.GetCardinality())
assert.EqualValues(t, 5, or.GetCardinality())
assert.EqualValues(t, 4, xor.GetCardinality())
// test creating size 0 and 1 from array
arr := newArrayContainerCapacity(0)
empty := newRunContainer16FromArray(arr)
onceler := newArrayContainerCapacity(1)
onceler.content = append(onceler.content, uint16(0))
oneZero := newRunContainer16FromArray(onceler)
assert.EqualValues(t, 0, empty.getCardinality())
assert.EqualValues(t, 1, oneZero.getCardinality())
assert.EqualValues(t, 0, empty.And(b0).GetCardinality())
assert.EqualValues(t, 3, empty.Or(b0).GetCardinality())
// exercise newRunContainer16FromVals() with 0 and 1 inputs.
empty2 := newRunContainer16FromVals(false, []uint16{}...)
assert.EqualValues(t, 0, empty2.getCardinality())
one2 := newRunContainer16FromVals(false, []uint16{1}...)
assert.EqualValues(t, 1, one2.getCardinality())
}
})
}
func TestRlePanics16(t *testing.T) {
t.Run("Some RunContainer calls/methods should panic if misused", func(t *testing.T) {
// newRunContainer16FromVals
assert.Panics(t, func() { newRunContainer16FromVals(true, 1, 0) })
arr := newArrayContainerRange(1, 3)
arr.content = []uint16{2, 3, 3, 2, 1}
assert.Panics(t, func() { newRunContainer16FromArray(arr) })
})
}
func TestRleCoverageOddsAndEnds16(t *testing.T) {
t.Run("Some RunContainer code paths that don't otherwise get coverage -- these should be tested to increase percentage of code coverage in testing", func(t *testing.T) {
rc := &runContainer16{}
assert.Equal(t, "runContainer16{}", rc.String())
rc.iv = make([]interval16, 1)
rc.iv[0] = newInterval16Range(3, 4)
assert.Equal(t, "runContainer16{0:[3, 4], }", rc.String())
a := newInterval16Range(5, 9)
b := newInterval16Range(0, 1)
c := newInterval16Range(1, 2)
// intersectInterval16s(a, b interval16)
isect, isEmpty := intersectInterval16s(a, b)
assert.True(t, isEmpty)
// [0,0] can't be trusted: assert.Equal(t, 0, isect.runlen())
isect, isEmpty = intersectInterval16s(b, c)
assert.False(t, isEmpty)
assert.EqualValues(t, 1, isect.runlen())
// runContainer16.union
{
ra := newRunContainer16FromVals(false, 4, 5)
rb := newRunContainer16FromVals(false, 4, 6, 8, 9, 10)
ra.union(rb)
assert.EqualValues(t, 2, rb.indexOfIntervalAtOrAfter(4, 2))
assert.EqualValues(t, 2, rb.indexOfIntervalAtOrAfter(3, 2))
}
// runContainer.intersect
{
ra := newRunContainer16()
rb := newRunContainer16()
assert.EqualValues(t, 0, ra.intersect(rb).getCardinality())
}
{
ra := newRunContainer16FromVals(false, 1)
rb := newRunContainer16FromVals(false, 4)
assert.EqualValues(t, 0, ra.intersect(rb).getCardinality())
}
// runContainer.Add
{
ra := newRunContainer16FromVals(false, 1)
rb := newRunContainer16FromVals(false, 4)
assert.EqualValues(t, 1, ra.getCardinality())
assert.EqualValues(t, 1, rb.getCardinality())
ra.Add(5)
assert.EqualValues(t, 2, ra.getCardinality())
// newRunIterator16()
empty := newRunContainer16()
it := empty.newRunIterator16()
assert.Panics(t, func() { it.next() })
it2 := ra.newRunIterator16()
it2.curIndex = len(it2.rc.iv)
assert.Panics(t, func() { it2.next() })
// runIterator16.peekNext()
emptyIt := empty.newRunIterator16()
assert.Panics(t, func() { emptyIt.peekNext() })
// newRunContainer16FromArray
arr := newArrayContainerRange(1, 6)
arr.content = []uint16{5, 5, 5, 6, 9}
rc3 := newRunContainer16FromArray(arr)
assert.EqualValues(t, 3, rc3.getCardinality())
// runContainer16SerializedSizeInBytes
// runContainer16.SerializedSizeInBytes
_ = runContainer16SerializedSizeInBytes(3)
_ = rc3.serializedSizeInBytes()
// findNextIntervalThatIntersectsStartingFrom
idx, _ := rc3.findNextIntervalThatIntersectsStartingFrom(0, 100)
assert.EqualValues(t, 1, idx)
// deleteAt / remove
rc3.Add(10)
rc3.removeKey(10)
rc3.removeKey(9)
assert.EqualValues(t, 2, rc3.getCardinality())
rc3.Add(9)
rc3.Add(10)
rc3.Add(12)
assert.EqualValues(t, 5, rc3.getCardinality())
it3 := rc3.newRunIterator16()
it3.next()
it3.next()
it3.next()
it3.next()
assert.EqualValues(t, 12, it3.peekNext())
assert.EqualValues(t, 12, it3.next())
}
// runContainer16.equals
{
rc16 := newRunContainer16()
assert.True(t, rc16.equals16(rc16))
rc16b := newRunContainer16()
assert.True(t, rc16.equals16(rc16b))
rc16.Add(1)
rc16b.Add(2)
assert.False(t, rc16.equals16(rc16b))
}
})
}
func TestRleStoringMax16(t *testing.T) {
t.Run("Storing the MaxUint16 should be possible, because it may be necessary to do so--users will assume that any valid uint16 should be storable. In particular the smaller 16-bit version will definitely expect full access to all bits.", func(t *testing.T) {
rc := newRunContainer16()
rc.Add(MaxUint16)
assert.True(t, rc.contains(MaxUint16))
assert.EqualValues(t, 1, rc.getCardinality())
rc.removeKey(MaxUint16)
assert.False(t, rc.contains(MaxUint16))
assert.EqualValues(t, 0, rc.getCardinality())
rc.set(false, MaxUint16-1, MaxUint16)
assert.EqualValues(t, 2, rc.getCardinality())
assert.True(t, rc.contains(MaxUint16-1))
assert.True(t, rc.contains(MaxUint16))
rc.removeKey(MaxUint16 - 1)
assert.EqualValues(t, 1, rc.getCardinality())
rc.removeKey(MaxUint16)
assert.EqualValues(t, 0, rc.getCardinality())
rc.set(false, MaxUint16-2, MaxUint16-1, MaxUint16)
assert.EqualValues(t, 3, rc.getCardinality())
assert.EqualValues(t, 1, rc.numIntervals())
rc.removeKey(MaxUint16 - 1)
assert.EqualValues(t, 2, rc.numIntervals())
assert.EqualValues(t, 2, rc.getCardinality())
})
}
// go test -bench BenchmarkFromBitmap -run -
func BenchmarkFromBitmap16(b *testing.B) {
b.StopTimer()
seed := int64(42)
rand.Seed(seed)
tr := trial{n: 10000, percentFill: .95, ntrial: 1, numRandomOpsPass: 100}
_, _, bc := getRandomSameThreeContainers(tr)
b.StartTimer()
for j := 0; j < b.N; j++ {
newRunContainer16FromBitmapContainer(bc)
}
}
func TestRle16RandomIntersectAgainstOtherContainers010(t *testing.T) {
t.Run("runContainer16 `and` operation against other container types should correctly do the intersection", func(t *testing.T) {
seed := int64(42)
rand.Seed(seed)
trials := []trial{
{n: 100, percentFill: .95, ntrial: 1},
}
tester := func(tr trial) {
for j := 0; j < tr.ntrial; j++ {
ma := make(map[int]bool)
mb := make(map[int]bool)
n := tr.n
a := []uint16{}
b := []uint16{}
draw := int(float64(n) * tr.percentFill)
for i := 0; i < draw; i++ {
r0 := rand.Intn(n)
a = append(a, uint16(r0))
ma[r0] = true
r1 := rand.Intn(n)
b = append(b, uint16(r1))
mb[r1] = true
}
// showArray16(a, "a")
// showArray16(b, "b")
// hash version of intersect:
hashi := make(map[int]bool)
for k := range ma {
if mb[k] {
hashi[k] = true
}
}
// RunContainer's Intersect
rc := newRunContainer16FromVals(false, a...)
// vs bitmapContainer
bc := newBitmapContainer()
for _, bv := range b {
bc.iadd(bv)
}
// vs arrayContainer
ac := newArrayContainer()
for _, bv := range b {
ac.iadd(bv)
}
// vs runContainer
rcb := newRunContainer16FromVals(false, b...)
rcVsBcIsect := rc.and(bc)
rcVsAcIsect := rc.and(ac)
rcVsRcbIsect := rc.and(rcb)
for k := range hashi {
assert.True(t, rcVsBcIsect.contains(uint16(k)))
assert.True(t, rcVsAcIsect.contains(uint16(k)))
assert.True(t, rcVsRcbIsect.contains(uint16(k)))
}
assert.Equal(t, len(hashi), rcVsBcIsect.getCardinality())
assert.Equal(t, len(hashi), rcVsAcIsect.getCardinality())
assert.Equal(t, len(hashi), rcVsRcbIsect.getCardinality())
}
}
for i := range trials {
tester(trials[i])
}
})
}
func TestRle16RandomUnionAgainstOtherContainers011(t *testing.T) {
t.Run("runContainer16 `or` operation against other container types should correctly do the intersection", func(t *testing.T) {
seed := int64(42)
rand.Seed(seed)
trials := []trial{
{n: 100, percentFill: .95, ntrial: 1},
}
tester := func(tr trial) {
for j := 0; j < tr.ntrial; j++ {
ma := make(map[int]bool)
mb := make(map[int]bool)
n := tr.n
a := []uint16{}
b := []uint16{}
draw := int(float64(n) * tr.percentFill)
for i := 0; i < draw; i++ {
r0 := rand.Intn(n)
a = append(a, uint16(r0))
ma[r0] = true
r1 := rand.Intn(n)
b = append(b, uint16(r1))
mb[r1] = true
}
// showArray16(a, "a")
// showArray16(b, "b")
// hash version of union
hashi := make(map[int]bool)
for k := range ma {
hashi[k] = true
}
for k := range mb {
hashi[k] = true
}
// RunContainer's 'or'
rc := newRunContainer16FromVals(false, a...)
// vs bitmapContainer
bc := newBitmapContainer()
for _, bv := range b {
bc.iadd(bv)