mirror of
https://github.com/tendermint/tendermint.git
synced 2026-01-03 19:53:58 +00:00
bit_array: Simplify subtraction
also, fix potential bug in Or function
This commit is contained in:
committed by
Anton Kaliaev
parent
c94133ed1b
commit
0755a5203d
@@ -75,87 +75,34 @@ func TestOr(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSub1(t *testing.T) {
|
||||
|
||||
bA1, _ := randBitArray(31)
|
||||
bA2, _ := randBitArray(51)
|
||||
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")
|
||||
func TestSub(t *testing.T) {
|
||||
testCases := []struct {
|
||||
initBA string
|
||||
subtractingBA string
|
||||
expectedBA string
|
||||
}{
|
||||
{`null`, `null`, `null`},
|
||||
{`"x"`, `null`, `null`},
|
||||
{`null`, `"x"`, `null`},
|
||||
{`"x"`, `"x"`, `"_"`},
|
||||
{`"xxxxxx"`, `"x_x_x_"`, `"_x_x_x"`},
|
||||
{`"x_x_x_"`, `"xxxxxx"`, `"______"`},
|
||||
{`"xxxxxx"`, `"x_x_x_xxxx"`, `"_x_x_x"`},
|
||||
{`"x_x_x_xxxx"`, `"xxxxxx"`, `"______xxxx"`},
|
||||
{`"xxxxxxxxxx"`, `"x_x_x_"`, `"_x_x_xxxxx"`},
|
||||
{`"x_x_x_"`, `"xxxxxxxxxx"`, `"______"`},
|
||||
}
|
||||
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 bA2.GetIndex(i) {
|
||||
expected = false
|
||||
}
|
||||
if bA3.GetIndex(i) != expected {
|
||||
t.Error("Wrong bit from bA3", i, bA1.GetIndex(i), bA2.GetIndex(i), bA3.GetIndex(i))
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
var bA *BitArray
|
||||
err := json.Unmarshal([]byte(tc.initBA), &bA)
|
||||
require.Nil(t, err)
|
||||
|
||||
func TestSub2(t *testing.T) {
|
||||
var o *BitArray
|
||||
err = json.Unmarshal([]byte(tc.subtractingBA), &o)
|
||||
require.Nil(t, err)
|
||||
|
||||
bA1, _ := randBitArray(51)
|
||||
bA2, _ := randBitArray(31)
|
||||
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 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")
|
||||
}
|
||||
got, _ := json.Marshal(bA.Sub(o))
|
||||
require.Equal(t, tc.expectedBA, string(got), "%s minus %s doesn't equal %s", tc.initBA, tc.subtractingBA, tc.expectedBA)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user