Skip to content

Commit ddf7399

Browse files
committed
tests: add piaudio unit tests
1 parent 23d4485 commit ddf7399

File tree

6 files changed

+76
-0
lines changed

6 files changed

+76
-0
lines changed

piaudio/decode_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright 2025 Jacek Olszak
2+
// This code is licensed under MIT license (see LICENSE for details)
3+
4+
package piaudio_test
5+
6+
import (
7+
_ "embed"
8+
"github.com/elgopher/pi/piaudio"
9+
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
11+
"testing"
12+
)
13+
14+
func TestDecodeRaw(t *testing.T) {
15+
t.Run("should decode raw sample", func(t *testing.T) {
16+
raw := []byte{0, 1, 255}
17+
sample := piaudio.DecodeRaw(raw, 22050)
18+
19+
assert.Equal(t, []int8{0, 1, -1}, sample.Data())
20+
assert.Equal(t, uint16(22050), sample.SampleRate())
21+
})
22+
}
23+
24+
var (
25+
//go:embed internal/test/valid.wav
26+
validWAV []byte
27+
//go:embed internal/test/invalid_16bit.wav
28+
invalid16bitWAV []byte
29+
//go:embed internal/test/invalid_stereo.wav
30+
invalidStereoWAV []byte
31+
//go:embed internal/test/invalid-sample-rate.wav
32+
invalidSampleRateWAV []byte
33+
//go:embed internal/test/invalid-not-pcm.wav
34+
invalidNotPCM []byte
35+
)
36+
37+
func TestDecodeWavOrErr(t *testing.T) {
38+
t.Run("should decode valid wav file", func(t *testing.T) {
39+
sample, err := piaudio.DecodeWavOrErr(validWAV)
40+
require.NoError(t, err)
41+
assert.Equal(t, []int8{0, 90, 127, 90, 0, -91, -128, -91}, sample.Data())
42+
assert.Equal(t, uint16(8363), sample.SampleRate())
43+
})
44+
45+
t.Run("should return error when wav is invalid", func(t *testing.T) {
46+
tests := map[string]struct {
47+
file []byte
48+
expectedErr string
49+
}{
50+
"16-bit": {
51+
file: invalid16bitWAV,
52+
expectedErr: "only 8-bit PCM supported, got 16 bits",
53+
},
54+
"stereo": {
55+
file: invalidStereoWAV,
56+
expectedErr: "only mono supported, got 2 channels",
57+
},
58+
"88200 sample rate": {
59+
file: invalidSampleRateWAV,
60+
expectedErr: "sample rate is too high. Max 48kHz supported, got 88200",
61+
},
62+
"not PCM": {
63+
file: invalidNotPCM,
64+
expectedErr: "only PCM supported",
65+
},
66+
}
67+
68+
for testName, testCase := range tests {
69+
t.Run(testName, func(t *testing.T) {
70+
sample, err := piaudio.DecodeWavOrErr(testCase.file)
71+
assert.Nil(t, sample)
72+
assert.ErrorContains(t, err, testCase.expectedErr)
73+
})
74+
}
75+
})
76+
}
432 Bytes
Binary file not shown.
132 Bytes
Binary file not shown.
128 Bytes
Binary file not shown.
132 Bytes
Binary file not shown.

piaudio/internal/test/valid.wav

120 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)