further test coverage

This commit is contained in:
Rob Watson 2021-01-27 20:46:00 +01:00
parent 5c836c4f70
commit c612714efa
2 changed files with 95 additions and 8 deletions

View File

@ -13,9 +13,10 @@ const fname = "/home/rob/share/home-office/example.mp3"
type (
AudioVersionId int
Layer int
header []byte
)
type Decoder struct{}
const (
AudioVersionMPEG25 AudioVersionId = iota
AudioVersionReserved
@ -54,6 +55,8 @@ var (
}
)
type header []byte
func (h header) HasSyncWord() bool {
return h[0] == 0xff && h[1]&0xe0 == 0xe0
}
@ -99,6 +102,9 @@ func (h header) PaddingBytes() int {
}
func (h header) IsValid() bool {
if len(h) < 4 {
return false
}
if !h.HasSyncWord() {
return false
}
@ -149,7 +155,6 @@ func (h header) BitRate() int {
j = 3
default:
return 0
}
}
return bitRates[j][h[2]>>4]

View File

@ -79,13 +79,49 @@ func TestLayer(t *testing.T) {
}
func TestPaddingBytes(t *testing.T) {
bytes := []byte{0xff, 0xe0, 0x2, 0}
h := header(bytes)
assert.Equal(t, 1, h.PaddingBytes())
testCases := []struct {
name string
bytes []byte
expPadding int
}{
{
"MPEG1 Layer3 128kbps 44.1k, no padding",
[]byte{0xff, 0xfa, 0x90, 0},
0,
},
{
"MPEG1 Layer3 128kbps 44.1k, padding",
[]byte{0xff, 0xfa, 0x92, 0},
1,
},
{
"MPEG2 Layer1 144kbps 24k, no padding",
[]byte{0xff, 0xf6, 0x94, 0},
0,
},
{
"MPEG2 Layer1 144kbps 24k, padding",
[]byte{0xff, 0xf6, 0x96, 0},
4,
},
{
"MPEG25 Layer2 56kbps 8k, no padding",
[]byte{0xff, 0xe4, 0x78, 0},
0,
},
{
"MPEG25 Layer2 56kbps 8k, padding",
[]byte{0xff, 0xe4, 0x7a, 0},
1,
},
}
bytes = []byte{0xff, 0xe0, 0x0, 0}
h = header(bytes)
assert.Equal(t, 0, h.PaddingBytes())
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
h := header(tc.bytes)
assert.Equal(t, tc.expPadding, h.PaddingBytes())
})
}
}
func TestIsProtected(t *testing.T) {
@ -385,3 +421,49 @@ func TestSampleRate(t *testing.T) {
})
}
}
func TestLen(t *testing.T) {
testCases := []struct {
name string
bytes []byte
expLen int
}{
{
"MPEG1 Layer3 128kbps 44.1k, no padding",
[]byte{0xff, 0xfa, 0x90, 0},
417,
},
{
"MPEG1 Layer3 128kbps 44.1k, padding",
[]byte{0xff, 0xfa, 0x92, 0},
418,
},
{
"MPEG2 Layer1 144kbps 24k, no padding",
[]byte{0xff, 0xf6, 0x94, 0},
288,
},
{
"MPEG2 Layer1 144kbps 24k, padding",
[]byte{0xff, 0xf6, 0x96, 0},
292,
},
{
"MPEG25 Layer2 56kbps 8k, no padding",
[]byte{0xff, 0xe4, 0x78, 0},
1008,
},
{
"MPEG25 Layer2 56kbps 8k, padding",
[]byte{0xff, 0xe4, 0x7a, 0},
1009,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
h := header(tc.bytes)
assert.Equal(t, tc.expLen, h.Len())
})
}
}