diff --git a/roaring/roaring_test.go b/roaring/roaring_test.go index a63b53390..d4f8dc8d7 100644 --- a/roaring/roaring_test.go +++ b/roaring/roaring_test.go @@ -163,7 +163,8 @@ func TestBitmap_Max(t *testing.T) { } } } -func TestBitmap_CountRange(t *testing.T) { + +func TestBitmap_BitmapCountRange(t *testing.T) { bm0 := roaring.NewBitmap(0, 2683177) for i := uint64(628); i < 2683301; i++ { bm0.Add(i) @@ -184,6 +185,32 @@ func TestBitmap_CountRange(t *testing.T) { if n := bm0.CountRange(0, 1); n != 1 { t.Fatalf("unexpected n: %d", n) } + + // Test the case where the range is outside of the bitmap space. + if n := bm0.CountRange(10000000, 10000001); n != 0 { + t.Fatalf("unexpected n: %d", n) + } +} + +func TestBitmap_ArrayCountRange(t *testing.T) { + bm0 := roaring.NewBitmap(0, 2683177, 2683313) + if n := bm0.CountRange(1, 2683313); n != 1 { + t.Fatalf("unexpected n: %d", n) + } +} + +func TestBitmap_RunCountRange(t *testing.T) { + bm0 := roaring.NewBitmap(0, 1, 2, 3, 4, 5, 12, 13, 14, 15, 16, 17, 1000000, 1000002, 1000003, 1000004, 1000005, 1000006, 1000010, 1000011, 1000012, 1000013, 1000014) + bm0.Optimize() // convert to runs + if n := bm0.CountRange(15, 1000003); n != 5 { + t.Fatalf("unexpected n: %d", n) + } + + bm1 := roaring.NewBitmap(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17) + bm1.Optimize() // convert to runs + if n := bm1.CountRange(5, 12); n != 7 { + t.Fatalf("unexpected n: %d", n) + } } func TestBitmap_Intersection(t *testing.T) { @@ -235,6 +262,70 @@ func TestBitmap_Intersection_Empty(t *testing.T) { } +func TestBitmap_IntersectArrayArray(t *testing.T) { + bm0 := roaring.NewBitmap(0, 1, 2683, 5005) + bm1 := roaring.NewBitmap(0, 2683, 2684, 5000) + + result := bm0.Intersect(bm1) + if n := result.Count(); n != 2 { + t.Fatalf("unexpected n: %d", n) + } +} + +func TestBitmap_IntersectBitmapBitmap(t *testing.T) { + bm0 := roaring.NewBitmap() + for i := uint64(0); i < 65536; i += 2 { + bm0.Add(i) + } + + bm1 := roaring.NewBitmap() + for i := uint64(0); i < 65536; i += 3 { + bm1.Add(i) + } + + result := bm0.Intersect(bm1) + if n := result.Count(); n != 10923 { + t.Fatalf("unexpected n: %d", n) + } +} + +func TestBitmap_IntersectRunRun(t *testing.T) { + // Intersect two runs that result in an array. + bm0 := roaring.NewBitmap(0, 1, 2, 3, 4, 5, 10, 11, 12, 13, 14, 15) + bm0.Optimize() // convert to runs + bm1 := roaring.NewBitmap(5, 6, 7, 8, 9, 10, 11) + bm1.Optimize() // convert to runs + result := bm0.Intersect(bm1) + if n := result.Count(); n != 3 { + t.Fatalf("unexpected n: %d", n) + } + + // Intersect two runs that result in a bitmap. + bm2 := roaring.NewBitmap() + runLen := uint64(25) + spaceLen := uint64(8) + offset := (runLen / 2) + spaceLen + for i := uint64(0); i < (65536 - runLen - offset); i += (runLen + spaceLen) { + for j := uint64(0); j < runLen; j++ { + bm2.Add(offset + i + j) + } + } + bm2.Optimize() // convert to runs + bm3 := roaring.NewBitmap() + runLen = uint64(32) + spaceLen = uint64(1) + for i := uint64(0); i < (65536 - runLen); i += (runLen + spaceLen) { + for j := uint64(0); j < runLen; j++ { + bm3.Add(i + j) + } + } + bm3.Optimize() // convert to runs + result = bm2.Intersect(bm3) + if n := result.Count(); n != 47628 { + t.Fatalf("unexpected n: %d", n) + } +} + func TestBitmap_Difference(t *testing.T) { bm0 := roaring.NewBitmap(0, 2683177) bm1 := roaring.NewBitmap() @@ -714,7 +805,7 @@ func TestIterator(t *testing.T) { } } -//testBM creates a bitmap with 3 containers(an array,bitmap, and run) +// testBM creates a bitmap with 3 containers: array, bitmap, and run. func testBM() *roaring.Bitmap { bm := roaring.NewBitmap()