Skip to content

Commit

Permalink
Roaring64: Copy and Parrallel test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
bstrausser committed Jun 23, 2024
1 parent 7efe682 commit 44c256c
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 8 deletions.
7 changes: 4 additions & 3 deletions roaring64/parallel64.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ func (c parChunk) size() int {
return c.ra.size()
}

// parNaiveStartAt returns the index of the first key that is inclusive between start and last
// Returns the size if there is no such key
func parNaiveStartAt(ra *roaringArray64, start uint32, last uint32) int {
for idx, key := range ra.keys {
if key >= start && key <= last {
Expand All @@ -170,7 +172,6 @@ func orOnRange(ra1, ra2 *roaringArray64, start, last uint32) *roaringArray64 {
key2 = ra2.getKeyAtIndex(idx2)

for key1 <= last && key2 <= last {

if key1 < key2 {
answer.appendCopy(*ra1, idx1)
idx1++
Expand All @@ -188,7 +189,7 @@ func orOnRange(ra1, ra2 *roaringArray64, start, last uint32) *roaringArray64 {
} else {
c1 := ra1.getContainerAtIndex(idx1)

//answer.appendContainer(key1, c1.lazyOR(ra2.getContainerAtIndex(idx2)), false)
// answer.appendContainer(key1, c1.lazyOR(ra2.getContainerAtIndex(idx2)), false)
answer.appendContainer(key1, roaring.Or(c1, ra2.getContainerAtIndex(idx2)), false)
idx1++
idx2++
Expand Down Expand Up @@ -261,7 +262,7 @@ func iorOnRange(ra1, ra2 *roaringArray64, start, last uint32) *roaringArray64 {
} else {
c1 := ra1.getWritableContainerAtIndex(idx1)

//ra1.containers[idx1] = c1.lazyIOR(ra2.getContainerAtIndex(idx2))
// ra1.containers[idx1] = c1.lazyIOR(ra2.getContainerAtIndex(idx2))
c1.Or(ra2.getContainerAtIndex(idx2))
ra1.setContainerAtIndex(idx1, c1)

Expand Down
33 changes: 28 additions & 5 deletions roaring64/roaring64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,29 @@ func TestIssue266(t *testing.T) {
}
}

func TestParOr64(t *testing.T) {
t.Run("Test 1", func(t *testing.T) {
a := BitmapOf(0, 1, 2, 3, 4)
b := BitmapOf(5, 6, 7, 8, 9, 10)
c := BitmapOf(11, 12, 13, 14, 15)
d := ParOr(0, a, b, c)
expected := BitmapOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
assert.True(t, d.Equals(expected))
})

t.Run("Test 2", func(t *testing.T) {
a := BitmapOf(0, 1, 2, 3, 4)

offset1 := uint64(2 << 16)
b := BitmapOf(offset1, offset1+1, offset1+2, offset1+3, offset1+5)
offset2 := uint64(4 << 16)
c := BitmapOf(offset2, offset2+1, offset2+2, offset2+3, offset2+5)
d := ParOr(0, a, b, c)
expected := BitmapOf(0, 1, 2, 3, 4, offset1, offset1+1, offset1+2, offset1+3, offset1+5, offset2, offset2+1, offset2+2, offset2+3, offset2+5)
assert.True(t, d.Equals(expected))
})
}

func TestRoaringRangeEnd(t *testing.T) {
r := New()
r.Add(roaring.MaxUint32)
Expand Down Expand Up @@ -245,7 +268,7 @@ func TestRangeRemovalFromContent(t *testing.T) {
bm.RemoveRange(0, 30000)
c := bm.GetCardinality()

assert.EqualValues(t, 00, c)
assert.EqualValues(t, 0o0, c)
}

func TestFlipOnEmpty(t *testing.T) {
Expand Down Expand Up @@ -624,7 +647,6 @@ func TestBitmap(t *testing.T) {

assert.Equal(t, len(arrayres), len(arrayand))
assert.True(t, ok)

})

t.Run("Test AND 4", func(t *testing.T) {
Expand Down Expand Up @@ -1401,6 +1423,7 @@ func TestBitmap(t *testing.T) {
assert.True(t, valide)
})
}

func TestXORtest4(t *testing.T) {
t.Run("XORtest 4", func(t *testing.T) {
rb := NewBitmap()
Expand Down Expand Up @@ -1895,9 +1918,9 @@ func TestSerialization(t *testing.T) {
//assert.Nil(t, err)
//assert.True(t, bufBmp.Equals(bmp))

//var base64 string
//base64, err = bufBmp.ToBase64()
//assert.Nil(t, err)
// var base64 string
// base64, err = bufBmp.ToBase64()
// assert.Nil(t, err)

//base64Bmp := New()
//_, err = base64Bmp.FromBase64(base64)
Expand Down
102 changes: 102 additions & 0 deletions roaring64/roaringarray64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,105 @@ func TestRoaringArray64AdvanceUntil(t *testing.T) {
assert.Equal(t, 4, bitmap.highlowcontainer.advanceUntil(3, 3))
assert.Equal(t, 5, bitmap.highlowcontainer.advanceUntil(4, 4))
}

func TestCopies(t *testing.T) {
tests := []struct {
name string
cow1 bool
cow2 bool
}{
{"AppendCopiesAfterCoW", true, true},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
r1 := 1 << 32
r2 := 2 << 32
r3 := 3 << 32
r4 := 4 << 32
// key := highbits(uint64(r4))

bitmap1 := New()
bitmap2 := New()
bitmap1.SetCopyOnWrite(test.cow1)
bitmap2.SetCopyOnWrite(test.cow2)

bitmap2.AddRange(uint64(r1)-1, uint64(r1)+2)
bitmap2.AddRange(uint64(r2)-1, uint64(r2)+2)
bitmap2.AddRange(uint64(r3)-1, uint64(r3)+2)
bitmap2.AddRange(uint64(r4)-1, uint64(r4)+2)

assert.False(t, bitmap1.Contains(uint64(r2)))
assert.False(t, bitmap1.Contains(uint64(r3)))
assert.Equal(t, 0, len(bitmap1.highlowcontainer.keys))
bitmap1.highlowcontainer.appendCopiesAfter(bitmap2.highlowcontainer, 0)
assert.Equal(t, 4, len(bitmap1.highlowcontainer.keys))
assert.True(t, bitmap1.Contains(uint64(r2)))
assert.True(t, bitmap1.Contains(uint64(r3)))

for idx1, c1 := range bitmap1.highlowcontainer.containers {
for idx2, c2 := range bitmap2.highlowcontainer.containers {
// idx+1 is required because appendCopiesAfter starts at key 1
if idx1+1 == idx2 {
if test.cow1 && test.cow2 {
assert.True(t, c1 == c2)
} else {
assert.False(t, c1 == c2)
}
}
}
}
})
}

tests = []struct {
name string
cow1 bool
cow2 bool
}{
{"AppendCopiesUntilCoW", true, true},
{"AppendCopiesUntilCoW", false, false},
{"AppendCopiesUntilMixedCoW", true, false},
{"AppendCopiesUntilMixedCoW", false, true},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
r1 := 1 << 32
r2 := 2 << 32
r3 := 3 << 32
r4 := 4 << 32
key := highbits(uint64(r4))

bitmap1 := New()
bitmap2 := New()
bitmap1.SetCopyOnWrite(test.cow1)
bitmap2.SetCopyOnWrite(test.cow2)

bitmap2.AddRange(uint64(r1)-1, uint64(r1)+2)
bitmap2.AddRange(uint64(r2)-1, uint64(r2)+2)
bitmap2.AddRange(uint64(r3)-1, uint64(r3)+2)
bitmap2.AddRange(uint64(r4)-1, uint64(r4)+2)

assert.False(t, bitmap1.Contains(uint64(r2)))
assert.False(t, bitmap1.Contains(uint64(r3)))
assert.Equal(t, 0, len(bitmap1.highlowcontainer.keys))
bitmap1.highlowcontainer.appendCopiesUntil(bitmap2.highlowcontainer, key)
assert.Equal(t, 4, len(bitmap1.highlowcontainer.keys))
assert.True(t, bitmap1.Contains(uint64(r2)))
assert.True(t, bitmap1.Contains(uint64(r3)))

for idx1, c1 := range bitmap1.highlowcontainer.containers {
for idx2, c2 := range bitmap2.highlowcontainer.containers {
if idx1 == idx2 {
if test.cow1 && test.cow2 {
assert.True(t, c1 == c2)
} else {
assert.False(t, c1 == c2)
}
}
}
}
})
}
}

0 comments on commit 44c256c

Please sign in to comment.