cpython/Lib/test/test_aifc.py

330 lines
12 KiB
Python
Raw Normal View History

from test.support import findfile, run_unittest, TESTFN, unlink
import unittest
import os
import io
import struct
import aifc
class AIFCTest(unittest.TestCase):
def setUp(self):
self.f = self.fout = None
self.sndfilepath = findfile('Sine-1000Hz-300ms.aif')
def tearDown(self):
if self.f is not None:
self.f.close()
if self.fout is not None:
try:
self.fout.close()
except (aifc.Error, AttributeError):
pass
unlink(TESTFN)
unlink(TESTFN + '.aiff')
def test_skipunknown(self):
#Issue 2245
#This file contains chunk types aifc doesn't recognize.
Merged revisions 70768,71657,71721,71729,71794,71976,72036-72037,72079,72085,72131-72134,72191,72197-72198,72219,72221,72225,72303,72434,72467,72476 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r70768 | andrew.kuchling | 2009-03-30 17:29:15 -0500 (Mon, 30 Mar 2009) | 1 line Typo fixes ........ r71657 | vinay.sajip | 2009-04-16 14:07:37 -0500 (Thu, 16 Apr 2009) | 1 line Issue #5768: Change to Unicode output logic and test case for same. ........ r71721 | benjamin.peterson | 2009-04-18 14:26:19 -0500 (Sat, 18 Apr 2009) | 1 line fix a few nits in unittest.py #5771 ........ r71729 | benjamin.peterson | 2009-04-18 16:03:10 -0500 (Sat, 18 Apr 2009) | 1 line move test to a more appropiate one ........ r71794 | vinay.sajip | 2009-04-22 07:10:47 -0500 (Wed, 22 Apr 2009) | 2 lines Issue #5170: Fixed regression caused when fixing #5768. ........ r71976 | mark.dickinson | 2009-04-26 14:54:55 -0500 (Sun, 26 Apr 2009) | 2 lines Fix typo in function name ........ r72036 | georg.brandl | 2009-04-27 12:04:23 -0500 (Mon, 27 Apr 2009) | 1 line #5848: small unittest doc patch. ........ r72037 | georg.brandl | 2009-04-27 12:09:53 -0500 (Mon, 27 Apr 2009) | 1 line #5840: dont claim we dont support TLS. ........ r72079 | r.david.murray | 2009-04-28 14:02:55 -0500 (Tue, 28 Apr 2009) | 2 lines Remove spurious 'u'. ........ r72085 | georg.brandl | 2009-04-28 16:48:35 -0500 (Tue, 28 Apr 2009) | 1 line Make the doctests in the docs pass, except for those in the turtle module. ........ r72131 | benjamin.peterson | 2009-04-29 17:43:35 -0500 (Wed, 29 Apr 2009) | 1 line fix test_shutil on ZFS #5676 ........ r72132 | georg.brandl | 2009-04-29 17:44:07 -0500 (Wed, 29 Apr 2009) | 1 line #5878: fix repr of re object. ........ r72133 | benjamin.peterson | 2009-04-29 17:44:15 -0500 (Wed, 29 Apr 2009) | 1 line make sure mode is removable while cleaning up test droppings ........ r72134 | benjamin.peterson | 2009-04-29 19:06:33 -0500 (Wed, 29 Apr 2009) | 1 line make sure to close file ........ r72191 | michael.foord | 2009-05-02 06:43:06 -0500 (Sat, 02 May 2009) | 9 lines Adds an exit parameter to unittest.main(). If False main no longer calls sys.exit. Closes issue 3379. Michael Foord ........ r72197 | benjamin.peterson | 2009-05-02 11:24:37 -0500 (Sat, 02 May 2009) | 1 line don't let sys.argv be used in the tests ........ r72198 | andrew.kuchling | 2009-05-02 12:12:15 -0500 (Sat, 02 May 2009) | 1 line Add items ........ r72219 | michael.foord | 2009-05-02 15:15:05 -0500 (Sat, 02 May 2009) | 8 lines Add addCleanup and doCleanups to unittest.TestCase. Closes issue 5679. Michael Foord ........ r72221 | benjamin.peterson | 2009-05-02 15:26:53 -0500 (Sat, 02 May 2009) | 1 line add myself ........ r72225 | michael.foord | 2009-05-02 17:43:34 -0500 (Sat, 02 May 2009) | 1 line ........ r72303 | benjamin.peterson | 2009-05-04 19:55:24 -0500 (Mon, 04 May 2009) | 1 line using sys._getframe(x), where x > 0 doesnt' work on IronPython ........ r72434 | r.david.murray | 2009-05-07 13:09:58 -0500 (Thu, 07 May 2009) | 2 lines Pre-opened test file needs to be opened in binary mode. ........ r72467 | georg.brandl | 2009-05-08 07:17:34 -0500 (Fri, 08 May 2009) | 1 line Fix name. ........ r72476 | thomas.heller | 2009-05-08 15:09:40 -0500 (Fri, 08 May 2009) | 4 lines Add a file that contains diffs between offical libffi files and the files in this repository. Should make it easier to merge new libffi versions. ........
2009-05-08 17:42:26 -03:00
self.f = aifc.open(self.sndfilepath)
def test_params(self):
f = self.f = aifc.open(self.sndfilepath)
self.assertEqual(f.getfp().name, self.sndfilepath)
self.assertEqual(f.getnchannels(), 2)
self.assertEqual(f.getsampwidth(), 2)
self.assertEqual(f.getframerate(), 48000)
self.assertEqual(f.getnframes(), 14400)
self.assertEqual(f.getcomptype(), b'NONE')
self.assertEqual(f.getcompname(), b'not compressed')
self.assertEqual(
f.getparams(),
(2, 2, 48000, 14400, b'NONE', b'not compressed'),
)
def test_read(self):
f = self.f = aifc.open(self.sndfilepath)
self.assertEqual(f.readframes(0), b'')
self.assertEqual(f.tell(), 0)
self.assertEqual(f.readframes(2), b'\x00\x00\x00\x00\x0b\xd4\x0b\xd4')
f.rewind()
pos0 = f.tell()
self.assertEqual(pos0, 0)
self.assertEqual(f.readframes(2), b'\x00\x00\x00\x00\x0b\xd4\x0b\xd4')
pos2 = f.tell()
self.assertEqual(pos2, 2)
self.assertEqual(f.readframes(2), b'\x17t\x17t"\xad"\xad')
f.setpos(pos2)
self.assertEqual(f.readframes(2), b'\x17t\x17t"\xad"\xad')
f.setpos(pos0)
self.assertEqual(f.readframes(2), b'\x00\x00\x00\x00\x0b\xd4\x0b\xd4')
with self.assertRaises(aifc.Error):
f.setpos(-1)
with self.assertRaises(aifc.Error):
f.setpos(f.getnframes() + 1)
def test_write(self):
f = self.f = aifc.open(self.sndfilepath)
fout = self.fout = aifc.open(TESTFN, 'wb')
fout.aifc()
fout.setparams(f.getparams())
for frame in range(f.getnframes()):
fout.writeframes(f.readframes(1))
fout.close()
fout = self.fout = aifc.open(TESTFN, 'rb')
f.rewind()
self.assertEqual(f.getparams(), fout.getparams())
self.assertEqual(f.readframes(5), fout.readframes(5))
def test_compress(self):
f = self.f = aifc.open(self.sndfilepath)
fout = self.fout = aifc.open(TESTFN, 'wb')
fout.aifc()
fout.setnchannels(f.getnchannels())
fout.setsampwidth(f.getsampwidth())
fout.setframerate(f.getframerate())
fout.setcomptype(b'ULAW', b'foo')
for frame in range(f.getnframes()):
fout.writeframes(f.readframes(1))
fout.close()
self.assertLess(
os.stat(TESTFN).st_size,
os.stat(self.sndfilepath).st_size*0.75,
)
fout = self.fout = aifc.open(TESTFN, 'rb')
f.rewind()
self.assertEqual(f.getparams()[0:3], fout.getparams()[0:3])
self.assertEqual(fout.getcomptype(), b'ULAW')
self.assertEqual(fout.getcompname(), b'foo')
def test_close(self):
class Wrapfile(object):
def __init__(self, file):
self.file = open(file, 'rb')
self.closed = False
def close(self):
self.file.close()
self.closed = True
def __getattr__(self, attr): return getattr(self.file, attr)
testfile = Wrapfile(self.sndfilepath)
f = self.f = aifc.open(testfile)
self.assertEqual(testfile.closed, False)
f.close()
self.assertEqual(testfile.closed, True)
def test_write_header_comptype_sampwidth(self):
for comptype in (b'ULAW', b'ulaw', b'ALAW', b'alaw', b'G722'):
fout = aifc.open(io.BytesIO(), 'wb')
fout.setnchannels(1)
fout.setframerate(1)
fout.setcomptype(comptype, b'')
fout.close()
self.assertEqual(fout.getsampwidth(), 2)
fout.initfp(None)
def test_write_markers_values(self):
fout = aifc.open(io.BytesIO(), 'wb')
self.assertEqual(fout.getmarkers(), None)
fout.setmark(1, 0, b'foo1')
fout.setmark(1, 1, b'foo2')
self.assertEqual(fout.getmark(1), (1, 1, b'foo2'))
self.assertEqual(fout.getmarkers(), [(1, 1, b'foo2')])
fout.initfp(None)
def test_read_markers(self):
fout = self.fout = aifc.open(TESTFN, 'wb')
fout.aiff()
fout.setparams((1, 1, 1, 1, b'NONE', b''))
fout.setmark(1, 0, b'odd')
fout.setmark(2, 0, b'even')
fout.writeframes(b'\x00')
fout.close()
f = self.f = aifc.open(TESTFN, 'rb')
self.assertEqual(f.getmarkers(), [(1, 0, b'odd'), (2, 0, b'even')])
self.assertEqual(f.getmark(1), (1, 0, b'odd'))
self.assertEqual(f.getmark(2), (2, 0, b'even'))
self.assertRaises(aifc.Error, f.getmark, 3)
class AIFCLowLevelTest(unittest.TestCase):
def test_read_written(self):
def read_written(self, what):
f = io.BytesIO()
getattr(aifc, '_write_' + what)(f, x)
f.seek(0)
return getattr(aifc, '_read_' + what)(f)
for x in (-1, 0, 0.1, 1):
self.assertEqual(read_written(x, 'float'), x)
for x in (float('NaN'), float('Inf')):
self.assertEqual(read_written(x, 'float'), aifc._HUGE_VAL)
for x in (b'', b'foo', b'a' * 255):
self.assertEqual(read_written(x, 'string'), x)
for x in (-0x7FFFFFFF, -1, 0, 1, 0x7FFFFFFF):
self.assertEqual(read_written(x, 'long'), x)
for x in (0, 1, 0xFFFFFFFF):
self.assertEqual(read_written(x, 'ulong'), x)
for x in (-0x7FFF, -1, 0, 1, 0x7FFF):
self.assertEqual(read_written(x, 'short'), x)
for x in (0, 1, 0xFFFF):
self.assertEqual(read_written(x, 'ushort'), x)
def test_read_raises(self):
f = io.BytesIO(b'\x00')
self.assertRaises(EOFError, aifc._read_ulong, f)
self.assertRaises(EOFError, aifc._read_long, f)
self.assertRaises(EOFError, aifc._read_ushort, f)
self.assertRaises(EOFError, aifc._read_short, f)
def test_write_long_string_raises(self):
f = io.BytesIO()
with self.assertRaises(ValueError):
aifc._write_string(f, b'too long' * 255)
def test_wrong_open_mode(self):
with self.assertRaises(aifc.Error):
aifc.open(TESTFN, 'wrong_mode')
def test_read_wrong_form(self):
b1 = io.BytesIO(b'WRNG' + struct.pack('>L', 0))
b2 = io.BytesIO(b'FORM' + struct.pack('>L', 4) + b'WRNG')
self.assertRaises(aifc.Error, aifc.open, b1)
self.assertRaises(aifc.Error, aifc.open, b2)
def test_read_no_comm_chunk(self):
b = io.BytesIO(b'FORM' + struct.pack('>L', 4) + b'AIFF')
self.assertRaises(aifc.Error, aifc.open, b)
def test_read_wrong_compression_type(self):
b = b'FORM' + struct.pack('>L', 4) + b'AIFC'
b += b'COMM' + struct.pack('>LhlhhLL', 23, 0, 0, 0, 0, 0, 0)
b += b'WRNG' + struct.pack('B', 0)
self.assertRaises(aifc.Error, aifc.open, io.BytesIO(b))
def test_read_wrong_marks(self):
b = b'FORM' + struct.pack('>L', 4) + b'AIFF'
b += b'COMM' + struct.pack('>LhlhhLL', 18, 0, 0, 0, 0, 0, 0)
b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8
b += b'MARK' + struct.pack('>LhB', 3, 1, 1)
with self.assertWarns(UserWarning):
f = aifc.open(io.BytesIO(b))
self.assertEqual(f.getmarkers(), None)
def test_read_comm_kludge_compname_even(self):
b = b'FORM' + struct.pack('>L', 4) + b'AIFC'
b += b'COMM' + struct.pack('>LhlhhLL', 18, 0, 0, 0, 0, 0, 0)
b += b'NONE' + struct.pack('B', 4) + b'even' + b'\x00'
b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8
with self.assertWarns(UserWarning):
f = aifc.open(io.BytesIO(b))
self.assertEqual(f.getcompname(), b'even')
def test_read_comm_kludge_compname_odd(self):
b = b'FORM' + struct.pack('>L', 4) + b'AIFC'
b += b'COMM' + struct.pack('>LhlhhLL', 18, 0, 0, 0, 0, 0, 0)
b += b'NONE' + struct.pack('B', 3) + b'odd'
b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8
with self.assertWarns(UserWarning):
f = aifc.open(io.BytesIO(b))
self.assertEqual(f.getcompname(), b'odd')
def test_write_params_raises(self):
fout = aifc.open(io.BytesIO(), 'wb')
wrong_params = (0, 0, 0, 0, b'WRNG', '')
self.assertRaises(aifc.Error, fout.setparams, wrong_params)
self.assertRaises(aifc.Error, fout.getparams)
self.assertRaises(aifc.Error, fout.setnchannels, 0)
self.assertRaises(aifc.Error, fout.getnchannels)
self.assertRaises(aifc.Error, fout.setsampwidth, 0)
self.assertRaises(aifc.Error, fout.getsampwidth)
self.assertRaises(aifc.Error, fout.setframerate, 0)
self.assertRaises(aifc.Error, fout.getframerate)
self.assertRaises(aifc.Error, fout.setcomptype, b'WRNG', '')
fout.aiff()
fout.setnchannels(1)
fout.setsampwidth(1)
fout.setframerate(1)
fout.setnframes(1)
fout.writeframes(b'\x00')
self.assertRaises(aifc.Error, fout.setparams, (1, 1, 1, 1, 1, 1))
self.assertRaises(aifc.Error, fout.setnchannels, 1)
self.assertRaises(aifc.Error, fout.setsampwidth, 1)
self.assertRaises(aifc.Error, fout.setframerate, 1)
self.assertRaises(aifc.Error, fout.setnframes, 1)
self.assertRaises(aifc.Error, fout.setcomptype, b'NONE', '')
self.assertRaises(aifc.Error, fout.aiff)
self.assertRaises(aifc.Error, fout.aifc)
def test_write_params_singles(self):
fout = aifc.open(io.BytesIO(), 'wb')
fout.aifc()
fout.setnchannels(1)
fout.setsampwidth(2)
fout.setframerate(3)
fout.setnframes(4)
fout.setcomptype(b'NONE', b'name')
self.assertEqual(fout.getnchannels(), 1)
self.assertEqual(fout.getsampwidth(), 2)
self.assertEqual(fout.getframerate(), 3)
self.assertEqual(fout.getnframes(), 0)
self.assertEqual(fout.tell(), 0)
self.assertEqual(fout.getcomptype(), b'NONE')
self.assertEqual(fout.getcompname(), b'name')
fout.writeframes(b'\x00' * 4 * fout.getsampwidth() * fout.getnchannels())
self.assertEqual(fout.getnframes(), 4)
self.assertEqual(fout.tell(), 4)
def test_write_params_bunch(self):
fout = aifc.open(io.BytesIO(), 'wb')
fout.aifc()
p = (1, 2, 3, 4, b'NONE', b'name')
fout.setparams(p)
self.assertEqual(fout.getparams(), p)
fout.initfp(None)
def test_write_header_raises(self):
fout = aifc.open(io.BytesIO(), 'wb')
self.assertRaises(aifc.Error, fout.close)
fout.setnchannels(1)
self.assertRaises(aifc.Error, fout.close)
fout.setsampwidth(1)
self.assertRaises(aifc.Error, fout.close)
fout.initfp(None)
def test_write_header_comptype_raises(self):
for comptype in (b'ULAW', b'ulaw', b'ALAW', b'alaw', b'G722'):
fout = aifc.open(io.BytesIO(), 'wb')
fout.setsampwidth(1)
fout.setcomptype(comptype, b'')
self.assertRaises(aifc.Error, fout.close)
fout.initfp(None)
def test_write_markers_raises(self):
fout = aifc.open(io.BytesIO(), 'wb')
self.assertRaises(aifc.Error, fout.setmark, 0, 0, b'')
self.assertRaises(aifc.Error, fout.setmark, 1, -1, b'')
self.assertRaises(aifc.Error, fout.setmark, 1, 0, None)
self.assertRaises(aifc.Error, fout.getmark, 1)
fout.initfp(None)
def test_write_aiff_by_extension(self):
sampwidth = 2
fout = self.fout = aifc.open(TESTFN + '.aiff', 'wb')
fout.setparams((1, sampwidth, 1, 1, b'ULAW', b''))
frames = b'\x00' * fout.getnchannels() * sampwidth
fout.writeframes(frames)
fout.close()
f = self.f = aifc.open(TESTFN + '.aiff', 'rb')
self.assertEqual(f.getcomptype(), b'NONE')
f.close()
def test_main():
run_unittest(AIFCTest)
run_unittest(AIFCLowLevelTest)
if __name__ == "__main__":
unittest.main()