From d202e6a1a0dc213d7ef8d6d9a7553135f70dc098 Mon Sep 17 00:00:00 2001 From: Matt Jaffee Date: Thu, 21 Mar 2019 15:46:19 -0500 Subject: [PATCH] quick fix for Bitmap.Any bug Want to make empty containers a thing of the past, but that can wait for another day. --- roaring/roaring.go | 2 +- roaring/roaring_internal_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/roaring/roaring.go b/roaring/roaring.go index 157203c41..f12d3fc38 100644 --- a/roaring/roaring.go +++ b/roaring/roaring.go @@ -331,7 +331,7 @@ func (b *Bitmap) Any() bool { // TODO (jaffee) I'm not sure if it's possible/legal to have an empty // container, so this loop may be totally unnecessary. In theory, any empty // container should be removed from the bitmap though. - for b := iter.Next(); b; iter.Next() { + for iter.Next() { _, c := iter.Value() if c.n > 0 { return true diff --git a/roaring/roaring_internal_test.go b/roaring/roaring_internal_test.go index 927d7c4b2..16f5e4e7b 100644 --- a/roaring/roaring_internal_test.go +++ b/roaring/roaring_internal_test.go @@ -3818,3 +3818,31 @@ func BenchmarkUnionInPlaceRegression(b *testing.B) { } }) } + +func TestBitmapAny(t *testing.T) { + bm := NewBTreeBitmap() + if bm.Any() { + t.Error("empty bitmap should have Any()==false") + } + bm.Add(1) + if !bm.Any() { + t.Error("bitmap with 1 bit should have Any()==true") + } + bm.Add(100000) + if !bm.Any() { + t.Error("bitmap with 2 bits should have Any()==true") + } + bm.Remove(1) + if !bm.Any() { + t.Error("bitmap with 1 bit left after removing 1 should have Any()==true") + } + bm.Add(1) + bm = bm.Difference(NewBTreeBitmap(1)) + if !bm.Any() { + t.Error("bitmap with 1 bit left after differencing 1 should have Any()==true") + } + bm.Remove(100000) + if bm.Any() { + t.Error("shouldn't be any left") + } +}