3 from test
.test_support
import run_unittest
10 if audioop
.getsample('\0\1', 2, 0) == 1:
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'
19 return '\0\0\0\0\1\0\0\0\2\0\0\0'
21 data
= [gendata1(), gendata2(), gendata4()]
30 class TestAudioop(unittest
.TestCase
):
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))
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)
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)
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)
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)
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)
72 str = str + chr(ord(s
)*2)
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])
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
93 self
.assertEqual(len(audioop
.lin2lin(d1
, got
, wtd
)), len(d2
))
95 def test_adpcm2lin(self
):
97 self
.assertEqual(audioop
.adpcm2lin('\0\0', 1, None), ('\0\0\0\0', (0,0)))
99 def test_lin2adpcm(self
):
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
):
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
):
120 d
= audioop
.lin2ulaw(data
[0], 1)
121 self
.assertEqual(audioop
.ulaw2lin(d
, 1), data
[0])
128 str = str + chr(ord(s
)*2)
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
):
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
):
146 data2
= data2
+ d
+ d
147 self
.assertEqual(audioop
.tomono(data2
, 1, 0.5, 0.5), data
[0])
149 def test_tostereo(self
):
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
):
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
):
177 for data
, size
in INVALID_DATA
:
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
)
203 run_unittest(TestAudioop
)
205 if __name__
== '__main__':