-
Notifications
You must be signed in to change notification settings - Fork 2
/
rw_test.go
169 lines (163 loc) · 3.09 KB
/
rw_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package exr
import (
"encoding/binary"
"reflect"
"testing"
)
func TestBitReader(t *testing.T) {
r := newBitReader([]byte{
0b00000000,
0b11111111,
0b00001111,
0b00110011,
0b01010101,
}, 40)
cases := []struct {
nreads []int
want [][]byte
}{
{
nreads: []int{6, 6, 6, 6, 6, 6, 4},
want: [][]byte{
[]byte{0b00000000},
[]byte{0b00111100},
[]byte{0b11110000},
[]byte{0b00111100},
[]byte{0b00110000},
[]byte{0b11010100},
[]byte{0b01010000},
},
},
{
nreads: []int{4, 8, 12, 16},
want: [][]byte{
[]byte{0b00000000},
[]byte{0b00001111},
[]byte{0b11110000, 0b11110000},
[]byte{0b00110011, 0b01010101},
},
},
{
// read more than the reader have
nreads: []int{50},
want: [][]byte{
[]byte{
0b00000000,
0b11111111,
0b00001111,
0b00110011,
0b01010101,
0b00000000,
0b00000000,
},
},
},
}
for i, c := range cases {
r.Seek(0)
got := make([][]byte, 0)
for _, n := range c.nreads {
got = append(got, r.Read(n))
}
n := r.Remain()
if n != 0 {
t.Fatalf("number of remaining bits should be 0, got %d", n)
}
if !reflect.DeepEqual(got, c.want) {
t.Fatalf("got[%d]: %v, want: %v", i, got, c.want)
}
}
}
type bitSlice struct {
n int
b []byte
}
func TestBitWriter(t *testing.T) {
cases := []struct {
bits []bitSlice
want []byte
}{
{
bits: []bitSlice{
{6, []byte{0b00000000}},
{6, []byte{0b00111100}},
{6, []byte{0b11110000}},
{6, []byte{0b00111100}},
{6, []byte{0b00110000}},
{6, []byte{0b11010100}},
{4, []byte{0b01010000}},
},
want: []byte{
0b00000000,
0b11111111,
0b00001111,
0b00110011,
0b01010101,
},
},
{
bits: []bitSlice{
{4, []byte{0b00000000}},
{8, []byte{0b00001111}},
{12, []byte{0b11110000, 0b11110000}},
{16, []byte{0b00110011, 0b01010101}},
},
want: []byte{
0b00000000,
0b11111111,
0b00001111,
0b00110011,
0b01010101,
},
},
{
bits: []bitSlice{
{40, []byte{
0b00000000,
0b11111111,
0b00001111,
0b00110011,
0b01010101,
}},
},
want: []byte{
0b00000000,
0b11111111,
0b00001111,
0b00110011,
0b01010101,
},
},
}
for i, c := range cases {
w := newBitWriter(40)
for _, b := range c.bits {
w.Write(b.n, b.b)
}
n := w.Remain()
if n != 0 {
t.Fatalf("number of remaining bits should be 0, got %d", n)
}
got := w.Data()
if !reflect.DeepEqual(got, c.want) {
t.Fatalf("got[%d] %v, want %v", i, got, c.want)
}
}
}
func TestBitByteConversionPreserveOriginal(t *testing.T) {
data := []byte{1, 0, 2, 0, 3, 0, 4, 0, 5, 0}
r := newByteReader(binary.LittleEndian, data)
_ = r.Uint16()
br := r.ToBitReader()
gotr := br.ToByteReader(r.ord)
if !reflect.DeepEqual(gotr, r) {
t.Fatalf("conversion expected to preserve original reader, it didn't")
}
w := newByteWriter(binary.LittleEndian, make([]byte, 10))
w.Uint16(1)
bw := w.ToBitWriter()
gotw := bw.ToByteWriter(w.ord)
if !reflect.DeepEqual(gotw, w) {
t.Fatalf("conversion expected to preserve original writer, it didn't")
}
}