Skip to content

Commit

Permalink
Tests and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
skullhole committed Aug 23, 2024
1 parent 1ceac61 commit f156426
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
2 changes: 2 additions & 0 deletions bitset.go
Original file line number Diff line number Diff line change
Expand Up @@ -1228,6 +1228,8 @@ func (b *BitSet) ShiftLeft(bits uint) {
nsize := wordsNeeded(top + bits)
if len(b.set) < nsize {
dst = make([]uint64, nsize, 2*nsize)
}
if top+bits >= b.length {
b.length = top + bits + 1
}

Expand Down
71 changes: 71 additions & 0 deletions bitset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1963,3 +1963,74 @@ func TestSetAll(t *testing.T) {
test(fmt.Sprintf("length %d", length), New(length), length)
}
}

func TestShiftLeft(t *testing.T) {
data := []uint{5, 28, 45, 72, 89}

test := func(name string, bits uint) {
t.Run(name, func(t *testing.T) {
b := New(200)
for _, i := range data {
b.Set(i)
}

b.ShiftLeft(bits)

if int(b.Count()) != len(data) {
t.Error("bad bits count")
}

for _, i := range data {
if !b.Test(i + bits) {
t.Errorf("bit %v is not set", i+bits)
}
}
})
}

test("zero", 0)
test("no page change", 19)
test("shift to full page", 38)
test("full page shift", 64)
test("no page split", 80)
test("with page split", 114)
test("with extension", 242)
}

func TestShiftRight(t *testing.T) {
data := []uint{5, 28, 45, 72, 89}

test := func(name string, bits uint) {
t.Run(name, func(t *testing.T) {
b := New(200)
for _, i := range data {
b.Set(i)
}

b.ShiftRight(bits)

count := 0
for _, i := range data {
if i > bits {
count++

if !b.Test(i - bits) {
t.Errorf("bit %v is not set", i-bits)
}
}
}

if int(b.Count()) != count {
t.Error("bad bits count")
}
})
}

test("zero", 0)
test("no page change", 3)
test("no page split", 20)
test("with page split", 40)
test("with extension", 70)
test("full shift", 89)
test("remove all", 242)
}

0 comments on commit f156426

Please sign in to comment.