This repository was archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbit_writer_test.go
86 lines (77 loc) · 1.53 KB
/
bit_writer_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
package gorilla
import (
"bytes"
"encoding/binary"
"testing"
fuzz "github.com/google/gofuzz"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_bitWriter_writeBit(t *testing.T) {
tests := []struct {
name string
binary string
hex uint8
}{
{
name: "write 1",
binary: "00000001",
hex: 0x1,
},
{
name: "write 8",
binary: "00001000",
hex: 0x8,
},
{
name: "write 113",
binary: "01110001",
hex: 0x71,
},
{
name: "write 255",
binary: "11111111",
hex: 0xff,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
buf := new(bytes.Buffer)
bw := newBitWriter(buf)
for i := 0; i < len(tt.binary); i++ {
var err error
if tt.binary[i] == '1' {
err = bw.writeBit(one)
} else {
err = bw.writeBit(zero)
}
require.Nil(t, err)
}
assert.Equal(t, tt.hex, buf.Bytes()[0])
})
}
}
func Test_bitWriter_writeBits(t *testing.T) {
f := fuzz.New().NilChance(0)
for i := 0; i < 10; i++ {
var u64 uint64
f.Fuzz(&u64)
buf := new(bytes.Buffer)
bw := newBitWriter(buf)
require.Nil(t, bw.writeBits(u64, 64))
wantBytes := make([]byte, 8)
binary.BigEndian.PutUint64(wantBytes, u64)
assert.Equal(t, wantBytes, buf.Bytes())
}
}
func Test_bitWriter_writeByte(t *testing.T) {
var b byte = 0x1
for i := 0; i < 256; i++ {
buf := new(bytes.Buffer)
require.Nil(t, buf.WriteByte(b))
bw := newBitWriter(buf)
require.Nil(t, bw.writeByte(b))
assert.Equal(t, b, buf.Bytes()[0])
b++
}
}