bitfield handle

This commit is contained in:
liwei committed 2019-07-06 14:35:19 +08:00
1 parent 544e8bc7b1
commit d6c88d0c4b
7 files changed
+216 -423

No files matched your search

+61 -2
View File
@@ -1,11 +1,13 @@
package peer
import (
"fmt"
"reflect"
"strconv"
"testing"
)
func TestMsgBitfield_get(t *testing.T) {
func TestMsgBitfield_IsComplete(t *testing.T) {
type args struct {
i int
}
@@ -66,13 +68,70 @@ func TestMsgBitfield_get(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.mb.get(tt.args.i); got != tt.want {
if got := tt.mb.IsComplete(tt.args.i); got != tt.want {
t.Errorf("MsgBitfield.get() = %v, want %v", got, tt.want)
}
})
}
}
func TestMsgBitfield_Have(t *testing.T) {
type args struct {
pieces []bool
}
tests := []struct {
name string
mb MsgBitfield
args args
want []int
}{
{
"case-1",
MsgBitfield([]byte{convertByte("11111111"), convertByte("00000000")}),
args{
[]bool{false, false, false, false, false, false, false, false},
},
[]int{0, 1, 2, 3, 4, 5, 6, 7},
},
{
"case-2",
MsgBitfield([]byte{convertByte("01010101"), convertByte("01000000")}),
args{
[]bool{false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
},
[]int{1, 3, 5, 7, 9},
},
{
"case-3",
MsgBitfield([]byte{convertByte("11010101"), convertByte("01000000")}),
args{
[]bool{false, true, false},
},
[]int{0},
},
{
"case-4",
MsgBitfield([]byte{convertByte("11010101")}),
args{
[]bool{false, true, false, false, true, false, false, true, false},
},
[]int{0, 3, 5},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.mb.Have(tt.args.pieces); !reflect.DeepEqual(got, tt.want) {
t.Errorf("MsgBitfield.Have() = %v, want %v", got, tt.want)
}
})
}
}
func TestMsg(t *testing.T) {
fmt.Println(Choke)
fmt.Println(Bitfield)
}
func convertByte(bit string) byte {
i, _ := strconv.ParseUint(bit, 2, 8)
return byte(i)