mirror of
https://github.com/python/cpython.git
synced 2025-11-20 10:57:44 +00:00
Convert test_ossaudiodev to use unittest.
This commit is contained in:
parent
f00c5b5873
commit
fb5b9890fb
1 changed files with 120 additions and 115 deletions
|
|
@ -1,7 +1,7 @@
|
||||||
from test import test_support
|
from test import test_support
|
||||||
test_support.requires('audio')
|
test_support.requires('audio')
|
||||||
|
|
||||||
from test.test_support import verbose, findfile, TestFailed, TestSkipped
|
from test.test_support import verbose, findfile, TestSkipped
|
||||||
|
|
||||||
import errno
|
import errno
|
||||||
import fcntl
|
import fcntl
|
||||||
|
|
@ -12,6 +12,7 @@ import select
|
||||||
import sunaudio
|
import sunaudio
|
||||||
import time
|
import time
|
||||||
import audioop
|
import audioop
|
||||||
|
import unittest
|
||||||
|
|
||||||
# Arggh, AFMT_S16_NE not defined on all platforms -- seems to be a
|
# Arggh, AFMT_S16_NE not defined on all platforms -- seems to be a
|
||||||
# fairly recent addition to OSS.
|
# fairly recent addition to OSS.
|
||||||
|
|
@ -33,25 +34,21 @@ def read_sound_file(path):
|
||||||
fp.close()
|
fp.close()
|
||||||
|
|
||||||
if enc != SND_FORMAT_MULAW_8:
|
if enc != SND_FORMAT_MULAW_8:
|
||||||
print "Expect .au file with 8-bit mu-law samples"
|
raise RuntimeError("Expect .au file with 8-bit mu-law samples")
|
||||||
return
|
|
||||||
|
|
||||||
# Convert the data to 16-bit signed.
|
# Convert the data to 16-bit signed.
|
||||||
data = audioop.ulaw2lin(data, 2)
|
data = audioop.ulaw2lin(data, 2)
|
||||||
return (data, rate, 16, nchannels)
|
return (data, rate, 16, nchannels)
|
||||||
|
|
||||||
# version of assert that still works with -O
|
class OSSAudioDevTests(unittest.TestCase):
|
||||||
def _assert(expr, message=None):
|
|
||||||
if not expr:
|
|
||||||
raise AssertionError(message or "assertion failed")
|
|
||||||
|
|
||||||
def play_sound_file(data, rate, ssize, nchannels):
|
def play_sound_file(self, data, rate, ssize, nchannels):
|
||||||
try:
|
try:
|
||||||
dsp = ossaudiodev.open('w')
|
dsp = ossaudiodev.open('w')
|
||||||
except IOError, msg:
|
except IOError, msg:
|
||||||
if msg[0] in (errno.EACCES, errno.ENOENT, errno.ENODEV, errno.EBUSY):
|
if msg[0] in (errno.EACCES, errno.ENOENT, errno.ENODEV, errno.EBUSY):
|
||||||
raise TestSkipped, msg
|
raise TestSkipped(msg)
|
||||||
raise TestFailed, msg
|
raise
|
||||||
|
|
||||||
# at least check that these methods can be invoked
|
# at least check that these methods can be invoked
|
||||||
dsp.bufsize()
|
dsp.bufsize()
|
||||||
|
|
@ -61,17 +58,18 @@ def play_sound_file(data, rate, ssize, nchannels):
|
||||||
dsp.fileno()
|
dsp.fileno()
|
||||||
|
|
||||||
# Make sure the read-only attributes work.
|
# Make sure the read-only attributes work.
|
||||||
_assert(dsp.closed is False, "dsp.closed is not False")
|
self.failUnless(dsp.close)
|
||||||
_assert(dsp.name == "/dev/dsp")
|
self.assertEqual(dsp.name, "/dev/dsp")
|
||||||
_assert(dsp.mode == 'w', "bad dsp.mode: %r" % dsp.mode)
|
self.assertEqual(dsp.mode, "w", "bad dsp.mode: %r" % dsp.mode)
|
||||||
|
|
||||||
# And make sure they're really read-only.
|
# And make sure they're really read-only.
|
||||||
for attr in ('closed', 'name', 'mode'):
|
for attr in ('closed', 'name', 'mode'):
|
||||||
try:
|
try:
|
||||||
setattr(dsp, attr, 42)
|
setattr(dsp, attr, 42)
|
||||||
raise RuntimeError("dsp.%s not read-only" % attr)
|
|
||||||
except TypeError:
|
except TypeError:
|
||||||
pass
|
pass
|
||||||
|
else:
|
||||||
|
self.fail("dsp.%s not read-only" % attr)
|
||||||
|
|
||||||
# Compute expected running time of sound sample (in seconds).
|
# Compute expected running time of sound sample (in seconds).
|
||||||
expected_time = float(len(data)) / (ssize/8) / nchannels / rate
|
expected_time = float(len(data)) / (ssize/8) / nchannels / rate
|
||||||
|
|
@ -87,11 +85,10 @@ def play_sound_file(data, rate, ssize, nchannels):
|
||||||
elapsed_time = t2 - t1
|
elapsed_time = t2 - t1
|
||||||
|
|
||||||
percent_diff = (abs(elapsed_time - expected_time) / expected_time) * 100
|
percent_diff = (abs(elapsed_time - expected_time) / expected_time) * 100
|
||||||
_assert(percent_diff <= 10.0, \
|
self.failUnless(percent_diff <= 10.0,
|
||||||
("elapsed time (%.2f sec) > 10%% off of expected time (%.2f sec)"
|
"elapsed time > 10% off of expected time")
|
||||||
% (elapsed_time, expected_time)))
|
|
||||||
|
|
||||||
def test_setparameters(dsp):
|
def set_parameters(self, dsp):
|
||||||
# Two configurations for testing:
|
# Two configurations for testing:
|
||||||
# config1 (8-bit, mono, 8 kHz) should work on even the most
|
# config1 (8-bit, mono, 8 kHz) should work on even the most
|
||||||
# ancient and crufty sound card, but maybe not on special-
|
# ancient and crufty sound card, but maybe not on special-
|
||||||
|
|
@ -114,13 +111,14 @@ def test_setparameters(dsp):
|
||||||
# setparameters() should be able to set this configuration in
|
# setparameters() should be able to set this configuration in
|
||||||
# either strict or non-strict mode.
|
# either strict or non-strict mode.
|
||||||
result = dsp.setparameters(fmt, channels, rate, False)
|
result = dsp.setparameters(fmt, channels, rate, False)
|
||||||
_assert(result == (fmt, channels, rate),
|
self.assertEqual(result, (fmt, channels, rate),
|
||||||
"setparameters%r: returned %r" % (config, result))
|
|
||||||
result = dsp.setparameters(fmt, channels, rate, True)
|
|
||||||
_assert(result == (fmt, channels, rate),
|
|
||||||
"setparameters%r: returned %r" % (config, result))
|
"setparameters%r: returned %r" % (config, result))
|
||||||
|
|
||||||
def test_bad_setparameters(dsp):
|
result = dsp.setparameters(fmt, channels, rate, True)
|
||||||
|
self.assertEqual(result, (fmt, channels, rate),
|
||||||
|
"setparameters%r: returned %r" % (config, result))
|
||||||
|
|
||||||
|
def set_bad_parameters(self, dsp):
|
||||||
|
|
||||||
# Now try some configurations that are presumably bogus: eg. 300
|
# Now try some configurations that are presumably bogus: eg. 300
|
||||||
# channels currently exceeds even Hollywood's ambitions, and
|
# channels currently exceeds even Hollywood's ambitions, and
|
||||||
|
|
@ -136,28 +134,35 @@ def test_bad_setparameters(dsp):
|
||||||
]:
|
]:
|
||||||
(fmt, channels, rate) = config
|
(fmt, channels, rate) = config
|
||||||
result = dsp.setparameters(fmt, channels, rate, False)
|
result = dsp.setparameters(fmt, channels, rate, False)
|
||||||
_assert(result != config,
|
self.failIfEqual(result, config,
|
||||||
"setparameters: unexpectedly got requested configuration")
|
"unexpectedly got requested configuration")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
result = dsp.setparameters(fmt, channels, rate, True)
|
result = dsp.setparameters(fmt, channels, rate, True)
|
||||||
raise AssertionError("setparameters: expected OSSAudioError")
|
|
||||||
except ossaudiodev.OSSAudioError, err:
|
except ossaudiodev.OSSAudioError, err:
|
||||||
print "setparameters: got OSSAudioError as expected"
|
pass
|
||||||
|
else:
|
||||||
|
self.fail("expected OSSAudioError")
|
||||||
|
|
||||||
def test():
|
def test_playback(self):
|
||||||
(data, rate, ssize, nchannels) = read_sound_file(findfile('audiotest.au'))
|
sound_info = read_sound_file(findfile('audiotest.au'))
|
||||||
play_sound_file(data, rate, ssize, nchannels)
|
self.play_sound_file(*sound_info)
|
||||||
|
|
||||||
|
def test_set_parameters(self):
|
||||||
dsp = ossaudiodev.open("w")
|
dsp = ossaudiodev.open("w")
|
||||||
try:
|
try:
|
||||||
test_setparameters(dsp)
|
self.set_parameters(dsp)
|
||||||
|
|
||||||
# Disabled because it fails under Linux 2.6 with ALSA's OSS
|
# Disabled because it fails under Linux 2.6 with ALSA's OSS
|
||||||
# emulation layer.
|
# emulation layer.
|
||||||
#test_bad_setparameters(dsp)
|
#self.set_bad_parameters(dsp)
|
||||||
finally:
|
finally:
|
||||||
dsp.close()
|
dsp.close()
|
||||||
_assert(dsp.closed is True, "dsp.closed is not True")
|
self.failUnless(dsp.closed)
|
||||||
|
|
||||||
test()
|
|
||||||
|
def test_main():
|
||||||
|
test_support.run_unittest(__name__)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
test_main()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue