|
|
|
@ -10,8 +10,8 @@ var (
|
|
|
|
|
// 1000 1011 1010 1101 1111 0000 0000 1101
|
|
|
|
|
badFood = []byte{0x8b, 0xad, 0xf0, 0x0d}
|
|
|
|
|
|
|
|
|
|
// 0000 1110 0001 1110 1110 0111 1011 1110 1110 1111
|
|
|
|
|
eLeetBeef = []byte{0x0e, 0x1e, 0xe7, 0xbe, 0xef}
|
|
|
|
|
// 1110 1100 0111 0000 1100 0000 0000 0001 1110 0010
|
|
|
|
|
ectoCooler = []byte{0xec, 0x70, 0xc0, 0x01, 0xe2}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// test the bit-level reads
|
|
|
|
@ -32,28 +32,30 @@ func TestReadBits(t *testing.T) {
|
|
|
|
|
r = NewBytesReader(badFood)
|
|
|
|
|
|
|
|
|
|
// 1000 1011 1010 1101 1111 0000 0000 1101
|
|
|
|
|
// ^
|
|
|
|
|
// ^
|
|
|
|
|
assert.Equal(uint64(0x01), r.ReadBits(1))
|
|
|
|
|
|
|
|
|
|
// 1000 1011 1010 1101 1111 0000 0000 1101
|
|
|
|
|
// ^-^
|
|
|
|
|
assert.Equal(uint64(0), r.ReadBits(3))
|
|
|
|
|
// ^-^
|
|
|
|
|
assert.Equal(uint64(0x5), r.ReadBits(3))
|
|
|
|
|
|
|
|
|
|
// 1000 1011 1010 1101 1111 0000 0000 1101
|
|
|
|
|
// ^--^
|
|
|
|
|
assert.Equal(uint64(0xb), r.ReadBits(4))
|
|
|
|
|
// ^--^
|
|
|
|
|
assert.Equal(uint64(0x8), r.ReadBits(4))
|
|
|
|
|
|
|
|
|
|
// 1000 1011 1010 1101 1111 0000 0000 1101
|
|
|
|
|
// ^----^
|
|
|
|
|
assert.Equal(uint64(0x15), r.ReadBits(5))
|
|
|
|
|
// ^----^
|
|
|
|
|
assert.Equal(uint64(0xd), r.ReadBits(5))
|
|
|
|
|
|
|
|
|
|
// 1000 1011 1010 1101 1111 0000 0000 1101
|
|
|
|
|
// ^----+----^
|
|
|
|
|
assert.Equal(uint64(0x17c), r.ReadBits(9))
|
|
|
|
|
// ^-^.........^-----^
|
|
|
|
|
// data bits: 1 1000 0101
|
|
|
|
|
assert.Equal(uint64(0x185), r.ReadBits(9))
|
|
|
|
|
|
|
|
|
|
// 1000 1011 1010 1101 1111 0000 0000 1101
|
|
|
|
|
// ^----+----+^
|
|
|
|
|
assert.Equal(uint64(0xd), r.ReadBits(10))
|
|
|
|
|
// ^^........^-------^
|
|
|
|
|
// data bits: 00 0011 0111
|
|
|
|
|
assert.Equal(uint64(0x37), r.ReadBits(10))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// test the Read calls, satisfying io.Reader
|
|
|
|
@ -74,16 +76,18 @@ func TestRead(t *testing.T) {
|
|
|
|
|
assert.Equal(badFood, buf)
|
|
|
|
|
|
|
|
|
|
r = NewBytesReader(badFood)
|
|
|
|
|
// 1000 1011 1010 1101 1111 0000 0000 1101
|
|
|
|
|
r.ReadBits(1)
|
|
|
|
|
// 0001 0111 0101 1011 1110 0000 0001 101
|
|
|
|
|
// 1000 1011 1010 1101 1111 0000 0000 1101
|
|
|
|
|
// ^------^..........^ : 1100 0101
|
|
|
|
|
// ^------^..........^ : 0101 0110
|
|
|
|
|
// ^------^..........^ : 1111 1000
|
|
|
|
|
buf = make([]byte, 3)
|
|
|
|
|
|
|
|
|
|
n, err = r.Read(buf)
|
|
|
|
|
assert.NoError(err)
|
|
|
|
|
assert.Equal(3, n)
|
|
|
|
|
|
|
|
|
|
expected := []byte{0x17, 0x5b, 0xe0}
|
|
|
|
|
expected := []byte{0xc5, 0x56, 0xf8}
|
|
|
|
|
assert.Equal(expected, buf)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -95,38 +99,61 @@ func TestUbitVar(t *testing.T) {
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// 1000 1011 1010 1101 1111 0000 0000 1101
|
|
|
|
|
// ||^---^ : data (0101)
|
|
|
|
|
// ^^ : prefix (00)
|
|
|
|
|
r = NewBytesReader(badFood)
|
|
|
|
|
|
|
|
|
|
// 1000 1011 1010 1101 1111 0000 0000 1101
|
|
|
|
|
// ^
|
|
|
|
|
r.ReadBits(1)
|
|
|
|
|
|
|
|
|
|
// 1000 1011 1010 1101 1111 0000 0000 1101
|
|
|
|
|
// ^^ : prefix 00
|
|
|
|
|
// ^---^ : data 0101
|
|
|
|
|
u = r.ReadUBitVar()
|
|
|
|
|
assert.Equal(uint64(5), u)
|
|
|
|
|
|
|
|
|
|
// 1000 1011 1010 1101 1111 0000 0000 1101
|
|
|
|
|
// ^.................^
|
|
|
|
|
r.ReadBits(2)
|
|
|
|
|
|
|
|
|
|
// 1000 1011 1010 1101 1111 0000 0000 1101
|
|
|
|
|
// ||| | ^--^ : msb (1111)
|
|
|
|
|
// ||^---^ : lsb (0110)
|
|
|
|
|
// ^^ : prefix (01)
|
|
|
|
|
// ^^ : prefix 01
|
|
|
|
|
// ^...............^-^ : msb 0001
|
|
|
|
|
// ^---^ : lsb 0110
|
|
|
|
|
u = r.ReadUBitVar()
|
|
|
|
|
assert.Equal(uint64(0xf6), u)
|
|
|
|
|
assert.Equal(uint64(0x16), u)
|
|
|
|
|
|
|
|
|
|
// 1000 1011 1010 1101 1111 0000 0000 1101
|
|
|
|
|
r = NewBytesReader(badFood)
|
|
|
|
|
|
|
|
|
|
// 1000 1011 1010 1101 1111 0000 0000 1101
|
|
|
|
|
// ||| |^----+---^ : msb (1110 1011)
|
|
|
|
|
// ||^---^ : lsb (0010)
|
|
|
|
|
// ^^ : prefix (10)
|
|
|
|
|
// ^^
|
|
|
|
|
r.ReadBits(2)
|
|
|
|
|
|
|
|
|
|
// 1000 1011 1010 1101 1111 0000 0000 1101
|
|
|
|
|
// ^^ : prefix 10
|
|
|
|
|
// ^-------^ : msb 1010 1101
|
|
|
|
|
// ^---^ : lsb 0010
|
|
|
|
|
u = r.ReadUBitVar()
|
|
|
|
|
assert.Equal(uint64(0xeb2), u)
|
|
|
|
|
|
|
|
|
|
r = NewBytesReader(eLeetBeef)
|
|
|
|
|
r.ReadBits(4)
|
|
|
|
|
// 0000 1110 0001 1110 1110 0111 1011 1110 1110 1111
|
|
|
|
|
// ||| |^----+----+----+----+----+----+---^ : msb (0111 1011 1001 1110
|
|
|
|
|
// ||| | 1111 1011 1011)
|
|
|
|
|
// ||^---^ : lsb (1000)
|
|
|
|
|
// ^^ : prefix (11)
|
|
|
|
|
assert.Equal(uint64(0xad2), u)
|
|
|
|
|
|
|
|
|
|
// 1110 1100 0111 0000 1100 0000 0000 0001 1110 0010
|
|
|
|
|
r = NewBytesReader(ectoCooler)
|
|
|
|
|
|
|
|
|
|
// 1110 1100 0111 0000 1100 0000 0000 0001 1110 0010
|
|
|
|
|
// ^^
|
|
|
|
|
r.ReadBits(2)
|
|
|
|
|
|
|
|
|
|
// 1110 1100 0111 0000 1100 0000 0000 0001 1110 0010
|
|
|
|
|
// ^^ : prefix 11
|
|
|
|
|
// ^--^ : msb 0010
|
|
|
|
|
// ^-------^ : 0000 0001
|
|
|
|
|
// ^-------^ : 1110 0000
|
|
|
|
|
// ^-------^ : 0111 0000
|
|
|
|
|
// ^---^ : lsb 1011
|
|
|
|
|
// data bits:
|
|
|
|
|
// 0010 0000 0001 1110 0000 0111 0000 1011
|
|
|
|
|
u = r.ReadUBitVar()
|
|
|
|
|
assert.Equal(uint64(0x7b9efbb8), u)
|
|
|
|
|
assert.Equal(uint64(0x201c070b), u)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestVarInt(t *testing.T) {
|
|
|
|
@ -139,7 +166,7 @@ func TestVarInt(t *testing.T) {
|
|
|
|
|
r = NewBytesReader(badFood)
|
|
|
|
|
r.ReadBits(24)
|
|
|
|
|
// 1000 1011 1010 1101 1111 0000 0000 1101
|
|
|
|
|
// |^------^ : data
|
|
|
|
|
// ^------^ : data
|
|
|
|
|
// ^ : stop
|
|
|
|
|
u = r.ReadVarInt()
|
|
|
|
|
assert.Equal(uint64(0xd), u)
|
|
|
|
@ -147,9 +174,9 @@ func TestVarInt(t *testing.T) {
|
|
|
|
|
r = NewBytesReader(badFood)
|
|
|
|
|
r.ReadBits(16)
|
|
|
|
|
// 1000 1011 1010 1101 1111 0000 0000 1101
|
|
|
|
|
// || | |^------^ : msb
|
|
|
|
|
// || | ^ : stop
|
|
|
|
|
// |^------^ : lsb
|
|
|
|
|
// ^------^ : msb
|
|
|
|
|
// ^ : stop
|
|
|
|
|
// ^------^ : lsb
|
|
|
|
|
// ^ : continue
|
|
|
|
|
// data bits:
|
|
|
|
|
// 0000 0110 1111 0000
|
|
|
|
@ -159,13 +186,13 @@ func TestVarInt(t *testing.T) {
|
|
|
|
|
|
|
|
|
|
r = NewBytesReader(badFood)
|
|
|
|
|
// 1000 1011 1010 1101 1111 0000 0000 1101
|
|
|
|
|
// || | || | || | |^------^ : msb (000 1101)
|
|
|
|
|
// || | || | || | ^ : stop
|
|
|
|
|
// || | || | |^------^ : data (111 0000)
|
|
|
|
|
// || | || | ^ : continue
|
|
|
|
|
// || | |^------^ : data (010 1101)
|
|
|
|
|
// || | ^ : continue
|
|
|
|
|
// |^------^ : lsb (000 1011)
|
|
|
|
|
// ^------^ : msb 000 1101
|
|
|
|
|
// ^ : stop
|
|
|
|
|
// ^------^ : data 111 0000
|
|
|
|
|
// ^ : continue
|
|
|
|
|
// ^------^ : data 010 1101
|
|
|
|
|
// ^ : continue
|
|
|
|
|
// ^------^ : lsb 000 1011
|
|
|
|
|
// ^ : continue
|
|
|
|
|
// data bits:
|
|
|
|
|
// 0001 1011 1100 0001 0110 1000 1011
|
|
|
|
|