Debian Codegraph @

buffer_test.go

raw
1 // Package v4l, a facade to the Video4Linux video capture interface
2 // Copyright (C) 2016 Zoltán Korándi <[email protected]>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 //go:build linux
18 // +build linux
19
20 package v4l
21
22 import (
23 "io"
24 "math/rand"
25 "testing"
26 )
27
28 const N = 100
29
30 func TestBuffer_Size(t *testing.T) {
31 buf := initBufferTest()
32 for i := 0; i < N+10; i++ {
33 size := buf.Size()
34 if size != N {
35 t.Errorf("got: %d, expected: %d (i=%d)\n", size, N, i)
36 return
37 }
38 buf.ReadByte()
39 }
40 }
41
42 func TestBuffer_Len(t *testing.T) {
43 buf := initBufferTest()
44 for i := 0; i < N+10; i++ {
45 length := buf.Len()
46 n := N - i
47 if n < 0 {
48 n = 0
49 }
50 if length != n {
51 t.Errorf("got: %d, expected: %d (i=%d)\n", length, n, i)
52 return
53 }
54 buf.ReadByte()
55 }
56 }
57
58 func TestBuffer_Read(t *testing.T) {
59 var (
60 buf = initBufferTest()
61 arr = make([]byte, 2*N)
62 err error
63 n, m int
64 )
65 for err == nil {
66 n, err = buf.Read(arr[m : m+15])
67 m += n
68 }
69 if err != io.EOF {
70 t.Errorf("expected EOF, got: %v (m=%d)\n", err, m)
71 return
72 }
73 if m != N {
74 t.Errorf("wrong byte count: %d (expected: %d)\n", m, N)
75 return
76 }
77 for i := 0; i < N; i++ {
78 if arr[i] != byte(i) {
79 t.Errorf("got: %d, expected: %d (i=%d)\n", arr[i], byte(i), i)
80 return
81 }
82 }
83 }
84
85 func TestBuffer_ReadAt(t *testing.T) {
86 buf := initBufferTest()
87 arr := make([]byte, N+10)
88 for start := -10; start < N+10; start++ {
89 for length := 0; length < N+10; length++ {
90 n, err := buf.ReadAt(arr[:length], int64(start))
91 if start < 0 {
92 if err == nil || n != 0 {
93 t.Errorf("read at %d should have failed (length=%d, n=%d)\n",
94 start, length, n)
95 return
96 }
97 continue
98 }
99 if n > length {
100 t.Errorf("read too many bytes (start=%d, length=%d, n=%d)\n",
101 start, length, n)
102 return
103 }
104 if n < length && err == nil {
105 t.Errorf("short read with nil error (start=%d, length=%d, n=%d)\n",
106 start, length, n)
107 return
108 }
109 if start+length <= N && n < length {
110 t.Errorf("short read (start=%d, length=%d, n=%d)\n",
111 start, length, n)
112 return
113 }
114 m := N - start
115 if m < 0 {
116 m = 0
117 }
118 if start+length > N && n != m {
119 t.Errorf("read %d bytes instead of %d (start=%d, length=%d)\n",
120 n, m, start, length)
121 return
122 }
123 if start+length > N && err != io.EOF {
124 t.Errorf("expected EOF, got: %v (start=%d, length=%d)\n",
125 err, start, length)
126 return
127 }
128 for i := 0; i < n; i++ {
129 if arr[i] != byte(start+i) {
130 t.Errorf("expected: %d, got: %d (start=%d, length=%d, i=%d)\n",
131 start+i, arr[i], start, length, i)
132 return
133 }
134 }
135 }
136 }
137 }
138
139 func TestBuffer_ReadByte(t *testing.T) {
140 buf := initBufferTest()
141 for i := 0; i < N; i++ {
142 b, err := buf.ReadByte()
143 if err != nil {
144 t.Errorf("unexpected error: %v (i=%d)\n", err, i)
145 return
146 }
147 if b != byte(i) {
148 t.Errorf("got: %d, expected: %d\n", b, i)
149 return
150 }
151 }
152 if _, err := buf.ReadByte(); err != io.EOF {
153 t.Error("expected EOF, got:", err)
154 return
155 }
156 }
157
158 func TestBuffer_Seek(t *testing.T) {
159 buf := initBufferTest()
160 rand.Seed(42)
161 for _, whence := range []int{io.SeekStart, io.SeekEnd} {
162 for i := 0; i < 10*N; i++ {
163 offs := rand.Int63n(N)
164 var m int64
165 if whence == io.SeekEnd {
166 offs = -offs - 1
167 m = N
168 }
169 m += offs
170 n, err := buf.Seek(offs, whence)
171 if err != nil {
172 t.Errorf("unexpected error: %v (i=%d, offs=%d, whence=%d)\n",
173 err, i, offs, whence)
174 return
175 }
176 if n != m {
177 t.Errorf("bad offset: %d (m=%d, i=%d, offs=%d, whence=%d)\n",
178 n, m, i, offs, whence)
179 return
180 }
181 b, err := buf.ReadByte()
182 if err != nil {
183 t.Errorf("unexpected error: %v (i=%d, offs=%d, whence=%d)\n",
184 err, i, offs, whence)
185 return
186 }
187 if b != byte(m) {
188 t.Errorf("read: %d (m=%d, i=%d, offs=%d, whence=%d)\n",
189 b, m, i, offs, whence)
190 return
191 }
192 }
193 }
194 buf = initBufferTest()
195 var p int64
196 for i := 0; i < 10*N; i++ {
197 m := rand.Int63n(N)
198 offs := m - p
199 n, err := buf.Seek(offs, io.SeekCurrent)
200 if err != nil {
201 t.Errorf("unexpected error: %v (i=%d, offs=%d, whence=%d)\n",
202 err, i, offs, io.SeekCurrent)
203 return
204 }
205 if n != m {
206 t.Errorf("bad offset: %d (m=%d, i=%d, offs=%d, whence=%d)\n",
207 n, m, i, offs, io.SeekCurrent)
208 return
209 }
210 b, err := buf.ReadByte()
211 if err != nil {
212 t.Errorf("unexpected error: %v (i=%d, offs=%d, whence=%d)\n",
213 err, i, offs, io.SeekCurrent)
214 return
215 }
216 if b != byte(m) {
217 t.Errorf("read: %d (m=%d, i=%d, offs=%d, whence=%d)\n",
218 b, m, i, offs, io.SeekCurrent)
219 return
220 }
221 p = n + 1
222 }
223 _, err := buf.Seek(-1, io.SeekStart)
224 if err == nil {
225 t.Error("seek to offset -1 should have failed")
226 return
227 }
228 }
229
230 func initBufferTest() *Buffer {
231 buf := make([]byte, N)
232 for i := range buf {
233 buf[i] = byte(i)
234 }
235 d := device{
236 buffers: [][]byte{nil, nil, buf, nil},
237 bufIndex: 2,
238 nCaptures: 1,
239 }
240 return &Buffer{
241 d: &d,
242 n: 1,
243 pos: 0,
244 }
245 }