forked from faiface/beep
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test Streamer return values of different audio format decoders
- Loading branch information
1 parent
fb9078b
commit 84dd3d8
Showing
8 changed files
with
126 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters