tests for binSearchRuns

This commit is contained in:
Todd Gruben 2017-06-14 17:13:15 -05:00 committed by Matt Jaffee
parent b7a790e320
commit 056670389e
2 changed files with 62 additions and 8 deletions

View file

@ -1342,7 +1342,7 @@ func (c *container) bitmapContains(v uint32) bool {
//runBinSearch uses a binary search of the runs and returns the index of nearest
//run.
func runBinSearch(v uint32, a []interval32) (int, bool) {
func binSearchRuns(v uint32, a []interval32) (int, bool) {
i := sort.Search(len(a),
func(i int) bool { return a[i].last >= v })
if i < len(a) {
@ -1354,7 +1354,7 @@ func runBinSearch(v uint32, a []interval32) (int, bool) {
//runContains determines if v is in the containers run set.
func (c *container) runContains(v uint32) bool {
_, found := runBinSearch(v, c.runs)
_, found := binSearchRuns(v, c.runs)
return found
}

View file

@ -15,8 +15,8 @@
package roaring
import (
"fmt"
"bytes"
"fmt"
"reflect"
"testing"
)
@ -1468,9 +1468,10 @@ func TestXorBitmapRun(t *testing.T) {
runs []interval32
exp []uint64
}{
{bitmap: []uint64{0x0, 0x0, 0x0},
runs: []interval32{{start: 129, last: 131}},
exp: []uint64{0x0, 0x0, 0x00000000000000E},
{
bitmap: []uint64{0x0, 0x0, 0x0},
runs: []interval32{{start: 129, last: 131}},
exp: []uint64{0x0, 0x0, 0x00000000000000E},
},
}
for i, test := range tests {
@ -1535,10 +1536,10 @@ func TestIteratorBitmap(t *testing.T) {
// this dataset will update to bitmap after enough Adds,
// but won't update to RLE until Optimize() is called
b := NewBitmap()
for i := uint64(61000); i<71000; i++ {
for i := uint64(61000); i < 71000; i++ {
b.Add(i)
}
for i := uint64(75000); i<75100; i++ {
for i := uint64(75000); i < 75100; i++ {
b.Add(i)
}
if !b.containers[0].isBitmap() {
@ -1638,3 +1639,56 @@ func TestIteratorRuns(t *testing.T) {
t.Fatalf("iterator did not eof correctly: %d, %v\n", val, eof)
}
}
func TestRunBinSearchContains(t *testing.T) {
tests := []struct {
runs []interval32
index uint32
exp struct {
index int
found bool
}
}{
{
runs: []interval32{{start: 0, last: 10}},
index: uint32(3),
exp: struct {
index int
found bool
}{index: 0, found: true},
},
{
runs: []interval32{{start: 0, last: 10}},
index: uint32(13),
exp: struct {
index int
found bool
}{index: 0, found: false},
},
{
runs: []interval32{{start: 0, last: 10}, {start: 20, last: 30}},
index: uint32(13),
exp: struct {
index int
found bool
}{index: 0, found: false},
},
{
runs: []interval32{{start: 0, last: 10}, {start: 20, last: 30}},
index: uint32(36),
exp: struct {
index int
found bool
}{index: 1, found: false},
},
}
for i, test := range tests {
index := test.index
runs := test.runs
idx, found := binSearchRuns(index, runs)
if test.exp.index != idx && test.exp.found != found {
t.Fatalf("test #%v expected %v , but got %v %v", i, test.exp, idx, found)
}
}
}