1 from test.test_support import findfile, run_unittest, TESTFN 2 import unittest 3 import os 4 import io 5 6 import aifc 7 8 9 class AIFCTest(unittest.TestCase): 10 11 def setUp(self): 12 self.f = self.fout = None 13 self.sndfilepath = findfile('Sine-1000Hz-300ms.aif') 14 15 def tearDown(self): 16 if self.f is not None: 17 self.f.close() 18 if self.fout is not None: 19 try: 20 self.fout.close() 21 except (aifc.Error, AttributeError): 22 pass 23 try: 24 os.remove(TESTFN) 25 except OSError: 26 pass 27 28 def test_skipunknown(self): 29 #Issue 2245 30 #This file contains chunk types aifc doesn't recognize. 31 self.f = aifc.open(self.sndfilepath) 32 33 def test_params(self): 34 f = self.f = aifc.open(self.sndfilepath) 35 self.assertEqual(f.getnchannels(), 2) 36 self.assertEqual(f.getsampwidth(), 2) 37 self.assertEqual(f.getframerate(), 48000) 38 self.assertEqual(f.getnframes(), 14400) 39 self.assertEqual(f.getcomptype(), 'NONE') 40 self.assertEqual(f.getcompname(), 'not compressed') 41 self.assertEqual(f.getparams(), (2, 2, 48000, 14400, 'NONE', 'not compressed')) 42 43 def test_read(self): 44 f = self.f = aifc.open(self.sndfilepath) 45 self.assertEqual(f.tell(), 0) 46 self.assertEqual(f.readframes(2), '\x00\x00\x00\x00\x0b\xd4\x0b\xd4') 47 f.rewind() 48 pos0 = f.tell() 49 self.assertEqual(pos0, 0) 50 self.assertEqual(f.readframes(2), '\x00\x00\x00\x00\x0b\xd4\x0b\xd4') 51 pos2 = f.tell() 52 self.assertEqual(pos2, 2) 53 self.assertEqual(f.readframes(2), '\x17t\x17t"\xad"\xad') 54 f.setpos(pos2) 55 self.assertEqual(f.readframes(2), '\x17t\x17t"\xad"\xad') 56 f.setpos(pos0) 57 self.assertEqual(f.readframes(2), '\x00\x00\x00\x00\x0b\xd4\x0b\xd4') 58 59 def test_write(self): 60 f = self.f = aifc.open(self.sndfilepath) 61 fout = self.fout = aifc.open(TESTFN, 'wb') 62 fout.aifc() 63 fout.setparams(f.getparams()) 64 for frame in range(f.getnframes()): 65 fout.writeframes(f.readframes(1)) 66 fout.close() 67 fout = self.fout = aifc.open(TESTFN, 'rb') 68 f.rewind() 69 self.assertEqual(f.getparams(), fout.getparams()) 70 self.assertEqual(f.readframes(5), fout.readframes(5)) 71 72 def test_compress(self): 73 f = self.f = aifc.open(self.sndfilepath) 74 fout = self.fout = aifc.open(TESTFN, 'wb') 75 fout.aifc() 76 fout.setnchannels(f.getnchannels()) 77 fout.setsampwidth(f.getsampwidth()) 78 fout.setframerate(f.getframerate()) 79 fout.setcomptype('ULAW', 'foo') 80 for frame in range(f.getnframes()): 81 fout.writeframes(f.readframes(1)) 82 fout.close() 83 self.assertLess( 84 os.stat(TESTFN).st_size, 85 os.stat(self.sndfilepath).st_size*0.75, 86 ) 87 fout = self.fout = aifc.open(TESTFN, 'rb') 88 f.rewind() 89 self.assertEqual(f.getparams()[0:3], fout.getparams()[0:3]) 90 self.assertEqual(fout.getcomptype(), 'ULAW') 91 self.assertEqual(fout.getcompname(), 'foo') 92 # XXX: this test fails, not sure if it should succeed or not 93 # self.assertEqual(f.readframes(5), fout.readframes(5)) 94 95 def test_close(self): 96 class Wrapfile(object): 97 def __init__(self, file): 98 self.file = open(file, 'rb') 99 self.closed = False 100 def close(self): 101 self.file.close() 102 self.closed = True 103 def __getattr__(self, attr): return getattr(self.file, attr) 104 testfile = Wrapfile(self.sndfilepath) 105 f = self.f = aifc.open(testfile) 106 self.assertEqual(testfile.closed, False) 107 f.close() 108 self.assertEqual(testfile.closed, True) 109 testfile = open(TESTFN, 'wb') 110 fout = aifc.open(testfile, 'wb') 111 self.assertFalse(testfile.closed) 112 with self.assertRaises(aifc.Error): 113 fout.close() 114 self.assertTrue(testfile.closed) 115 fout.close() # do nothing 116 117 118 class AIFCLowLevelTest(unittest.TestCase): 119 120 def test_read_written(self): 121 def read_written(self, what): 122 f = io.BytesIO() 123 getattr(aifc, '_write_' + what)(f, x) 124 f.seek(0) 125 return getattr(aifc, '_read_' + what)(f) 126 for x in (-1, 0, 0.1, 1): 127 self.assertEqual(read_written(x, 'float'), x) 128 for x in (float('NaN'), float('Inf')): 129 self.assertEqual(read_written(x, 'float'), aifc._HUGE_VAL) 130 for x in (b'', b'foo', b'a' * 255): 131 self.assertEqual(read_written(x, 'string'), x) 132 for x in (-0x7FFFFFFF, -1, 0, 1, 0x7FFFFFFF): 133 self.assertEqual(read_written(x, 'long'), x) 134 for x in (0, 1, 0xFFFFFFFF): 135 self.assertEqual(read_written(x, 'ulong'), x) 136 for x in (-0x7FFF, -1, 0, 1, 0x7FFF): 137 self.assertEqual(read_written(x, 'short'), x) 138 for x in (0, 1, 0xFFFF): 139 self.assertEqual(read_written(x, 'ushort'), x) 140 141 def test_read_raises(self): 142 f = io.BytesIO(b'\x00') 143 self.assertRaises(EOFError, aifc._read_ulong, f) 144 self.assertRaises(EOFError, aifc._read_long, f) 145 self.assertRaises(EOFError, aifc._read_ushort, f) 146 self.assertRaises(EOFError, aifc._read_short, f) 147 148 def test_write_long_string_raises(self): 149 f = io.BytesIO() 150 with self.assertRaises(ValueError): 151 aifc._write_string(f, b'too long' * 255) 152 153 154 def test_main(): 155 run_unittest(AIFCTest) 156 run_unittest(AIFCLowLevelTest) 157 158 159 if __name__ == "__main__": 160 unittest.main() 161