From 056670389ef538f206160cf96e43256eca717320 Mon Sep 17 00:00:00 2001 From: Todd Gruben Date: Wed, 14 Jun 2017 17:13:15 -0500 Subject: [PATCH] tests for binSearchRuns --- roaring/roaring.go | 4 +- roaring/roaring_internal_test.go | 66 +++++++++++++++++++++++++++++--- 2 files changed, 62 insertions(+), 8 deletions(-) diff --git a/roaring/roaring.go b/roaring/roaring.go index d6a52acc3..2de26d812 100644 --- a/roaring/roaring.go +++ b/roaring/roaring.go @@ -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 } diff --git a/roaring/roaring_internal_test.go b/roaring/roaring_internal_test.go index 62973bf00..400f99b66 100644 --- a/roaring/roaring_internal_test.go +++ b/roaring/roaring_internal_test.go @@ -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) + } + } +}