2019-12-29 20:24:06 -05:00
|
|
|
package bitfield
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestHasPiece(t *testing.T) {
|
|
|
|
|
bf := Bitfield{0b01010100, 0b01010100}
|
2019-12-30 10:34:41 -05:00
|
|
|
outputs := []bool{false, true, false, true, false, true, false, false, false, true, false, true, false, true, false, false, false, false, false, false}
|
2019-12-29 20:24:06 -05:00
|
|
|
for i := 0; i < len(outputs); i++ {
|
|
|
|
|
assert.Equal(t, outputs[i], bf.HasPiece(i))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestSetPiece(t *testing.T) {
|
|
|
|
|
tests := []struct {
|
|
|
|
|
input Bitfield
|
|
|
|
|
index int
|
|
|
|
|
outpt Bitfield
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
input: Bitfield{0b01010100, 0b01010100},
|
2019-12-29 20:44:11 -05:00
|
|
|
index: 4, // v (set)
|
2019-12-29 20:24:06 -05:00
|
|
|
outpt: Bitfield{0b01011100, 0b01010100},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
input: Bitfield{0b01010100, 0b01010100},
|
2019-12-29 20:44:11 -05:00
|
|
|
index: 9, // v (noop)
|
2019-12-29 20:24:06 -05:00
|
|
|
outpt: Bitfield{0b01010100, 0b01010100},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
input: Bitfield{0b01010100, 0b01010100},
|
2019-12-29 20:44:11 -05:00
|
|
|
index: 15, // v (set)
|
2019-12-29 20:24:06 -05:00
|
|
|
outpt: Bitfield{0b01010100, 0b01010101},
|
|
|
|
|
},
|
2019-12-30 10:34:41 -05:00
|
|
|
{
|
|
|
|
|
input: Bitfield{0b01010100, 0b01010100},
|
|
|
|
|
index: 19, // v (noop)
|
|
|
|
|
outpt: Bitfield{0b01010100, 0b01010100},
|
|
|
|
|
},
|
2019-12-29 20:24:06 -05:00
|
|
|
}
|
|
|
|
|
for _, test := range tests {
|
|
|
|
|
bf := test.input
|
|
|
|
|
bf.SetPiece(test.index)
|
|
|
|
|
assert.Equal(t, test.outpt, bf)
|
|
|
|
|
}
|
|
|
|
|
}
|