blob: c14e8b87a856f7f052237378819eec79899cda44 [file] [log] [blame]
Guido van Rossumfa7fcb91994-01-12 09:55:11 +00001import audioop
Benjamin Peterson41181742008-07-02 20:22:54 +00002import unittest
3from test.support import run_unittest
4
Antoine Pitrouf99f3332012-02-04 16:44:21 +01005endian = 'big' if audioop.getsample(b'\0\1', 2, 0) == 1 else 'little'
Guido van Rossumfa7fcb91994-01-12 09:55:11 +00006
7def gendata1():
Martin v. Löwis3a2e5ce2007-07-20 07:36:26 +00008 return b'\0\1\2'
Guido van Rossumfa7fcb91994-01-12 09:55:11 +00009
10def gendata2():
Antoine Pitrouf99f3332012-02-04 16:44:21 +010011 if endian == 'big':
Martin v. Löwis3a2e5ce2007-07-20 07:36:26 +000012 return b'\0\0\0\1\0\2'
Roger E. Masse7a61f871997-01-03 19:20:17 +000013 else:
Martin v. Löwis3a2e5ce2007-07-20 07:36:26 +000014 return b'\0\0\1\0\2\0'
Guido van Rossumfa7fcb91994-01-12 09:55:11 +000015
16def gendata4():
Antoine Pitrouf99f3332012-02-04 16:44:21 +010017 if endian == 'big':
Martin v. Löwis3a2e5ce2007-07-20 07:36:26 +000018 return b'\0\0\0\0\0\0\0\1\0\0\0\2'
Roger E. Masse7a61f871997-01-03 19:20:17 +000019 else:
Martin v. Löwis3a2e5ce2007-07-20 07:36:26 +000020 return b'\0\0\0\0\1\0\0\0\2\0\0\0'
Guido van Rossumfa7fcb91994-01-12 09:55:11 +000021
Benjamin Peterson41181742008-07-02 20:22:54 +000022data = [gendata1(), gendata2(), gendata4()]
Guido van Rossumfa7fcb91994-01-12 09:55:11 +000023
Victor Stinnerbc5c54b2010-07-03 13:44:22 +000024INVALID_DATA = [
Antoine Pitrou75ff65e2012-01-28 22:01:59 +010025 (b'abc', 0),
26 (b'abc', 2),
27 (b'abc', 4),
Victor Stinnerbc5c54b2010-07-03 13:44:22 +000028]
29
Guido van Rossum4fdb7441996-12-20 03:13:06 +000030
Benjamin Peterson41181742008-07-02 20:22:54 +000031class TestAudioop(unittest.TestCase):
Guido van Rossumfa7fcb91994-01-12 09:55:11 +000032
Benjamin Peterson41181742008-07-02 20:22:54 +000033 def test_max(self):
34 self.assertEqual(audioop.max(data[0], 1), 2)
35 self.assertEqual(audioop.max(data[1], 2), 2)
36 self.assertEqual(audioop.max(data[2], 4), 2)
Guido van Rossumfa7fcb91994-01-12 09:55:11 +000037
Benjamin Peterson41181742008-07-02 20:22:54 +000038 def test_minmax(self):
39 self.assertEqual(audioop.minmax(data[0], 1), (0, 2))
40 self.assertEqual(audioop.minmax(data[1], 2), (0, 2))
41 self.assertEqual(audioop.minmax(data[2], 4), (0, 2))
Guido van Rossumfa7fcb91994-01-12 09:55:11 +000042
Benjamin Peterson41181742008-07-02 20:22:54 +000043 def test_maxpp(self):
44 self.assertEqual(audioop.maxpp(data[0], 1), 0)
45 self.assertEqual(audioop.maxpp(data[1], 2), 0)
46 self.assertEqual(audioop.maxpp(data[2], 4), 0)
Guido van Rossumfa7fcb91994-01-12 09:55:11 +000047
Benjamin Peterson41181742008-07-02 20:22:54 +000048 def test_avg(self):
49 self.assertEqual(audioop.avg(data[0], 1), 1)
50 self.assertEqual(audioop.avg(data[1], 2), 1)
51 self.assertEqual(audioop.avg(data[2], 4), 1)
Guido van Rossumfa7fcb91994-01-12 09:55:11 +000052
Benjamin Peterson41181742008-07-02 20:22:54 +000053 def test_avgpp(self):
54 self.assertEqual(audioop.avgpp(data[0], 1), 0)
55 self.assertEqual(audioop.avgpp(data[1], 2), 0)
56 self.assertEqual(audioop.avgpp(data[2], 4), 0)
Guido van Rossumfa7fcb91994-01-12 09:55:11 +000057
Benjamin Peterson41181742008-07-02 20:22:54 +000058 def test_rms(self):
59 self.assertEqual(audioop.rms(data[0], 1), 1)
60 self.assertEqual(audioop.rms(data[1], 2), 1)
61 self.assertEqual(audioop.rms(data[2], 4), 1)
Guido van Rossumfa7fcb91994-01-12 09:55:11 +000062
Benjamin Peterson41181742008-07-02 20:22:54 +000063 def test_cross(self):
64 self.assertEqual(audioop.cross(data[0], 1), 0)
65 self.assertEqual(audioop.cross(data[1], 2), 0)
66 self.assertEqual(audioop.cross(data[2], 4), 0)
Guido van Rossumfa7fcb91994-01-12 09:55:11 +000067
Benjamin Peterson41181742008-07-02 20:22:54 +000068 def test_add(self):
69 data2 = []
70 for d in data:
71 str = bytearray(len(d))
72 for i,b in enumerate(d):
73 str[i] = 2*b
74 data2.append(str)
75 self.assertEqual(audioop.add(data[0], data[0], 1), data2[0])
76 self.assertEqual(audioop.add(data[1], data[1], 2), data2[1])
77 self.assertEqual(audioop.add(data[2], data[2], 4), data2[2])
Guido van Rossumfa7fcb91994-01-12 09:55:11 +000078
Benjamin Peterson41181742008-07-02 20:22:54 +000079 def test_bias(self):
80 # Note: this test assumes that avg() works
81 d1 = audioop.bias(data[0], 1, 100)
82 d2 = audioop.bias(data[1], 2, 100)
83 d4 = audioop.bias(data[2], 4, 100)
84 self.assertEqual(audioop.avg(d1, 1), 101)
85 self.assertEqual(audioop.avg(d2, 2), 101)
86 self.assertEqual(audioop.avg(d4, 4), 101)
Guido van Rossumfa7fcb91994-01-12 09:55:11 +000087
Benjamin Peterson41181742008-07-02 20:22:54 +000088 def test_lin2lin(self):
89 # too simple: we test only the size
90 for d1 in data:
91 for d2 in data:
92 got = len(d1)//3
93 wtd = len(d2)//3
94 self.assertEqual(len(audioop.lin2lin(d1, got, wtd)), len(d2))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000095
Benjamin Peterson41181742008-07-02 20:22:54 +000096 def test_adpcm2lin(self):
97 # Very cursory test
Antoine Pitrou75ff65e2012-01-28 22:01:59 +010098 self.assertEqual(audioop.adpcm2lin(b'\0\0', 1, None), (b'\0' * 4, (0,0)))
99 self.assertEqual(audioop.adpcm2lin(b'\0\0', 2, None), (b'\0' * 8, (0,0)))
100 self.assertEqual(audioop.adpcm2lin(b'\0\0', 4, None), (b'\0' * 16, (0,0)))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000101
Benjamin Peterson41181742008-07-02 20:22:54 +0000102 def test_lin2adpcm(self):
103 # Very cursory test
104 self.assertEqual(audioop.lin2adpcm(b'\0\0\0\0', 1, None), (b'\0\0', (0,0)))
Guido van Rossumfa7fcb91994-01-12 09:55:11 +0000105
Benjamin Peterson41181742008-07-02 20:22:54 +0000106 def test_lin2alaw(self):
107 self.assertEqual(audioop.lin2alaw(data[0], 1), b'\xd5\xc5\xf5')
108 self.assertEqual(audioop.lin2alaw(data[1], 2), b'\xd5\xd5\xd5')
109 self.assertEqual(audioop.lin2alaw(data[2], 4), b'\xd5\xd5\xd5')
Guido van Rossumfa7fcb91994-01-12 09:55:11 +0000110
Benjamin Peterson41181742008-07-02 20:22:54 +0000111 def test_alaw2lin(self):
112 # Cursory
113 d = audioop.lin2alaw(data[0], 1)
114 self.assertEqual(audioop.alaw2lin(d, 1), data[0])
Antoine Pitrouf99f3332012-02-04 16:44:21 +0100115 if endian == 'big':
116 self.assertEqual(audioop.alaw2lin(d, 2),
117 b'\x00\x08\x01\x08\x02\x10')
118 self.assertEqual(audioop.alaw2lin(d, 4),
119 b'\x00\x08\x00\x00\x01\x08\x00\x00\x02\x10\x00\x00')
120 else:
121 self.assertEqual(audioop.alaw2lin(d, 2),
122 b'\x08\x00\x08\x01\x10\x02')
123 self.assertEqual(audioop.alaw2lin(d, 4),
124 b'\x00\x00\x08\x00\x00\x00\x08\x01\x00\x00\x10\x02')
Guido van Rossumfa7fcb91994-01-12 09:55:11 +0000125
Benjamin Peterson41181742008-07-02 20:22:54 +0000126 def test_lin2ulaw(self):
127 self.assertEqual(audioop.lin2ulaw(data[0], 1), b'\xff\xe7\xdb')
128 self.assertEqual(audioop.lin2ulaw(data[1], 2), b'\xff\xff\xff')
129 self.assertEqual(audioop.lin2ulaw(data[2], 4), b'\xff\xff\xff')
Guido van Rossumc8504e21997-02-14 15:58:00 +0000130
Benjamin Peterson41181742008-07-02 20:22:54 +0000131 def test_ulaw2lin(self):
132 # Cursory
133 d = audioop.lin2ulaw(data[0], 1)
134 self.assertEqual(audioop.ulaw2lin(d, 1), data[0])
Antoine Pitrouf99f3332012-02-04 16:44:21 +0100135 if endian == 'big':
136 self.assertEqual(audioop.ulaw2lin(d, 2),
137 b'\x00\x00\x01\x04\x02\x0c')
138 self.assertEqual(audioop.ulaw2lin(d, 4),
139 b'\x00\x00\x00\x00\x01\x04\x00\x00\x02\x0c\x00\x00')
140 else:
141 self.assertEqual(audioop.ulaw2lin(d, 2),
142 b'\x00\x00\x04\x01\x0c\x02')
143 self.assertEqual(audioop.ulaw2lin(d, 4),
144 b'\x00\x00\x00\x00\x00\x00\x04\x01\x00\x00\x0c\x02')
Guido van Rossumfa7fcb91994-01-12 09:55:11 +0000145
Benjamin Peterson41181742008-07-02 20:22:54 +0000146 def test_mul(self):
147 data2 = []
148 for d in data:
149 str = bytearray(len(d))
150 for i,b in enumerate(d):
151 str[i] = 2*b
152 data2.append(str)
153 self.assertEqual(audioop.mul(data[0], 1, 2), data2[0])
154 self.assertEqual(audioop.mul(data[1],2, 2), data2[1])
155 self.assertEqual(audioop.mul(data[2], 4, 2), data2[2])
Guido van Rossumfa7fcb91994-01-12 09:55:11 +0000156
Benjamin Peterson41181742008-07-02 20:22:54 +0000157 def test_ratecv(self):
158 state = None
159 d1, state = audioop.ratecv(data[0], 1, 1, 8000, 16000, state)
160 d2, state = audioop.ratecv(data[0], 1, 1, 8000, 16000, state)
161 self.assertEqual(d1 + d2, b'\000\000\001\001\002\001\000\000\001\001\002')
Guido van Rossumfa7fcb91994-01-12 09:55:11 +0000162
Benjamin Peterson41181742008-07-02 20:22:54 +0000163 def test_reverse(self):
164 self.assertEqual(audioop.reverse(data[0], 1), b'\2\1\0')
Guido van Rossumfa7fcb91994-01-12 09:55:11 +0000165
Benjamin Peterson41181742008-07-02 20:22:54 +0000166 def test_tomono(self):
167 data2 = bytearray()
168 for d in data[0]:
169 data2.append(d)
170 data2.append(d)
171 self.assertEqual(audioop.tomono(data2, 1, 0.5, 0.5), data[0])
Guido van Rossumfa7fcb91994-01-12 09:55:11 +0000172
Benjamin Peterson41181742008-07-02 20:22:54 +0000173 def test_tostereo(self):
174 data2 = bytearray()
175 for d in data[0]:
176 data2.append(d)
177 data2.append(d)
178 self.assertEqual(audioop.tostereo(data[0], 1, 1, 1), data2)
Guido van Rossumfa7fcb91994-01-12 09:55:11 +0000179
Benjamin Peterson41181742008-07-02 20:22:54 +0000180 def test_findfactor(self):
181 self.assertEqual(audioop.findfactor(data[1], data[1]), 1.0)
Guido van Rossumfa7fcb91994-01-12 09:55:11 +0000182
Benjamin Peterson41181742008-07-02 20:22:54 +0000183 def test_findfit(self):
184 self.assertEqual(audioop.findfit(data[1], data[1]), (0, 1.0))
185
186 def test_findmax(self):
187 self.assertEqual(audioop.findmax(data[1], 1), 2)
188
189 def test_getsample(self):
190 for i in range(3):
191 self.assertEqual(audioop.getsample(data[0], 1, i), i)
192 self.assertEqual(audioop.getsample(data[1], 2, i), i)
193 self.assertEqual(audioop.getsample(data[2], 4, i), i)
194
Georg Brandl3dbca812008-07-23 16:10:53 +0000195 def test_negativelen(self):
Georg Brandl86b2fb92008-07-16 03:43:04 +0000196 # from issue 3306, previously it segfaulted
197 self.assertRaises(audioop.error,
198 audioop.findmax, ''.join(chr(x) for x in range(256)), -2392392)
Guido van Rossumfa7fcb91994-01-12 09:55:11 +0000199
Victor Stinnerbc5c54b2010-07-03 13:44:22 +0000200 def test_issue7673(self):
201 state = None
202 for data, size in INVALID_DATA:
203 size2 = size
204 self.assertRaises(audioop.error, audioop.getsample, data, size, 0)
205 self.assertRaises(audioop.error, audioop.max, data, size)
206 self.assertRaises(audioop.error, audioop.minmax, data, size)
207 self.assertRaises(audioop.error, audioop.avg, data, size)
208 self.assertRaises(audioop.error, audioop.rms, data, size)
209 self.assertRaises(audioop.error, audioop.avgpp, data, size)
210 self.assertRaises(audioop.error, audioop.maxpp, data, size)
211 self.assertRaises(audioop.error, audioop.cross, data, size)
212 self.assertRaises(audioop.error, audioop.mul, data, size, 1.0)
213 self.assertRaises(audioop.error, audioop.tomono, data, size, 0.5, 0.5)
214 self.assertRaises(audioop.error, audioop.tostereo, data, size, 0.5, 0.5)
215 self.assertRaises(audioop.error, audioop.add, data, data, size)
216 self.assertRaises(audioop.error, audioop.bias, data, size, 0)
217 self.assertRaises(audioop.error, audioop.reverse, data, size)
218 self.assertRaises(audioop.error, audioop.lin2lin, data, size, size2)
219 self.assertRaises(audioop.error, audioop.ratecv, data, size, 1, 1, 1, state)
220 self.assertRaises(audioop.error, audioop.lin2ulaw, data, size)
Victor Stinnerbc5c54b2010-07-03 13:44:22 +0000221 self.assertRaises(audioop.error, audioop.lin2alaw, data, size)
Victor Stinnerbc5c54b2010-07-03 13:44:22 +0000222 self.assertRaises(audioop.error, audioop.lin2adpcm, data, size, state)
Antoine Pitrou75ff65e2012-01-28 22:01:59 +0100223
224 def test_wrongsize(self):
225 data = b'abc'
226 state = None
227 for size in (-1, 3, 5):
228 self.assertRaises(audioop.error, audioop.ulaw2lin, data, size)
229 self.assertRaises(audioop.error, audioop.alaw2lin, data, size)
Victor Stinnerbc5c54b2010-07-03 13:44:22 +0000230 self.assertRaises(audioop.error, audioop.adpcm2lin, data, size, state)
231
Christian Heimes180510d2008-03-03 19:15:45 +0000232def test_main():
Benjamin Peterson41181742008-07-02 20:22:54 +0000233 run_unittest(TestAudioop)
Christian Heimes180510d2008-03-03 19:15:45 +0000234
235if __name__ == '__main__':
236 test_main()