-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitbuffer_test.go
54 lines (47 loc) · 987 Bytes
/
bitbuffer_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
package encode
import (
"fmt"
"strconv"
"testing"
"github.com/stretchr/testify/require"
)
func TestBitbufferBasic(t *testing.T) {
check := func(ss []string) {
size := 0
for _, s := range ss {
size += len(s)
}
w := bitBuffer{
b: make([]byte, (size+7)/8),
i: 0,
}
for _, s := range ss {
x := uint64(0)
for _, c := range s {
x <<= 1
if c == '1' {
x |= 1
}
}
w.writeBits(x, len(s))
}
r := bitBuffer{b: w.b, i: 0}
expected := ""
actual := ""
for _, s := range ss {
bits, err := r.readBits(len(s))
require.NoError(t, err)
expected += s
actual += fmt.Sprintf("%0"+strconv.Itoa(len(s))+"b", bits)
}
require.Equal(t, expected, actual)
}
check([]string{"11011011"})
check([]string{"1101101"})
check([]string{"110101"})
check([]string{"1101"})
check([]string{"1"})
check([]string{"1", "0", "1"})
check([]string{"11", "01", "10", "1"})
check([]string{"0100", "1", "110101", "1011", "00110000", "1"})
}