2019-01-14 03:02:49 +00:00
|
|
|
package streaming
|
|
|
|
|
2019-01-16 02:53:13 +00:00
|
|
|
import (
|
|
|
|
"bytes"
|
2019-02-10 00:25:03 +00:00
|
|
|
"log"
|
2019-01-16 02:53:13 +00:00
|
|
|
"testing"
|
|
|
|
)
|
2019-01-14 03:02:49 +00:00
|
|
|
|
2019-02-10 00:25:03 +00:00
|
|
|
var data = []byte{
|
|
|
|
0x01, // 00000001
|
|
|
|
0x23, // 00100011
|
|
|
|
0x45, // 01000101
|
|
|
|
0x67, // 01100111
|
|
|
|
0x89, // 01100111
|
|
|
|
0xAB, // 10101011
|
|
|
|
0xCD, // 11001101
|
|
|
|
0xEF, // 11101111
|
|
|
|
}
|
|
|
|
|
2019-02-10 03:12:04 +00:00
|
|
|
func TestBitReader(t *testing.T) {
|
2019-01-23 02:49:23 +00:00
|
|
|
r := NewBitReader(bytes.NewReader(data))
|
2019-01-16 03:44:02 +00:00
|
|
|
|
2019-02-10 03:12:04 +00:00
|
|
|
test := func(count int, expected uint64) {
|
2019-02-10 16:55:14 +00:00
|
|
|
if value := r.ReadUint(count); value != expected {
|
2019-02-10 03:12:04 +00:00
|
|
|
log.Printf("value: %.16x, expected: %.16x\n", value, expected)
|
|
|
|
t.Fail()
|
|
|
|
}
|
2019-02-10 16:55:14 +00:00
|
|
|
|
|
|
|
if err := r.Error(); err != nil {
|
|
|
|
log.Printf("error: %s\n", err.Error())
|
|
|
|
t.Fail()
|
|
|
|
}
|
2019-02-10 03:12:04 +00:00
|
|
|
}
|
2019-01-16 03:44:02 +00:00
|
|
|
|
2019-02-10 03:12:04 +00:00
|
|
|
test(0, 0x00)
|
|
|
|
test(8, 0x01)
|
|
|
|
test(16, 0x4523)
|
|
|
|
test(3, 0x67&0x07)
|
|
|
|
test(13, 0x8967>>3)
|
|
|
|
test(13, 0xcdab&0x1fff)
|
|
|
|
test(2, (0xcdab>>13)&3)
|
|
|
|
test(9, 0xefcdab>>15)
|
2019-01-14 03:02:49 +00:00
|
|
|
}
|