-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathreader_test.go
172 lines (154 loc) · 3.93 KB
/
reader_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
package bits_test
import (
"bytes"
"io"
"testing"
"github.com/Eyevinn/mp4ff/bits"
)
func TestAccErrReader(t *testing.T) {
t.Run("Read bits", func(t *testing.T) {
input := []byte{0xff, 0x0f} // 1111 1111 0000 1111
rd := bytes.NewReader(input)
reader := bits.NewReader(rd)
cases := []struct {
readNrBits int
want uint
nrBytesRead int
nrBitsRead int
}{
{2, 3, 1, 2}, // 11
{3, 7, 1, 5}, // 111
{5, 28, 2, 10}, // 11100
{3, 1, 2, 13}, // 001
{3, 7, 2, 16}, // 111
}
for _, tc := range cases {
got := reader.Read(tc.readNrBits)
if got != tc.want {
t.Errorf("Read(%d)=%b, want=%b", tc.readNrBits, got, tc.want)
}
if reader.NrBytesRead() != tc.nrBytesRead {
t.Errorf("NrBytesRead()=%d, want=%d", reader.NrBytesRead(), tc.nrBytesRead)
}
if reader.NrBitsRead() != tc.nrBitsRead {
t.Errorf("NrBitsRead()=%d, want=%d", reader.NrBitsRead(), tc.nrBitsRead)
}
}
err := reader.AccError()
if err != nil {
t.Errorf("Got accumulated error: %s", err.Error())
}
})
t.Run("Read signed bits", func(t *testing.T) {
input := []byte{0xff, 0x0f} // 1111 1111 0000 1111
rd := bytes.NewReader(input)
reader := bits.NewReader(rd)
cases := []struct {
n int
want uint
}{
{2, 3}, // 11
{3, 7}, // 111
{5, 28}, // 11100
{3, 1}, // 001
{3, 7}, // 111
}
for _, tc := range cases {
got := reader.Read(tc.n)
if got != tc.want {
t.Errorf("Read(%d)=%b, want=%b", tc.n, got, tc.want)
}
}
err := reader.AccError()
if err != nil {
t.Errorf("Got accumulated error: %s", err.Error())
}
})
t.Run("Read remaining bytes", func(t *testing.T) {
input := []byte{0xef, 0x0f} // 1110 1111 0000 1111
rd := bytes.NewReader(input)
r := bits.NewReader(rd)
gotRemaining := r.ReadRemainingBytes()
if !bytes.Equal(gotRemaining, input) {
t.Errorf("ReadRemainingBytes()=%b, want=%b", gotRemaining, input)
}
// Next check with non-bytealigned status
rd = bytes.NewReader(input)
r = bits.NewReader(rd)
_ = r.ReadFlag()
_ = r.ReadRemainingBytes()
err := r.AccError()
if err == nil {
t.Errorf("Expected error due to not byte-aligned, but got nil")
}
})
t.Run("Read flags", func(t *testing.T) {
input := []byte{0xe5} // 1110 0101
rd := bytes.NewReader(input)
r := bits.NewReader(rd)
for i := 0; i < 8; i++ {
expectedValue := (input[0] >> (7 - i) & 1) > 0
gotFlag := r.ReadFlag()
if gotFlag != expectedValue {
t.Errorf("Read flag %t, but wanted %t, case %d", gotFlag, expectedValue, i)
}
}
_ = r.ReadFlag()
wantedError := io.EOF
if err := r.AccError(); err != wantedError {
t.Errorf("wanted error %s, got %s", wantedError, err.Error())
}
})
}
func TestAccErrReaderSigned(t *testing.T) {
input := []byte{0xff, 0x0c} // 1111 1111 0000 1100
rd := bytes.NewReader(input)
reader := bits.NewReader(rd)
cases := []struct {
readNrBits int
want int
}{
{2, -1}, // 11
{3, -1}, // 111
{5, -4}, // 11100
{3, 1}, // 001
{3, -4}, // 100
}
for _, tc := range cases {
got := reader.ReadSigned(tc.readNrBits)
if got != tc.want {
t.Errorf("Read(%d)=%b, want=%b", tc.readNrBits, got, tc.want)
}
}
err := reader.AccError()
if err != nil {
t.Errorf("Got accumulated error: %s", err.Error())
}
}
func TestBadAccErrReader(t *testing.T) {
// Check that reading beyond EOF provides value = 0 after acc error
input := []byte{0xff, 0x0f} // 1111 1111 0000 1111
rd := bytes.NewReader(input)
reader := bits.NewReader(rd)
cases := []struct {
err error
n int
want uint
}{
{nil, 2, 3}, // 11
{nil, 3, 7}, // 111
{io.EOF, 12, 0}, // 0 because of error
{io.EOF, 3, 0}, // 0 because of acc error
{io.EOF, 3, 0}, // 0 because of acc error
}
for _, tc := range cases {
got := reader.Read(tc.n)
if got != tc.want {
t.Errorf("Read(%d)=%b, want=%b", tc.n, got, tc.want)
}
}
err := reader.AccError()
if err != io.EOF {
t.Errorf("Wanted io.EOF but got %v", err)
}
}