forked from RoaringBitmap/roaring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserialization_test.go
945 lines (769 loc) · 21.1 KB
/
serialization_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
package roaring
// to run just these tests: go test -run TestSerialization*
import (
"bytes"
"encoding/binary"
"encoding/gob"
"fmt"
"io/ioutil"
"math/rand"
"os"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestSerializationOfEmptyBitmap(t *testing.T) {
rb := NewBitmap()
buf := &bytes.Buffer{}
_, err := rb.WriteTo(buf)
if err != nil {
t.Errorf("Failed writing")
}
newrb := NewBitmap()
_, err = newrb.ReadFrom(buf)
if err != nil {
t.Errorf("Failed reading: %v", err)
}
if !rb.Equals(newrb) {
p("rb = '%s'", rb)
p("but newrb = '%s'", newrb)
t.Errorf("Cannot retrieve serialized version; rb != newrb")
}
}
func TestBase64_036(t *testing.T) {
rb := BitmapOf(1, 2, 3, 4, 5, 100, 1000)
bstr, _ := rb.ToBase64()
if bstr == "" {
t.Errorf("ToBase64 failed returned empty string")
}
newrb := NewBitmap()
_, err := newrb.FromBase64(bstr)
if err != nil {
t.Errorf("Failed reading from base64 string")
}
if !rb.Equals(newrb) {
t.Errorf("comparing the base64 to and from failed cannot retrieve serialized version")
}
}
func TestSerializationBasic037(t *testing.T) {
rb := BitmapOf(1, 2, 3, 4, 5, 100, 1000)
buf := &bytes.Buffer{}
_, err := rb.WriteTo(buf)
if err != nil {
t.Errorf("Failed writing")
}
newrb := NewBitmap()
_, err = newrb.ReadFrom(buf)
if err != nil {
t.Errorf("Failed reading")
}
if !rb.Equals(newrb) {
p("rb = '%s'", rb)
p("but newrb = '%s'", newrb)
t.Errorf("Cannot retrieve serialized version; rb != newrb")
}
}
func TestSerializationToFile038(t *testing.T) {
rb := BitmapOf(1, 2, 3, 4, 5, 100, 1000)
fname := "myfile.bin"
fout, err := os.OpenFile(fname, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0660)
if err != nil {
t.Errorf("Can't open a file for writing")
}
_, err = rb.WriteTo(fout)
if err != nil {
t.Errorf("Failed writing")
}
fout.Close()
newrb := NewBitmap()
fin, err := os.Open(fname)
if err != nil {
t.Errorf("Failed reading")
}
defer func() {
fin.Close()
err := os.Remove(fname)
if err != nil {
t.Errorf("could not delete %s ", fname)
}
}()
_, _ = newrb.ReadFrom(fin)
if !rb.Equals(newrb) {
t.Errorf("Cannot retrieve serialized version")
}
}
func TestSerializationReadRunsFromFile039(t *testing.T) {
fn := "testdata/bitmapwithruns.bin"
p("reading file '%s'", fn)
by, err := ioutil.ReadFile(fn)
if err != nil {
panic(err)
}
newrb := NewBitmap()
_, err = newrb.ReadFrom(bytes.NewBuffer(by))
if err != nil {
t.Errorf("Failed reading %s: %s", fn, err)
}
}
func TestSerializationBasic4WriteAndReadFile040(t *testing.T) {
//fname := "testdata/all3.msgp.snappy"
fname := "testdata/all3.classic"
rb := NewBitmap()
for k := uint32(0); k < 100000; k += 1000 {
rb.Add(k)
}
for k := uint32(100000); k < 200000; k++ {
rb.Add(3 * k)
}
for k := uint32(700000); k < 800000; k++ {
rb.Add(k)
}
rb.highlowcontainer.runOptimize()
p("TestSerializationBasic4WriteAndReadFile is writing to '%s'", fname)
fout, err := os.Create(fname)
if err != nil {
t.Errorf("Failed creating '%s'", fname)
}
_, err = rb.WriteTo(fout)
if err != nil {
t.Errorf("Failed writing to '%s'", fname)
}
fout.Close()
fin, err := os.Open(fname)
if err != nil {
t.Errorf("Failed to Open '%s'", fname)
}
defer fin.Close()
newrb := NewBitmap()
_, err = newrb.ReadFrom(fin)
if err != nil {
t.Errorf("Failed reading from '%s': %s", fname, err)
}
if !rb.Equals(newrb) {
t.Errorf("Bad serialization")
}
}
func TestSerializationFromJava051(t *testing.T) {
fname := "testdata/bitmapwithoutruns.bin"
newrb := NewBitmap()
fin, err := os.Open(fname)
if err != nil {
t.Errorf("Failed reading")
}
defer func() {
fin.Close()
}()
_, _ = newrb.ReadFrom(fin)
fmt.Println(newrb.GetCardinality())
rb := NewBitmap()
for k := uint32(0); k < 100000; k += 1000 {
rb.Add(k)
}
for k := uint32(100000); k < 200000; k++ {
rb.Add(3 * k)
}
for k := uint32(700000); k < 800000; k++ {
rb.Add(k)
}
fmt.Println(rb.GetCardinality())
if !rb.Equals(newrb) {
t.Errorf("Bad serialization")
}
}
func TestSerializationFromJavaWithRuns052(t *testing.T) {
fname := "testdata/bitmapwithruns.bin"
newrb := NewBitmap()
fin, err := os.Open(fname)
if err != nil {
t.Errorf("Failed reading")
}
defer func() {
fin.Close()
}()
_, _ = newrb.ReadFrom(fin)
rb := NewBitmap()
for k := uint32(0); k < 100000; k += 1000 {
rb.Add(k)
}
for k := uint32(100000); k < 200000; k++ {
rb.Add(3 * k)
}
for k := uint32(700000); k < 800000; k++ {
rb.Add(k)
}
if !rb.Equals(newrb) {
t.Errorf("Bad serialization")
}
}
func TestSerializationBasic2_041(t *testing.T) {
rb := BitmapOf(1, 2, 3, 4, 5, 100, 1000, 10000, 100000, 1000000)
buf := &bytes.Buffer{}
sz := rb.GetSerializedSizeInBytes()
ub := BoundSerializedSizeInBytes(rb.GetCardinality(), 1000001)
if sz > ub+10 {
t.Errorf("Bad GetSerializedSizeInBytes; sz=%v, upper-bound=%v", sz, ub)
}
l := int(rb.GetSerializedSizeInBytes())
_, err := rb.WriteTo(buf)
if err != nil {
t.Errorf("Failed writing")
}
if l != buf.Len() {
t.Errorf("Bad GetSerializedSizeInBytes")
}
newrb := NewBitmap()
_, err = newrb.ReadFrom(buf)
if err != nil {
t.Errorf("Failed reading")
}
if !rb.Equals(newrb) {
t.Errorf("Cannot retrieve serialized version")
}
}
func TestSerializationBasic3_042(t *testing.T) {
Convey("roaringarray.writeTo and .readFrom should serialize and unserialize when containing all 3 container types", t, func() {
rb := BitmapOf(1, 2, 3, 4, 5, 100, 1000, 10000, 100000, 1000000)
for i := 5000000; i < 5000000+2*(1<<16); i++ {
rb.AddInt(i)
}
// confirm all three types present
var bc, ac, rc bool
for _, v := range rb.highlowcontainer.containers {
switch cn := v.(type) {
case *bitmapContainer:
bc = true
case *arrayContainer:
ac = true
case *runContainer16:
rc = true
default:
panic(fmt.Errorf("Unrecognized container implementation: %T", cn))
}
}
if !bc {
t.Errorf("no bitmapContainer found, change your test input so we test all three!")
}
if !ac {
t.Errorf("no arrayContainer found, change your test input so we test all three!")
}
if !rc {
t.Errorf("no runContainer16 found, change your test input so we test all three!")
}
var buf bytes.Buffer
_, err := rb.WriteTo(&buf)
if err != nil {
t.Errorf("Failed writing")
}
newrb := NewBitmap()
_, err = newrb.ReadFrom(&buf)
if err != nil {
t.Errorf("Failed reading")
}
c1, c2 := rb.GetCardinality(), newrb.GetCardinality()
So(c2, ShouldEqual, c1)
So(newrb.Equals(rb), ShouldBeTrue)
//fmt.Printf("\n Basic3: good: match on card = %v", c1)
})
}
func TestGobcoding043(t *testing.T) {
rb := BitmapOf(1, 2, 3, 4, 5, 100, 1000)
buf := new(bytes.Buffer)
encoder := gob.NewEncoder(buf)
err := encoder.Encode(rb)
if err != nil {
t.Errorf("Gob encoding failed")
}
var b Bitmap
decoder := gob.NewDecoder(buf)
err = decoder.Decode(&b)
if err != nil {
t.Errorf("Gob decoding failed")
}
if !b.Equals(rb) {
t.Errorf("Decoded bitmap does not equal input bitmap")
}
}
func TestSerializationRunContainerMsgpack028(t *testing.T) {
Convey("runContainer writeTo and readFrom should return logically equivalent containers", t, func() {
seed := int64(42)
p("seed is %v", seed)
rand.Seed(seed)
trials := []trial{
{n: 10, percentFill: .2, ntrial: 10},
{n: 10, percentFill: .8, ntrial: 10},
{n: 10, percentFill: .50, ntrial: 10},
/*
trial{n: 10, percentFill: .01, ntrial: 10},
trial{n: 1000, percentFill: .50, ntrial: 10},
trial{n: 1000, percentFill: .99, ntrial: 10},
*/
}
tester := func(tr trial) {
for j := 0; j < tr.ntrial; j++ {
p("TestSerializationRunContainerMsgpack028 on check# j=%v", j)
ma := make(map[int]bool)
n := tr.n
a := []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
}
orig := newRunContainer16FromVals(false, a...)
// serialize
var buf bytes.Buffer
_, err := orig.writeToMsgpack(&buf)
if err != nil {
panic(err)
}
// deserialize
restored := &runContainer16{}
_, err = restored.readFromMsgpack(&buf)
if err != nil {
panic(err)
}
// and compare
So(restored.equals(orig), ShouldBeTrue)
}
p("done with serialization of runContainer16 check for trial %#v", tr)
}
for i := range trials {
tester(trials[i])
}
})
}
func TestSerializationArrayOnly032(t *testing.T) {
Convey("arrayContainer writeTo and readFrom should return logically equivalent containers, so long as you pre-size the write target properly", t, func() {
seed := int64(42)
p("seed is %v", seed)
rand.Seed(seed)
trials := []trial{
{n: 101, percentFill: .50, ntrial: 10},
}
tester := func(tr trial) {
for j := 0; j < tr.ntrial; j++ {
p(" on check# j=%v", j)
ma := make(map[int]bool)
n := tr.n
draw := int(float64(n) * tr.percentFill)
for i := 0; i < draw; i++ {
r0 := rand.Intn(n)
ma[r0] = true
}
//showArray16(a, "a")
// vs arrayContainer
ac := newArrayContainer()
for k := range ma {
ac.iadd(uint16(k))
}
buf := &bytes.Buffer{}
_, err := ac.writeTo(buf)
panicOn(err)
// have to pre-size the array write-target properly
// by telling it the cardinality to read.
ac2 := newArrayContainerSize(int(ac.getCardinality()))
_, err = ac2.readFrom(buf)
panicOn(err)
So(ac2.String(), ShouldResemble, ac.String())
}
p("done with randomized writeTo/readFrom for arrayContainer"+
" checks for trial %#v", tr)
}
for i := range trials {
tester(trials[i])
}
})
}
func TestSerializationRunOnly033(t *testing.T) {
Convey("runContainer16 writeTo and readFrom should return logically equivalent containers", t, func() {
seed := int64(42)
p("seed is %v", seed)
rand.Seed(seed)
trials := []trial{
{n: 100, percentFill: .50, ntrial: 1},
}
tester := func(tr trial) {
for j := 0; j < tr.ntrial; j++ {
p(" on check# j=%v", j)
ma := make(map[int]bool)
n := tr.n
draw := int(float64(n) * tr.percentFill)
for i := 0; i < draw; i++ {
r0 := rand.Intn(n)
ma[r0] = true
}
ac := newRunContainer16()
for k := range ma {
ac.iadd(uint16(k))
}
buf := &bytes.Buffer{}
_, err := ac.writeTo(buf)
panicOn(err)
ac2 := newRunContainer16()
_, err = ac2.readFrom(buf)
panicOn(err)
So(ac2.equals(ac), ShouldBeTrue)
So(ac2.String(), ShouldResemble, ac.String())
}
p("done with randomized writeTo/readFrom for runContainer16"+
" checks for trial %#v", tr)
}
for i := range trials {
tester(trials[i])
}
})
}
func TestSerializationBitmapOnly034(t *testing.T) {
Convey("bitmapContainer writeTo and readFrom should return logically equivalent containers", t, func() {
seed := int64(42)
p("seed is %v", seed)
rand.Seed(seed)
trials := []trial{
{n: 1010, percentFill: .50, ntrial: 10},
}
tester := func(tr trial) {
for j := 0; j < tr.ntrial; j++ {
p(" on check# j=%v", j)
ma := make(map[int]bool)
n := tr.n
draw := int(float64(n) * tr.percentFill)
for i := 0; i < draw; i++ {
r0 := rand.Intn(n)
ma[r0] = true
}
//showArray16(a, "a")
bc := newBitmapContainer()
for k := range ma {
bc.iadd(uint16(k))
}
buf := &bytes.Buffer{}
_, err := bc.writeTo(buf)
panicOn(err)
bc2 := newBitmapContainer()
_, err = bc2.readFrom(buf)
panicOn(err)
So(bc2.String(), ShouldResemble, bc.String())
So(bc2.equals(bc), ShouldBeTrue)
}
p("done with randomized writeTo/readFrom for bitmapContainer"+
" checks for trial %#v", tr)
}
for i := range trials {
tester(trials[i])
}
})
}
func TestSerializationBasicMsgpack035(t *testing.T) {
Convey("roaringarray.writeToMsgpack and .readFromMsgpack should serialize and unserialize when containing all 3 container types", t, func() {
rb := BitmapOf(1, 2, 3, 4, 5, 100, 1000, 10000, 100000, 1000000)
for i := 5000000; i < 5000000+2*(1<<16); i++ {
rb.AddInt(i)
}
// confirm all three types present
var bc, ac, rc bool
for _, v := range rb.highlowcontainer.containers {
switch cn := v.(type) {
case *bitmapContainer:
bc = true
So(cn.containerType(), ShouldEqual, bitmapContype)
case *arrayContainer:
ac = true
So(cn.containerType(), ShouldEqual, arrayContype)
case *runContainer16:
rc = true
So(cn.containerType(), ShouldEqual, run16Contype)
default:
panic(fmt.Errorf("Unrecognized container implementation: %T", cn))
}
}
if !bc {
t.Errorf("no bitmapContainer found, change your test input so we test all three!")
}
if !ac {
t.Errorf("no arrayContainer found, change your test input so we test all three!")
}
if !rc {
t.Errorf("no runContainer16 found, change your test input so we test all three!")
}
var buf bytes.Buffer
_, err := rb.WriteToMsgpack(&buf)
if err != nil {
t.Errorf("Failed writing")
}
newrb := NewBitmap()
_, err = newrb.ReadFromMsgpack(&buf)
if err != nil {
t.Errorf("Failed reading")
}
c1, c2 := rb.GetCardinality(), newrb.GetCardinality()
So(c2, ShouldEqual, c1)
So(newrb.Equals(rb), ShouldBeTrue)
//fmt.Printf("\n Basic3: good: match on card = %v", c1)
})
}
func TestSerializationRunContainer32Msgpack050(t *testing.T) {
Convey("runContainer32 writeToMsgpack and readFromMsgpack should save/load data", t, func() {
seed := int64(42)
p("seed is %v", seed)
rand.Seed(seed)
trials := []trial{
{n: 10, percentFill: .2, ntrial: 1},
/* trial{n: 10, percentFill: .8, ntrial: 10},
trial{n: 10, percentFill: .50, ntrial: 10},
trial{n: 10, percentFill: .01, ntrial: 10},
trial{n: 1000, percentFill: .50, ntrial: 10},
trial{n: 1000, percentFill: .99, ntrial: 10},
*/
}
tester := func(tr trial) {
for j := 0; j < tr.ntrial; j++ {
p("TestSerializationRunContainer32Msgpack050 on check# j=%v", j)
ma := make(map[int]bool)
n := tr.n
a := []uint32{}
draw := int(float64(n) * tr.percentFill)
for i := 0; i < draw; i++ {
r0 := rand.Intn(n)
a = append(a, uint32(r0))
ma[r0] = true
}
orig := newRunContainer32FromVals(false, a...)
// serialize
var buf bytes.Buffer
_, err := orig.writeToMsgpack(&buf)
if err != nil {
panic(err)
}
// deserialize
restored := &runContainer32{}
_, err = restored.readFromMsgpack(&buf)
if err != nil {
panic(err)
}
// and compare
So(restored.equals32(orig), ShouldBeTrue)
orig.removeKey(1)
// coverage
var notEq = newRunContainer32Range(1, 1)
So(notEq.equals32(orig), ShouldBeFalse)
bc := newBitmapContainer()
bc.iadd(1)
bc.iadd(2)
rc22 := newRunContainer32FromBitmapContainer(bc)
So(rc22.cardinality(), ShouldEqual, 2)
}
p("done with msgpack serialization of runContainer32 check for trial %#v", tr)
}
for i := range trials {
tester(trials[i])
}
})
}
func TestByteSliceAsUint16Slice(t *testing.T) {
t.Run("valid slice", func(t *testing.T) {
expectedSize := 2
slice := make([]byte, 4)
binary.LittleEndian.PutUint16(slice, 42)
binary.LittleEndian.PutUint16(slice[2:], 43)
uint16Slice := byteSliceAsUint16Slice(slice)
if len(uint16Slice) != expectedSize {
t.Errorf("Expected output slice length %d, got %d", expectedSize, len(uint16Slice))
}
if cap(uint16Slice) != expectedSize {
t.Errorf("Expected output slice cap %d, got %d", expectedSize, cap(uint16Slice))
}
if uint16Slice[0] != 42 || uint16Slice[1] != 43 {
t.Errorf("Unexpected value found in result slice")
}
})
t.Run("empty slice", func(t *testing.T) {
slice := make([]byte, 0, 0)
uint16Slice := byteSliceAsUint16Slice(slice)
if len(uint16Slice) != 0 {
t.Errorf("Expected output slice length 0, got %d", len(uint16Slice))
}
if cap(uint16Slice) != 0 {
t.Errorf("Expected output slice cap 0, got %d", len(uint16Slice))
}
})
t.Run("invalid slice size", func(t *testing.T) {
defer func() {
// All fine
_ = recover()
}()
slice := make([]byte, 1, 1)
byteSliceAsUint16Slice(slice)
t.Errorf("byteSliceAsUint16Slice should panic on invalid slice size")
})
}
func TestByteSliceAsUint64Slice(t *testing.T) {
t.Run("valid slice", func(t *testing.T) {
expectedSize := 2
slice := make([]byte, 16)
binary.LittleEndian.PutUint64(slice, 42)
binary.LittleEndian.PutUint64(slice[8:], 43)
uint64Slice := byteSliceAsUint64Slice(slice)
if len(uint64Slice) != expectedSize {
t.Errorf("Expected output slice length %d, got %d", expectedSize, len(uint64Slice))
}
if cap(uint64Slice) != expectedSize {
t.Errorf("Expected output slice cap %d, got %d", expectedSize, cap(uint64Slice))
}
if uint64Slice[0] != 42 || uint64Slice[1] != 43 {
t.Errorf("Unexpected value found in result slice")
}
})
t.Run("empty slice", func(t *testing.T) {
slice := make([]byte, 0, 0)
uint64Slice := byteSliceAsUint64Slice(slice)
if len(uint64Slice) != 0 {
t.Errorf("Expected output slice length 0, got %d", len(uint64Slice))
}
if len(uint64Slice) != 0 {
t.Errorf("Expected output slice length 0, got %d", len(uint64Slice))
}
})
t.Run("invalid slice size", func(t *testing.T) {
defer func() {
// All fine
_ = recover()
}()
slice := make([]byte, 1, 1)
byteSliceAsUint64Slice(slice)
t.Errorf("byteSliceAsUint64Slice should panic on invalid slice size")
})
}
func TestByteSliceAsInterval16Slice(t *testing.T) {
t.Run("valid slice", func(t *testing.T) {
expectedSize := 2
slice := make([]byte, 8)
binary.LittleEndian.PutUint16(slice, 10)
binary.LittleEndian.PutUint16(slice[2:], 2)
binary.LittleEndian.PutUint16(slice[4:], 20)
binary.LittleEndian.PutUint16(slice[6:], 2)
intervalSlice := byteSliceAsInterval16Slice(slice)
if len(intervalSlice) != expectedSize {
t.Errorf("Expected output slice length %d, got %d", expectedSize, len(intervalSlice))
}
if cap(intervalSlice) != expectedSize {
t.Errorf("Expected output slice cap %d, got %d", expectedSize, len(intervalSlice))
}
i1 := interval16{10, 12}
i2 := interval16{20, 22}
if intervalSlice[0] != i1 || intervalSlice[1] != i2 {
t.Errorf("Unexpected items in result slice")
}
})
t.Run("empty slice", func(t *testing.T) {
slice := make([]byte, 0, 0)
intervalSlice := byteSliceAsInterval16Slice(slice)
if len(intervalSlice) != 0 {
t.Errorf("Expected output slice length 0, got %d", len(intervalSlice))
}
if len(intervalSlice) != 0 {
t.Errorf("Expected output slice length 0, got %d", len(intervalSlice))
}
})
t.Run("invalid slice length", func(t *testing.T) {
defer func() {
// All fine
_ = recover()
}()
slice := make([]byte, 1, 1)
byteSliceAsInterval16Slice(slice)
t.Errorf("byteSliceAsInterval16Slice should panic on invalid slice size")
})
}
func TestBitmap_FromBuffer(t *testing.T) {
t.Run("empty bitmap", func(t *testing.T) {
rb := NewBitmap()
buf := &bytes.Buffer{}
_, err := rb.WriteTo(buf)
if err != nil {
t.Fatalf("Failed writing")
}
newRb := NewBitmap()
newRb.FromBuffer(buf.Bytes())
if err != nil {
t.Errorf("Failed reading: %v", err)
}
if !rb.Equals(newRb) {
t.Errorf("Cannot retrieve serialized version; rb != newRb")
}
})
t.Run("basic bitmap of 7 elements", func(t *testing.T) {
rb := BitmapOf(1, 2, 3, 4, 5, 100, 1000)
buf := &bytes.Buffer{}
_, err := rb.WriteTo(buf)
if err != nil {
t.Fatalf("Failed writing")
}
newRb := NewBitmap()
_, err = newRb.FromBuffer(buf.Bytes())
if err != nil {
t.Errorf("Failed reading")
}
if !rb.Equals(newRb) {
t.Errorf("Cannot retrieve serialized version; rb != newRb")
}
})
t.Run("bitmap with runs", func(t *testing.T) {
file := "testdata/bitmapwithruns.bin"
buf, err := ioutil.ReadFile(file)
if err != nil {
t.Fatalf("Failed to read file")
}
rb := NewBitmap()
_, err = rb.FromBuffer(buf)
if err != nil {
t.Errorf("Failed reading %s: %s", file, err)
}
if rb.Stats().RunContainers != 3 {
t.Errorf("Bitmap should contain 3 run containers, was: %d", rb.Stats().RunContainers)
}
if rb.Stats().Containers != 11 {
t.Errorf("Bitmap should contain a total of 11 containers, was %d", rb.Stats().Containers)
}
})
t.Run("bitmap without runs", func(t *testing.T) {
fn := "testdata/bitmapwithruns.bin"
buf, err := ioutil.ReadFile(fn)
if err != nil {
t.Fatalf("Failed to read file")
}
rb := NewBitmap()
_, err = rb.FromBuffer(buf)
if err != nil {
t.Errorf("Failed reading %s: %s", fn, err)
}
})
t.Run("all3.classic bitmap", func(t *testing.T) {
file := "testdata/all3.classic"
buf, err := ioutil.ReadFile(file)
if err != nil {
t.Fatalf("Failed to read file")
}
rb := NewBitmap()
_, err = rb.FromBuffer(buf)
if err != nil {
t.Errorf("Failed reading %s: %s", file, err)
}
})
t.Run("marking all containers as requiring COW", func(t *testing.T) {
file := "testdata/bitmapwithruns.bin"
buf, err := ioutil.ReadFile(file)
if err != nil {
t.Fatalf("Failed to read file")
}
rb := NewBitmap()
_, err = rb.FromBuffer(buf)
if err != nil {
t.Fatalf("Failed reading %s: %s", file, err)
}
for i, cow := range rb.highlowcontainer.needCopyOnWrite {
if !cow {
t.Errorf("Container at pos %d was not marked as needs-copy-on-write", i)
}
}
})
}