Skip to content

Commit 1a28a7f

Browse files
committed
Fix for #183
1 parent f29e7f7 commit 1a28a7f

File tree

4 files changed

+48
-11
lines changed

4 files changed

+48
-11
lines changed

aggregation_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package roaring
33
// to run just these tests: go test -run TestParAggregations
44

55
import (
6-
"testing"
76
"fmt"
7+
"testing"
88
)
99

1010
func testAggregations(t *testing.T,

bitmapcontainer.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -712,11 +712,11 @@ func (bc *bitmapContainer) getCardinalityInRange(start, end uint) int {
712712
endword := (end - 1) / 64
713713
const allones = ^uint64(0)
714714
if firstword == endword {
715-
return int(popcount(bc.bitmap[firstword] & ((allones << (start % 64)) & (allones >> (64 - (end % 64))))))
715+
return int(popcount(bc.bitmap[firstword] & ((allones << (start % 64)) & (allones >> ((64 - end) & 63)))))
716716
}
717717
answer := popcount(bc.bitmap[firstword] & (allones << (start % 64)))
718-
answer += popcntSlice(bc.bitmap[firstword+1 : endword+1])
719-
answer += popcount(bc.bitmap[endword] & (allones >> (64 - (end % 64))))
718+
answer += popcntSlice(bc.bitmap[firstword+1 : endword])
719+
answer += popcount(bc.bitmap[endword] & (allones >> ((64 - end) & 63)))
720720
return int(answer)
721721
}
722722

bitmapcontainer_test.go

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package roaring
22

33
import (
4+
. "github.com/smartystreets/goconvey/convey"
45
"math/rand"
56
"testing"
6-
7-
. "github.com/smartystreets/goconvey/convey"
87
)
98

109
func TestBitmapContainerNumberOfRuns024(t *testing.T) {
@@ -69,8 +68,46 @@ func TestBitmapContainerNumberOfRuns024(t *testing.T) {
6968

7069
func TestBitmapcontainerAndCardinality(t *testing.T) {
7170
Convey("bitmap containers get cardinality in range, miss the last index, issue #183", t, func() {
72-
c1 := newRunContainer16Range(0, 65535)
73-
c2 := newBitmapContainerwithRange(0, 65535)
74-
So(65536, ShouldEqual, c1.andCardinality(c2))
71+
for r := 0; r <= 65535; r += 1 {
72+
c1 := newRunContainer16Range(0, uint16(r))
73+
c2 := newBitmapContainerwithRange(0, int(r))
74+
So(r+1, ShouldEqual, c1.andCardinality(c2))
75+
}
76+
})
77+
}
78+
79+
func TestIssue181(t *testing.T) {
80+
81+
Convey("Initial issue 181", t, func() {
82+
a := New()
83+
var x uint32
84+
85+
// adding 1M integers
86+
for i := 1; i <= 1000000; i++ {
87+
x += uint32(rand.Intn(10) + 1)
88+
a.Add(x)
89+
}
90+
b := New()
91+
for i := 1; i <= int(x); i++ {
92+
b.Add(uint32(i))
93+
}
94+
So(b.AndCardinality(a), ShouldEqual, a.AndCardinality(b))
95+
So(b.AndCardinality(a), ShouldEqual, And(a, b).GetCardinality())
96+
})
97+
Convey("Second version of issue 181", t, func() {
98+
a := New()
99+
var x uint32
100+
101+
// adding 1M integers
102+
for i := 1; i <= 1000000; i++ {
103+
x += uint32(rand.Intn(10) + 1)
104+
a.Add(x)
105+
}
106+
b := New()
107+
b.AddRange(1, uint64(x))
108+
109+
So(b.AndCardinality(a), ShouldEqual, a.AndCardinality(b))
110+
So(b.AndCardinality(a), ShouldEqual, And(a, b).GetCardinality())
111+
75112
})
76113
}

serialization_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,10 +1009,10 @@ func TestSerializationCrashers(t *testing.T) {
10091009
orig := make([]byte, len(data))
10101010
copy(orig, data)
10111011

1012-
catchPanic(t, func() { NewBitmap().FromBuffer(data) }, "FromBuffer(" + crasher + ")")
1012+
catchPanic(t, func() { NewBitmap().FromBuffer(data) }, "FromBuffer("+crasher+")")
10131013

10141014
// reset for next one
10151015
copy(data, orig)
1016-
catchPanic(t, func() { NewBitmap().ReadFrom(bytes.NewReader(data)) }, "ReadFrom(" + crasher + ")")
1016+
catchPanic(t, func() { NewBitmap().ReadFrom(bytes.NewReader(data)) }, "ReadFrom("+crasher+")")
10171017
}
10181018
}

0 commit comments

Comments
 (0)