forked from RoaringBitmap/roaring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runcontainer_test.go
2434 lines (1956 loc) · 58.7 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"
. "github.com/smartystreets/goconvey/convey"
"math/rand"
"sort"
"strings"
"testing"
)
// 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
}
func TestRleInterval16s(t *testing.T) {
Convey("canMerge, and mergeInterval16s should do what they say", t, func() {
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)
So(empty, ShouldBeFalse)
So(aIb, ShouldResemble, b)
So(canMerge16(b, c), ShouldBeTrue)
So(canMerge16(c, b), ShouldBeTrue)
So(canMerge16(a, h), ShouldBeTrue)
So(canMerge16(d, e), ShouldBeTrue)
So(canMerge16(f, g), ShouldBeTrue)
So(canMerge16(c, h), ShouldBeTrue)
So(canMerge16(b, h), ShouldBeFalse)
So(canMerge16(h, b), ShouldBeFalse)
So(canMerge16(c, i), ShouldBeFalse)
So(mergeInterval16s(b, c), ShouldResemble, e)
So(mergeInterval16s(c, b), ShouldResemble, e)
So(mergeInterval16s(h, i), ShouldResemble, h)
So(mergeInterval16s(i, h), ShouldResemble, h)
////// start
So(mergeInterval16s(newInterval16Range(0, 0), newInterval16Range(1, 1)), ShouldResemble, newInterval16Range(0, 1))
So(mergeInterval16s(newInterval16Range(1, 1), newInterval16Range(0, 0)), ShouldResemble, newInterval16Range(0, 1))
So(mergeInterval16s(newInterval16Range(0, 4), newInterval16Range(3, 5)), ShouldResemble, newInterval16Range(0, 5))
So(mergeInterval16s(newInterval16Range(0, 4), newInterval16Range(3, 4)), ShouldResemble, newInterval16Range(0, 4))
So(mergeInterval16s(newInterval16Range(0, 8), newInterval16Range(1, 7)), ShouldResemble, newInterval16Range(0, 8))
So(mergeInterval16s(newInterval16Range(1, 7), newInterval16Range(0, 8)), ShouldResemble, newInterval16Range(0, 8))
So(func() { _ = mergeInterval16s(newInterval16Range(0, 0), newInterval16Range(2, 3)) }, ShouldPanic)
})
}
func TestRunOffset(t *testing.T) {
v := newRunContainer16TakeOwnership([]interval16{newInterval16Range(34, 39)})
offtest := uint16(65500)
w := v.addOffset(offtest)
w0card := w[0].getCardinality()
w1card := w[1].getCardinality()
t.Logf("%d %d", w0card, w1card)
if w0card+w1card != 6 {
t.Errorf("Bogus cardinality.")
}
expected := []int{65534, 65535, 65536, 65537, 65538, 65539}
wout := make([]int, len(expected))
for i := 0; i < w0card; i++ {
wout[i] = w[0].selectInt(uint16(i))
}
for i := 0; i < w1card; i++ {
wout[i+w0card] = w[1].selectInt(uint16(i)) + 65536
}
t.Logf("%v %v", wout, expected)
for i, x := range wout {
if x != expected[i] {
t.Errorf("found discrepancy %d!=%d", x, expected[i])
}
}
}
func TestRleRunIterator16(t *testing.T) {
Convey("RunIterator16 unit tests for Cur, Next, HasNext, and Remove should pass", t, func() {
{
rc := newRunContainer16()
msg := rc.String()
_ = msg
So(rc.cardinality(), ShouldEqual, 0)
it := rc.newRunIterator16()
So(it.hasNext(), ShouldBeFalse)
}
{
rc := newRunContainer16TakeOwnership([]interval16{newInterval16Range(4, 4)})
So(rc.cardinality(), ShouldEqual, 1)
it := rc.newRunIterator16()
So(it.hasNext(), ShouldBeTrue)
So(it.next(), ShouldResemble, uint16(4))
So(it.cur(), ShouldResemble, uint16(4))
}
{
rc := newRunContainer16CopyIv([]interval16{newInterval16Range(4, 9)})
So(rc.cardinality(), ShouldEqual, 6)
it := rc.newRunIterator16()
So(it.hasNext(), ShouldBeTrue)
for i := 4; i < 10; i++ {
So(it.next(), ShouldEqual, uint16(i))
}
So(it.hasNext(), ShouldBeFalse)
}
{
// basic nextMany test
rc := newRunContainer16CopyIv([]interval16{newInterval16Range(4, 9)})
So(rc.cardinality(), ShouldEqual, 6)
it := rc.newManyRunIterator16()
buf := make([]uint32, 10)
n := it.nextMany(0, buf)
So(n, ShouldEqual, 6)
expected := []uint32{4, 5, 6, 7, 8, 9, 0, 0, 0, 0}
for i, e := range expected {
So(buf[i], ShouldEqual, e)
}
}
{
// nextMany with len(buf) == 0
rc := newRunContainer16CopyIv([]interval16{newInterval16Range(4, 9)})
So(rc.cardinality(), ShouldEqual, 6)
it := rc.newManyRunIterator16()
var buf []uint32
n := it.nextMany(0, buf)
So(n, ShouldEqual, 0)
}
{
// basic nextMany test across ranges
rc := newRunContainer16CopyIv([]interval16{
newInterval16Range(4, 7),
newInterval16Range(11, 13),
newInterval16Range(18, 21)})
So(rc.cardinality(), ShouldEqual, 11)
it := rc.newManyRunIterator16()
buf := make([]uint32, 15)
n := it.nextMany(0, buf)
So(n, ShouldEqual, 11)
expected := []uint32{4, 5, 6, 7, 11, 12, 13, 18, 19, 20, 21, 0, 0, 0, 0}
for i, e := range expected {
So(buf[i], ShouldEqual, e)
}
}
{
// 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)
So(rc.cardinality(), ShouldEqual, expectedCard)
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
So(seen+n, ShouldBeLessThanOrEqualTo, expectedCard)
for i, e := range expectedVals[seen : seen+n] {
So(buf[i], ShouldEqual, e+hs)
}
seen += n
// if we have more values to return then we shouldn't leave empty slots in the buffer
if seen < expectedCard {
So(n, ShouldEqual, bufSize)
}
}
So(seen, ShouldEqual, expectedCard)
}
}
{
// basic nextMany interaction with hasNext
rc := newRunContainer16CopyIv([]interval16{newInterval16Range(4, 4)})
So(rc.cardinality(), ShouldEqual, 1)
it := rc.newManyRunIterator16()
So(it.hasNext(), ShouldBeTrue)
buf := make([]uint32, 4)
n := it.nextMany(0, buf)
So(n, ShouldEqual, 1)
expected := []uint32{4, 0, 0, 0}
for i, e := range expected {
So(buf[i], ShouldEqual, e)
}
So(it.hasNext(), ShouldBeFalse)
buf = make([]uint32, 4)
n = it.nextMany(0, buf)
So(n, ShouldEqual, 0)
expected = []uint32{0, 0, 0, 0}
for i, e := range expected {
So(buf[i], ShouldEqual, e)
}
}
{
rc := newRunContainer16TakeOwnership([]interval16{newInterval16Range(4, 9)})
card := rc.cardinality()
So(card, ShouldEqual, 6)
it := rc.newRunIterator16()
So(it.hasNext(), ShouldBeTrue)
for i := 4; i < 6; i++ {
So(it.next(), ShouldEqual, uint16(i))
}
So(it.cur(), ShouldEqual, uint16(5))
So(it.remove(), ShouldEqual, uint16(5))
So(rc.cardinality(), ShouldEqual, 5)
it2 := rc.newRunIterator16()
So(rc.cardinality(), ShouldEqual, 5)
So(it2.next(), ShouldEqual, uint16(4))
for i := 6; i < 10; i++ {
So(it2.next(), ShouldEqual, uint16(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)
So(rc.cardinality(), ShouldEqual, 8)
it := rc.newRunIterator16()
So(it.next(), ShouldEqual, uint16(0))
So(it.next(), ShouldEqual, uint16(2))
So(it.next(), ShouldEqual, uint16(4))
So(it.next(), ShouldEqual, uint16(6))
So(it.next(), ShouldEqual, uint16(7))
So(it.next(), ShouldEqual, uint16(10))
So(it.next(), ShouldEqual, uint16(11))
So(it.next(), ShouldEqual, uint16(MaxUint16))
So(it.hasNext(), ShouldEqual, false)
newInterval16Range(0, MaxUint16)
rc2 := newRunContainer16TakeOwnership([]interval16{newInterval16Range(0, MaxUint16)})
rc2 = rc2.union(rc)
So(rc2.numIntervals(), ShouldEqual, 1)
}
})
}
func TestRleRunReverseIterator16(t *testing.T) {
Convey("RunReverseIterator16 unit tests for cur, next, hasNext, and remove should pass", t, func() {
{
rc := newRunContainer16()
it := rc.newRunReverseIterator16()
So(it.hasNext(), ShouldBeFalse)
So(func() { it.next() }, ShouldPanic)
So(func() { it.remove() }, ShouldPanic)
}
{
rc := newRunContainer16TakeOwnership([]interval16{newInterval16Range(0, 0)})
it := rc.newRunReverseIterator16()
So(it.hasNext(), ShouldBeTrue)
So(it.next(), ShouldResemble, uint16(0))
So(it.cur(), ShouldResemble, uint16(0))
So(func() { it.next() }, ShouldPanic)
So(it.remove(), ShouldEqual, uint16(0))
So(func() { it.remove() }, ShouldPanic)
So(it.hasNext(), ShouldBeFalse)
So(func() { it.next() }, ShouldPanic)
}
{
rc := newRunContainer16TakeOwnership([]interval16{newInterval16Range(4, 4)})
it := rc.newRunReverseIterator16()
So(it.hasNext(), ShouldBeTrue)
So(it.next(), ShouldResemble, uint16(4))
So(it.cur(), ShouldResemble, uint16(4))
So(it.hasNext(), ShouldBeFalse)
}
{
rc := newRunContainer16TakeOwnership([]interval16{newInterval16Range(MaxUint16, MaxUint16)})
it := rc.newRunReverseIterator16()
So(it.hasNext(), ShouldBeTrue)
So(it.next(), ShouldResemble, uint16(MaxUint16))
So(it.cur(), ShouldResemble, uint16(MaxUint16))
So(it.hasNext(), ShouldBeFalse)
}
{
rc := newRunContainer16TakeOwnership([]interval16{newInterval16Range(4, 9)})
it := rc.newRunReverseIterator16()
So(it.hasNext(), ShouldBeTrue)
for i := 9; i >= 4; i-- {
So(it.next(), ShouldEqual, uint16(i))
if i > 4 {
So(it.hasNext(), ShouldBeTrue)
} else if i == 4 {
So(it.hasNext(), ShouldBeFalse)
}
}
So(it.hasNext(), ShouldBeFalse)
So(func() { it.next() }, ShouldPanic)
}
{
rc := newRunContainer16TakeOwnership([]interval16{newInterval16Range(4, 9)})
it := rc.newRunReverseIterator16()
So(it.hasNext(), ShouldBeTrue)
for i := 9; i >= 5; i-- {
So(it.next(), ShouldEqual, uint16(i))
}
So(it.cur(), ShouldEqual, uint16(5))
So(it.remove(), ShouldEqual, uint16(5))
So(rc.cardinality(), ShouldEqual, 5)
it2 := rc.newRunReverseIterator16()
So(rc.cardinality(), ShouldEqual, 5)
So(it2.next(), ShouldEqual, uint16(9))
for i := 8; i > 5; i-- {
So(it2.next(), ShouldEqual, uint16(i))
}
}
{
rc := newRunContainer16TakeOwnership([]interval16{
newInterval16Range(0, 0),
newInterval16Range(2, 2),
newInterval16Range(4, 4),
newInterval16Range(6, 7),
newInterval16Range(10, 12),
newInterval16Range(MaxUint16, MaxUint16),
})
it := rc.newRunReverseIterator16()
So(it.next(), ShouldEqual, uint16(MaxUint16))
So(it.next(), ShouldEqual, uint16(12))
So(it.next(), ShouldEqual, uint16(11))
So(it.next(), ShouldEqual, uint16(10))
So(it.next(), ShouldEqual, uint16(7))
So(it.next(), ShouldEqual, uint16(6))
So(it.next(), ShouldEqual, uint16(4))
So(it.next(), ShouldEqual, uint16(2))
So(it.next(), ShouldEqual, uint16(0))
So(it.hasNext(), ShouldEqual, false)
So(func() { it.next() }, ShouldPanic)
}
})
}
func TestRleRunSearch16(t *testing.T) {
Convey("RunContainer16.search should respect the prior bounds we provide for efficiency of searching through a subset of the intervals", t, func() {
{
vals := []uint16{0, 2, 4, 6, 8, 10, 12, 14, 16, 18, MaxUint16 - 3, MaxUint16}
exAt := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11} // expected at
absent := []uint16{1, 3, 5, 7, 9, 11, 13, 15, 17, 19, MaxUint16 - 2}
rc := newRunContainer16FromVals(true, vals...)
So(rc.cardinality(), ShouldEqual, 12)
var where int64
var present bool
for i, v := range vals {
where, present, _ = rc.search(int64(v), nil)
So(present, ShouldBeTrue)
So(where, ShouldEqual, exAt[i])
}
for i, v := range absent {
where, present, _ = rc.search(int64(v), nil)
So(present, ShouldBeFalse)
So(where, ShouldEqual, i)
}
// delete the MaxUint16 so we can test
// the behavior when searching near upper limit.
So(rc.cardinality(), ShouldEqual, 12)
So(rc.numIntervals(), ShouldEqual, 12)
rc.removeKey(MaxUint16)
So(rc.cardinality(), ShouldEqual, 11)
So(rc.numIntervals(), ShouldEqual, 11)
where, present, _ = rc.search(MaxUint16, nil)
So(present, ShouldBeFalse)
So(where, ShouldEqual, 10)
var numCompares int
where, present, numCompares = rc.search(MaxUint16, nil)
So(present, ShouldBeFalse)
So(where, ShouldEqual, 10)
So(numCompares, ShouldEqual, 3)
opts := &searchOptions{
startIndex: 5,
}
where, present, numCompares = rc.search(MaxUint16, opts)
So(present, ShouldBeFalse)
So(where, ShouldEqual, 10)
So(numCompares, ShouldEqual, 2)
where, present, _ = rc.search(MaxUint16-3, opts)
So(present, ShouldBeTrue)
So(where, ShouldEqual, 10)
// with the bound in place, MaxUint16-3 should not be found
opts.endxIndex = 10
where, present, _ = rc.search(MaxUint16-3, opts)
So(present, ShouldBeFalse)
So(where, ShouldEqual, 9)
}
})
}
func TestRleIntersection16(t *testing.T) {
Convey("RunContainer16.intersect of two RunContainer16(s) should return their intersection", t, func() {
{
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:]...)
So(haveOverlap16(newInterval16Range(0, 2), newInterval16Range(2, 2)), ShouldBeTrue)
So(haveOverlap16(newInterval16Range(0, 2), newInterval16Range(3, 3)), ShouldBeFalse)
isect := a.intersect(b)
So(isect.cardinality(), ShouldEqual, 3)
So(isect.contains(4), ShouldBeTrue)
So(isect.contains(6), ShouldBeTrue)
So(isect.contains(8), ShouldBeTrue)
newInterval16Range(0, MaxUint16)
d := newRunContainer16TakeOwnership([]interval16{newInterval16Range(0, MaxUint16)})
isect = isect.intersect(d)
So(isect.cardinality(), ShouldEqual, 3)
So(isect.contains(4), ShouldBeTrue)
So(isect.contains(6), ShouldBeTrue)
So(isect.contains(8), ShouldBeTrue)
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)
So(isect.cardinality(), ShouldEqual, 8)
So(isect.contains(3), ShouldBeTrue)
So(isect.contains(4), ShouldBeTrue)
So(isect.contains(8), ShouldBeTrue)
So(isect.contains(9), ShouldBeTrue)
So(isect.contains(14), ShouldBeTrue)
So(isect.contains(15), ShouldBeTrue)
So(isect.contains(16), ShouldBeTrue)
So(isect.contains(22), ShouldBeTrue)
}
{
// check for symmetry
isect = f.intersect(e)
So(isect.cardinality(), ShouldEqual, 8)
So(isect.contains(3), ShouldBeTrue)
So(isect.contains(4), ShouldBeTrue)
So(isect.contains(8), ShouldBeTrue)
So(isect.contains(9), ShouldBeTrue)
So(isect.contains(14), ShouldBeTrue)
So(isect.contains(15), ShouldBeTrue)
So(isect.contains(16), ShouldBeTrue)
So(isect.contains(22), ShouldBeTrue)
}
}
})
}
func TestRleRandomIntersection16(t *testing.T) {
Convey("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", t, func() {
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 {
So(isect.contains(uint16(k)), ShouldBeTrue)
}
So(isect.cardinality(), ShouldEqual, len(hashi))
}
}
for i := range trials {
tester(trials[i])
}
})
}
func TestRleRandomUnion16(t *testing.T) {
Convey("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", t, func() {
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
So(hashu[int(v)], ShouldBeTrue)
}
for k := range hashu {
So(union.contains(uint16(k)), ShouldBeTrue)
}
So(union.cardinality(), ShouldEqual, len(hashu))
// 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
So(union.cardinality(), ShouldEqual, len(hashu))
for k := range hashu {
So(union.contains(uint16(k)), ShouldBeTrue)
}
}
}
for i := range trials {
tester(trials[i])
}
})
}
func TestRleAndOrXor16(t *testing.T) {
Convey("RunContainer And, Or, Xor tests", t, func() {
{
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)
So(and.GetCardinality(), ShouldEqual, 1)
So(or.GetCardinality(), ShouldEqual, 5)
So(xor.GetCardinality(), ShouldEqual, 4)
// 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)
So(empty.cardinality(), ShouldEqual, 0)
So(oneZero.cardinality(), ShouldEqual, 1)
So(empty.And(b0).GetCardinality(), ShouldEqual, 0)
So(empty.Or(b0).GetCardinality(), ShouldEqual, 3)
// exercise newRunContainer16FromVals() with 0 and 1 inputs.
empty2 := newRunContainer16FromVals(false, []uint16{}...)
So(empty2.cardinality(), ShouldEqual, 0)
one2 := newRunContainer16FromVals(false, []uint16{1}...)
So(one2.cardinality(), ShouldEqual, 1)
}
})
}
func TestRlePanics16(t *testing.T) {
Convey("Some RunContainer calls/methods should panic if misused", t, func() {
// newRunContainer16FromVals
So(func() { newRunContainer16FromVals(true, 1, 0) }, ShouldPanic)
arr := newArrayContainerRange(1, 3)
arr.content = []uint16{2, 3, 3, 2, 1}
So(func() { newRunContainer16FromArray(arr) }, ShouldPanic)
})
}
func TestRleCoverageOddsAndEnds16(t *testing.T) {
Convey("Some RunContainer code paths that don't otherwise get coverage -- these should be tested to increase percentage of code coverage in testing", t, func() {
rc := &runContainer16{}
So(rc.String(), ShouldEqual, "runContainer16{}")
rc.iv = make([]interval16, 1)
rc.iv[0] = newInterval16Range(3, 4)
So(rc.String(), ShouldEqual, "runContainer16{0:[3, 4], }")
a := newInterval16Range(5, 9)
b := newInterval16Range(0, 1)
c := newInterval16Range(1, 2)
// intersectInterval16s(a, b interval16)
isect, isEmpty := intersectInterval16s(a, b)
So(isEmpty, ShouldBeTrue)
// [0,0] can't be trusted: So(isect.runlen(), ShouldEqual, 0)
isect, isEmpty = intersectInterval16s(b, c)
So(isEmpty, ShouldBeFalse)
So(isect.runlen(), ShouldEqual, 1)
// runContainer16.union
{
ra := newRunContainer16FromVals(false, 4, 5)
rb := newRunContainer16FromVals(false, 4, 6, 8, 9, 10)
ra.union(rb)
So(rb.indexOfIntervalAtOrAfter(4, 2), ShouldEqual, 2)
So(rb.indexOfIntervalAtOrAfter(3, 2), ShouldEqual, 2)
}
// runContainer.intersect
{
ra := newRunContainer16()
rb := newRunContainer16()
So(ra.intersect(rb).cardinality(), ShouldEqual, 0)
}
{
ra := newRunContainer16FromVals(false, 1)
rb := newRunContainer16FromVals(false, 4)
So(ra.intersect(rb).cardinality(), ShouldEqual, 0)
}
// runContainer.Add
{
ra := newRunContainer16FromVals(false, 1)
rb := newRunContainer16FromVals(false, 4)
So(ra.cardinality(), ShouldEqual, 1)
So(rb.cardinality(), ShouldEqual, 1)
ra.Add(5)
So(ra.cardinality(), ShouldEqual, 2)
// newRunIterator16()
empty := newRunContainer16()
it := empty.newRunIterator16()
So(func() { it.next() }, ShouldPanic)
it2 := ra.newRunIterator16()
it2.curIndex = int64(len(it2.rc.iv))
So(func() { it2.next() }, ShouldPanic)
// runIterator16.remove()
emptyIt := empty.newRunIterator16()
So(func() { emptyIt.remove() }, ShouldPanic)
// newRunContainer16FromArray
arr := newArrayContainerRange(1, 6)
arr.content = []uint16{5, 5, 5, 6, 9}
rc3 := newRunContainer16FromArray(arr)
So(rc3.cardinality(), ShouldEqual, 3)
// runContainer16SerializedSizeInBytes
// runContainer16.SerializedSizeInBytes
_ = runContainer16SerializedSizeInBytes(3)
_ = rc3.serializedSizeInBytes()
// findNextIntervalThatIntersectsStartingFrom
idx, _ := rc3.findNextIntervalThatIntersectsStartingFrom(0, 100)
So(idx, ShouldEqual, 1)
// deleteAt / remove
rc3.Add(10)
rc3.removeKey(10)
rc3.removeKey(9)
So(rc3.cardinality(), ShouldEqual, 2)
rc3.Add(9)
rc3.Add(10)
rc3.Add(12)
So(rc3.cardinality(), ShouldEqual, 5)
it3 := rc3.newRunIterator16()
it3.next()
it3.next()
it3.next()
it3.next()
So(it3.cur(), ShouldEqual, uint16(10))
it3.remove()
So(it3.next(), ShouldEqual, uint16(12))
}
// runContainer16.equals
{
rc16 := newRunContainer16()
So(rc16.equals16(rc16), ShouldBeTrue)
rc16b := newRunContainer16()
So(rc16.equals16(rc16b), ShouldBeTrue)
rc16.Add(1)
rc16b.Add(2)
So(rc16.equals16(rc16b), ShouldBeFalse)
}
})
}
func TestRleStoringMax16(t *testing.T) {
Convey("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.", t, func() {
rc := newRunContainer16()
rc.Add(MaxUint16)
So(rc.contains(MaxUint16), ShouldBeTrue)
So(rc.cardinality(), ShouldEqual, 1)
rc.removeKey(MaxUint16)
So(rc.contains(MaxUint16), ShouldBeFalse)
So(rc.cardinality(), ShouldEqual, 0)
rc.set(false, MaxUint16-1, MaxUint16)
So(rc.cardinality(), ShouldEqual, 2)
So(rc.contains(MaxUint16-1), ShouldBeTrue)
So(rc.contains(MaxUint16), ShouldBeTrue)
rc.removeKey(MaxUint16 - 1)
So(rc.cardinality(), ShouldEqual, 1)
rc.removeKey(MaxUint16)
So(rc.cardinality(), ShouldEqual, 0)
rc.set(false, MaxUint16-2, MaxUint16-1, MaxUint16)
So(rc.cardinality(), ShouldEqual, 3)
So(rc.numIntervals(), ShouldEqual, 1)
rc.removeKey(MaxUint16 - 1)
So(rc.numIntervals(), ShouldEqual, 2)
So(rc.cardinality(), ShouldEqual, 2)
})
}
// 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) {
Convey("runContainer16 `and` operation against other container types should correctly do the intersection", t, func() {
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 {
So(rcVsBcIsect.contains(uint16(k)), ShouldBeTrue)
So(rcVsAcIsect.contains(uint16(k)), ShouldBeTrue)
So(rcVsRcbIsect.contains(uint16(k)), ShouldBeTrue)
}
So(rcVsBcIsect.getCardinality(), ShouldEqual, len(hashi))
So(rcVsAcIsect.getCardinality(), ShouldEqual, len(hashi))
So(rcVsRcbIsect.getCardinality(), ShouldEqual, len(hashi))
}
}