forked from faiface/beep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compositors_test.go
179 lines (149 loc) · 3.33 KB
/
compositors_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
package beep_test
import (
"math/rand"
"reflect"
"testing"
"github.com/faiface/beep"
)
// randomDataStreamer generates random samples of duration d and returns a StreamSeeker which streams
// them and the data itself.
func randomDataStreamer(numSamples int) (s beep.StreamSeeker, data [][2]float64) {
data = make([][2]float64, numSamples)
for i := range data {
data[i][0] = rand.Float64()*2 - 1
data[i][1] = rand.Float64()*2 - 1
}
return &dataStreamer{data, 0}, data
}
type dataStreamer struct {
data [][2]float64
pos int
}
func (ds *dataStreamer) Stream(samples [][2]float64) (n int, ok bool) {
if ds.pos >= len(ds.data) {
return 0, false
}
n = copy(samples, ds.data[ds.pos:])
ds.pos += n
return n, true
}
func (ds *dataStreamer) Err() error {
return nil
}
func (ds *dataStreamer) Len() int {
return len(ds.data)
}
func (ds *dataStreamer) Position() int {
return ds.pos
}
func (ds *dataStreamer) Seek(p int) error {
ds.pos = p
return nil
}
// collect drains Streamer s and returns all of the samples it streamed.
func collect(s beep.Streamer) [][2]float64 {
var (
result [][2]float64
buf [479][2]float64
)
for {
n, ok := s.Stream(buf[:])
if !ok {
return result
}
result = append(result, buf[:n]...)
}
}
func TestTake(t *testing.T) {
for i := 0; i < 7; i++ {
total := rand.Intn(1e5) + 1e4
s, data := randomDataStreamer(total)
take := rand.Intn(total)
want := data[:take]
got := collect(beep.Take(take, s))
if !reflect.DeepEqual(want, got) {
t.Error("Take not working correctly")
}
}
}
func TestLoop(t *testing.T) {
for i := 0; i < 7; i++ {
for n := 0; n < 5; n++ {
s, data := randomDataStreamer(10)
var want [][2]float64
for j := 0; j < n; j++ {
want = append(want, data...)
}
got := collect(beep.Loop(n, s))
if !reflect.DeepEqual(want, got) {
t.Error("Loop not working correctly")
}
}
}
}
func TestSeq(t *testing.T) {
var (
n = 7
s = make([]beep.Streamer, n)
data = make([][][2]float64, n)
)
for i := range s {
s[i], data[i] = randomDataStreamer(rand.Intn(1e5) + 1e4)
}
var want [][2]float64
for _, d := range data {
want = append(want, d...)
}
got := collect(beep.Seq(s...))
if !reflect.DeepEqual(want, got) {
t.Errorf("Seq not working properly")
}
}
func TestMix(t *testing.T) {
var (
n = 7
s = make([]beep.Streamer, n)
data = make([][][2]float64, n)
)
for i := range s {
s[i], data[i] = randomDataStreamer(rand.Intn(1e5) + 1e4)
}
maxLen := 0
for _, d := range data {
if len(d) > maxLen {
maxLen = len(d)
}
}
want := make([][2]float64, maxLen)
for _, d := range data {
for i := range d {
want[i][0] += d[i][0]
want[i][1] += d[i][1]
}
}
got := collect(beep.Mix(s...))
if !reflect.DeepEqual(want, got) {
t.Error("Mix not working correctly")
}
}
func TestDup(t *testing.T) {
for i := 0; i < 7; i++ {
s, data := randomDataStreamer(rand.Intn(1e5) + 1e4)
st, su := beep.Dup(s)
var tData, uData [][2]float64
for {
buf := make([][2]float64, rand.Intn(1e4))
tn, tok := st.Stream(buf)
tData = append(tData, buf[:tn]...)
buf = make([][2]float64, rand.Intn(1e4))
un, uok := su.Stream(buf)
uData = append(uData, buf[:un]...)
if !tok && !uok {
break
}
}
if !reflect.DeepEqual(data, tData) || !reflect.DeepEqual(data, uData) {
t.Error("Dup not working correctly")
}
}
}