Skip to content

Commit 996a67e

Browse files
authored
Merge pull request #208 from whisk/master
fix off by 1 error in ShiftRight
2 parents d33f3c7 + 57b6694 commit 996a67e

2 files changed

Lines changed: 40 additions & 3 deletions

File tree

bitset.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1531,7 +1531,7 @@ func (b *BitSet) ShiftRight(bits uint) {
15311531
return
15321532
}
15331533

1534-
if bits >= top {
1534+
if bits > top {
15351535
b.set = make([]uint64, wordsNeeded(b.length))
15361536
return
15371537
}

bitset_test.go

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2409,7 +2409,7 @@ func TestShiftRight(t *testing.T) {
24092409

24102410
count := 0
24112411
for _, i := range data {
2412-
if i > bits {
2412+
if i >= bits {
24132413
count++
24142414

24152415
if !b.Test(i - bits) {
@@ -2419,7 +2419,7 @@ func TestShiftRight(t *testing.T) {
24192419
}
24202420

24212421
if int(b.Count()) != count {
2422-
t.Error("bad bits count")
2422+
t.Errorf("bad bits count: expected %d, got %d", count, b.Count())
24232423
}
24242424
})
24252425
}
@@ -2434,6 +2434,43 @@ func TestShiftRight(t *testing.T) {
24342434
test("remove all", 242)
24352435
}
24362436

2437+
func TestShiftRightFull(t *testing.T) {
2438+
testCases := []struct{
2439+
data []uint
2440+
shiftDistance uint
2441+
}{
2442+
{
2443+
[]uint{20}, 20,
2444+
},
2445+
{
2446+
[]uint{0, 20, 40, 1260, 1280}, 1,
2447+
},
2448+
{
2449+
[]uint{0, 20, 40, 1260, 1280}, 1281,
2450+
},
2451+
}
2452+
2453+
test := func(data []uint, shiftDistance uint) {
2454+
b := New(0)
2455+
for i := range data {
2456+
b.Set(data[i])
2457+
}
2458+
b.ShiftRight(shiftDistance)
2459+
for i := range data {
2460+
shiftedBit := int(data[i])-int(shiftDistance)
2461+
if shiftedBit >= 0 {
2462+
if !b.Test(uint(shiftedBit)) {
2463+
t.Errorf("bit %d should be set after ShiftRight(%d) if bit %d was set prior", data[i]-shiftDistance, shiftDistance, data[i])
2464+
}
2465+
}
2466+
}
2467+
}
2468+
2469+
for i := range testCases {
2470+
test(testCases[i].data, testCases[i].shiftDistance)
2471+
}
2472+
}
2473+
24372474
func TestWord(t *testing.T) {
24382475
data := []uint64{0x0bfd85fc01af96dd, 0x3fe212a7eae11414, 0x7aa412221245dee1, 0x557092c1711306d5}
24392476
testCases := map[string]struct {

0 commit comments

Comments
 (0)