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

Python code coverage for Lib/test/test_audioop.py

#countcontent
1n/aimport audioop
2n/aimport sys
3n/aimport unittest
4n/a
5n/adef pack(width, data):
6n/a return b''.join(v.to_bytes(width, sys.byteorder, signed=True) for v in data)
7n/a
8n/adef unpack(width, data):
9n/a return [int.from_bytes(data[i: i + width], sys.byteorder, signed=True)
10n/a for i in range(0, len(data), width)]
11n/a
12n/apacks = {w: (lambda *data, width=w: pack(width, data)) for w in (1, 2, 3, 4)}
13n/amaxvalues = {w: (1 << (8 * w - 1)) - 1 for w in (1, 2, 3, 4)}
14n/aminvalues = {w: -1 << (8 * w - 1) for w in (1, 2, 3, 4)}
15n/a
16n/adatas = {
17n/a 1: b'\x00\x12\x45\xbb\x7f\x80\xff',
18n/a 2: packs[2](0, 0x1234, 0x4567, -0x4567, 0x7fff, -0x8000, -1),
19n/a 3: packs[3](0, 0x123456, 0x456789, -0x456789, 0x7fffff, -0x800000, -1),
20n/a 4: packs[4](0, 0x12345678, 0x456789ab, -0x456789ab,
21n/a 0x7fffffff, -0x80000000, -1),
22n/a}
23n/a
24n/aINVALID_DATA = [
25n/a (b'abc', 0),
26n/a (b'abc', 2),
27n/a (b'ab', 3),
28n/a (b'abc', 4),
29n/a]
30n/a
31n/a
32n/aclass TestAudioop(unittest.TestCase):
33n/a
34n/a def test_max(self):
35n/a for w in 1, 2, 3, 4:
36n/a self.assertEqual(audioop.max(b'', w), 0)
37n/a self.assertEqual(audioop.max(bytearray(), w), 0)
38n/a self.assertEqual(audioop.max(memoryview(b''), w), 0)
39n/a p = packs[w]
40n/a self.assertEqual(audioop.max(p(5), w), 5)
41n/a self.assertEqual(audioop.max(p(5, -8, -1), w), 8)
42n/a self.assertEqual(audioop.max(p(maxvalues[w]), w), maxvalues[w])
43n/a self.assertEqual(audioop.max(p(minvalues[w]), w), -minvalues[w])
44n/a self.assertEqual(audioop.max(datas[w], w), -minvalues[w])
45n/a
46n/a def test_minmax(self):
47n/a for w in 1, 2, 3, 4:
48n/a self.assertEqual(audioop.minmax(b'', w),
49n/a (0x7fffffff, -0x80000000))
50n/a self.assertEqual(audioop.minmax(bytearray(), w),
51n/a (0x7fffffff, -0x80000000))
52n/a self.assertEqual(audioop.minmax(memoryview(b''), w),
53n/a (0x7fffffff, -0x80000000))
54n/a p = packs[w]
55n/a self.assertEqual(audioop.minmax(p(5), w), (5, 5))
56n/a self.assertEqual(audioop.minmax(p(5, -8, -1), w), (-8, 5))
57n/a self.assertEqual(audioop.minmax(p(maxvalues[w]), w),
58n/a (maxvalues[w], maxvalues[w]))
59n/a self.assertEqual(audioop.minmax(p(minvalues[w]), w),
60n/a (minvalues[w], minvalues[w]))
61n/a self.assertEqual(audioop.minmax(datas[w], w),
62n/a (minvalues[w], maxvalues[w]))
63n/a
64n/a def test_maxpp(self):
65n/a for w in 1, 2, 3, 4:
66n/a self.assertEqual(audioop.maxpp(b'', w), 0)
67n/a self.assertEqual(audioop.maxpp(bytearray(), w), 0)
68n/a self.assertEqual(audioop.maxpp(memoryview(b''), w), 0)
69n/a self.assertEqual(audioop.maxpp(packs[w](*range(100)), w), 0)
70n/a self.assertEqual(audioop.maxpp(packs[w](9, 10, 5, 5, 0, 1), w), 10)
71n/a self.assertEqual(audioop.maxpp(datas[w], w),
72n/a maxvalues[w] - minvalues[w])
73n/a
74n/a def test_avg(self):
75n/a for w in 1, 2, 3, 4:
76n/a self.assertEqual(audioop.avg(b'', w), 0)
77n/a self.assertEqual(audioop.avg(bytearray(), w), 0)
78n/a self.assertEqual(audioop.avg(memoryview(b''), w), 0)
79n/a p = packs[w]
80n/a self.assertEqual(audioop.avg(p(5), w), 5)
81n/a self .assertEqual(audioop.avg(p(5, 8), w), 6)
82n/a self.assertEqual(audioop.avg(p(5, -8), w), -2)
83n/a self.assertEqual(audioop.avg(p(maxvalues[w], maxvalues[w]), w),
84n/a maxvalues[w])
85n/a self.assertEqual(audioop.avg(p(minvalues[w], minvalues[w]), w),
86n/a minvalues[w])
87n/a self.assertEqual(audioop.avg(packs[4](0x50000000, 0x70000000), 4),
88n/a 0x60000000)
89n/a self.assertEqual(audioop.avg(packs[4](-0x50000000, -0x70000000), 4),
90n/a -0x60000000)
91n/a
92n/a def test_avgpp(self):
93n/a for w in 1, 2, 3, 4:
94n/a self.assertEqual(audioop.avgpp(b'', w), 0)
95n/a self.assertEqual(audioop.avgpp(bytearray(), w), 0)
96n/a self.assertEqual(audioop.avgpp(memoryview(b''), w), 0)
97n/a self.assertEqual(audioop.avgpp(packs[w](*range(100)), w), 0)
98n/a self.assertEqual(audioop.avgpp(packs[w](9, 10, 5, 5, 0, 1), w), 10)
99n/a self.assertEqual(audioop.avgpp(datas[1], 1), 196)
100n/a self.assertEqual(audioop.avgpp(datas[2], 2), 50534)
101n/a self.assertEqual(audioop.avgpp(datas[3], 3), 12937096)
102n/a self.assertEqual(audioop.avgpp(datas[4], 4), 3311897002)
103n/a
104n/a def test_rms(self):
105n/a for w in 1, 2, 3, 4:
106n/a self.assertEqual(audioop.rms(b'', w), 0)
107n/a self.assertEqual(audioop.rms(bytearray(), w), 0)
108n/a self.assertEqual(audioop.rms(memoryview(b''), w), 0)
109n/a p = packs[w]
110n/a self.assertEqual(audioop.rms(p(*range(100)), w), 57)
111n/a self.assertAlmostEqual(audioop.rms(p(maxvalues[w]) * 5, w),
112n/a maxvalues[w], delta=1)
113n/a self.assertAlmostEqual(audioop.rms(p(minvalues[w]) * 5, w),
114n/a -minvalues[w], delta=1)
115n/a self.assertEqual(audioop.rms(datas[1], 1), 77)
116n/a self.assertEqual(audioop.rms(datas[2], 2), 20001)
117n/a self.assertEqual(audioop.rms(datas[3], 3), 5120523)
118n/a self.assertEqual(audioop.rms(datas[4], 4), 1310854152)
119n/a
120n/a def test_cross(self):
121n/a for w in 1, 2, 3, 4:
122n/a self.assertEqual(audioop.cross(b'', w), -1)
123n/a self.assertEqual(audioop.cross(bytearray(), w), -1)
124n/a self.assertEqual(audioop.cross(memoryview(b''), w), -1)
125n/a p = packs[w]
126n/a self.assertEqual(audioop.cross(p(0, 1, 2), w), 0)
127n/a self.assertEqual(audioop.cross(p(1, 2, -3, -4), w), 1)
128n/a self.assertEqual(audioop.cross(p(-1, -2, 3, 4), w), 1)
129n/a self.assertEqual(audioop.cross(p(0, minvalues[w]), w), 1)
130n/a self.assertEqual(audioop.cross(p(minvalues[w], maxvalues[w]), w), 1)
131n/a
132n/a def test_add(self):
133n/a for w in 1, 2, 3, 4:
134n/a self.assertEqual(audioop.add(b'', b'', w), b'')
135n/a self.assertEqual(audioop.add(bytearray(), bytearray(), w), b'')
136n/a self.assertEqual(audioop.add(memoryview(b''), memoryview(b''), w), b'')
137n/a self.assertEqual(audioop.add(datas[w], b'\0' * len(datas[w]), w),
138n/a datas[w])
139n/a self.assertEqual(audioop.add(datas[1], datas[1], 1),
140n/a b'\x00\x24\x7f\x80\x7f\x80\xfe')
141n/a self.assertEqual(audioop.add(datas[2], datas[2], 2),
142n/a packs[2](0, 0x2468, 0x7fff, -0x8000, 0x7fff, -0x8000, -2))
143n/a self.assertEqual(audioop.add(datas[3], datas[3], 3),
144n/a packs[3](0, 0x2468ac, 0x7fffff, -0x800000,
145n/a 0x7fffff, -0x800000, -2))
146n/a self.assertEqual(audioop.add(datas[4], datas[4], 4),
147n/a packs[4](0, 0x2468acf0, 0x7fffffff, -0x80000000,
148n/a 0x7fffffff, -0x80000000, -2))
149n/a
150n/a def test_bias(self):
151n/a for w in 1, 2, 3, 4:
152n/a for bias in 0, 1, -1, 127, -128, 0x7fffffff, -0x80000000:
153n/a self.assertEqual(audioop.bias(b'', w, bias), b'')
154n/a self.assertEqual(audioop.bias(bytearray(), w, bias), b'')
155n/a self.assertEqual(audioop.bias(memoryview(b''), w, bias), b'')
156n/a self.assertEqual(audioop.bias(datas[1], 1, 1),
157n/a b'\x01\x13\x46\xbc\x80\x81\x00')
158n/a self.assertEqual(audioop.bias(datas[1], 1, -1),
159n/a b'\xff\x11\x44\xba\x7e\x7f\xfe')
160n/a self.assertEqual(audioop.bias(datas[1], 1, 0x7fffffff),
161n/a b'\xff\x11\x44\xba\x7e\x7f\xfe')
162n/a self.assertEqual(audioop.bias(datas[1], 1, -0x80000000),
163n/a datas[1])
164n/a self.assertEqual(audioop.bias(datas[2], 2, 1),
165n/a packs[2](1, 0x1235, 0x4568, -0x4566, -0x8000, -0x7fff, 0))
166n/a self.assertEqual(audioop.bias(datas[2], 2, -1),
167n/a packs[2](-1, 0x1233, 0x4566, -0x4568, 0x7ffe, 0x7fff, -2))
168n/a self.assertEqual(audioop.bias(datas[2], 2, 0x7fffffff),
169n/a packs[2](-1, 0x1233, 0x4566, -0x4568, 0x7ffe, 0x7fff, -2))
170n/a self.assertEqual(audioop.bias(datas[2], 2, -0x80000000),
171n/a datas[2])
172n/a self.assertEqual(audioop.bias(datas[3], 3, 1),
173n/a packs[3](1, 0x123457, 0x45678a, -0x456788,
174n/a -0x800000, -0x7fffff, 0))
175n/a self.assertEqual(audioop.bias(datas[3], 3, -1),
176n/a packs[3](-1, 0x123455, 0x456788, -0x45678a,
177n/a 0x7ffffe, 0x7fffff, -2))
178n/a self.assertEqual(audioop.bias(datas[3], 3, 0x7fffffff),
179n/a packs[3](-1, 0x123455, 0x456788, -0x45678a,
180n/a 0x7ffffe, 0x7fffff, -2))
181n/a self.assertEqual(audioop.bias(datas[3], 3, -0x80000000),
182n/a datas[3])
183n/a self.assertEqual(audioop.bias(datas[4], 4, 1),
184n/a packs[4](1, 0x12345679, 0x456789ac, -0x456789aa,
185n/a -0x80000000, -0x7fffffff, 0))
186n/a self.assertEqual(audioop.bias(datas[4], 4, -1),
187n/a packs[4](-1, 0x12345677, 0x456789aa, -0x456789ac,
188n/a 0x7ffffffe, 0x7fffffff, -2))
189n/a self.assertEqual(audioop.bias(datas[4], 4, 0x7fffffff),
190n/a packs[4](0x7fffffff, -0x6dcba989, -0x3a987656, 0x3a987654,
191n/a -2, -1, 0x7ffffffe))
192n/a self.assertEqual(audioop.bias(datas[4], 4, -0x80000000),
193n/a packs[4](-0x80000000, -0x6dcba988, -0x3a987655, 0x3a987655,
194n/a -1, 0, 0x7fffffff))
195n/a
196n/a def test_lin2lin(self):
197n/a for w in 1, 2, 3, 4:
198n/a self.assertEqual(audioop.lin2lin(datas[w], w, w), datas[w])
199n/a self.assertEqual(audioop.lin2lin(bytearray(datas[w]), w, w),
200n/a datas[w])
201n/a self.assertEqual(audioop.lin2lin(memoryview(datas[w]), w, w),
202n/a datas[w])
203n/a
204n/a self.assertEqual(audioop.lin2lin(datas[1], 1, 2),
205n/a packs[2](0, 0x1200, 0x4500, -0x4500, 0x7f00, -0x8000, -0x100))
206n/a self.assertEqual(audioop.lin2lin(datas[1], 1, 3),
207n/a packs[3](0, 0x120000, 0x450000, -0x450000,
208n/a 0x7f0000, -0x800000, -0x10000))
209n/a self.assertEqual(audioop.lin2lin(datas[1], 1, 4),
210n/a packs[4](0, 0x12000000, 0x45000000, -0x45000000,
211n/a 0x7f000000, -0x80000000, -0x1000000))
212n/a self.assertEqual(audioop.lin2lin(datas[2], 2, 1),
213n/a b'\x00\x12\x45\xba\x7f\x80\xff')
214n/a self.assertEqual(audioop.lin2lin(datas[2], 2, 3),
215n/a packs[3](0, 0x123400, 0x456700, -0x456700,
216n/a 0x7fff00, -0x800000, -0x100))
217n/a self.assertEqual(audioop.lin2lin(datas[2], 2, 4),
218n/a packs[4](0, 0x12340000, 0x45670000, -0x45670000,
219n/a 0x7fff0000, -0x80000000, -0x10000))
220n/a self.assertEqual(audioop.lin2lin(datas[3], 3, 1),
221n/a b'\x00\x12\x45\xba\x7f\x80\xff')
222n/a self.assertEqual(audioop.lin2lin(datas[3], 3, 2),
223n/a packs[2](0, 0x1234, 0x4567, -0x4568, 0x7fff, -0x8000, -1))
224n/a self.assertEqual(audioop.lin2lin(datas[3], 3, 4),
225n/a packs[4](0, 0x12345600, 0x45678900, -0x45678900,
226n/a 0x7fffff00, -0x80000000, -0x100))
227n/a self.assertEqual(audioop.lin2lin(datas[4], 4, 1),
228n/a b'\x00\x12\x45\xba\x7f\x80\xff')
229n/a self.assertEqual(audioop.lin2lin(datas[4], 4, 2),
230n/a packs[2](0, 0x1234, 0x4567, -0x4568, 0x7fff, -0x8000, -1))
231n/a self.assertEqual(audioop.lin2lin(datas[4], 4, 3),
232n/a packs[3](0, 0x123456, 0x456789, -0x45678a,
233n/a 0x7fffff, -0x800000, -1))
234n/a
235n/a def test_adpcm2lin(self):
236n/a self.assertEqual(audioop.adpcm2lin(b'\x07\x7f\x7f', 1, None),
237n/a (b'\x00\x00\x00\xff\x00\xff', (-179, 40)))
238n/a self.assertEqual(audioop.adpcm2lin(bytearray(b'\x07\x7f\x7f'), 1, None),
239n/a (b'\x00\x00\x00\xff\x00\xff', (-179, 40)))
240n/a self.assertEqual(audioop.adpcm2lin(memoryview(b'\x07\x7f\x7f'), 1, None),
241n/a (b'\x00\x00\x00\xff\x00\xff', (-179, 40)))
242n/a self.assertEqual(audioop.adpcm2lin(b'\x07\x7f\x7f', 2, None),
243n/a (packs[2](0, 0xb, 0x29, -0x16, 0x72, -0xb3), (-179, 40)))
244n/a self.assertEqual(audioop.adpcm2lin(b'\x07\x7f\x7f', 3, None),
245n/a (packs[3](0, 0xb00, 0x2900, -0x1600, 0x7200,
246n/a -0xb300), (-179, 40)))
247n/a self.assertEqual(audioop.adpcm2lin(b'\x07\x7f\x7f', 4, None),
248n/a (packs[4](0, 0xb0000, 0x290000, -0x160000, 0x720000,
249n/a -0xb30000), (-179, 40)))
250n/a
251n/a # Very cursory test
252n/a for w in 1, 2, 3, 4:
253n/a self.assertEqual(audioop.adpcm2lin(b'\0' * 5, w, None),
254n/a (b'\0' * w * 10, (0, 0)))
255n/a
256n/a def test_lin2adpcm(self):
257n/a self.assertEqual(audioop.lin2adpcm(datas[1], 1, None),
258n/a (b'\x07\x7f\x7f', (-221, 39)))
259n/a self.assertEqual(audioop.lin2adpcm(bytearray(datas[1]), 1, None),
260n/a (b'\x07\x7f\x7f', (-221, 39)))
261n/a self.assertEqual(audioop.lin2adpcm(memoryview(datas[1]), 1, None),
262n/a (b'\x07\x7f\x7f', (-221, 39)))
263n/a for w in 2, 3, 4:
264n/a self.assertEqual(audioop.lin2adpcm(datas[w], w, None),
265n/a (b'\x07\x7f\x7f', (31, 39)))
266n/a
267n/a # Very cursory test
268n/a for w in 1, 2, 3, 4:
269n/a self.assertEqual(audioop.lin2adpcm(b'\0' * w * 10, w, None),
270n/a (b'\0' * 5, (0, 0)))
271n/a
272n/a def test_invalid_adpcm_state(self):
273n/a # state must be a tuple or None, not an integer
274n/a self.assertRaises(TypeError, audioop.adpcm2lin, b'\0', 1, 555)
275n/a self.assertRaises(TypeError, audioop.lin2adpcm, b'\0', 1, 555)
276n/a # Issues #24456, #24457: index out of range
277n/a self.assertRaises(ValueError, audioop.adpcm2lin, b'\0', 1, (0, -1))
278n/a self.assertRaises(ValueError, audioop.adpcm2lin, b'\0', 1, (0, 89))
279n/a self.assertRaises(ValueError, audioop.lin2adpcm, b'\0', 1, (0, -1))
280n/a self.assertRaises(ValueError, audioop.lin2adpcm, b'\0', 1, (0, 89))
281n/a # value out of range
282n/a self.assertRaises(ValueError, audioop.adpcm2lin, b'\0', 1, (-0x8001, 0))
283n/a self.assertRaises(ValueError, audioop.adpcm2lin, b'\0', 1, (0x8000, 0))
284n/a self.assertRaises(ValueError, audioop.lin2adpcm, b'\0', 1, (-0x8001, 0))
285n/a self.assertRaises(ValueError, audioop.lin2adpcm, b'\0', 1, (0x8000, 0))
286n/a
287n/a def test_lin2alaw(self):
288n/a self.assertEqual(audioop.lin2alaw(datas[1], 1),
289n/a b'\xd5\x87\xa4\x24\xaa\x2a\x5a')
290n/a self.assertEqual(audioop.lin2alaw(bytearray(datas[1]), 1),
291n/a b'\xd5\x87\xa4\x24\xaa\x2a\x5a')
292n/a self.assertEqual(audioop.lin2alaw(memoryview(datas[1]), 1),
293n/a b'\xd5\x87\xa4\x24\xaa\x2a\x5a')
294n/a for w in 2, 3, 4:
295n/a self.assertEqual(audioop.lin2alaw(datas[w], w),
296n/a b'\xd5\x87\xa4\x24\xaa\x2a\x55')
297n/a
298n/a def test_alaw2lin(self):
299n/a encoded = b'\x00\x03\x24\x2a\x51\x54\x55\x58\x6b\x71\x7f'\
300n/a b'\x80\x83\xa4\xaa\xd1\xd4\xd5\xd8\xeb\xf1\xff'
301n/a src = [-688, -720, -2240, -4032, -9, -3, -1, -27, -244, -82, -106,
302n/a 688, 720, 2240, 4032, 9, 3, 1, 27, 244, 82, 106]
303n/a for w in 1, 2, 3, 4:
304n/a decoded = packs[w](*(x << (w * 8) >> 13 for x in src))
305n/a self.assertEqual(audioop.alaw2lin(encoded, w), decoded)
306n/a self.assertEqual(audioop.alaw2lin(bytearray(encoded), w), decoded)
307n/a self.assertEqual(audioop.alaw2lin(memoryview(encoded), w), decoded)
308n/a
309n/a encoded = bytes(range(256))
310n/a for w in 2, 3, 4:
311n/a decoded = audioop.alaw2lin(encoded, w)
312n/a self.assertEqual(audioop.lin2alaw(decoded, w), encoded)
313n/a
314n/a def test_lin2ulaw(self):
315n/a self.assertEqual(audioop.lin2ulaw(datas[1], 1),
316n/a b'\xff\xad\x8e\x0e\x80\x00\x67')
317n/a self.assertEqual(audioop.lin2ulaw(bytearray(datas[1]), 1),
318n/a b'\xff\xad\x8e\x0e\x80\x00\x67')
319n/a self.assertEqual(audioop.lin2ulaw(memoryview(datas[1]), 1),
320n/a b'\xff\xad\x8e\x0e\x80\x00\x67')
321n/a for w in 2, 3, 4:
322n/a self.assertEqual(audioop.lin2ulaw(datas[w], w),
323n/a b'\xff\xad\x8e\x0e\x80\x00\x7e')
324n/a
325n/a def test_ulaw2lin(self):
326n/a encoded = b'\x00\x0e\x28\x3f\x57\x6a\x76\x7c\x7e\x7f'\
327n/a b'\x80\x8e\xa8\xbf\xd7\xea\xf6\xfc\xfe\xff'
328n/a src = [-8031, -4447, -1471, -495, -163, -53, -18, -6, -2, 0,
329n/a 8031, 4447, 1471, 495, 163, 53, 18, 6, 2, 0]
330n/a for w in 1, 2, 3, 4:
331n/a decoded = packs[w](*(x << (w * 8) >> 14 for x in src))
332n/a self.assertEqual(audioop.ulaw2lin(encoded, w), decoded)
333n/a self.assertEqual(audioop.ulaw2lin(bytearray(encoded), w), decoded)
334n/a self.assertEqual(audioop.ulaw2lin(memoryview(encoded), w), decoded)
335n/a
336n/a # Current u-law implementation has two codes fo 0: 0x7f and 0xff.
337n/a encoded = bytes(range(127)) + bytes(range(128, 256))
338n/a for w in 2, 3, 4:
339n/a decoded = audioop.ulaw2lin(encoded, w)
340n/a self.assertEqual(audioop.lin2ulaw(decoded, w), encoded)
341n/a
342n/a def test_mul(self):
343n/a for w in 1, 2, 3, 4:
344n/a self.assertEqual(audioop.mul(b'', w, 2), b'')
345n/a self.assertEqual(audioop.mul(bytearray(), w, 2), b'')
346n/a self.assertEqual(audioop.mul(memoryview(b''), w, 2), b'')
347n/a self.assertEqual(audioop.mul(datas[w], w, 0),
348n/a b'\0' * len(datas[w]))
349n/a self.assertEqual(audioop.mul(datas[w], w, 1),
350n/a datas[w])
351n/a self.assertEqual(audioop.mul(datas[1], 1, 2),
352n/a b'\x00\x24\x7f\x80\x7f\x80\xfe')
353n/a self.assertEqual(audioop.mul(datas[2], 2, 2),
354n/a packs[2](0, 0x2468, 0x7fff, -0x8000, 0x7fff, -0x8000, -2))
355n/a self.assertEqual(audioop.mul(datas[3], 3, 2),
356n/a packs[3](0, 0x2468ac, 0x7fffff, -0x800000,
357n/a 0x7fffff, -0x800000, -2))
358n/a self.assertEqual(audioop.mul(datas[4], 4, 2),
359n/a packs[4](0, 0x2468acf0, 0x7fffffff, -0x80000000,
360n/a 0x7fffffff, -0x80000000, -2))
361n/a
362n/a def test_ratecv(self):
363n/a for w in 1, 2, 3, 4:
364n/a self.assertEqual(audioop.ratecv(b'', w, 1, 8000, 8000, None),
365n/a (b'', (-1, ((0, 0),))))
366n/a self.assertEqual(audioop.ratecv(bytearray(), w, 1, 8000, 8000, None),
367n/a (b'', (-1, ((0, 0),))))
368n/a self.assertEqual(audioop.ratecv(memoryview(b''), w, 1, 8000, 8000, None),
369n/a (b'', (-1, ((0, 0),))))
370n/a self.assertEqual(audioop.ratecv(b'', w, 5, 8000, 8000, None),
371n/a (b'', (-1, ((0, 0),) * 5)))
372n/a self.assertEqual(audioop.ratecv(b'', w, 1, 8000, 16000, None),
373n/a (b'', (-2, ((0, 0),))))
374n/a self.assertEqual(audioop.ratecv(datas[w], w, 1, 8000, 8000, None)[0],
375n/a datas[w])
376n/a self.assertEqual(audioop.ratecv(datas[w], w, 1, 8000, 8000, None, 1, 0)[0],
377n/a datas[w])
378n/a
379n/a state = None
380n/a d1, state = audioop.ratecv(b'\x00\x01\x02', 1, 1, 8000, 16000, state)
381n/a d2, state = audioop.ratecv(b'\x00\x01\x02', 1, 1, 8000, 16000, state)
382n/a self.assertEqual(d1 + d2, b'\000\000\001\001\002\001\000\000\001\001\002')
383n/a
384n/a for w in 1, 2, 3, 4:
385n/a d0, state0 = audioop.ratecv(datas[w], w, 1, 8000, 16000, None)
386n/a d, state = b'', None
387n/a for i in range(0, len(datas[w]), w):
388n/a d1, state = audioop.ratecv(datas[w][i:i + w], w, 1,
389n/a 8000, 16000, state)
390n/a d += d1
391n/a self.assertEqual(d, d0)
392n/a self.assertEqual(state, state0)
393n/a
394n/a expected = {
395n/a 1: packs[1](0, 0x0d, 0x37, -0x26, 0x55, -0x4b, -0x14),
396n/a 2: packs[2](0, 0x0da7, 0x3777, -0x2630, 0x5673, -0x4a64, -0x129a),
397n/a 3: packs[3](0, 0x0da740, 0x377776, -0x262fca,
398n/a 0x56740c, -0x4a62fd, -0x1298c0),
399n/a 4: packs[4](0, 0x0da740da, 0x37777776, -0x262fc962,
400n/a 0x56740da6, -0x4a62fc96, -0x1298bf26),
401n/a }
402n/a for w in 1, 2, 3, 4:
403n/a self.assertEqual(audioop.ratecv(datas[w], w, 1, 8000, 8000, None, 3, 1)[0],
404n/a expected[w])
405n/a self.assertEqual(audioop.ratecv(datas[w], w, 1, 8000, 8000, None, 30, 10)[0],
406n/a expected[w])
407n/a
408n/a def test_reverse(self):
409n/a for w in 1, 2, 3, 4:
410n/a self.assertEqual(audioop.reverse(b'', w), b'')
411n/a self.assertEqual(audioop.reverse(bytearray(), w), b'')
412n/a self.assertEqual(audioop.reverse(memoryview(b''), w), b'')
413n/a self.assertEqual(audioop.reverse(packs[w](0, 1, 2), w),
414n/a packs[w](2, 1, 0))
415n/a
416n/a def test_tomono(self):
417n/a for w in 1, 2, 3, 4:
418n/a data1 = datas[w]
419n/a data2 = bytearray(2 * len(data1))
420n/a for k in range(w):
421n/a data2[k::2*w] = data1[k::w]
422n/a self.assertEqual(audioop.tomono(data2, w, 1, 0), data1)
423n/a self.assertEqual(audioop.tomono(data2, w, 0, 1), b'\0' * len(data1))
424n/a for k in range(w):
425n/a data2[k+w::2*w] = data1[k::w]
426n/a self.assertEqual(audioop.tomono(data2, w, 0.5, 0.5), data1)
427n/a self.assertEqual(audioop.tomono(bytearray(data2), w, 0.5, 0.5),
428n/a data1)
429n/a self.assertEqual(audioop.tomono(memoryview(data2), w, 0.5, 0.5),
430n/a data1)
431n/a
432n/a def test_tostereo(self):
433n/a for w in 1, 2, 3, 4:
434n/a data1 = datas[w]
435n/a data2 = bytearray(2 * len(data1))
436n/a for k in range(w):
437n/a data2[k::2*w] = data1[k::w]
438n/a self.assertEqual(audioop.tostereo(data1, w, 1, 0), data2)
439n/a self.assertEqual(audioop.tostereo(data1, w, 0, 0), b'\0' * len(data2))
440n/a for k in range(w):
441n/a data2[k+w::2*w] = data1[k::w]
442n/a self.assertEqual(audioop.tostereo(data1, w, 1, 1), data2)
443n/a self.assertEqual(audioop.tostereo(bytearray(data1), w, 1, 1), data2)
444n/a self.assertEqual(audioop.tostereo(memoryview(data1), w, 1, 1),
445n/a data2)
446n/a
447n/a def test_findfactor(self):
448n/a self.assertEqual(audioop.findfactor(datas[2], datas[2]), 1.0)
449n/a self.assertEqual(audioop.findfactor(bytearray(datas[2]),
450n/a bytearray(datas[2])), 1.0)
451n/a self.assertEqual(audioop.findfactor(memoryview(datas[2]),
452n/a memoryview(datas[2])), 1.0)
453n/a self.assertEqual(audioop.findfactor(b'\0' * len(datas[2]), datas[2]),
454n/a 0.0)
455n/a
456n/a def test_findfit(self):
457n/a self.assertEqual(audioop.findfit(datas[2], datas[2]), (0, 1.0))
458n/a self.assertEqual(audioop.findfit(bytearray(datas[2]),
459n/a bytearray(datas[2])), (0, 1.0))
460n/a self.assertEqual(audioop.findfit(memoryview(datas[2]),
461n/a memoryview(datas[2])), (0, 1.0))
462n/a self.assertEqual(audioop.findfit(datas[2], packs[2](1, 2, 0)),
463n/a (1, 8038.8))
464n/a self.assertEqual(audioop.findfit(datas[2][:-2] * 5 + datas[2], datas[2]),
465n/a (30, 1.0))
466n/a
467n/a def test_findmax(self):
468n/a self.assertEqual(audioop.findmax(datas[2], 1), 5)
469n/a self.assertEqual(audioop.findmax(bytearray(datas[2]), 1), 5)
470n/a self.assertEqual(audioop.findmax(memoryview(datas[2]), 1), 5)
471n/a
472n/a def test_getsample(self):
473n/a for w in 1, 2, 3, 4:
474n/a data = packs[w](0, 1, -1, maxvalues[w], minvalues[w])
475n/a self.assertEqual(audioop.getsample(data, w, 0), 0)
476n/a self.assertEqual(audioop.getsample(bytearray(data), w, 0), 0)
477n/a self.assertEqual(audioop.getsample(memoryview(data), w, 0), 0)
478n/a self.assertEqual(audioop.getsample(data, w, 1), 1)
479n/a self.assertEqual(audioop.getsample(data, w, 2), -1)
480n/a self.assertEqual(audioop.getsample(data, w, 3), maxvalues[w])
481n/a self.assertEqual(audioop.getsample(data, w, 4), minvalues[w])
482n/a
483n/a def test_byteswap(self):
484n/a swapped_datas = {
485n/a 1: datas[1],
486n/a 2: packs[2](0, 0x3412, 0x6745, -0x6646, -0x81, 0x80, -1),
487n/a 3: packs[3](0, 0x563412, -0x7698bb, 0x7798ba, -0x81, 0x80, -1),
488n/a 4: packs[4](0, 0x78563412, -0x547698bb, 0x557698ba,
489n/a -0x81, 0x80, -1),
490n/a }
491n/a for w in 1, 2, 3, 4:
492n/a self.assertEqual(audioop.byteswap(b'', w), b'')
493n/a self.assertEqual(audioop.byteswap(datas[w], w), swapped_datas[w])
494n/a self.assertEqual(audioop.byteswap(swapped_datas[w], w), datas[w])
495n/a self.assertEqual(audioop.byteswap(bytearray(datas[w]), w),
496n/a swapped_datas[w])
497n/a self.assertEqual(audioop.byteswap(memoryview(datas[w]), w),
498n/a swapped_datas[w])
499n/a
500n/a def test_negativelen(self):
501n/a # from issue 3306, previously it segfaulted
502n/a self.assertRaises(audioop.error,
503n/a audioop.findmax, bytes(range(256)), -2392392)
504n/a
505n/a def test_issue7673(self):
506n/a state = None
507n/a for data, size in INVALID_DATA:
508n/a size2 = size
509n/a self.assertRaises(audioop.error, audioop.getsample, data, size, 0)
510n/a self.assertRaises(audioop.error, audioop.max, data, size)
511n/a self.assertRaises(audioop.error, audioop.minmax, data, size)
512n/a self.assertRaises(audioop.error, audioop.avg, data, size)
513n/a self.assertRaises(audioop.error, audioop.rms, data, size)
514n/a self.assertRaises(audioop.error, audioop.avgpp, data, size)
515n/a self.assertRaises(audioop.error, audioop.maxpp, data, size)
516n/a self.assertRaises(audioop.error, audioop.cross, data, size)
517n/a self.assertRaises(audioop.error, audioop.mul, data, size, 1.0)
518n/a self.assertRaises(audioop.error, audioop.tomono, data, size, 0.5, 0.5)
519n/a self.assertRaises(audioop.error, audioop.tostereo, data, size, 0.5, 0.5)
520n/a self.assertRaises(audioop.error, audioop.add, data, data, size)
521n/a self.assertRaises(audioop.error, audioop.bias, data, size, 0)
522n/a self.assertRaises(audioop.error, audioop.reverse, data, size)
523n/a self.assertRaises(audioop.error, audioop.lin2lin, data, size, size2)
524n/a self.assertRaises(audioop.error, audioop.ratecv, data, size, 1, 1, 1, state)
525n/a self.assertRaises(audioop.error, audioop.lin2ulaw, data, size)
526n/a self.assertRaises(audioop.error, audioop.lin2alaw, data, size)
527n/a self.assertRaises(audioop.error, audioop.lin2adpcm, data, size, state)
528n/a
529n/a def test_string(self):
530n/a data = 'abcd'
531n/a size = 2
532n/a self.assertRaises(TypeError, audioop.getsample, data, size, 0)
533n/a self.assertRaises(TypeError, audioop.max, data, size)
534n/a self.assertRaises(TypeError, audioop.minmax, data, size)
535n/a self.assertRaises(TypeError, audioop.avg, data, size)
536n/a self.assertRaises(TypeError, audioop.rms, data, size)
537n/a self.assertRaises(TypeError, audioop.avgpp, data, size)
538n/a self.assertRaises(TypeError, audioop.maxpp, data, size)
539n/a self.assertRaises(TypeError, audioop.cross, data, size)
540n/a self.assertRaises(TypeError, audioop.mul, data, size, 1.0)
541n/a self.assertRaises(TypeError, audioop.tomono, data, size, 0.5, 0.5)
542n/a self.assertRaises(TypeError, audioop.tostereo, data, size, 0.5, 0.5)
543n/a self.assertRaises(TypeError, audioop.add, data, data, size)
544n/a self.assertRaises(TypeError, audioop.bias, data, size, 0)
545n/a self.assertRaises(TypeError, audioop.reverse, data, size)
546n/a self.assertRaises(TypeError, audioop.lin2lin, data, size, size)
547n/a self.assertRaises(TypeError, audioop.ratecv, data, size, 1, 1, 1, None)
548n/a self.assertRaises(TypeError, audioop.lin2ulaw, data, size)
549n/a self.assertRaises(TypeError, audioop.lin2alaw, data, size)
550n/a self.assertRaises(TypeError, audioop.lin2adpcm, data, size, None)
551n/a
552n/a def test_wrongsize(self):
553n/a data = b'abcdefgh'
554n/a state = None
555n/a for size in (-1, 0, 5, 1024):
556n/a self.assertRaises(audioop.error, audioop.ulaw2lin, data, size)
557n/a self.assertRaises(audioop.error, audioop.alaw2lin, data, size)
558n/a self.assertRaises(audioop.error, audioop.adpcm2lin, data, size, state)
559n/a
560n/aif __name__ == '__main__':
561n/a unittest.main()