Skip to content

Commit

Permalink
Merge pull request #387 from RoaringBitmap/verify_issue_386
Browse files Browse the repository at this point in the history
Verifying issue 386
  • Loading branch information
guymolinari authored May 20, 2023
2 parents 1bf03bd + d93a6b4 commit 8d41a8e
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 5 deletions.
29 changes: 29 additions & 0 deletions roaring64/roaring64cow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,35 @@ import (
"github.com/stretchr/testify/assert"
)

func TestIssue386(t *testing.T) {
a := NewBitmap()
for i := uint64(0); i < 1000; i++ {
a.Add(i)
}
a.SetCopyOnWrite(true)
aClone := a.Clone()
assert.False(t, a.Contains(1001))
assert.EqualValues(t, 1000, a.GetCardinality())
assert.EqualValues(t, 1000, aClone.GetCardinality())
b := NewBitmap()
b.Add(1001)
assert.False(t, a.Contains(1001))
assert.True(t, b.Contains(1001))
assert.EqualValues(t, 1000, a.GetCardinality())
assert.EqualValues(t, 1000, aClone.GetCardinality())
c := NewBitmap()
c.Or(aClone)
assert.EqualValues(t, 1000, c.GetCardinality())
assert.EqualValues(t, 1000, a.GetCardinality())
assert.EqualValues(t, 1000, aClone.GetCardinality())
c.Or(b)
assert.EqualValues(t, 1001, c.GetCardinality())
assert.True(t, c.Contains(1001))
assert.False(t, a.Contains(1001))
assert.EqualValues(t, 1000, a.GetCardinality())
assert.EqualValues(t, 1000, aClone.GetCardinality())
}

func TestCloneOfCOW(t *testing.T) {
rb1 := NewBitmap()
rb1.SetCopyOnWrite(true)
Expand Down
13 changes: 8 additions & 5 deletions roaring64/roaringarray64.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package roaring64

import "github.com/RoaringBitmap/roaring"
import (
"github.com/RoaringBitmap/roaring"
)

type roaringArray64 struct {
keys []uint32
Expand All @@ -12,9 +14,10 @@ type roaringArray64 struct {
// runOptimize compresses the element containers to minimize space consumed.
// Q: how does this interact with copyOnWrite and needCopyOnWrite?
// A: since we aren't changing the logical content, just the representation,
// we don't bother to check the needCopyOnWrite bits. We replace
// (possibly all) elements of ra.containers in-place with space
// optimized versions.
//
// we don't bother to check the needCopyOnWrite bits. We replace
// (possibly all) elements of ra.containers in-place with space
// optimized versions.
func (ra *roaringArray64) runOptimize() {
for i := range ra.containers {
ra.containers[i].RunOptimize()
Expand All @@ -39,7 +42,7 @@ func (ra *roaringArray64) appendCopy(sa roaringArray64, startingindex int) {
// since there is no copy-on-write, we need to clone the container (this is important)
ra.appendContainer(sa.keys[startingindex], sa.containers[startingindex].Clone(), copyonwrite)
} else {
ra.appendContainer(sa.keys[startingindex], sa.containers[startingindex], copyonwrite)
ra.appendContainer(sa.keys[startingindex], sa.containers[startingindex].Clone(), copyonwrite)
if !sa.needsCopyOnWrite(startingindex) {
sa.setNeedsCopyOnWrite(startingindex)
}
Expand Down
29 changes: 29 additions & 0 deletions roaringcow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,35 @@ import (
"github.com/stretchr/testify/assert"
)

func TestIssue386(t *testing.T) {
a := NewBitmap()
for i := uint32(0); i < 1000; i++ {
a.Add(i)
}
a.SetCopyOnWrite(true)
aClone := a.Clone()
assert.False(t, a.Contains(1001))
assert.EqualValues(t, 1000, a.GetCardinality())
assert.EqualValues(t, 1000, aClone.GetCardinality())
b := NewBitmap()
b.Add(1001)
assert.False(t, a.Contains(1001))
assert.True(t, b.Contains(1001))
assert.EqualValues(t, 1000, a.GetCardinality())
assert.EqualValues(t, 1000, aClone.GetCardinality())
c := NewBitmap()
c.Or(aClone)
assert.EqualValues(t, 1000, c.GetCardinality())
assert.EqualValues(t, 1000, a.GetCardinality())
assert.EqualValues(t, 1000, aClone.GetCardinality())
c.Or(b)
assert.EqualValues(t, 1001, c.GetCardinality())
assert.True(t, c.Contains(1001))
assert.False(t, a.Contains(1001))
assert.EqualValues(t, 1000, a.GetCardinality())
assert.EqualValues(t, 1000, aClone.GetCardinality())
}

func TestCloneOfCOW(t *testing.T) {
rb1 := NewBitmap()
rb1.SetCopyOnWrite(true)
Expand Down

0 comments on commit 8d41a8e

Please sign in to comment.