This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
bit_reader_test.go
99 lines (94 loc) · 1.89 KB
/
bit_reader_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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package gorilla
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_bitReader_readBit(t *testing.T) {
var b byte = 0x1
for i := 0; i < 256; i++ {
buf := new(bytes.Buffer)
require.Nil(t, buf.WriteByte(b))
br := newBitReader(buf)
actual, err := br.readBit()
require.Nil(t, err)
assert.Equal(t, bit((b&0x80) != 0), actual)
b++
}
}
func Test_bitReader_readBits(t *testing.T) {
tests := []struct {
name string
nbits int
byteToRead byte
want uint64
wantErr error
}{
{
name: "read a bit from 00000001",
nbits: 1,
byteToRead: 0x1,
want: 0,
wantErr: nil,
},
{
name: "read 5 bits from 00000001",
nbits: 5,
byteToRead: 0x1,
want: 0,
wantErr: nil,
},
{
name: "read 8 bits from 00000001",
nbits: 8,
byteToRead: 0x1,
want: 0x1,
wantErr: nil,
},
{
name: "read a bit from 11111111",
nbits: 1,
byteToRead: 0xff,
want: 0x1,
wantErr: nil,
},
{
name: "read 5 bits from 11111111",
nbits: 5,
byteToRead: 0xff,
want: 0x1f,
wantErr: nil,
},
{
name: "read 8 bits from 11111111",
nbits: 8,
byteToRead: 0xff,
want: 0xff,
wantErr: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
buf := new(bytes.Buffer)
err := buf.WriteByte(tt.byteToRead)
require.Nil(t, err)
b := newBitReader(buf)
got, err := b.readBits(tt.nbits)
assert.Equal(t, tt.wantErr, err)
assert.Equal(t, tt.want, got)
})
}
}
func Test_bitReader_readByte(t *testing.T) {
var b byte = 0x1
for i := 0; i < 256; i++ {
buf := new(bytes.Buffer)
require.Nil(t, buf.WriteByte(b))
br := newBitReader(buf)
byt, err := br.readByte()
require.Nil(t, err)
assert.Equal(t, b, byt)
b++
}
}