diff --git a/roaring/roaring.go b/roaring/roaring.go index 22d2821db..9a636a519 100644 --- a/roaring/roaring.go +++ b/roaring/roaring.go @@ -5756,7 +5756,37 @@ func xorBitmapRun(a, b *Container) *Container { return output } -// CompareEquality is used mostly in test cases to confirm that two bitmaps came +// CompareBitmapSlice checks whether a bitmap has the same values in it +// that a provided slice does. +func CompareBitmapSlice(b *Bitmap, vals []uint64) (bool, error) { + count := b.Count() + if count != uint64(len(vals)) { + return false, fmt.Errorf("length mismatch: bitmap has %d bits, slice has %d", count, len(vals)) + } + for _, v := range vals { + if !b.Contains(v) { + return false, fmt.Errorf("bitmap lacks expected value %d", v) + } + } + return true, nil +} + +// CompareBitmapMap checks whether a bitmap has the same values in it +// that a provided map[uint64]struct{} has as keys. +func CompareBitmapMap(b *Bitmap, vals map[uint64]struct{}) (bool, error) { + count := b.Count() + if count != uint64(len(vals)) { + return false, fmt.Errorf("length mismatch: bitmap has %d bits, map has %d", count, len(vals)) + } + for v := range vals { + if !b.Contains(v) { + return false, fmt.Errorf("bitmap lacks expected value %d", v) + } + } + return true, nil +} + +// BitwiseEqual is used mostly in test cases to confirm that two bitmaps came // out the same. It does not expect corresponding opN, or OpWriter, but expects // identical bit contents. It does not expect identical representations; a bitmap // container can be identical to an array container. It returns a boolean value, diff --git a/roaring/roaring_test.go b/roaring/roaring_test.go index e31cbf2ee..c8eb2617a 100644 --- a/roaring/roaring_test.go +++ b/roaring/roaring_test.go @@ -1570,14 +1570,12 @@ func testBitmapMarshalQuick(t *testing.T, n int, min, max uint64, sorted bool) { t.Fatal(err) } - // Verify the original bitmap has the correct set of values. - if exp, got := generator.Uint64SetSlice(set), bm.Slice(); !reflect.DeepEqual(exp, got) { - t.Fatalf("mismatch: %s\n\nexp=%+v\n\ngot=%+v\n\n", diff(exp, got), exp, got) + if _, err := roaring.CompareBitmapMap(bm, set); err != nil { + t.Fatalf("source mismatch: %v", err) } - // Verify the bitmap loaded with the ops log has the correct set of values. - if exp, got := generator.Uint64SetSlice(set), bm2.Slice(); !reflect.DeepEqual(exp, got) { - t.Fatalf("mismatch: %s\n\nexp=%+v\n\ngot=%+v\n\n", diff(exp, got), exp, got) + if _, err := roaring.CompareBitmapMap(bm2, set); err != nil { + t.Fatalf("unmarshalled mismatch: %v", err) } }