forked from icza/s2prot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitpackedbuff_test.go
218 lines (190 loc) · 5.01 KB
/
bitpackedbuff_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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package s2prot
import (
"bytes"
"testing"
)
func TestEOFD(t *testing.T) {
bb := &bitPackedBuff{contents: []byte{}, bigEndian: true}
if !bb.EOF() {
t.Error("EOF falsely NOT reported.")
}
bb = &bitPackedBuff{contents: []byte{1, 2, 3}, bigEndian: true}
if bb.EOF() {
t.Error("EOF falsely reported.")
}
bb.readBits(1)
if bb.EOF() {
t.Error("EOF falsely reported.")
}
bb.readBits(7)
if bb.EOF() {
t.Error("EOF falsely reported.")
}
bb.readBits(1)
if bb.EOF() {
t.Error("EOF falsely reported.")
}
bb.readBits(12)
if bb.EOF() {
t.Error("EOF falsely reported.")
}
bb.readBits(3)
if !bb.EOF() {
t.Error("EOF falsely NOT reported.")
}
}
func TestByteAlign(t *testing.T) {
bb := &bitPackedBuff{contents: []byte{1, 2, 3}, bigEndian: true}
bb.byteAlign()
if bb.readBits(8) != 1 {
t.Error("Unexpected value!")
}
bb.readBits(1)
bb.byteAlign()
if bb.readBits(8) != 3 {
t.Error("Unexpected value!")
}
}
func TestReadBits1(t *testing.T) {
bb := &bitPackedBuff{contents: []byte{0xaa, 0xaa}, bigEndian: true}
for expected := false; !bb.EOF(); expected = !expected {
if bb.readBits1() != expected {
t.Error("Unexpected value!")
}
}
}
func TestReadBits8(t *testing.T) {
bb := &bitPackedBuff{contents: []byte{1, 2, 3, 4}, bigEndian: true}
if bb.readBits8() != 1 {
t.Error("Unexpected value!")
}
bb.readBits(3)
if bb.readBits8() != 3 {
t.Error("Unexpected value!")
}
if bb.readBits8() != 4 {
t.Error("Unexpected value!")
}
bb = &bitPackedBuff{contents: []byte{1, 2, 3, 4}, bigEndian: false}
if bb.readBits8() != 1 {
t.Error("Unexpected value!")
}
bb.readBits(3)
if bb.readBits8() != 0x60 {
t.Error("Unexpected value!")
}
if bb.readBits8() != 0x80 {
t.Error("Unexpected value!")
}
}
func TestReadBits(t *testing.T) {
bb := &bitPackedBuff{contents: []byte{1, 2, 3, 4}, bigEndian: true}
if bb.readBits(0) != 0 {
t.Error("Unexpected value!")
}
if bb.readBits(8) != 1 {
t.Error("Unexpected value!")
}
bb = &bitPackedBuff{contents: []byte{1, 2, 3, 4}, bigEndian: true}
if bb.readBits(3) != 1 {
t.Error("Unexpected value!")
}
if bb.readBits(13) != 2 {
t.Error("Unexpected value!")
}
if bb.readBits(1) != 1 {
t.Error("Unexpected value!")
}
if bb.readBits(15) != 0x0104 {
t.Error("Unexpected value!")
}
bb = &bitPackedBuff{contents: []byte{1, 2, 3, 4}, bigEndian: false}
if bb.readBits(3) != 1 {
t.Error("Unexpected value!")
}
if bb.readBits(13) != 0x40 {
t.Error("Unexpected value!")
}
if bb.readBits(1) != 1 {
t.Error("Unexpected value!")
}
if bb.readBits(15) != 0x0201 {
t.Error("Unexpected value!")
}
}
func TestReadBitsSpecial(t *testing.T) {
bb := &bitPackedBuff{contents: []byte{1, 2, 3, 4, 5, 6, 7, 8}, bigEndian: true}
bb.readBits(3) // Non-empty cache
if bb.readBits(8) != 2 {
t.Error("Unexpected value!")
}
// 111111111 222222222 333333333 444444444 555555555 666666666 777777777 888888888
// 0000 0001 0000 0010 0000 0011 0000 0100 0000 0101 0000 0110 0000 0111 0000 1000
// 0000 0000 0001 1100
if bb.readBits(16) != 0x001c {
t.Error("Unexpected value!")
}
// 0000 0000 0010 1000 0011 0000 0011 1000
if bb.readBits(32) != 0x00283038 {
t.Error("Unexpected value!")
}
bb = &bitPackedBuff{contents: []byte{1, 2, 3, 4, 5, 6, 7}, bigEndian: false}
// Empty cache
if bb.readBits(8) != 1 {
t.Error("Unexpected value!")
}
if bb.readBits(16) != 0x0302 {
t.Error("Unexpected value!")
}
if bb.readBits(32) != 0x07060504 {
t.Error("Unexpected value!")
}
}
func TestReadAligned(t *testing.T) {
bb := &bitPackedBuff{contents: []byte{1, 2, 3, 4, 5, 6, 7, 8}, bigEndian: true}
if !bytes.Equal([]byte{}, bb.readAligned(0)) {
t.Error("Unexpected value!")
}
if !bytes.Equal([]byte{1}, bb.readAligned(1)) {
t.Error("Unexpected value!")
}
if !bytes.Equal([]byte{2, 3}, bb.readAligned(2)) {
t.Error("Unexpected value!")
}
bb.readBits(3)
if !bytes.Equal([]byte{5, 6, 7, 8}, bb.readAligned(4)) {
t.Error("Unexpected value!")
}
}
func TestReadUnaligned(t *testing.T) {
bb := &bitPackedBuff{contents: []byte{1, 2, 3, 4, 5, 6, 7, 8}, bigEndian: true}
if !bytes.Equal([]byte{}, bb.readUnaligned(0)) {
t.Error("Unexpected value!")
}
if !bytes.Equal([]byte{1, 2}, bb.readUnaligned(2)) {
t.Error("Unexpected value!")
}
bb.readBits(3)
if !bytes.Equal([]byte{0x04, 0x05}, bb.readUnaligned(2)) {
t.Error("Unexpected value!")
}
if !bytes.Equal([]byte{0x06, 0x07, 0x00}, bb.readUnaligned(3)) {
t.Error("Unexpected value!")
}
bb = &bitPackedBuff{contents: []byte{1, 2, 3, 4, 5, 6, 7, 8}, bigEndian: false}
if !bytes.Equal([]byte{}, bb.readUnaligned(0)) {
t.Error("Unexpected value!")
}
if !bytes.Equal([]byte{1, 2}, bb.readUnaligned(2)) {
t.Error("Unexpected value!")
}
bb.readBits(3)
// 111111111 222222222 333333333 444444444 555555555 666666666 777777777 888888888
// 0000 0001 0000 0010 0000 0011 0000 0100 0000 0101 0000 0110 0000 0111 0000 1000
if !bytes.Equal([]byte{0x80, 0xa0}, bb.readUnaligned(2)) {
t.Error("Unexpected value!")
}
if !bytes.Equal([]byte{0xc0, 0xe0, 0x00}, bb.readUnaligned(3)) {
t.Error("Unexpected value!")
}
}