fix ReadVarSize bug
This commit is contained in:
3
bin.go
3
bin.go
@@ -1022,9 +1022,10 @@ func ReadVarSize(src io.ByteReader) (_ int, err error) {
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if b >= 128 {
|
||||
num |= uint64(b&127) << uint(i*7)
|
||||
return
|
||||
return int(num), nil
|
||||
}
|
||||
num |= uint64(b) << uint(i*7)
|
||||
}
|
||||
|
||||
32
bin_test.go
32
bin_test.go
@@ -669,3 +669,35 @@ func TestInt32(t *testing.T) {
|
||||
|
||||
fmt.Println(num)
|
||||
}
|
||||
|
||||
func TestVarSize(t *testing.T) {
|
||||
var (
|
||||
buf = bytes.NewBuffer(nil)
|
||||
|
||||
nums = []int{
|
||||
127, // 7
|
||||
16383, // 14
|
||||
2097151, // 21
|
||||
268435455, // 28
|
||||
34359738367, // 35
|
||||
4398046511103, // 42
|
||||
562949953421311, // 49
|
||||
72057594037927935, // 56
|
||||
9223372036854775807, // 63
|
||||
}
|
||||
)
|
||||
|
||||
for _, num := range nums {
|
||||
WriteVarSize(buf, num)
|
||||
|
||||
decoded, err := ReadVarSize(buf)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if decoded != num {
|
||||
t.Fatalf("num %d decoded as %d\n", num, decoded)
|
||||
}
|
||||
buf.Reset()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user