forked from zikichombo/sound
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipe_test.go
65 lines (61 loc) · 1.29 KB
/
pipe_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
// Copyright 2018 The ZikiChombo Authors. All rights reserved. Use of this source
// code is governed by a license that can be found in the License file.
package sound
import (
"io"
"testing"
)
func TestPipe(t *testing.T) {
mono := MonoCd()
_ = mono
testPipe(t, mono, 1024, 1024)
testPipe(t, mono, 256, 1024)
testPipe(t, mono, 1024, 1)
stereo := StereoCd()
testPipe(t, stereo, 1024, 1024)
testPipe(t, stereo, 3, 4)
testPipe(t, stereo, 1024, 1)
}
func testPipe(t *testing.T, valve Form, n, m int) {
nC := valve.Channels()
r, w := Pipe(valve)
wb := make([]float64, n*nC)
rb := make([]float64, m*nC)
for i := range wb {
wb[i] = float64(i / n)
}
go func() {
for i := 0; i < 128; i++ {
if err := w.Send(wb); err != nil {
t.Fatal(err)
}
}
w.Close()
}()
ttl := 0
for ttl < n*128 {
n, e := r.Receive(rb)
if n != len(rb)/nC && e == nil {
t.Fatal("nothing read, no error")
}
if e == io.EOF {
if n != 0 {
t.Errorf("read %d with eof\n", n)
}
break
}
if n != len(rb)/nC {
t.Errorf("read %d not %d\n", n, len(rb)/valve.Channels())
}
rEnd := n * nC
for i := range rb[:rEnd] {
if int(rb[i]) != i/n {
t.Errorf("at %d got %d not %d\n", i, int(rb[i]), i/n)
}
}
ttl += n
}
if ttl != n*128 {
t.Errorf("ttl got %d not %d\n", ttl, n*128)
}
}