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

Python code coverage for Lib/test/audiotests.py

#countcontent
1n/afrom test.support import findfile, TESTFN, unlink
2n/aimport array
3n/aimport io
4n/aimport pickle
5n/a
6n/a
7n/aclass UnseekableIO(io.FileIO):
8n/a def tell(self):
9n/a raise io.UnsupportedOperation
10n/a
11n/a def seek(self, *args, **kwargs):
12n/a raise io.UnsupportedOperation
13n/a
14n/a
15n/aclass AudioTests:
16n/a close_fd = False
17n/a
18n/a def setUp(self):
19n/a self.f = self.fout = None
20n/a
21n/a def tearDown(self):
22n/a if self.f is not None:
23n/a self.f.close()
24n/a if self.fout is not None:
25n/a self.fout.close()
26n/a unlink(TESTFN)
27n/a
28n/a def check_params(self, f, nchannels, sampwidth, framerate, nframes,
29n/a comptype, compname):
30n/a self.assertEqual(f.getnchannels(), nchannels)
31n/a self.assertEqual(f.getsampwidth(), sampwidth)
32n/a self.assertEqual(f.getframerate(), framerate)
33n/a self.assertEqual(f.getnframes(), nframes)
34n/a self.assertEqual(f.getcomptype(), comptype)
35n/a self.assertEqual(f.getcompname(), compname)
36n/a
37n/a params = f.getparams()
38n/a self.assertEqual(params,
39n/a (nchannels, sampwidth, framerate, nframes, comptype, compname))
40n/a self.assertEqual(params.nchannels, nchannels)
41n/a self.assertEqual(params.sampwidth, sampwidth)
42n/a self.assertEqual(params.framerate, framerate)
43n/a self.assertEqual(params.nframes, nframes)
44n/a self.assertEqual(params.comptype, comptype)
45n/a self.assertEqual(params.compname, compname)
46n/a
47n/a for proto in range(pickle.HIGHEST_PROTOCOL + 1):
48n/a dump = pickle.dumps(params, proto)
49n/a self.assertEqual(pickle.loads(dump), params)
50n/a
51n/a
52n/aclass AudioWriteTests(AudioTests):
53n/a
54n/a def create_file(self, testfile):
55n/a f = self.fout = self.module.open(testfile, 'wb')
56n/a f.setnchannels(self.nchannels)
57n/a f.setsampwidth(self.sampwidth)
58n/a f.setframerate(self.framerate)
59n/a f.setcomptype(self.comptype, self.compname)
60n/a return f
61n/a
62n/a def check_file(self, testfile, nframes, frames):
63n/a with self.module.open(testfile, 'rb') as f:
64n/a self.assertEqual(f.getnchannels(), self.nchannels)
65n/a self.assertEqual(f.getsampwidth(), self.sampwidth)
66n/a self.assertEqual(f.getframerate(), self.framerate)
67n/a self.assertEqual(f.getnframes(), nframes)
68n/a self.assertEqual(f.readframes(nframes), frames)
69n/a
70n/a def test_write_params(self):
71n/a f = self.create_file(TESTFN)
72n/a f.setnframes(self.nframes)
73n/a f.writeframes(self.frames)
74n/a self.check_params(f, self.nchannels, self.sampwidth, self.framerate,
75n/a self.nframes, self.comptype, self.compname)
76n/a f.close()
77n/a
78n/a def test_write_context_manager_calls_close(self):
79n/a # Close checks for a minimum header and will raise an error
80n/a # if it is not set, so this proves that close is called.
81n/a with self.assertRaises(self.module.Error):
82n/a with self.module.open(TESTFN, 'wb'):
83n/a pass
84n/a with self.assertRaises(self.module.Error):
85n/a with open(TESTFN, 'wb') as testfile:
86n/a with self.module.open(testfile):
87n/a pass
88n/a
89n/a def test_context_manager_with_open_file(self):
90n/a with open(TESTFN, 'wb') as testfile:
91n/a with self.module.open(testfile) as f:
92n/a f.setnchannels(self.nchannels)
93n/a f.setsampwidth(self.sampwidth)
94n/a f.setframerate(self.framerate)
95n/a f.setcomptype(self.comptype, self.compname)
96n/a self.assertEqual(testfile.closed, self.close_fd)
97n/a with open(TESTFN, 'rb') as testfile:
98n/a with self.module.open(testfile) as f:
99n/a self.assertFalse(f.getfp().closed)
100n/a params = f.getparams()
101n/a self.assertEqual(params.nchannels, self.nchannels)
102n/a self.assertEqual(params.sampwidth, self.sampwidth)
103n/a self.assertEqual(params.framerate, self.framerate)
104n/a if not self.close_fd:
105n/a self.assertIsNone(f.getfp())
106n/a self.assertEqual(testfile.closed, self.close_fd)
107n/a
108n/a def test_context_manager_with_filename(self):
109n/a # If the file doesn't get closed, this test won't fail, but it will
110n/a # produce a resource leak warning.
111n/a with self.module.open(TESTFN, 'wb') as f:
112n/a f.setnchannels(self.nchannels)
113n/a f.setsampwidth(self.sampwidth)
114n/a f.setframerate(self.framerate)
115n/a f.setcomptype(self.comptype, self.compname)
116n/a with self.module.open(TESTFN) as f:
117n/a self.assertFalse(f.getfp().closed)
118n/a params = f.getparams()
119n/a self.assertEqual(params.nchannels, self.nchannels)
120n/a self.assertEqual(params.sampwidth, self.sampwidth)
121n/a self.assertEqual(params.framerate, self.framerate)
122n/a if not self.close_fd:
123n/a self.assertIsNone(f.getfp())
124n/a
125n/a def test_write(self):
126n/a f = self.create_file(TESTFN)
127n/a f.setnframes(self.nframes)
128n/a f.writeframes(self.frames)
129n/a f.close()
130n/a
131n/a self.check_file(TESTFN, self.nframes, self.frames)
132n/a
133n/a def test_write_bytearray(self):
134n/a f = self.create_file(TESTFN)
135n/a f.setnframes(self.nframes)
136n/a f.writeframes(bytearray(self.frames))
137n/a f.close()
138n/a
139n/a self.check_file(TESTFN, self.nframes, self.frames)
140n/a
141n/a def test_write_array(self):
142n/a f = self.create_file(TESTFN)
143n/a f.setnframes(self.nframes)
144n/a f.writeframes(array.array('h', self.frames))
145n/a f.close()
146n/a
147n/a self.check_file(TESTFN, self.nframes, self.frames)
148n/a
149n/a def test_write_memoryview(self):
150n/a f = self.create_file(TESTFN)
151n/a f.setnframes(self.nframes)
152n/a f.writeframes(memoryview(self.frames))
153n/a f.close()
154n/a
155n/a self.check_file(TESTFN, self.nframes, self.frames)
156n/a
157n/a def test_incompleted_write(self):
158n/a with open(TESTFN, 'wb') as testfile:
159n/a testfile.write(b'ababagalamaga')
160n/a f = self.create_file(testfile)
161n/a f.setnframes(self.nframes + 1)
162n/a f.writeframes(self.frames)
163n/a f.close()
164n/a
165n/a with open(TESTFN, 'rb') as testfile:
166n/a self.assertEqual(testfile.read(13), b'ababagalamaga')
167n/a self.check_file(testfile, self.nframes, self.frames)
168n/a
169n/a def test_multiple_writes(self):
170n/a with open(TESTFN, 'wb') as testfile:
171n/a testfile.write(b'ababagalamaga')
172n/a f = self.create_file(testfile)
173n/a f.setnframes(self.nframes)
174n/a framesize = self.nchannels * self.sampwidth
175n/a f.writeframes(self.frames[:-framesize])
176n/a f.writeframes(self.frames[-framesize:])
177n/a f.close()
178n/a
179n/a with open(TESTFN, 'rb') as testfile:
180n/a self.assertEqual(testfile.read(13), b'ababagalamaga')
181n/a self.check_file(testfile, self.nframes, self.frames)
182n/a
183n/a def test_overflowed_write(self):
184n/a with open(TESTFN, 'wb') as testfile:
185n/a testfile.write(b'ababagalamaga')
186n/a f = self.create_file(testfile)
187n/a f.setnframes(self.nframes - 1)
188n/a f.writeframes(self.frames)
189n/a f.close()
190n/a
191n/a with open(TESTFN, 'rb') as testfile:
192n/a self.assertEqual(testfile.read(13), b'ababagalamaga')
193n/a self.check_file(testfile, self.nframes, self.frames)
194n/a
195n/a def test_unseekable_read(self):
196n/a with self.create_file(TESTFN) as f:
197n/a f.setnframes(self.nframes)
198n/a f.writeframes(self.frames)
199n/a
200n/a with UnseekableIO(TESTFN, 'rb') as testfile:
201n/a self.check_file(testfile, self.nframes, self.frames)
202n/a
203n/a def test_unseekable_write(self):
204n/a with UnseekableIO(TESTFN, 'wb') as testfile:
205n/a with self.create_file(testfile) as f:
206n/a f.setnframes(self.nframes)
207n/a f.writeframes(self.frames)
208n/a
209n/a self.check_file(TESTFN, self.nframes, self.frames)
210n/a
211n/a def test_unseekable_incompleted_write(self):
212n/a with UnseekableIO(TESTFN, 'wb') as testfile:
213n/a testfile.write(b'ababagalamaga')
214n/a f = self.create_file(testfile)
215n/a f.setnframes(self.nframes + 1)
216n/a try:
217n/a f.writeframes(self.frames)
218n/a except OSError:
219n/a pass
220n/a try:
221n/a f.close()
222n/a except OSError:
223n/a pass
224n/a
225n/a with open(TESTFN, 'rb') as testfile:
226n/a self.assertEqual(testfile.read(13), b'ababagalamaga')
227n/a self.check_file(testfile, self.nframes + 1, self.frames)
228n/a
229n/a def test_unseekable_overflowed_write(self):
230n/a with UnseekableIO(TESTFN, 'wb') as testfile:
231n/a testfile.write(b'ababagalamaga')
232n/a f = self.create_file(testfile)
233n/a f.setnframes(self.nframes - 1)
234n/a try:
235n/a f.writeframes(self.frames)
236n/a except OSError:
237n/a pass
238n/a try:
239n/a f.close()
240n/a except OSError:
241n/a pass
242n/a
243n/a with open(TESTFN, 'rb') as testfile:
244n/a self.assertEqual(testfile.read(13), b'ababagalamaga')
245n/a framesize = self.nchannels * self.sampwidth
246n/a self.check_file(testfile, self.nframes - 1, self.frames[:-framesize])
247n/a
248n/a
249n/aclass AudioTestsWithSourceFile(AudioTests):
250n/a
251n/a @classmethod
252n/a def setUpClass(cls):
253n/a cls.sndfilepath = findfile(cls.sndfilename, subdir='audiodata')
254n/a
255n/a def test_read_params(self):
256n/a f = self.f = self.module.open(self.sndfilepath)
257n/a #self.assertEqual(f.getfp().name, self.sndfilepath)
258n/a self.check_params(f, self.nchannels, self.sampwidth, self.framerate,
259n/a self.sndfilenframes, self.comptype, self.compname)
260n/a
261n/a def test_close(self):
262n/a with open(self.sndfilepath, 'rb') as testfile:
263n/a f = self.f = self.module.open(testfile)
264n/a self.assertFalse(testfile.closed)
265n/a f.close()
266n/a self.assertEqual(testfile.closed, self.close_fd)
267n/a with open(TESTFN, 'wb') as testfile:
268n/a fout = self.fout = self.module.open(testfile, 'wb')
269n/a self.assertFalse(testfile.closed)
270n/a with self.assertRaises(self.module.Error):
271n/a fout.close()
272n/a self.assertEqual(testfile.closed, self.close_fd)
273n/a fout.close() # do nothing
274n/a
275n/a def test_read(self):
276n/a framesize = self.nchannels * self.sampwidth
277n/a chunk1 = self.frames[:2 * framesize]
278n/a chunk2 = self.frames[2 * framesize: 4 * framesize]
279n/a f = self.f = self.module.open(self.sndfilepath)
280n/a self.assertEqual(f.readframes(0), b'')
281n/a self.assertEqual(f.tell(), 0)
282n/a self.assertEqual(f.readframes(2), chunk1)
283n/a f.rewind()
284n/a pos0 = f.tell()
285n/a self.assertEqual(pos0, 0)
286n/a self.assertEqual(f.readframes(2), chunk1)
287n/a pos2 = f.tell()
288n/a self.assertEqual(pos2, 2)
289n/a self.assertEqual(f.readframes(2), chunk2)
290n/a f.setpos(pos2)
291n/a self.assertEqual(f.readframes(2), chunk2)
292n/a f.setpos(pos0)
293n/a self.assertEqual(f.readframes(2), chunk1)
294n/a with self.assertRaises(self.module.Error):
295n/a f.setpos(-1)
296n/a with self.assertRaises(self.module.Error):
297n/a f.setpos(f.getnframes() + 1)
298n/a
299n/a def test_copy(self):
300n/a f = self.f = self.module.open(self.sndfilepath)
301n/a fout = self.fout = self.module.open(TESTFN, 'wb')
302n/a fout.setparams(f.getparams())
303n/a i = 0
304n/a n = f.getnframes()
305n/a while n > 0:
306n/a i += 1
307n/a fout.writeframes(f.readframes(i))
308n/a n -= i
309n/a fout.close()
310n/a fout = self.fout = self.module.open(TESTFN, 'rb')
311n/a f.rewind()
312n/a self.assertEqual(f.getparams(), fout.getparams())
313n/a self.assertEqual(f.readframes(f.getnframes()),
314n/a fout.readframes(fout.getnframes()))
315n/a
316n/a def test_read_not_from_start(self):
317n/a with open(TESTFN, 'wb') as testfile:
318n/a testfile.write(b'ababagalamaga')
319n/a with open(self.sndfilepath, 'rb') as f:
320n/a testfile.write(f.read())
321n/a
322n/a with open(TESTFN, 'rb') as testfile:
323n/a self.assertEqual(testfile.read(13), b'ababagalamaga')
324n/a with self.module.open(testfile, 'rb') as f:
325n/a self.assertEqual(f.getnchannels(), self.nchannels)
326n/a self.assertEqual(f.getsampwidth(), self.sampwidth)
327n/a self.assertEqual(f.getframerate(), self.framerate)
328n/a self.assertEqual(f.getnframes(), self.sndfilenframes)
329n/a self.assertEqual(f.readframes(self.nframes), self.frames)