Skip to content

Commit

Permalink
Fix a bug in bit_array's sub function (#2506)
Browse files Browse the repository at this point in the history
  • Loading branch information
james-ray authored and melekes committed Oct 3, 2018
1 parent f3d08f9 commit c94133e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion libs/common/bit_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func (bA *BitArray) Sub(o *BitArray) *BitArray {
if bA.Bits > o.Bits {
c := bA.copy()
for i := 0; i < len(o.Elems)-1; i++ {
c.Elems[i] &= ^c.Elems[i]
c.Elems[i] &= ^o.Elems[i]
}
i := len(o.Elems) - 1
if i >= 0 {
Expand Down
28 changes: 28 additions & 0 deletions libs/common/bit_array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,34 @@ func TestSub2(t *testing.T) {
}
}

func TestSub3(t *testing.T) {

bA1, _ := randBitArray(231)
bA2, _ := randBitArray(81)
bA3 := bA1.Sub(bA2)

bNil := (*BitArray)(nil)
require.Equal(t, bNil.Sub(bA1), (*BitArray)(nil))
require.Equal(t, bA1.Sub(nil), (*BitArray)(nil))
require.Equal(t, bNil.Sub(nil), (*BitArray)(nil))

if bA3.Bits != bA1.Bits {
t.Error("Expected bA1 bits")
}
if len(bA3.Elems) != len(bA1.Elems) {
t.Error("Expected bA1 elems length")
}
for i := 0; i < bA3.Bits; i++ {
expected := bA1.GetIndex(i)
if i < bA2.Bits && bA2.GetIndex(i){
expected = false
}
if bA3.GetIndex(i) != expected {
t.Error("Wrong bit from bA3")
}
}
}

func TestPickRandom(t *testing.T) {
for idx := 0; idx < 123; idx++ {
bA1 := NewBitArray(123)
Expand Down

0 comments on commit c94133e

Please sign in to comment.