Ezio Melotti | 48d578c | 2012-03-12 23:57:18 +0200 | [diff] [blame] | 1 | from test.support import findfile, run_unittest, TESTFN, unlink |
R. David Murray | b507d2e | 2009-04-29 15:34:32 +0000 | [diff] [blame] | 2 | import unittest |
R. David Murray | eb01a6c | 2009-04-29 20:40:42 +0000 | [diff] [blame] | 3 | import os |
Sandro Tosi | bdd5354 | 2012-01-01 18:04:37 +0100 | [diff] [blame] | 4 | import io |
Ezio Melotti | 8576953 | 2012-03-12 23:52:40 +0200 | [diff] [blame] | 5 | import struct |
R David Murray | 4d35e75 | 2013-07-25 16:12:01 -0400 | [diff] [blame] | 6 | import pickle |
R. David Murray | b507d2e | 2009-04-29 15:34:32 +0000 | [diff] [blame] | 7 | |
| 8 | import aifc |
| 9 | |
| 10 | |
| 11 | class AIFCTest(unittest.TestCase): |
| 12 | |
| 13 | def setUp(self): |
R. David Murray | eb01a6c | 2009-04-29 20:40:42 +0000 | [diff] [blame] | 14 | self.f = self.fout = None |
R. David Murray | b507d2e | 2009-04-29 15:34:32 +0000 | [diff] [blame] | 15 | self.sndfilepath = findfile('Sine-1000Hz-300ms.aif') |
| 16 | |
R. David Murray | eb01a6c | 2009-04-29 20:40:42 +0000 | [diff] [blame] | 17 | def tearDown(self): |
| 18 | if self.f is not None: |
| 19 | self.f.close() |
| 20 | if self.fout is not None: |
| 21 | try: |
| 22 | self.fout.close() |
| 23 | except (aifc.Error, AttributeError): |
| 24 | pass |
Ezio Melotti | 8576953 | 2012-03-12 23:52:40 +0200 | [diff] [blame] | 25 | unlink(TESTFN) |
| 26 | unlink(TESTFN + '.aiff') |
R. David Murray | eb01a6c | 2009-04-29 20:40:42 +0000 | [diff] [blame] | 27 | |
R. David Murray | b507d2e | 2009-04-29 15:34:32 +0000 | [diff] [blame] | 28 | def test_skipunknown(self): |
| 29 | #Issue 2245 |
| 30 | #This file contains chunk types aifc doesn't recognize. |
Benjamin Peterson | 25c95f1 | 2009-05-08 20:42:26 +0000 | [diff] [blame] | 31 | self.f = aifc.open(self.sndfilepath) |
R. David Murray | b507d2e | 2009-04-29 15:34:32 +0000 | [diff] [blame] | 32 | |
| 33 | def test_params(self): |
R. David Murray | eb01a6c | 2009-04-29 20:40:42 +0000 | [diff] [blame] | 34 | f = self.f = aifc.open(self.sndfilepath) |
R David Murray | 4d35e75 | 2013-07-25 16:12:01 -0400 | [diff] [blame] | 35 | params = f.getparams() |
Ezio Melotti | 8576953 | 2012-03-12 23:52:40 +0200 | [diff] [blame] | 36 | self.assertEqual(f.getfp().name, self.sndfilepath) |
R. David Murray | b507d2e | 2009-04-29 15:34:32 +0000 | [diff] [blame] | 37 | self.assertEqual(f.getnchannels(), 2) |
| 38 | self.assertEqual(f.getsampwidth(), 2) |
| 39 | self.assertEqual(f.getframerate(), 48000) |
| 40 | self.assertEqual(f.getnframes(), 14400) |
R. David Murray | b507d2e | 2009-04-29 15:34:32 +0000 | [diff] [blame] | 41 | self.assertEqual(f.getcomptype(), b'NONE') |
| 42 | self.assertEqual(f.getcompname(), b'not compressed') |
| 43 | self.assertEqual( |
| 44 | f.getparams(), |
| 45 | (2, 2, 48000, 14400, b'NONE', b'not compressed'), |
| 46 | ) |
R. David Murray | b507d2e | 2009-04-29 15:34:32 +0000 | [diff] [blame] | 47 | |
R David Murray | 4d35e75 | 2013-07-25 16:12:01 -0400 | [diff] [blame] | 48 | params = f.getparams() |
| 49 | self.assertEqual(params.nchannels, 2) |
| 50 | self.assertEqual(params.sampwidth, 2) |
| 51 | self.assertEqual(params.framerate, 48000) |
| 52 | self.assertEqual(params.nframes, 14400) |
| 53 | self.assertEqual(params.comptype, b'NONE') |
| 54 | self.assertEqual(params.compname, b'not compressed') |
| 55 | |
| 56 | def test_params_added(self): |
| 57 | f = self.f = aifc.open(TESTFN, 'wb') |
| 58 | f.aiff() |
| 59 | f.setparams((1, 1, 1, 1, b'NONE', b'')) |
| 60 | f.close() |
| 61 | |
| 62 | f = self.f = aifc.open(TESTFN, 'rb') |
| 63 | params = f.getparams() |
| 64 | self.assertEqual(params.nchannels, f.getnchannels()) |
| 65 | self.assertEqual(params.sampwidth, f.getsampwidth()) |
| 66 | self.assertEqual(params.framerate, f.getframerate()) |
| 67 | self.assertEqual(params.nframes, f.getnframes()) |
| 68 | self.assertEqual(params.comptype, f.getcomptype()) |
| 69 | self.assertEqual(params.compname, f.getcompname()) |
| 70 | |
| 71 | def test_getparams_picklable(self): |
| 72 | self.f = aifc.open(self.sndfilepath) |
| 73 | params = self.f.getparams() |
| 74 | dump = pickle.dumps(params) |
| 75 | self.assertEqual(pickle.loads(dump), params) |
| 76 | self.f.close() |
| 77 | |
Serhiy Storchaka | 44c66c7 | 2012-12-29 22:54:49 +0200 | [diff] [blame] | 78 | def test_context_manager(self): |
| 79 | with open(self.sndfilepath, 'rb') as testfile: |
| 80 | with aifc.open(testfile) as f: |
| 81 | pass |
| 82 | self.assertEqual(testfile.closed, True) |
| 83 | with open(TESTFN, 'wb') as testfile: |
| 84 | with self.assertRaises(aifc.Error): |
| 85 | with aifc.open(testfile, 'wb') as fout: |
| 86 | pass |
| 87 | self.assertEqual(testfile.closed, True) |
| 88 | fout.close() # do nothing |
| 89 | |
R. David Murray | b507d2e | 2009-04-29 15:34:32 +0000 | [diff] [blame] | 90 | def test_read(self): |
R. David Murray | eb01a6c | 2009-04-29 20:40:42 +0000 | [diff] [blame] | 91 | f = self.f = aifc.open(self.sndfilepath) |
Ezio Melotti | 8576953 | 2012-03-12 23:52:40 +0200 | [diff] [blame] | 92 | self.assertEqual(f.readframes(0), b'') |
R. David Murray | b507d2e | 2009-04-29 15:34:32 +0000 | [diff] [blame] | 93 | self.assertEqual(f.tell(), 0) |
| 94 | self.assertEqual(f.readframes(2), b'\x00\x00\x00\x00\x0b\xd4\x0b\xd4') |
| 95 | f.rewind() |
| 96 | pos0 = f.tell() |
| 97 | self.assertEqual(pos0, 0) |
| 98 | self.assertEqual(f.readframes(2), b'\x00\x00\x00\x00\x0b\xd4\x0b\xd4') |
| 99 | pos2 = f.tell() |
| 100 | self.assertEqual(pos2, 2) |
| 101 | self.assertEqual(f.readframes(2), b'\x17t\x17t"\xad"\xad') |
| 102 | f.setpos(pos2) |
| 103 | self.assertEqual(f.readframes(2), b'\x17t\x17t"\xad"\xad') |
| 104 | f.setpos(pos0) |
| 105 | self.assertEqual(f.readframes(2), b'\x00\x00\x00\x00\x0b\xd4\x0b\xd4') |
Ezio Melotti | 8576953 | 2012-03-12 23:52:40 +0200 | [diff] [blame] | 106 | with self.assertRaises(aifc.Error): |
| 107 | f.setpos(-1) |
| 108 | with self.assertRaises(aifc.Error): |
| 109 | f.setpos(f.getnframes() + 1) |
R. David Murray | b507d2e | 2009-04-29 15:34:32 +0000 | [diff] [blame] | 110 | |
R. David Murray | eb01a6c | 2009-04-29 20:40:42 +0000 | [diff] [blame] | 111 | def test_write(self): |
| 112 | f = self.f = aifc.open(self.sndfilepath) |
| 113 | fout = self.fout = aifc.open(TESTFN, 'wb') |
| 114 | fout.aifc() |
| 115 | fout.setparams(f.getparams()) |
| 116 | for frame in range(f.getnframes()): |
| 117 | fout.writeframes(f.readframes(1)) |
| 118 | fout.close() |
| 119 | fout = self.fout = aifc.open(TESTFN, 'rb') |
| 120 | f.rewind() |
| 121 | self.assertEqual(f.getparams(), fout.getparams()) |
| 122 | self.assertEqual(f.readframes(5), fout.readframes(5)) |
| 123 | |
| 124 | def test_compress(self): |
| 125 | f = self.f = aifc.open(self.sndfilepath) |
| 126 | fout = self.fout = aifc.open(TESTFN, 'wb') |
| 127 | fout.aifc() |
| 128 | fout.setnchannels(f.getnchannels()) |
| 129 | fout.setsampwidth(f.getsampwidth()) |
| 130 | fout.setframerate(f.getframerate()) |
| 131 | fout.setcomptype(b'ULAW', b'foo') |
| 132 | for frame in range(f.getnframes()): |
| 133 | fout.writeframes(f.readframes(1)) |
| 134 | fout.close() |
| 135 | self.assertLess( |
| 136 | os.stat(TESTFN).st_size, |
| 137 | os.stat(self.sndfilepath).st_size*0.75, |
| 138 | ) |
| 139 | fout = self.fout = aifc.open(TESTFN, 'rb') |
| 140 | f.rewind() |
| 141 | self.assertEqual(f.getparams()[0:3], fout.getparams()[0:3]) |
| 142 | self.assertEqual(fout.getcomptype(), b'ULAW') |
| 143 | self.assertEqual(fout.getcompname(), b'foo') |
R. David Murray | b507d2e | 2009-04-29 15:34:32 +0000 | [diff] [blame] | 144 | |
R. David Murray | 9935274 | 2009-05-07 18:24:38 +0000 | [diff] [blame] | 145 | def test_close(self): |
| 146 | class Wrapfile(object): |
| 147 | def __init__(self, file): |
| 148 | self.file = open(file, 'rb') |
| 149 | self.closed = False |
| 150 | def close(self): |
| 151 | self.file.close() |
| 152 | self.closed = True |
| 153 | def __getattr__(self, attr): return getattr(self.file, attr) |
| 154 | testfile = Wrapfile(self.sndfilepath) |
| 155 | f = self.f = aifc.open(testfile) |
| 156 | self.assertEqual(testfile.closed, False) |
| 157 | f.close() |
| 158 | self.assertEqual(testfile.closed, True) |
Serhiy Storchaka | 051722d | 2012-12-29 22:30:56 +0200 | [diff] [blame] | 159 | testfile = open(TESTFN, 'wb') |
| 160 | fout = aifc.open(testfile, 'wb') |
| 161 | self.assertFalse(testfile.closed) |
| 162 | with self.assertRaises(aifc.Error): |
| 163 | fout.close() |
| 164 | self.assertTrue(testfile.closed) |
| 165 | fout.close() # do nothing |
R. David Murray | 9935274 | 2009-05-07 18:24:38 +0000 | [diff] [blame] | 166 | |
Sandro Tosi | bdd5354 | 2012-01-01 18:04:37 +0100 | [diff] [blame] | 167 | def test_write_header_comptype_sampwidth(self): |
| 168 | for comptype in (b'ULAW', b'ulaw', b'ALAW', b'alaw', b'G722'): |
Ezio Melotti | 8576953 | 2012-03-12 23:52:40 +0200 | [diff] [blame] | 169 | fout = aifc.open(io.BytesIO(), 'wb') |
Sandro Tosi | bdd5354 | 2012-01-01 18:04:37 +0100 | [diff] [blame] | 170 | fout.setnchannels(1) |
| 171 | fout.setframerate(1) |
| 172 | fout.setcomptype(comptype, b'') |
| 173 | fout.close() |
| 174 | self.assertEqual(fout.getsampwidth(), 2) |
| 175 | fout.initfp(None) |
| 176 | |
Sandro Tosi | 70efbef | 2012-01-01 22:53:08 +0100 | [diff] [blame] | 177 | def test_write_markers_values(self): |
Ezio Melotti | 8576953 | 2012-03-12 23:52:40 +0200 | [diff] [blame] | 178 | fout = aifc.open(io.BytesIO(), 'wb') |
Sandro Tosi | 70efbef | 2012-01-01 22:53:08 +0100 | [diff] [blame] | 179 | self.assertEqual(fout.getmarkers(), None) |
| 180 | fout.setmark(1, 0, b'foo1') |
| 181 | fout.setmark(1, 1, b'foo2') |
| 182 | self.assertEqual(fout.getmark(1), (1, 1, b'foo2')) |
| 183 | self.assertEqual(fout.getmarkers(), [(1, 1, b'foo2')]) |
| 184 | fout.initfp(None) |
| 185 | |
| 186 | def test_read_markers(self): |
| 187 | fout = self.fout = aifc.open(TESTFN, 'wb') |
| 188 | fout.aiff() |
| 189 | fout.setparams((1, 1, 1, 1, b'NONE', b'')) |
| 190 | fout.setmark(1, 0, b'odd') |
| 191 | fout.setmark(2, 0, b'even') |
| 192 | fout.writeframes(b'\x00') |
| 193 | fout.close() |
| 194 | f = self.f = aifc.open(TESTFN, 'rb') |
| 195 | self.assertEqual(f.getmarkers(), [(1, 0, b'odd'), (2, 0, b'even')]) |
| 196 | self.assertEqual(f.getmark(1), (1, 0, b'odd')) |
| 197 | self.assertEqual(f.getmark(2), (2, 0, b'even')) |
| 198 | self.assertRaises(aifc.Error, f.getmark, 3) |
| 199 | |
R. David Murray | b507d2e | 2009-04-29 15:34:32 +0000 | [diff] [blame] | 200 | |
Antoine Pitrou | 03757ec | 2012-01-17 17:13:04 +0100 | [diff] [blame] | 201 | class AIFCLowLevelTest(unittest.TestCase): |
| 202 | |
| 203 | def test_read_written(self): |
| 204 | def read_written(self, what): |
| 205 | f = io.BytesIO() |
| 206 | getattr(aifc, '_write_' + what)(f, x) |
| 207 | f.seek(0) |
| 208 | return getattr(aifc, '_read_' + what)(f) |
| 209 | for x in (-1, 0, 0.1, 1): |
| 210 | self.assertEqual(read_written(x, 'float'), x) |
| 211 | for x in (float('NaN'), float('Inf')): |
| 212 | self.assertEqual(read_written(x, 'float'), aifc._HUGE_VAL) |
| 213 | for x in (b'', b'foo', b'a' * 255): |
| 214 | self.assertEqual(read_written(x, 'string'), x) |
| 215 | for x in (-0x7FFFFFFF, -1, 0, 1, 0x7FFFFFFF): |
| 216 | self.assertEqual(read_written(x, 'long'), x) |
| 217 | for x in (0, 1, 0xFFFFFFFF): |
| 218 | self.assertEqual(read_written(x, 'ulong'), x) |
| 219 | for x in (-0x7FFF, -1, 0, 1, 0x7FFF): |
| 220 | self.assertEqual(read_written(x, 'short'), x) |
| 221 | for x in (0, 1, 0xFFFF): |
| 222 | self.assertEqual(read_written(x, 'ushort'), x) |
| 223 | |
| 224 | def test_read_raises(self): |
| 225 | f = io.BytesIO(b'\x00') |
| 226 | self.assertRaises(EOFError, aifc._read_ulong, f) |
| 227 | self.assertRaises(EOFError, aifc._read_long, f) |
| 228 | self.assertRaises(EOFError, aifc._read_ushort, f) |
| 229 | self.assertRaises(EOFError, aifc._read_short, f) |
| 230 | |
| 231 | def test_write_long_string_raises(self): |
| 232 | f = io.BytesIO() |
| 233 | with self.assertRaises(ValueError): |
| 234 | aifc._write_string(f, b'too long' * 255) |
| 235 | |
Ezio Melotti | 8576953 | 2012-03-12 23:52:40 +0200 | [diff] [blame] | 236 | def test_wrong_open_mode(self): |
| 237 | with self.assertRaises(aifc.Error): |
| 238 | aifc.open(TESTFN, 'wrong_mode') |
| 239 | |
| 240 | def test_read_wrong_form(self): |
| 241 | b1 = io.BytesIO(b'WRNG' + struct.pack('>L', 0)) |
| 242 | b2 = io.BytesIO(b'FORM' + struct.pack('>L', 4) + b'WRNG') |
| 243 | self.assertRaises(aifc.Error, aifc.open, b1) |
| 244 | self.assertRaises(aifc.Error, aifc.open, b2) |
| 245 | |
| 246 | def test_read_no_comm_chunk(self): |
| 247 | b = io.BytesIO(b'FORM' + struct.pack('>L', 4) + b'AIFF') |
| 248 | self.assertRaises(aifc.Error, aifc.open, b) |
| 249 | |
| 250 | def test_read_wrong_compression_type(self): |
| 251 | b = b'FORM' + struct.pack('>L', 4) + b'AIFC' |
| 252 | b += b'COMM' + struct.pack('>LhlhhLL', 23, 0, 0, 0, 0, 0, 0) |
| 253 | b += b'WRNG' + struct.pack('B', 0) |
| 254 | self.assertRaises(aifc.Error, aifc.open, io.BytesIO(b)) |
| 255 | |
| 256 | def test_read_wrong_marks(self): |
| 257 | b = b'FORM' + struct.pack('>L', 4) + b'AIFF' |
| 258 | b += b'COMM' + struct.pack('>LhlhhLL', 18, 0, 0, 0, 0, 0, 0) |
| 259 | b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8 |
| 260 | b += b'MARK' + struct.pack('>LhB', 3, 1, 1) |
Ezio Melotti | 48d578c | 2012-03-12 23:57:18 +0200 | [diff] [blame] | 261 | with self.assertWarns(UserWarning): |
Ezio Melotti | 8576953 | 2012-03-12 23:52:40 +0200 | [diff] [blame] | 262 | f = aifc.open(io.BytesIO(b)) |
Ezio Melotti | 8576953 | 2012-03-12 23:52:40 +0200 | [diff] [blame] | 263 | self.assertEqual(f.getmarkers(), None) |
| 264 | |
| 265 | def test_read_comm_kludge_compname_even(self): |
| 266 | b = b'FORM' + struct.pack('>L', 4) + b'AIFC' |
| 267 | b += b'COMM' + struct.pack('>LhlhhLL', 18, 0, 0, 0, 0, 0, 0) |
| 268 | b += b'NONE' + struct.pack('B', 4) + b'even' + b'\x00' |
| 269 | b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8 |
Ezio Melotti | 48d578c | 2012-03-12 23:57:18 +0200 | [diff] [blame] | 270 | with self.assertWarns(UserWarning): |
Ezio Melotti | 8576953 | 2012-03-12 23:52:40 +0200 | [diff] [blame] | 271 | f = aifc.open(io.BytesIO(b)) |
Ezio Melotti | 8576953 | 2012-03-12 23:52:40 +0200 | [diff] [blame] | 272 | self.assertEqual(f.getcompname(), b'even') |
| 273 | |
| 274 | def test_read_comm_kludge_compname_odd(self): |
| 275 | b = b'FORM' + struct.pack('>L', 4) + b'AIFC' |
| 276 | b += b'COMM' + struct.pack('>LhlhhLL', 18, 0, 0, 0, 0, 0, 0) |
| 277 | b += b'NONE' + struct.pack('B', 3) + b'odd' |
| 278 | b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8 |
Ezio Melotti | 48d578c | 2012-03-12 23:57:18 +0200 | [diff] [blame] | 279 | with self.assertWarns(UserWarning): |
Ezio Melotti | 8576953 | 2012-03-12 23:52:40 +0200 | [diff] [blame] | 280 | f = aifc.open(io.BytesIO(b)) |
Ezio Melotti | 8576953 | 2012-03-12 23:52:40 +0200 | [diff] [blame] | 281 | self.assertEqual(f.getcompname(), b'odd') |
| 282 | |
| 283 | def test_write_params_raises(self): |
| 284 | fout = aifc.open(io.BytesIO(), 'wb') |
| 285 | wrong_params = (0, 0, 0, 0, b'WRNG', '') |
| 286 | self.assertRaises(aifc.Error, fout.setparams, wrong_params) |
| 287 | self.assertRaises(aifc.Error, fout.getparams) |
| 288 | self.assertRaises(aifc.Error, fout.setnchannels, 0) |
| 289 | self.assertRaises(aifc.Error, fout.getnchannels) |
| 290 | self.assertRaises(aifc.Error, fout.setsampwidth, 0) |
| 291 | self.assertRaises(aifc.Error, fout.getsampwidth) |
| 292 | self.assertRaises(aifc.Error, fout.setframerate, 0) |
| 293 | self.assertRaises(aifc.Error, fout.getframerate) |
| 294 | self.assertRaises(aifc.Error, fout.setcomptype, b'WRNG', '') |
| 295 | fout.aiff() |
| 296 | fout.setnchannels(1) |
| 297 | fout.setsampwidth(1) |
| 298 | fout.setframerate(1) |
| 299 | fout.setnframes(1) |
| 300 | fout.writeframes(b'\x00') |
| 301 | self.assertRaises(aifc.Error, fout.setparams, (1, 1, 1, 1, 1, 1)) |
| 302 | self.assertRaises(aifc.Error, fout.setnchannels, 1) |
| 303 | self.assertRaises(aifc.Error, fout.setsampwidth, 1) |
| 304 | self.assertRaises(aifc.Error, fout.setframerate, 1) |
| 305 | self.assertRaises(aifc.Error, fout.setnframes, 1) |
| 306 | self.assertRaises(aifc.Error, fout.setcomptype, b'NONE', '') |
| 307 | self.assertRaises(aifc.Error, fout.aiff) |
| 308 | self.assertRaises(aifc.Error, fout.aifc) |
| 309 | |
| 310 | def test_write_params_singles(self): |
| 311 | fout = aifc.open(io.BytesIO(), 'wb') |
| 312 | fout.aifc() |
| 313 | fout.setnchannels(1) |
| 314 | fout.setsampwidth(2) |
| 315 | fout.setframerate(3) |
| 316 | fout.setnframes(4) |
| 317 | fout.setcomptype(b'NONE', b'name') |
| 318 | self.assertEqual(fout.getnchannels(), 1) |
| 319 | self.assertEqual(fout.getsampwidth(), 2) |
| 320 | self.assertEqual(fout.getframerate(), 3) |
| 321 | self.assertEqual(fout.getnframes(), 0) |
| 322 | self.assertEqual(fout.tell(), 0) |
| 323 | self.assertEqual(fout.getcomptype(), b'NONE') |
| 324 | self.assertEqual(fout.getcompname(), b'name') |
| 325 | fout.writeframes(b'\x00' * 4 * fout.getsampwidth() * fout.getnchannels()) |
| 326 | self.assertEqual(fout.getnframes(), 4) |
| 327 | self.assertEqual(fout.tell(), 4) |
| 328 | |
| 329 | def test_write_params_bunch(self): |
| 330 | fout = aifc.open(io.BytesIO(), 'wb') |
| 331 | fout.aifc() |
| 332 | p = (1, 2, 3, 4, b'NONE', b'name') |
| 333 | fout.setparams(p) |
| 334 | self.assertEqual(fout.getparams(), p) |
| 335 | fout.initfp(None) |
| 336 | |
| 337 | def test_write_header_raises(self): |
| 338 | fout = aifc.open(io.BytesIO(), 'wb') |
| 339 | self.assertRaises(aifc.Error, fout.close) |
Serhiy Storchaka | 051722d | 2012-12-29 22:30:56 +0200 | [diff] [blame] | 340 | fout = aifc.open(io.BytesIO(), 'wb') |
Ezio Melotti | 8576953 | 2012-03-12 23:52:40 +0200 | [diff] [blame] | 341 | fout.setnchannels(1) |
| 342 | self.assertRaises(aifc.Error, fout.close) |
Serhiy Storchaka | 051722d | 2012-12-29 22:30:56 +0200 | [diff] [blame] | 343 | fout = aifc.open(io.BytesIO(), 'wb') |
| 344 | fout.setnchannels(1) |
Ezio Melotti | 8576953 | 2012-03-12 23:52:40 +0200 | [diff] [blame] | 345 | fout.setsampwidth(1) |
| 346 | self.assertRaises(aifc.Error, fout.close) |
Ezio Melotti | 8576953 | 2012-03-12 23:52:40 +0200 | [diff] [blame] | 347 | |
| 348 | def test_write_header_comptype_raises(self): |
| 349 | for comptype in (b'ULAW', b'ulaw', b'ALAW', b'alaw', b'G722'): |
| 350 | fout = aifc.open(io.BytesIO(), 'wb') |
| 351 | fout.setsampwidth(1) |
| 352 | fout.setcomptype(comptype, b'') |
| 353 | self.assertRaises(aifc.Error, fout.close) |
| 354 | fout.initfp(None) |
| 355 | |
| 356 | def test_write_markers_raises(self): |
| 357 | fout = aifc.open(io.BytesIO(), 'wb') |
| 358 | self.assertRaises(aifc.Error, fout.setmark, 0, 0, b'') |
| 359 | self.assertRaises(aifc.Error, fout.setmark, 1, -1, b'') |
| 360 | self.assertRaises(aifc.Error, fout.setmark, 1, 0, None) |
| 361 | self.assertRaises(aifc.Error, fout.getmark, 1) |
| 362 | fout.initfp(None) |
| 363 | |
| 364 | def test_write_aiff_by_extension(self): |
| 365 | sampwidth = 2 |
Ezio Melotti | 4b843a7 | 2013-03-01 21:10:26 +0200 | [diff] [blame] | 366 | filename = TESTFN + '.aiff' |
| 367 | fout = self.fout = aifc.open(filename, 'wb') |
| 368 | self.addCleanup(unlink, filename) |
Ezio Melotti | 8576953 | 2012-03-12 23:52:40 +0200 | [diff] [blame] | 369 | fout.setparams((1, sampwidth, 1, 1, b'ULAW', b'')) |
| 370 | frames = b'\x00' * fout.getnchannels() * sampwidth |
| 371 | fout.writeframes(frames) |
| 372 | fout.close() |
Ezio Melotti | 4b843a7 | 2013-03-01 21:10:26 +0200 | [diff] [blame] | 373 | f = self.f = aifc.open(filename, 'rb') |
Ezio Melotti | 8576953 | 2012-03-12 23:52:40 +0200 | [diff] [blame] | 374 | self.assertEqual(f.getcomptype(), b'NONE') |
| 375 | f.close() |
| 376 | |
Antoine Pitrou | 03757ec | 2012-01-17 17:13:04 +0100 | [diff] [blame] | 377 | |
R. David Murray | b507d2e | 2009-04-29 15:34:32 +0000 | [diff] [blame] | 378 | def test_main(): |
| 379 | run_unittest(AIFCTest) |
Antoine Pitrou | 03757ec | 2012-01-17 17:13:04 +0100 | [diff] [blame] | 380 | run_unittest(AIFCLowLevelTest) |
R. David Murray | b507d2e | 2009-04-29 15:34:32 +0000 | [diff] [blame] | 381 | |
| 382 | |
| 383 | if __name__ == "__main__": |
| 384 | unittest.main() |