Changeset 391 for python/trunk/Lib/test/test_aifc.py
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Lib/test/test_aifc.py
r2 r391 1 from test.test_support import findfile, run_unittest 1 from test.test_support import (findfile, TESTFN, unlink, captured_stdout, 2 run_unittest) 2 3 import unittest 3 4 from test import audiotests 5 import os 6 import io 7 import sys 8 import struct 4 9 import aifc 5 10 6 11 7 class AIFCTest(unittest.TestCase): 8 9 def setUp(self): 10 self.sndfilepath = findfile('Sine-1000Hz-300ms.aif') 11 12 class AifcPCM8Test(audiotests.AudioWriteTests, 13 audiotests.AudioTestsWithSourceFile, 14 unittest.TestCase): 15 module = aifc 16 sndfilename = 'pluck-pcm8.aiff' 17 sndfilenframes = 3307 18 nchannels = 2 19 sampwidth = 1 20 framerate = 11025 21 nframes = 48 22 comptype = 'NONE' 23 compname = 'not compressed' 24 frames = audiotests.fromhex("""\ 25 02FF 4B00 3104 8008 CB06 4803 BF01 03FE B8FA B4F3 29EB 1AE6 \ 26 EDE4 C6E2 0EE0 EFE0 57E2 FBE8 13EF D8F7 97FB F5FC 08FB DFFB \ 27 11FA 3EFB BCFC 66FF CF04 4309 C10E 5112 EE17 8216 7F14 8012 \ 28 490E 520D EF0F CE0F E40C 630A 080A 2B0B 510E 8B11 B60E 440A \ 29 """) 30 close_fd = True 31 32 33 class AifcPCM16Test(audiotests.AudioWriteTests, 34 audiotests.AudioTestsWithSourceFile, 35 unittest.TestCase): 36 module = aifc 37 sndfilename = 'pluck-pcm16.aiff' 38 sndfilenframes = 3307 39 nchannels = 2 40 sampwidth = 2 41 framerate = 11025 42 nframes = 48 43 comptype = 'NONE' 44 compname = 'not compressed' 45 frames = audiotests.fromhex("""\ 46 022EFFEA 4B5D00F6 311804EA 80E10840 CBE106B1 48A903F5 BFE601B2 036CFE7B \ 47 B858FA3E B4B1F34F 299AEBCA 1A5DE6DA EDFAE491 C628E275 0E09E0B5 EF2AE029 \ 48 5758E271 FB35E83F 1376EF86 D82BF727 9790FB76 F5FAFC0F 0867FB9C DF30FB43 \ 49 117EFA36 3EE5FB5B BC79FCB1 66D9FF5D CF150412 431D097C C1BA0EC8 512112A1 \ 50 EEE21753 82071665 7FFF1443 8004128F 49A20EAF 52BB0DBA EFB40F60 CE3C0FBF \ 51 E4B30CEC 63430A5C 08C80A20 2BBB0B08 514A0E43 8BCF1139 B6F60EEB 44120A5E \ 52 """) 53 close_fd = True 54 55 56 class AifcPCM24Test(audiotests.AudioWriteTests, 57 audiotests.AudioTestsWithSourceFile, 58 unittest.TestCase): 59 module = aifc 60 sndfilename = 'pluck-pcm24.aiff' 61 sndfilenframes = 3307 62 nchannels = 2 63 sampwidth = 3 64 framerate = 11025 65 nframes = 48 66 comptype = 'NONE' 67 compname = 'not compressed' 68 frames = audiotests.fromhex("""\ 69 022D65FFEB9D 4B5A0F00FA54 3113C304EE2B 80DCD6084303 \ 70 CBDEC006B261 48A99803F2F8 BFE82401B07D 036BFBFE7B5D \ 71 B85756FA3EC9 B4B055F3502B 299830EBCB62 1A5CA7E6D99A \ 72 EDFA3EE491BD C625EBE27884 0E05A9E0B6CF EF2929E02922 \ 73 5758D8E27067 FB3557E83E16 1377BFEF8402 D82C5BF7272A \ 74 978F16FB7745 F5F865FC1013 086635FB9C4E DF30FCFB40EE \ 75 117FE0FA3438 3EE6B8FB5AC3 BC77A3FCB2F4 66D6DAFF5F32 \ 76 CF13B9041275 431D69097A8C C1BB600EC74E 5120B912A2BA \ 77 EEDF641754C0 8207001664B7 7FFFFF14453F 8000001294E6 \ 78 499C1B0EB3B2 52B73E0DBCA0 EFB2B20F5FD8 CE3CDB0FBE12 \ 79 E4B49C0CEA2D 6344A80A5A7C 08C8FE0A1FFE 2BB9860B0A0E \ 80 51486F0E44E1 8BCC64113B05 B6F4EC0EEB36 4413170A5B48 \ 81 """) 82 close_fd = True 83 84 85 class AifcPCM32Test(audiotests.AudioWriteTests, 86 audiotests.AudioTestsWithSourceFile, 87 unittest.TestCase): 88 module = aifc 89 sndfilename = 'pluck-pcm32.aiff' 90 sndfilenframes = 3307 91 nchannels = 2 92 sampwidth = 4 93 framerate = 11025 94 nframes = 48 95 comptype = 'NONE' 96 compname = 'not compressed' 97 frames = audiotests.fromhex("""\ 98 022D65BCFFEB9D92 4B5A0F8000FA549C 3113C34004EE2BC0 80DCD680084303E0 \ 99 CBDEC0C006B26140 48A9980003F2F8FC BFE8248001B07D92 036BFB60FE7B5D34 \ 100 B8575600FA3EC920 B4B05500F3502BC0 29983000EBCB6240 1A5CA7A0E6D99A60 \ 101 EDFA3E80E491BD40 C625EB80E27884A0 0E05A9A0E0B6CFE0 EF292940E0292280 \ 102 5758D800E2706700 FB3557D8E83E1640 1377BF00EF840280 D82C5B80F7272A80 \ 103 978F1600FB774560 F5F86510FC101364 086635A0FB9C4E20 DF30FC40FB40EE28 \ 104 117FE0A0FA3438B0 3EE6B840FB5AC3F0 BC77A380FCB2F454 66D6DA80FF5F32B4 \ 105 CF13B980041275B0 431D6980097A8C00 C1BB60000EC74E00 5120B98012A2BAA0 \ 106 EEDF64C01754C060 820700001664B780 7FFFFFFF14453F40 800000001294E6E0 \ 107 499C1B000EB3B270 52B73E000DBCA020 EFB2B2E00F5FD880 CE3CDB400FBE1270 \ 108 E4B49CC00CEA2D90 6344A8800A5A7CA0 08C8FE800A1FFEE0 2BB986C00B0A0E00 \ 109 51486F800E44E190 8BCC6480113B0580 B6F4EC000EEB3630 441317800A5B48A0 \ 110 """) 111 close_fd = True 112 113 114 class AifcULAWTest(audiotests.AudioWriteTests, 115 audiotests.AudioTestsWithSourceFile, 116 unittest.TestCase): 117 module = aifc 118 sndfilename = 'pluck-ulaw.aifc' 119 sndfilenframes = 3307 120 nchannels = 2 121 sampwidth = 2 122 framerate = 11025 123 nframes = 48 124 comptype = 'ulaw' 125 compname = '' 126 frames = audiotests.fromhex("""\ 127 022CFFE8 497C0104 307C04DC 8284083C CB84069C 497C03DC BE8401AC 036CFE74 \ 128 B684FA24 B684F344 2A7CEC04 19FCE704 EE04E504 C584E204 0E3CE104 EF04DF84 \ 129 557CE204 FB24E804 12FCEF04 D784F744 9684FB64 F5C4FC24 083CFBA4 DF84FB24 \ 130 11FCFA24 3E7CFB64 BA84FCB4 657CFF5C CF84041C 417C093C C1840EBC 517C12FC \ 131 EF0416FC 828415FC 7D7C13FC 828412FC 497C0EBC 517C0DBC F0040F3C CD840FFC \ 132 E5040CBC 617C0A3C 08BC0A3C 2C7C0B3C 517C0E3C 8A8410FC B6840EBC 457C0A3C \ 133 """) 134 if sys.byteorder != 'big': 135 frames = audiotests.byteswap2(frames) 136 close_fd = True 137 138 139 class AifcMiscTest(audiotests.AudioTests, unittest.TestCase): 12 140 def test_skipunknown(self): 13 141 #Issue 2245 14 142 #This file contains chunk types aifc doesn't recognize. 15 f = aifc.open(self.sndfilepath) 143 self.f = aifc.open(findfile('Sine-1000Hz-300ms.aif')) 144 145 def test_write_markers_values(self): 146 fout = aifc.open(io.BytesIO(), 'wb') 147 self.assertEqual(fout.getmarkers(), None) 148 fout.setmark(1, 0, 'foo1') 149 fout.setmark(1, 1, 'foo2') 150 self.assertEqual(fout.getmark(1), (1, 1, 'foo2')) 151 self.assertEqual(fout.getmarkers(), [(1, 1, 'foo2')]) 152 fout.initfp(None) 153 154 def test_read_markers(self): 155 fout = self.fout = aifc.open(TESTFN, 'wb') 156 fout.aiff() 157 fout.setparams((1, 1, 1, 1, 'NONE', '')) 158 fout.setmark(1, 0, 'odd') 159 fout.setmark(2, 0, 'even') 160 fout.writeframes('\x00') 161 fout.close() 162 f = self.f = aifc.open(TESTFN, 'rb') 163 self.assertEqual(f.getmarkers(), [(1, 0, 'odd'), (2, 0, 'even')]) 164 self.assertEqual(f.getmark(1), (1, 0, 'odd')) 165 self.assertEqual(f.getmark(2), (2, 0, 'even')) 166 self.assertRaises(aifc.Error, f.getmark, 3) 167 168 169 class AIFCLowLevelTest(unittest.TestCase): 170 171 def test_read_written(self): 172 def read_written(self, what): 173 f = io.BytesIO() 174 getattr(aifc, '_write_' + what)(f, x) 175 f.seek(0) 176 return getattr(aifc, '_read_' + what)(f) 177 for x in (-1, 0, 0.1, 1): 178 self.assertEqual(read_written(x, 'float'), x) 179 for x in (float('NaN'), float('Inf')): 180 self.assertEqual(read_written(x, 'float'), aifc._HUGE_VAL) 181 for x in ('', 'foo', 'a' * 255): 182 self.assertEqual(read_written(x, 'string'), x) 183 for x in (-0x7FFFFFFF, -1, 0, 1, 0x7FFFFFFF): 184 self.assertEqual(read_written(x, 'long'), x) 185 for x in (0, 1, 0xFFFFFFFF): 186 self.assertEqual(read_written(x, 'ulong'), x) 187 for x in (-0x7FFF, -1, 0, 1, 0x7FFF): 188 self.assertEqual(read_written(x, 'short'), x) 189 for x in (0, 1, 0xFFFF): 190 self.assertEqual(read_written(x, 'ushort'), x) 191 192 def test_read_raises(self): 193 f = io.BytesIO('\x00') 194 self.assertRaises(EOFError, aifc._read_ulong, f) 195 self.assertRaises(EOFError, aifc._read_long, f) 196 self.assertRaises(EOFError, aifc._read_ushort, f) 197 self.assertRaises(EOFError, aifc._read_short, f) 198 199 def test_write_long_string_raises(self): 200 f = io.BytesIO() 201 with self.assertRaises(ValueError): 202 aifc._write_string(f, 'too long' * 255) 203 204 def test_wrong_open_mode(self): 205 with self.assertRaises(aifc.Error): 206 aifc.open(TESTFN, 'wrong_mode') 207 208 def test_read_wrong_form(self): 209 b1 = io.BytesIO('WRNG' + struct.pack('>L', 0)) 210 b2 = io.BytesIO('FORM' + struct.pack('>L', 4) + 'WRNG') 211 self.assertRaises(aifc.Error, aifc.open, b1) 212 self.assertRaises(aifc.Error, aifc.open, b2) 213 214 def test_read_no_comm_chunk(self): 215 b = io.BytesIO('FORM' + struct.pack('>L', 4) + 'AIFF') 216 self.assertRaises(aifc.Error, aifc.open, b) 217 218 def test_read_wrong_compression_type(self): 219 b = 'FORM' + struct.pack('>L', 4) + 'AIFC' 220 b += 'COMM' + struct.pack('>LhlhhLL', 23, 0, 0, 0, 0, 0, 0) 221 b += 'WRNG' + struct.pack('B', 0) 222 self.assertRaises(aifc.Error, aifc.open, io.BytesIO(b)) 223 224 def test_read_wrong_marks(self): 225 b = 'FORM' + struct.pack('>L', 4) + 'AIFF' 226 b += 'COMM' + struct.pack('>LhlhhLL', 18, 0, 0, 0, 0, 0, 0) 227 b += 'SSND' + struct.pack('>L', 8) + '\x00' * 8 228 b += 'MARK' + struct.pack('>LhB', 3, 1, 1) 229 with captured_stdout() as s: 230 f = aifc.open(io.BytesIO(b)) 231 self.assertEqual(s.getvalue(), 'Warning: MARK chunk contains ' 232 'only 0 markers instead of 1\n') 233 self.assertEqual(f.getmarkers(), None) 234 235 def test_read_comm_kludge_compname_even(self): 236 b = 'FORM' + struct.pack('>L', 4) + 'AIFC' 237 b += 'COMM' + struct.pack('>LhlhhLL', 18, 0, 0, 0, 0, 0, 0) 238 b += 'NONE' + struct.pack('B', 4) + 'even' + '\x00' 239 b += 'SSND' + struct.pack('>L', 8) + '\x00' * 8 240 with captured_stdout() as s: 241 f = aifc.open(io.BytesIO(b)) 242 self.assertEqual(s.getvalue(), 'Warning: bad COMM chunk size\n') 243 self.assertEqual(f.getcompname(), 'even') 244 245 def test_read_comm_kludge_compname_odd(self): 246 b = 'FORM' + struct.pack('>L', 4) + 'AIFC' 247 b += 'COMM' + struct.pack('>LhlhhLL', 18, 0, 0, 0, 0, 0, 0) 248 b += 'NONE' + struct.pack('B', 3) + 'odd' 249 b += 'SSND' + struct.pack('>L', 8) + '\x00' * 8 250 with captured_stdout() as s: 251 f = aifc.open(io.BytesIO(b)) 252 self.assertEqual(s.getvalue(), 'Warning: bad COMM chunk size\n') 253 self.assertEqual(f.getcompname(), 'odd') 254 255 def test_write_params_raises(self): 256 fout = aifc.open(io.BytesIO(), 'wb') 257 wrong_params = (0, 0, 0, 0, 'WRNG', '') 258 self.assertRaises(aifc.Error, fout.setparams, wrong_params) 259 self.assertRaises(aifc.Error, fout.getparams) 260 self.assertRaises(aifc.Error, fout.setnchannels, 0) 261 self.assertRaises(aifc.Error, fout.getnchannels) 262 self.assertRaises(aifc.Error, fout.setsampwidth, 0) 263 self.assertRaises(aifc.Error, fout.getsampwidth) 264 self.assertRaises(aifc.Error, fout.setframerate, 0) 265 self.assertRaises(aifc.Error, fout.getframerate) 266 self.assertRaises(aifc.Error, fout.setcomptype, 'WRNG', '') 267 fout.aiff() 268 fout.setnchannels(1) 269 fout.setsampwidth(1) 270 fout.setframerate(1) 271 fout.setnframes(1) 272 fout.writeframes('\x00') 273 self.assertRaises(aifc.Error, fout.setparams, (1, 1, 1, 1, 1, 1)) 274 self.assertRaises(aifc.Error, fout.setnchannels, 1) 275 self.assertRaises(aifc.Error, fout.setsampwidth, 1) 276 self.assertRaises(aifc.Error, fout.setframerate, 1) 277 self.assertRaises(aifc.Error, fout.setnframes, 1) 278 self.assertRaises(aifc.Error, fout.setcomptype, 'NONE', '') 279 self.assertRaises(aifc.Error, fout.aiff) 280 self.assertRaises(aifc.Error, fout.aifc) 281 282 def test_write_params_singles(self): 283 fout = aifc.open(io.BytesIO(), 'wb') 284 fout.aifc() 285 fout.setnchannels(1) 286 fout.setsampwidth(2) 287 fout.setframerate(3) 288 fout.setnframes(4) 289 fout.setcomptype('NONE', 'name') 290 self.assertEqual(fout.getnchannels(), 1) 291 self.assertEqual(fout.getsampwidth(), 2) 292 self.assertEqual(fout.getframerate(), 3) 293 self.assertEqual(fout.getnframes(), 0) 294 self.assertEqual(fout.tell(), 0) 295 self.assertEqual(fout.getcomptype(), 'NONE') 296 self.assertEqual(fout.getcompname(), 'name') 297 fout.writeframes('\x00' * 4 * fout.getsampwidth() * fout.getnchannels()) 298 self.assertEqual(fout.getnframes(), 4) 299 self.assertEqual(fout.tell(), 4) 300 301 def test_write_params_bunch(self): 302 fout = aifc.open(io.BytesIO(), 'wb') 303 fout.aifc() 304 p = (1, 2, 3, 4, 'NONE', 'name') 305 fout.setparams(p) 306 self.assertEqual(fout.getparams(), p) 307 fout.initfp(None) 308 309 def test_write_header_raises(self): 310 fout = aifc.open(io.BytesIO(), 'wb') 311 self.assertRaises(aifc.Error, fout.close) 312 fout = aifc.open(io.BytesIO(), 'wb') 313 fout.setnchannels(1) 314 self.assertRaises(aifc.Error, fout.close) 315 fout = aifc.open(io.BytesIO(), 'wb') 316 fout.setnchannels(1) 317 fout.setsampwidth(1) 318 self.assertRaises(aifc.Error, fout.close) 319 320 def test_write_header_comptype_raises(self): 321 for comptype in ('ULAW', 'ulaw', 'ALAW', 'alaw', 'G722'): 322 fout = aifc.open(io.BytesIO(), 'wb') 323 fout.setsampwidth(1) 324 fout.setcomptype(comptype, '') 325 self.assertRaises(aifc.Error, fout.close) 326 fout.initfp(None) 327 328 def test_write_markers_raises(self): 329 fout = aifc.open(io.BytesIO(), 'wb') 330 self.assertRaises(aifc.Error, fout.setmark, 0, 0, '') 331 self.assertRaises(aifc.Error, fout.setmark, 1, -1, '') 332 self.assertRaises(aifc.Error, fout.setmark, 1, 0, None) 333 self.assertRaises(aifc.Error, fout.getmark, 1) 334 fout.initfp(None) 335 336 def test_write_aiff_by_extension(self): 337 sampwidth = 2 338 fout = self.fout = aifc.open(TESTFN + '.aiff', 'wb') 339 fout.setparams((1, sampwidth, 1, 1, 'ULAW', '')) 340 frames = '\x00' * fout.getnchannels() * sampwidth 341 fout.writeframes(frames) 342 fout.close() 343 f = self.f = aifc.open(TESTFN + '.aiff', 'rb') 344 self.assertEqual(f.getcomptype(), 'NONE') 16 345 f.close() 17 346 18 def test_params(self):19 f = aifc.open(self.sndfilepath)20 self.assertEqual(f.getnchannels(), 2)21 self.assertEqual(f.getsampwidth(), 2)22 self.assertEqual(f.getframerate(), 48000)23 self.assertEqual(f.getnframes(), 14400)24 self.assertEqual(f.getcomptype(), 'NONE')25 self.assertEqual(f.getcompname(), 'not compressed')26 self.assertEqual(f.getparams(), (2, 2, 48000, 14400, 'NONE', 'not compressed'))27 f.close()28 29 def test_read(self):30 f = aifc.open(self.sndfilepath)31 self.assertEqual(f.tell(), 0)32 self.assertEqual(f.readframes(2), '\x00\x00\x00\x00\x0b\xd4\x0b\xd4')33 f.rewind()34 pos0 = f.tell()35 self.assertEqual(pos0, 0)36 self.assertEqual(f.readframes(2), '\x00\x00\x00\x00\x0b\xd4\x0b\xd4')37 pos2 = f.tell()38 self.assertEqual(pos2, 2)39 self.assertEqual(f.readframes(2), '\x17t\x17t"\xad"\xad')40 f.setpos(pos2)41 self.assertEqual(f.readframes(2), '\x17t\x17t"\xad"\xad')42 f.setpos(pos0)43 self.assertEqual(f.readframes(2), '\x00\x00\x00\x00\x0b\xd4\x0b\xd4')44 f.close()45 46 #XXX Need more tests!47 48 def test_close(self):49 class Wrapfile(object):50 def __init__(self, file):51 self.file = open(file, 'rb')52 self.closed = False53 def close(self):54 self.file.close()55 self.closed = True56 def __getattr__(self, attr): return getattr(self.file, attr)57 testfile = Wrapfile(self.sndfilepath)58 f = self.f = aifc.open(testfile)59 self.assertEqual(testfile.closed, False)60 f.close()61 self.assertEqual(testfile.closed, True)62 63 347 64 348 def test_main(): 65 run_unittest(AIFCTest) 66 349 run_unittest(AifcPCM8Test, AifcPCM16Test, AifcPCM16Test, AifcPCM24Test, 350 AifcPCM32Test, AifcULAWTest, 351 AifcMiscTest, AIFCLowLevelTest) 67 352 68 353 if __name__ == "__main__": 69 unittest.main()354 test_main()
Note:
See TracChangeset
for help on using the changeset viewer.