38 lines
883 B
Go
38 lines
883 B
Go
package audio
|
|
|
|
import "testing"
|
|
|
|
func TestPCM16CodecRoundTrip(t *testing.T) {
|
|
c := NewPCM16Codec()
|
|
in := []byte{0x01, 0x02, 0x03, 0x04, 0xff, 0x7f}
|
|
enc, err := c.Encode(in)
|
|
if err != nil {
|
|
t.Fatalf("encode: %v", err)
|
|
}
|
|
dec, err := c.Decode(enc)
|
|
if err != nil {
|
|
t.Fatalf("decode: %v", err)
|
|
}
|
|
if string(dec) != string(in) {
|
|
t.Fatalf("round-trip mismatch: got % X want % X", dec, in)
|
|
}
|
|
}
|
|
|
|
func TestPCM16CodecTrimsOddByte(t *testing.T) {
|
|
c := NewPCM16Codec()
|
|
dec, err := c.Decode([]byte{0x10, 0x20, 0x30}) // 3 bytes = 1 sample + stray
|
|
if err != nil {
|
|
t.Fatalf("decode: %v", err)
|
|
}
|
|
if len(dec) != 2 {
|
|
t.Fatalf("expected the stray byte trimmed to 2, got %d", len(dec))
|
|
}
|
|
}
|
|
|
|
func TestPCM16CodecRejectsSingleByte(t *testing.T) {
|
|
c := NewPCM16Codec()
|
|
if _, err := c.Decode([]byte{0x10}); err == nil {
|
|
t.Fatalf("expected an error for a sub-sample payload")
|
|
}
|
|
}
|