-
Notifications
You must be signed in to change notification settings - Fork 0
/
ring_test.go
219 lines (203 loc) · 5.86 KB
/
ring_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
219
/*
Cyclic buffer for fragmented mp4 stream in order to save memory when pre-recording an event
*/
package mp4ring
import (
"container/ring"
"reflect"
"testing"
)
// examples of headers
const (
ft = ` ftypisom isomiso2avc1mp41`
mv = ` moov lmvhd _� @ �trak \tkhd @ p mdia mdhd _� U� -hdlr vide VideoHandler *minf vmhd $dinf dref url �stbl �stsd �avc1 p H H �� 8avcC � (�� g� (�ٲ�� O��-@@@P (� ' h��D� stts stsc stsz stco (mvex trex `
r = ` lmoof mfhd 4 Ttraf tfhd 8 � � tfdt �| trun t � �`
)
func Test_getHeaderBoxInfo(t *testing.T) {
type args struct {
data []byte
}
tests := []struct {
name string
args args
wantBoxHeader boxHeader
wantErr bool
}{
{name: "ftyp", args: args{[]byte(ft + mv + r)}, wantBoxHeader: boxHeader{Size: 538976288, FourccType: [4]byte{102, 116, 121, 112}, Size64: 7598539510785253408}, wantErr: false},
{name: "moov", args: args{[]byte(mv + ft + r)}, wantBoxHeader: boxHeader{Size: 538976288, FourccType: [4]byte{109, 111, 111, 118}, Size64: 2314885858533468260}, wantErr: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotBoxHeader, err := getHeaderBoxInfo(tt.args.data)
if (err != nil) != tt.wantErr {
t.Errorf("getHeaderBoxInfo() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotBoxHeader, tt.wantBoxHeader) {
t.Errorf("getHeaderBoxInfo() = %v, want %v", gotBoxHeader, tt.wantBoxHeader)
}
})
}
}
func TestNew(t *testing.T) {
var (
b *Buffer
err error
)
b, err = New(10)
if b == nil || err != nil {
t.Fatal(`failed to create ring buffer`)
}
b, err = New(-12) //nolint:ineffassign,staticcheck
if err == nil {
t.Fatal(`failed check size buffer`)
}
}
func TestBuffer_Close(t *testing.T) {
tests := []struct {
name string
buf *Buffer
wantErr bool
}{
{name: "ok", buf: &Buffer{ftyp: []byte(ft), moov: []byte(mv), r: ring.New(1), size: 0, isClosed: false}, wantErr: false},
{name: "nil", buf: nil, wantErr: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.buf.Close(); (err != nil) != tt.wantErr {
t.Errorf("Buffer.Close() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestBuffer_Write(t *testing.T) {
type fields struct {
ftyp []byte
moov []byte
r *ring.Ring
size int64
isClosed bool
}
type args struct {
buf []byte
}
tests := []struct {
name string
fields fields
args args
want int
wantErr bool
}{
{name: "closed",
fields: fields{ftyp: []byte{}, moov: []byte{}, r: ring.New(1), size: 0, isClosed: true},
args: args{[]byte(r)},
want: 0,
wantErr: true,
},
{name: "ftyp",
fields: fields{ftyp: []byte{}, moov: []byte{}, r: ring.New(1), size: 0, isClosed: false},
args: args{[]byte(ft)},
want: 32,
wantErr: false,
},
{name: "moov",
fields: fields{ftyp: []byte{}, moov: []byte{}, r: ring.New(1), size: 0, isClosed: false},
args: args{[]byte(mv)},
want: 682,
wantErr: false,
},
{name: "other",
fields: fields{ftyp: []byte{}, moov: []byte{}, r: ring.New(1), size: 0, isClosed: false},
args: args{[]byte(r)},
want: 118,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b := &Buffer{
ftyp: tt.fields.ftyp,
moov: tt.fields.moov,
r: tt.fields.r,
size: tt.fields.size,
isClosed: tt.fields.isClosed,
}
got, err := b.Write(tt.args.buf)
if (err != nil) != tt.wantErr {
t.Errorf("Buffer.Write() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("Buffer.Write() = %v, want %v", got, tt.want)
}
})
}
}
func TestBuffer_Bytes(t *testing.T) {
rr := ring.New(1)
rr.Value = []byte(r)
type fields struct {
ftyp []byte
moov []byte
ri *ring.Ring
size int64
isClosed bool
}
tests := []struct {
name string
fields fields
want []byte
}{
{name: "ok", fields: fields{ftyp: []byte(ft), moov: []byte(mv), ri: rr, size: 0, isClosed: false}, want: []byte(ft + mv + r)},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b := &Buffer{
ftyp: tt.fields.ftyp,
moov: tt.fields.moov,
r: tt.fields.ri,
size: tt.fields.size,
isClosed: tt.fields.isClosed,
}
if got := b.Bytes(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Buffer.Bytes() = %v, want %v", got, tt.want)
}
})
}
}
func TestBuffer_Size(t *testing.T) {
rr := ring.New(10)
for i := 0; i < 10; i++ {
rr.Value = []byte(r)
rr = rr.Next()
}
type fields struct {
ftyp []byte
moov []byte
ri *ring.Ring
size int64
isClosed bool
}
tests := []struct {
name string
fields fields
want string
}{
{name: "0.70", fields: fields{ftyp: []byte(ft), moov: []byte(mv), ri: ring.New(1), size: 0, isClosed: false}, want: "0.70"},
{name: "1.85", fields: fields{ftyp: []byte(ft), moov: []byte(mv), ri: rr, size: 0, isClosed: false}, want: "1.85"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b := &Buffer{
ftyp: tt.fields.ftyp,
moov: tt.fields.moov,
r: tt.fields.ri,
size: tt.fields.size,
isClosed: tt.fields.isClosed,
}
if got := b.Size(); got != tt.want {
t.Errorf("Buffer.Size() = %v, want %v", got, tt.want)
}
})
}
}