update unit tests for bit reader

This commit is contained in:
Alex Yatskov 2019-02-09 16:25:03 -08:00
parent a67b06a511
commit 08d71f5bc4

View File

@ -2,12 +2,11 @@ package streaming
import ( import (
"bytes" "bytes"
"fmt" "log"
"testing" "testing"
) )
func TestBitReader(t *testing.T) { var data = []byte{
data := []byte{
0x01, // 00000001 0x01, // 00000001
0x23, // 00100011 0x23, // 00100011
0x45, // 01000101 0x45, // 01000101
@ -18,21 +17,50 @@ func TestBitReader(t *testing.T) {
0xEF, // 11101111 0xEF, // 11101111
} }
func testRead(test *testing.T, reader *BitReader, count int, expected uint64) {
value, err := reader.ReadUint64(count)
if err != nil {
log.Printf("error: %s\n", err.Error())
test.Fail()
} else if value != expected {
log.Printf("value: %.16x, expected: %.16x\n", value, expected)
test.Fail()
}
}
func TestUnsigned(t *testing.T) {
r := NewBitReader(bytes.NewReader(data)) r := NewBitReader(bytes.NewReader(data))
readPass := func(c int, v uint64) { testRead(t, r, 0, 0x00)
if value, err := r.ReadUint64(c); value != v || err != nil { testRead(t, r, 8, 0x01)
fmt.Printf("%.16x (expected %.16x)\n", value, v) testRead(t, r, 16, 0x4523)
t.Fail() testRead(t, r, 3, 0x67&0x07)
} testRead(t, r, 13, 0x8967>>3)
testRead(t, r, 13, 0xcdab&0x1fff)
testRead(t, r, 2, (0xcdab>>13)&3)
testRead(t, r, 9, 0xefcdab>>15)
} }
readPass(0, 0x00) func TestUnsignedSmall(t *testing.T) {
readPass(8, 0x01) r := NewBitReader(bytes.NewReader(data))
readPass(16, 0x4523)
readPass(3, 0x67&0x07) testRead(t, r, 0, 0x00)
readPass(13, 0x8967>>3) testRead(t, r, 8, 0x01)
readPass(13, 0xcdab&0x1fff) testRead(t, r, 8, 0x23)
readPass(2, (0xcdab>>13)&3) testRead(t, r, 8, 0x45)
readPass(9, 0xefcdab>>15) testRead(t, r, 16, 0x8967)
}
func TestUnsignedTiny(t *testing.T) {
r := NewBitReader(bytes.NewReader(data))
testRead(t, r, 0, 0x00)
testRead(t, r, 8, 0x01)
testRead(t, r, 16, 0x4523)
testRead(t, r, 3, 0x67&0x07)
testRead(t, r, 13, 0x8967>>3)
testRead(t, r, 13, 0xCDAB&0x1fff)
testRead(t, r, 2, (0xcdab>>13)&0x03)
testRead(t, r, 5, (0xefcdab>>15)&0x1f)
testRead(t, r, 4, (0xefcdab>>20)&0x0f)
} }