-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbstream_test.go
192 lines (154 loc) · 3.4 KB
/
bstream_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
package bstream
import (
"reflect"
"testing"
)
func TestWriteBit(t *testing.T) {
b := NewBStreamWriter(5)
b.WriteBit(one)
if b.stream[0] != 128 {
t.Error("first bit error")
}
b.WriteBit(one)
if b.stream[0] != 192 {
t.Error("second bit error")
}
b.WriteBit(one)
if b.stream[0] != 224 {
t.Error("third bit error")
}
}
func TestWriteOneByte(t *testing.T) {
b := NewBStreamWriter(5)
b.WriteOneByte(0xff)
if b.stream[0] != 255 {
t.Error("first byte error")
}
b.WriteOneByte(0xa0)
if b.stream[1] != 160 {
t.Error("second byte error")
}
b.WriteOneByte(0x00)
if b.stream[2] != 0 {
t.Error("third byte error")
}
if l := len(b.Bytes()); l != 3 {
t.Errorf("Expected %v bytes but got %v", 3, l)
}
}
func TestWriteCombo(t *testing.T) {
b := NewBStreamWriter(5)
b.WriteBit(one)
b.WriteOneByte(0xaa)
if b.stream[0] != 0xD5 || b.stream[1] != 0x00 {
t.Error("write bits wrong")
}
c := NewBStreamWriter(5)
c.WriteBits(0xaa, 8)
if c.stream[0] != 170 {
t.Error("write bits wrong.")
}
c.WriteBits(0x0a0a, 8)
if c.stream[1] != 0x0a {
t.Error("write bit error when too few")
}
c.WriteBits(0x0a0a, 16)
if len(c.stream) > 4 {
t.Error("write bit error when too much")
}
}
func TestReadBit(t *testing.T) {
b := NewBStreamWriter(5)
b.WriteBits(0xa0, 8)
bit, err := b.ReadBit()
if err != nil || bit == zero {
t.Error("Read first bit error")
}
bit, err = b.ReadBit()
if err != nil || bit == one {
t.Error("Read second bit error")
}
}
func TestReadByte(t *testing.T) {
b := NewBStreamWriter(5)
b.WriteBits(0xa5a5, 16)
bit, err := b.ReadBit()
if err != nil || bit == zero {
t.Error("Read first bit error")
}
byt, err := b.ReadByte()
if byt != 75 {
t.Error("Read byte error")
}
}
func TestWriteBits(t *testing.T) {
b := NewBStreamWriter(24)
b.WriteBits(0xa5a5, 16)
b.WriteBits(0x07, 3)
ret, err := b.ReadBits(12)
if err != nil || ret != 2650 {
t.Error("ReadBits error")
}
ret, err = b.ReadBits(4)
if err != nil || ret != 5 {
t.Error("ReadBits second error")
}
ret, err = b.ReadBits(3)
if err != nil || ret != 7 {
t.Error("ReadBits third error")
}
}
func TestDualMode(t *testing.T) {
b := NewBStreamWriter(1)
b.WriteBit(one)
b.WriteBit(zero)
b.WriteBit(one)
if bit, err := b.ReadBit(); err != nil || bit == zero {
t.Error("Read first bit error")
}
b.WriteBit(one)
if bit, err := b.ReadBit(); err != nil || bit == one {
t.Error("Read second bit error")
}
if bit, err := b.ReadBit(); err != nil || bit == zero {
t.Error("Read third bit error")
}
b.WriteBit(one)
if bit, err := b.ReadBit(); err != nil || bit == zero {
t.Error("Read fourth bit error")
}
if bit, err := b.ReadBit(); err != nil || bit == zero {
t.Error("Read fifth bit error")
}
if len(b.stream) != 1 || b.stream[0] != 0xB8 {
t.Error("Wrong stream result")
}
}
func TestPreserveInput(t *testing.T) {
var data = []byte{0xAA, 0xAA}
input := make([]byte, len(data))
copy(input, data)
bs := NewBStreamReader(input)
bs.ReadBits(1)
bs.ReadByte()
if !reflect.DeepEqual(data, input) {
t.Error("Expected input to remain same after reading")
}
}
func BenchmarkWriteBits(b *testing.B) {
bb := NewBStreamWriter(255)
b.ResetTimer()
for i := 0; i < b.N; i++ {
bb.WriteBits(uint64(i), 8)
}
}
func BenchmarkReadBits(b *testing.B) {
bb := NewBStreamWriter(255)
for i := 0; i < b.N; i++ {
bb.WriteBits(uint64(i), 8)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
bb.ReadBits(2)
}
}