Browse Source

Fix a bug in bit_array's sub function (#2506)

pull/2533/head
JamesRay 6 years ago
committed by Anton Kaliaev
parent
commit
c94133ed1b
2 changed files with 29 additions and 1 deletions
  1. +1
    -1
      libs/common/bit_array.go
  2. +28
    -0
      libs/common/bit_array_test.go

+ 1
- 1
libs/common/bit_array.go View File

@ -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 {


+ 28
- 0
libs/common/bit_array_test.go View File

@ -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)


Loading…
Cancel
Save