Skip to content

Commit

Permalink
Test Streamer return values of different audio format decoders
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkKremer committed Nov 4, 2023
1 parent fb9078b commit 84dd3d8
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 21 deletions.
24 changes: 4 additions & 20 deletions flac/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,14 @@ import (
"github.com/gopxl/beep/internal/testtools"
)

func TestDecoder_Stream(t *testing.T) {
func TestDecoder_ReturnBehaviour(t *testing.T) {
f, err := os.Open(testtools.TestFilePath("valid_44100hz_22050_samples.flac"))
assert.NoError(t, err)
defer f.Close()

streamer, _, err := flac.Decode(f)
s, _, err := flac.Decode(f)
assert.NoError(t, err)
assert.Equal(t, 22050, s.Len())

// Case 1: return ok with all requested samples
buf := testtools.CollectNum(22000, streamer)
assert.Lenf(t, buf, 22000, "streamer quit prematurely; expected %d samples, got %d", 22000, len(buf))
assert.NoError(t, streamer.Err())

buf = make([][2]float64, 512)

// Case 2: return ok with 0 < n < 512 samples
n, ok := streamer.Stream(buf[:])
assert.True(t, ok)
assert.Equal(t, 50, n)
assert.NoError(t, streamer.Err())

// Case 3: return !ok with n == 0
n, ok = streamer.Stream(buf[:])
assert.False(t, ok)
assert.Equal(t, 0, n)
assert.NoError(t, streamer.Err())
testtools.AssertStreamerHasCorrectReturnBehaviour(t, s, s.Len())
}
Binary file added internal/testdata/valid_44100hz_22050_samples.mp3
Binary file not shown.
Binary file not shown.
Binary file added internal/testdata/valid_44100hz_22050_samples.wav
Binary file not shown.
59 changes: 59 additions & 0 deletions internal/testtools/asserts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package testtools

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"

"github.com/gopxl/beep"
)

// AssertStreamerHasCorrectReturnBehaviour tests whether the return values returned
// by the streamer s adhere to the description on the Streamer interface.
func AssertStreamerHasCorrectReturnBehaviour(t *testing.T, s beep.Streamer, expectedSamples int) {
const leaveUnreadInFirstCase = 50

if expectedSamples < leaveUnreadInFirstCase+1 {
panic(fmt.Sprintf("AssertStreamerHasCorrectReturnBehaviour must be called with at least %d samples.", leaveUnreadInFirstCase+1))
}

// 1. n == len(samples) && ok
buf := make([][2]float64, 512)
samplesLeft := expectedSamples - leaveUnreadInFirstCase
for samplesLeft > 0 {
toRead := len(buf)
if toRead > samplesLeft {
toRead = samplesLeft
}
n, ok := s.Stream(buf[:toRead])
if !ok {
t.Fatalf("streamer returned !ok before it was expected to be drained")
}
if n < toRead {
t.Fatalf("streamer didn't return all requested samples before it was expected to be drained")
}
if s.Err() != nil {
t.Fatalf("unexpected error in streamer: %v", s.Err())
}
samplesLeft -= n
}

// 2. 0 < n && n < len(samples) && ok
n, ok := s.Stream(buf)
assert.True(t, ok)
assert.Equal(t, leaveUnreadInFirstCase, n)
assert.NoError(t, s.Err())

// 3. n == 0 && !ok
n, ok = s.Stream(buf)
assert.False(t, ok)
assert.Equal(t, 0, n)
assert.NoError(t, s.Err())

// Repeat calls after case 3 must return the same result.
n, ok = s.Stream(buf)
assert.False(t, ok)
assert.Equal(t, 0, n)
assert.NoError(t, s.Err())
}
23 changes: 23 additions & 0 deletions mp3/decode_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package mp3_test

import (
"os"
"testing"

"github.com/stretchr/testify/assert"

"github.com/gopxl/beep/internal/testtools"
"github.com/gopxl/beep/mp3"
)

func TestDecoder_ReturnBehaviour(t *testing.T) {
f, err := os.Open(testtools.TestFilePath("valid_44100hz_22050_samples.mp3"))
assert.NoError(t, err)
defer f.Close()

s, _, err := mp3.Decode(f)
assert.NoError(t, err)
//assert.Equal(t, 22050, s.Len()) // todo: mp3 seems to return more samples than there are in the file. Uncomment this when fixed.

testtools.AssertStreamerHasCorrectReturnBehaviour(t, s, s.Len())
}
23 changes: 23 additions & 0 deletions vorbis/decode_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package vorbis_test

import (
"os"
"testing"

"github.com/stretchr/testify/assert"

"github.com/gopxl/beep/internal/testtools"
"github.com/gopxl/beep/vorbis"
)

func TestDecoder_ReturnBehaviour(t *testing.T) {
f, err := os.Open(testtools.TestFilePath("valid_44100hz_22050_samples.ogg"))
assert.NoError(t, err)
defer f.Close()

s, _, err := vorbis.Decode(f)
assert.NoError(t, err)
assert.Equal(t, 22050, s.Len())

testtools.AssertStreamerHasCorrectReturnBehaviour(t, s, s.Len())
}
18 changes: 17 additions & 1 deletion wav/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ package wav

import (
"bytes"
"os"
"testing"

"github.com/gopxl/beep"
"github.com/gopxl/beep/internal/testtools"

"github.com/stretchr/testify/assert"
"testing"
)

func TestDecode(t *testing.T) {
Expand Down Expand Up @@ -103,3 +107,15 @@ func TestDecode(t *testing.T) {
DataSize: 20, // 5 samples * 2 bytes/sample precision * 2 channels = 20 bytes
}, d.h)
}

func TestDecoder_ReturnBehaviour(t *testing.T) {
f, err := os.Open(testtools.TestFilePath("valid_44100hz_22050_samples.wav"))
assert.NoError(t, err)
defer f.Close()

s, _, err := Decode(f)
assert.NoError(t, err)
assert.Equal(t, 22050, s.Len())

testtools.AssertStreamerHasCorrectReturnBehaviour(t, s, s.Len())
}

0 comments on commit 84dd3d8

Please sign in to comment.