-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathreader_test.go
116 lines (105 loc) · 2.02 KB
/
reader_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
package wave
import (
"runtime/debug"
"testing"
)
var (
scaleFrameTests = []struct {
input int
bits int
out Frame
}{
{
0,
16,
0,
},
{
127,
8,
1,
},
{
-127,
8,
-1,
},
{
32_767,
16,
1,
},
{
32_767,
16,
1,
},
{
0,
16,
0,
},
{
-32_767,
16,
-1,
},
}
// readFileTest to iterate over various files and make sure the output meets
// the expected Wave struct.
readFileTests = []struct {
file string
expected Wave
}{}
)
// TestReadFile reads a golden file and ensures that is parsed as expected
func TestReadFile(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Fatalf("Should not have panic'd!\n%v", r)
}
}()
goldenfile := "./golden/maybe-next-time.wav"
wav, err := ReadWaveFile(goldenfile)
if err != nil {
t.Fatalf("Should be able to read wave file: %v", err)
}
// assert that the wav file looks as expected.
if wav.SampleRate != 44100 {
t.Fatalf("Expected SR 44100, got: %v", wav.SampleRate)
}
if wav.NumChannels != 2 {
t.Fatalf("Expected 2 channels, got: %v", wav.NumChannels)
}
}
func TestScaleFrames(t *testing.T) {
for _, test := range scaleFrameTests {
t.Run("", func(t *testing.T) {
res := scaleFrame(test.input, test.bits)
if res != test.out {
t.Fatalf("expected %v, got %v", test.out, res)
}
})
}
}
// TestJunkChunkFile tests that .wave files with a JUNK chunk can be read
// https://www.daubnet.com/en/file-format-riff
func TestJunkChunkFile(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Fatalf("Should not have panic'd reading JUNK files\n%v", string(debug.Stack()))
}
}()
goldenfile := "./golden/chunk_junk.wav"
wav, err := ReadWaveFile(goldenfile)
if err != nil {
t.Fatalf("should be able to read wave file: %v", err)
}
// assert that the wav file looks as expected.
if wav.SampleRate != 48000 {
t.Fatalf("Expected SR 48000, got: %v", wav.SampleRate)
}
if wav.NumChannels != 2 {
t.Fatalf("Expected 2 channels, got: %v", wav.NumChannels)
}
}