ยปCore Development>Code coverage>Lib/test/test_aifc.py

Python code coverage for Lib/test/test_aifc.py

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