-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrack_test.go
474 lines (452 loc) · 9.8 KB
/
track_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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
package track_test
import (
"testing"
"github.com/faiface/beep"
track "github.com/zephyrtronium/beep-track"
)
type someandstop struct {
l, r float64
n int
closed bool
}
func (s *someandstop) Stream(samples [][2]float64) (int, bool) {
if s.n <= 0 || s.closed {
return 0, false
}
if s.n > len(samples) {
for i := range samples {
samples[i] = [2]float64{s.l, s.r}
}
s.n -= len(samples)
return len(samples), true
}
n := s.n
for i := 0; i < n; i++ {
samples[i] = [2]float64{s.l, s.r}
}
s.n = 0
return n, true
}
func (s *someandstop) Err() error {
return nil
}
func (s *someandstop) Close() error {
s.closed = true
return nil
}
var _ beep.StreamCloser = (*someandstop)(nil)
func TestStreamDefaults(t *testing.T) {
s := track.New(nil, nil)
r := make([][2]float64, 64)
n, ok := s.Stream(r)
if n != len(r) {
t.Errorf("wrong number of samples: want %d, got %d", len(r), n)
}
if !ok {
t.Errorf("stream read not ok")
}
for i, v := range r {
if v != [2]float64{} {
t.Errorf("wrong sample at %d: want [0 0], got %f", i, v)
}
}
}
func TestStreamZero(t *testing.T) {
var s track.Track
r := make([][2]float64, 64)
n, ok := s.Stream(r)
if n != len(r) {
t.Errorf("wrong number of samples: want %d, got %d", len(r), n)
}
if !ok {
t.Errorf("stream read not ok")
}
for i, v := range r {
if v != [2]float64{} {
t.Errorf("wrong sample at %d: want [0 0], got %f", i, v)
}
}
}
func TestStreamStart(t *testing.T) {
a := &someandstop{l: 1, r: 1, n: 1}
s := track.New(&someandstop{l: -1, r: -1, n: 1 << 30}, a)
r := make([][2]float64, 64)
n, ok := s.Stream(r)
if n != len(r) {
t.Errorf("wrong number of samples: want %d, got %d", len(r), n)
}
if !ok {
t.Errorf("stream read not ok")
}
if r[0] != [2]float64{1, 1} {
t.Errorf("wrong first sample: want [1 1], got %f", r[0])
}
for i := 1; i < len(r); i++ {
if r[i] != [2]float64{-1, -1} {
t.Errorf("wrong non-first sample at %d: want [-1 -1], got %f", i, r[i])
}
}
if !a.closed {
t.Errorf("active streamer not closed")
}
}
func TestStreamSetStart(t *testing.T) {
a := &someandstop{l: 1, r: 1, n: 1}
s := track.New(&someandstop{l: -1, r: -1, n: 1 << 30}, nil)
s.Set(a)
r := make([][2]float64, 64)
n, ok := s.Stream(r)
if n != len(r) {
t.Errorf("wrong number of samples: want %d, got %d", len(r), n)
}
if !ok {
t.Errorf("stream read not ok")
}
if r[0] != [2]float64{1, 1} {
t.Errorf("wrong first sample: want [1 1], got %f", r[0])
}
for i := 1; i < len(r); i++ {
if r[i] != [2]float64{-1, -1} {
t.Errorf("wrong non-first sample at %d: want [-1 -1], got %f", i, r[i])
}
}
if !a.closed {
t.Errorf("active streamer not closed")
}
}
func TestStreamSet(t *testing.T) {
a := &someandstop{l: 1, r: 1, n: 1}
s := track.New(&someandstop{l: -1, r: -1, n: 1 << 30}, nil)
r := make([][2]float64, 64)
n, ok := s.Stream(r)
if n != len(r) {
t.Errorf("wrong number of samples before set: want %d, got %d", len(r), n)
}
if !ok {
t.Errorf("stream read before set not ok")
}
for i := 0; i < len(r); i++ {
if r[i] != [2]float64{-1, -1} {
t.Errorf("wrong sample before set at %d: want [-1 -1], got %f", i, r[i])
}
}
s.Set(a)
n, ok = s.Stream(r)
if n != len(r) {
t.Errorf("wrong number of samples after set: want %d, got %d", len(r), n)
}
if !ok {
t.Errorf("stream read after set not ok")
}
if r[0] != [2]float64{1, 1} {
t.Errorf("wrong first sample after set: want [1 1], got %f", r[0])
}
for i := 1; i < len(r); i++ {
if r[i] != [2]float64{-1, -1} {
t.Errorf("wrong non-first sample after set at %d: want [-1 -1], got %f", i, r[i])
}
}
if !a.closed {
t.Errorf("active streamer not closed")
}
}
func TestStreamSetZero(t *testing.T) {
a := &someandstop{l: 1, r: 1, n: 1}
var s track.Track
r := make([][2]float64, 64)
s.Set(a)
n, ok := s.Stream(r)
if n != len(r) {
t.Errorf("wrong number of samples: want %d, got %d", len(r), n)
}
if !ok {
t.Errorf("stream read not ok")
}
if r[0] != [2]float64{1, 1} {
t.Errorf("wrong first sample: want [1 1], got %f", r[0])
}
for i := 1; i < len(r); i++ {
if r[i] != [2]float64{} {
t.Errorf("wrong non-first sample at %d: want [-1 -1], got %f", i, r[i])
}
}
if !a.closed {
t.Errorf("active streamer not closed")
}
}
func TestStreamSetConcurrent(t *testing.T) {
s := track.New(nil, nil)
for i := 0; i < 1000; i++ {
a := &someandstop{l: 1, r: 0, n: 4095}
b := &someandstop{l: 0, r: 1, n: 4095}
ch := make(chan string)
go func() {
r := make([][2]float64, 512)
var havea, donea, haveb, doneb bool
// signal to start setting
ch <- ""
for {
n, _ := s.Stream(r)
if n != len(r) {
ch <- "wrong number of samples"
return
}
for _, v := range r {
switch v {
case [2]float64{}:
if havea {
donea = true
}
if haveb {
doneb = true
}
case [2]float64{1, 0}:
if donea {
ch <- "got samples from a twice"
return
}
if haveb && !doneb {
doneb = true
}
havea = true
case [2]float64{0, 1}:
if doneb {
ch <- "got samples from b twice"
return
}
if havea && !donea {
donea = true
}
haveb = true
default:
ch <- "got weird sample"
return
}
}
if donea && doneb {
close(ch)
return
}
}
}()
<-ch
// TODO: these don't quit if the test fails
go s.Set(a)
go s.Set(b)
if m, ok := <-ch; ok {
t.Error(m)
}
}
}
func TestStreamSetNil(t *testing.T) {
s := track.New(nil, nil)
s.Set(nil)
s.Set(beep.Silence(1))
r := make([][2]float64, 64)
n, ok := s.Stream(r)
if n != len(r) {
t.Errorf("wrong number of samples: expected %d, got %d", len(r), n)
}
if !ok {
t.Error("stream read not ok")
}
s.Set(nil)
}
func TestCloseSilence(t *testing.T) {
s := track.New(nil, nil)
r := make([][2]float64, 64)
if err := s.Close(); err != nil {
t.Error("error from close:", err)
}
n, ok := s.Stream(r)
if n > 0 {
t.Errorf("expected 0, got %d samples after closing: %v", n, r[:n])
}
if ok {
t.Error("unexpected successful stream")
}
}
func TestCloseActive(t *testing.T) {
a := &someandstop{l: 1, r: 1, n: 1 << 30}
s := track.New(nil, a)
r := make([][2]float64, 64)
if err := s.Close(); err != nil {
t.Error("error from close:", err)
}
n, ok := s.Stream(r)
if n > 0 {
t.Errorf("expected 0, got %d samples after closing: %v", n, r[:n])
}
if ok {
t.Error("unexpected successful stream")
}
}
func TestSetClose(t *testing.T) {
defer func() {
if err := recover(); err == nil {
t.Error("missing panic setting active streamer on closed track")
}
}()
s := track.New(nil, beep.Silence(1))
if err := s.Close(); err != nil {
t.Error("error from close:", err)
}
s.Set(beep.Silence(1))
}
func TestSilenceFails(t *testing.T) {
s := track.New(beep.Silence(1), nil)
if err := s.Err(); err != nil {
t.Errorf("unexpected error %v before streaming samples", err)
}
r := make([][2]float64, 64)
n, ok := s.Stream(r)
if n != 1 {
t.Errorf("wrong number of samples: expected 1, got %d", n)
}
if !ok {
t.Error("expected stream to succeed")
}
if s.Err() == nil {
t.Error("expected error after first incomplete stream")
}
n, ok = s.Stream(r)
if n != 0 {
t.Errorf("wrong number of samples: expected 0, got %d", n)
}
if ok {
t.Error("expected stream to fail")
}
if s.Err() == nil {
t.Error("expected error")
}
}
func TestInterrupt(t *testing.T) {
a := &someandstop{l: 1, r: 1, n: 1}
s := track.New(nil, a)
if err := s.Interrupt(); err != nil {
t.Error("unexpected error from interrupt:", err)
}
if !a.closed {
t.Error("active streamer not closed by interrupt")
}
r := make([][2]float64, 64)
n, ok := s.Stream(r)
if !ok {
t.Error("expected stream to succeed")
}
if n != len(r) {
t.Errorf("wrong number of samples: expected %d, got %d", len(r), n)
}
for i, v := range r {
if v != [2]float64{} {
t.Errorf("wrong sample value at %d: expected [0 0], got %f", i, v)
}
}
}
func TestInterruptSilent(t *testing.T) {
s := track.New(nil, nil)
if err := s.Interrupt(); err != nil {
t.Error("unexpected error form interrupt:", err)
}
r := make([][2]float64, 64)
n, ok := s.Stream(r)
if !ok {
t.Error("expected stream to succeed")
}
if n != len(r) {
t.Errorf("wrong number of samples: expected %d, got %d", len(r), n)
}
for i, v := range r {
if v != [2]float64{} {
t.Errorf("wrong sample value at %d: expected [0 0], got %f", i, v)
}
}
}
func TestInterruptConcurrent(t *testing.T) {
s := track.New(nil, nil)
for i := 0; i < 1000; i++ {
a := &someandstop{l: 1, r: 0, n: 1 << 30}
b := &someandstop{l: 0, r: 1, n: 1 << 30}
ch := make(chan string)
intr := make(chan bool)
go func() {
r := make([][2]float64, 64)
var havea, haveb, donea, doneb bool
// signal to start interrupting
ch <- ""
for {
n, _ := s.Stream(r)
if n != len(r) {
ch <- "wrong number of samples"
return
}
for _, v := range r {
switch v {
case [2]float64{}:
if havea {
donea = true
}
if haveb {
doneb = true
}
case [2]float64{1, 0}:
if donea {
ch <- "got samples from a twice"
return
}
if haveb && !doneb {
doneb = true
}
if !havea {
intr <- true
}
havea = true
case [2]float64{0, 1}:
if doneb {
ch <- "got samples from b twice"
return
}
if havea && !donea {
donea = true
}
if !haveb {
intr <- true
}
haveb = true
default:
ch <- "got weird sample"
return
}
}
if donea && doneb {
close(ch)
return
}
}
}()
<-ch
w := make(chan bool, 1)
go func() {
<-w
s.Set(a)
}()
go func() {
<-w
s.Set(b)
}()
go func() {
<-intr
s.Interrupt()
w <- true
}()
go func() {
<-intr
s.Interrupt()
w <- true
}()
w <- true
if m, ok := <-ch; ok {
t.Fatal(m)
}
}
}