-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitreader_test.go
62 lines (46 loc) · 1.3 KB
/
bitreader_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package bgen
import (
"encoding/binary"
"testing"
)
func TestBitReader(t *testing.T) {
var target uint64 = 3
data := make([]byte, 8) // Big enough to hold a uint64
binary.LittleEndian.PutUint64(data, target)
val := 0
br := newBitReader(data, 7)
for i := 0; i < len(data); i++ {
bit := br.getBit(i)
val |= 1 << bit
}
if target != uint64(val) {
t.Errorf("Got %d, expected %d", val, target)
}
}
func TestBitReaderLittleEndian7Bit(t *testing.T) {
value := []byte{93}
br := newBitReader(value, 7)
valBig := br.Next()
if valBig != 93 {
t.Errorf("First 7 bits of %d yielded %d, expected %d", value[0], valBig, 93)
}
}
func TestBitReader8Bit(t *testing.T) {
value := []byte{93, 0, 0xff}
br := newBitReader(value, 8)
for i := 0; i < len(value); i++ {
valBig := br.Next()
if valBig != uint32(value[i]) {
t.Errorf("%d) Next 8 bits of %d yielded %d, expected %d", i, value, valBig, value[i])
}
}
}
func TestBitReaderLittleEndian16Bit(t *testing.T) {
value := []byte{93, 115}
br := newBitReader(value, 16)
valLittle := br.Next()
properLittleEndian := binary.LittleEndian.Uint16(value)
if valLittle != uint32(properLittleEndian) {
t.Errorf("First 12 bits of %016b\n yielded %016b from bigendian,\n different from %016b from littleendian", value, properLittleEndian, valLittle)
}
}