move sections
[python/dscho.git] / Lib / test / test_audioop.py
blobe03ceb5b145bc703003f852317f90ed527047b45
1 import audioop
2 import unittest
3 from test.test_support import run_unittest
6 def gendata1():
7 return '\0\1\2'
9 def gendata2():
10 if audioop.getsample('\0\1', 2, 0) == 1:
11 return '\0\0\0\1\0\2'
12 else:
13 return '\0\0\1\0\2\0'
15 def gendata4():
16 if audioop.getsample('\0\0\0\1', 4, 0) == 1:
17 return '\0\0\0\0\0\0\0\1\0\0\0\2'
18 else:
19 return '\0\0\0\0\1\0\0\0\2\0\0\0'
21 data = [gendata1(), gendata2(), gendata4()]
23 INVALID_DATA = [
24 ('abc', 0),
25 ('abc', 2),
26 ('abc', 4),
30 class TestAudioop(unittest.TestCase):
32 def test_max(self):
33 self.assertEqual(audioop.max(data[0], 1), 2)
34 self.assertEqual(audioop.max(data[1], 2), 2)
35 self.assertEqual(audioop.max(data[2], 4), 2)
37 def test_minmax(self):
38 self.assertEqual(audioop.minmax(data[0], 1), (0, 2))
39 self.assertEqual(audioop.minmax(data[1], 2), (0, 2))
40 self.assertEqual(audioop.minmax(data[2], 4), (0, 2))
42 def test_maxpp(self):
43 self.assertEqual(audioop.maxpp(data[0], 1), 0)
44 self.assertEqual(audioop.maxpp(data[1], 2), 0)
45 self.assertEqual(audioop.maxpp(data[2], 4), 0)
47 def test_avg(self):
48 self.assertEqual(audioop.avg(data[0], 1), 1)
49 self.assertEqual(audioop.avg(data[1], 2), 1)
50 self.assertEqual(audioop.avg(data[2], 4), 1)
52 def test_avgpp(self):
53 self.assertEqual(audioop.avgpp(data[0], 1), 0)
54 self.assertEqual(audioop.avgpp(data[1], 2), 0)
55 self.assertEqual(audioop.avgpp(data[2], 4), 0)
57 def test_rms(self):
58 self.assertEqual(audioop.rms(data[0], 1), 1)
59 self.assertEqual(audioop.rms(data[1], 2), 1)
60 self.assertEqual(audioop.rms(data[2], 4), 1)
62 def test_cross(self):
63 self.assertEqual(audioop.cross(data[0], 1), 0)
64 self.assertEqual(audioop.cross(data[1], 2), 0)
65 self.assertEqual(audioop.cross(data[2], 4), 0)
67 def test_add(self):
68 data2 = []
69 for d in data:
70 str = ''
71 for s in d:
72 str = str + chr(ord(s)*2)
73 data2.append(str)
74 self.assertEqual(audioop.add(data[0], data[0], 1), data2[0])
75 self.assertEqual(audioop.add(data[1], data[1], 2), data2[1])
76 self.assertEqual(audioop.add(data[2], data[2], 4), data2[2])
78 def test_bias(self):
79 # Note: this test assumes that avg() works
80 d1 = audioop.bias(data[0], 1, 100)
81 d2 = audioop.bias(data[1], 2, 100)
82 d4 = audioop.bias(data[2], 4, 100)
83 self.assertEqual(audioop.avg(d1, 1), 101)
84 self.assertEqual(audioop.avg(d2, 2), 101)
85 self.assertEqual(audioop.avg(d4, 4), 101)
87 def test_lin2lin(self):
88 # too simple: we test only the size
89 for d1 in data:
90 for d2 in data:
91 got = len(d1)//3
92 wtd = len(d2)//3
93 self.assertEqual(len(audioop.lin2lin(d1, got, wtd)), len(d2))
95 def test_adpcm2lin(self):
96 # Very cursory test
97 self.assertEqual(audioop.adpcm2lin('\0\0', 1, None), ('\0\0\0\0', (0,0)))
99 def test_lin2adpcm(self):
100 # Very cursory test
101 self.assertEqual(audioop.lin2adpcm('\0\0\0\0', 1, None), ('\0\0', (0,0)))
103 def test_lin2alaw(self):
104 self.assertEqual(audioop.lin2alaw(data[0], 1), '\xd5\xc5\xf5')
105 self.assertEqual(audioop.lin2alaw(data[1], 2), '\xd5\xd5\xd5')
106 self.assertEqual(audioop.lin2alaw(data[2], 4), '\xd5\xd5\xd5')
108 def test_alaw2lin(self):
109 # Cursory
110 d = audioop.lin2alaw(data[0], 1)
111 self.assertEqual(audioop.alaw2lin(d, 1), data[0])
113 def test_lin2ulaw(self):
114 self.assertEqual(audioop.lin2ulaw(data[0], 1), '\xff\xe7\xdb')
115 self.assertEqual(audioop.lin2ulaw(data[1], 2), '\xff\xff\xff')
116 self.assertEqual(audioop.lin2ulaw(data[2], 4), '\xff\xff\xff')
118 def test_ulaw2lin(self):
119 # Cursory
120 d = audioop.lin2ulaw(data[0], 1)
121 self.assertEqual(audioop.ulaw2lin(d, 1), data[0])
123 def test_mul(self):
124 data2 = []
125 for d in data:
126 str = ''
127 for s in d:
128 str = str + chr(ord(s)*2)
129 data2.append(str)
130 self.assertEqual(audioop.mul(data[0], 1, 2), data2[0])
131 self.assertEqual(audioop.mul(data[1],2, 2), data2[1])
132 self.assertEqual(audioop.mul(data[2], 4, 2), data2[2])
134 def test_ratecv(self):
135 state = None
136 d1, state = audioop.ratecv(data[0], 1, 1, 8000, 16000, state)
137 d2, state = audioop.ratecv(data[0], 1, 1, 8000, 16000, state)
138 self.assertEqual(d1 + d2, '\000\000\001\001\002\001\000\000\001\001\002')
140 def test_reverse(self):
141 self.assertEqual(audioop.reverse(data[0], 1), '\2\1\0')
143 def test_tomono(self):
144 data2 = ''
145 for d in data[0]:
146 data2 = data2 + d + d
147 self.assertEqual(audioop.tomono(data2, 1, 0.5, 0.5), data[0])
149 def test_tostereo(self):
150 data2 = ''
151 for d in data[0]:
152 data2 = data2 + d + d
153 self.assertEqual(audioop.tostereo(data[0], 1, 1, 1), data2)
155 def test_findfactor(self):
156 self.assertEqual(audioop.findfactor(data[1], data[1]), 1.0)
158 def test_findfit(self):
159 self.assertEqual(audioop.findfit(data[1], data[1]), (0, 1.0))
161 def test_findmax(self):
162 self.assertEqual(audioop.findmax(data[1], 1), 2)
164 def test_getsample(self):
165 for i in range(3):
166 self.assertEqual(audioop.getsample(data[0], 1, i), i)
167 self.assertEqual(audioop.getsample(data[1], 2, i), i)
168 self.assertEqual(audioop.getsample(data[2], 4, i), i)
170 def test_negativelen(self):
171 # from issue 3306, previously it segfaulted
172 self.assertRaises(audioop.error,
173 audioop.findmax, ''.join( chr(x) for x in xrange(256)), -2392392)
175 def test_issue7673(self):
176 state = None
177 for data, size in INVALID_DATA:
178 size2 = size
179 self.assertRaises(audioop.error, audioop.getsample, data, size, 0)
180 self.assertRaises(audioop.error, audioop.max, data, size)
181 self.assertRaises(audioop.error, audioop.minmax, data, size)
182 self.assertRaises(audioop.error, audioop.avg, data, size)
183 self.assertRaises(audioop.error, audioop.rms, data, size)
184 self.assertRaises(audioop.error, audioop.avgpp, data, size)
185 self.assertRaises(audioop.error, audioop.maxpp, data, size)
186 self.assertRaises(audioop.error, audioop.cross, data, size)
187 self.assertRaises(audioop.error, audioop.mul, data, size, 1.0)
188 self.assertRaises(audioop.error, audioop.tomono, data, size, 0.5, 0.5)
189 self.assertRaises(audioop.error, audioop.tostereo, data, size, 0.5, 0.5)
190 self.assertRaises(audioop.error, audioop.add, data, data, size)
191 self.assertRaises(audioop.error, audioop.bias, data, size, 0)
192 self.assertRaises(audioop.error, audioop.reverse, data, size)
193 self.assertRaises(audioop.error, audioop.lin2lin, data, size, size2)
194 self.assertRaises(audioop.error, audioop.ratecv, data, size, 1, 1, 1, state)
195 self.assertRaises(audioop.error, audioop.lin2ulaw, data, size)
196 self.assertRaises(audioop.error, audioop.ulaw2lin, data, size)
197 self.assertRaises(audioop.error, audioop.lin2alaw, data, size)
198 self.assertRaises(audioop.error, audioop.alaw2lin, data, size)
199 self.assertRaises(audioop.error, audioop.lin2adpcm, data, size, state)
200 self.assertRaises(audioop.error, audioop.adpcm2lin, data, size, state)
202 def test_main():
203 run_unittest(TestAudioop)
205 if __name__ == '__main__':
206 test_main()