|
1 # Test audioop. |
|
2 import audioop |
|
3 from test.test_support import verbose |
|
4 |
|
5 def gendata1(): |
|
6 return '\0\1\2' |
|
7 |
|
8 def gendata2(): |
|
9 if verbose: |
|
10 print 'getsample' |
|
11 if audioop.getsample('\0\1', 2, 0) == 1: |
|
12 return '\0\0\0\1\0\2' |
|
13 else: |
|
14 return '\0\0\1\0\2\0' |
|
15 |
|
16 def gendata4(): |
|
17 if verbose: |
|
18 print 'getsample' |
|
19 if audioop.getsample('\0\0\0\1', 4, 0) == 1: |
|
20 return '\0\0\0\0\0\0\0\1\0\0\0\2' |
|
21 else: |
|
22 return '\0\0\0\0\1\0\0\0\2\0\0\0' |
|
23 |
|
24 def testmax(data): |
|
25 if verbose: |
|
26 print 'max' |
|
27 if audioop.max(data[0], 1) != 2 or \ |
|
28 audioop.max(data[1], 2) != 2 or \ |
|
29 audioop.max(data[2], 4) != 2: |
|
30 return 0 |
|
31 return 1 |
|
32 |
|
33 def testminmax(data): |
|
34 if verbose: |
|
35 print 'minmax' |
|
36 if audioop.minmax(data[0], 1) != (0, 2) or \ |
|
37 audioop.minmax(data[1], 2) != (0, 2) or \ |
|
38 audioop.minmax(data[2], 4) != (0, 2): |
|
39 return 0 |
|
40 return 1 |
|
41 |
|
42 def testmaxpp(data): |
|
43 if verbose: |
|
44 print 'maxpp' |
|
45 if audioop.maxpp(data[0], 1) != 0 or \ |
|
46 audioop.maxpp(data[1], 2) != 0 or \ |
|
47 audioop.maxpp(data[2], 4) != 0: |
|
48 return 0 |
|
49 return 1 |
|
50 |
|
51 def testavg(data): |
|
52 if verbose: |
|
53 print 'avg' |
|
54 if audioop.avg(data[0], 1) != 1 or \ |
|
55 audioop.avg(data[1], 2) != 1 or \ |
|
56 audioop.avg(data[2], 4) != 1: |
|
57 return 0 |
|
58 return 1 |
|
59 |
|
60 def testavgpp(data): |
|
61 if verbose: |
|
62 print 'avgpp' |
|
63 if audioop.avgpp(data[0], 1) != 0 or \ |
|
64 audioop.avgpp(data[1], 2) != 0 or \ |
|
65 audioop.avgpp(data[2], 4) != 0: |
|
66 return 0 |
|
67 return 1 |
|
68 |
|
69 def testrms(data): |
|
70 if audioop.rms(data[0], 1) != 1 or \ |
|
71 audioop.rms(data[1], 2) != 1 or \ |
|
72 audioop.rms(data[2], 4) != 1: |
|
73 return 0 |
|
74 return 1 |
|
75 |
|
76 def testcross(data): |
|
77 if verbose: |
|
78 print 'cross' |
|
79 if audioop.cross(data[0], 1) != 0 or \ |
|
80 audioop.cross(data[1], 2) != 0 or \ |
|
81 audioop.cross(data[2], 4) != 0: |
|
82 return 0 |
|
83 return 1 |
|
84 |
|
85 def testadd(data): |
|
86 if verbose: |
|
87 print 'add' |
|
88 data2 = [] |
|
89 for d in data: |
|
90 str = '' |
|
91 for s in d: |
|
92 str = str + chr(ord(s)*2) |
|
93 data2.append(str) |
|
94 if audioop.add(data[0], data[0], 1) != data2[0] or \ |
|
95 audioop.add(data[1], data[1], 2) != data2[1] or \ |
|
96 audioop.add(data[2], data[2], 4) != data2[2]: |
|
97 return 0 |
|
98 return 1 |
|
99 |
|
100 def testbias(data): |
|
101 if verbose: |
|
102 print 'bias' |
|
103 # Note: this test assumes that avg() works |
|
104 d1 = audioop.bias(data[0], 1, 100) |
|
105 d2 = audioop.bias(data[1], 2, 100) |
|
106 d4 = audioop.bias(data[2], 4, 100) |
|
107 if audioop.avg(d1, 1) != 101 or \ |
|
108 audioop.avg(d2, 2) != 101 or \ |
|
109 audioop.avg(d4, 4) != 101: |
|
110 return 0 |
|
111 return 1 |
|
112 |
|
113 def testlin2lin(data): |
|
114 if verbose: |
|
115 print 'lin2lin' |
|
116 # too simple: we test only the size |
|
117 for d1 in data: |
|
118 for d2 in data: |
|
119 got = len(d1)//3 |
|
120 wtd = len(d2)//3 |
|
121 if len(audioop.lin2lin(d1, got, wtd)) != len(d2): |
|
122 return 0 |
|
123 return 1 |
|
124 |
|
125 def testadpcm2lin(data): |
|
126 # Very cursory test |
|
127 if audioop.adpcm2lin('\0\0', 1, None) != ('\0\0\0\0', (0,0)): |
|
128 return 0 |
|
129 return 1 |
|
130 |
|
131 def testlin2adpcm(data): |
|
132 if verbose: |
|
133 print 'lin2adpcm' |
|
134 # Very cursory test |
|
135 if audioop.lin2adpcm('\0\0\0\0', 1, None) != ('\0\0', (0,0)): |
|
136 return 0 |
|
137 return 1 |
|
138 |
|
139 def testlin2alaw(data): |
|
140 if verbose: |
|
141 print 'lin2alaw' |
|
142 if audioop.lin2alaw(data[0], 1) != '\xd5\xc5\xf5' or \ |
|
143 audioop.lin2alaw(data[1], 2) != '\xd5\xd5\xd5' or \ |
|
144 audioop.lin2alaw(data[2], 4) != '\xd5\xd5\xd5': |
|
145 return 0 |
|
146 return 1 |
|
147 |
|
148 def testalaw2lin(data): |
|
149 if verbose: |
|
150 print 'alaw2lin' |
|
151 # Cursory |
|
152 d = audioop.lin2alaw(data[0], 1) |
|
153 if audioop.alaw2lin(d, 1) != data[0]: |
|
154 return 0 |
|
155 return 1 |
|
156 |
|
157 def testlin2ulaw(data): |
|
158 if verbose: |
|
159 print 'lin2ulaw' |
|
160 if audioop.lin2ulaw(data[0], 1) != '\xff\xe7\xdb' or \ |
|
161 audioop.lin2ulaw(data[1], 2) != '\xff\xff\xff' or \ |
|
162 audioop.lin2ulaw(data[2], 4) != '\xff\xff\xff': |
|
163 return 0 |
|
164 return 1 |
|
165 |
|
166 def testulaw2lin(data): |
|
167 if verbose: |
|
168 print 'ulaw2lin' |
|
169 # Cursory |
|
170 d = audioop.lin2ulaw(data[0], 1) |
|
171 if audioop.ulaw2lin(d, 1) != data[0]: |
|
172 return 0 |
|
173 return 1 |
|
174 |
|
175 def testmul(data): |
|
176 if verbose: |
|
177 print 'mul' |
|
178 data2 = [] |
|
179 for d in data: |
|
180 str = '' |
|
181 for s in d: |
|
182 str = str + chr(ord(s)*2) |
|
183 data2.append(str) |
|
184 if audioop.mul(data[0], 1, 2) != data2[0] or \ |
|
185 audioop.mul(data[1],2, 2) != data2[1] or \ |
|
186 audioop.mul(data[2], 4, 2) != data2[2]: |
|
187 return 0 |
|
188 return 1 |
|
189 |
|
190 def testratecv(data): |
|
191 if verbose: |
|
192 print 'ratecv' |
|
193 state = None |
|
194 d1, state = audioop.ratecv(data[0], 1, 1, 8000, 16000, state) |
|
195 d2, state = audioop.ratecv(data[0], 1, 1, 8000, 16000, state) |
|
196 if d1 + d2 != '\000\000\001\001\002\001\000\000\001\001\002': |
|
197 return 0 |
|
198 return 1 |
|
199 |
|
200 def testreverse(data): |
|
201 if verbose: |
|
202 print 'reverse' |
|
203 if audioop.reverse(data[0], 1) != '\2\1\0': |
|
204 return 0 |
|
205 return 1 |
|
206 |
|
207 def testtomono(data): |
|
208 if verbose: |
|
209 print 'tomono' |
|
210 data2 = '' |
|
211 for d in data[0]: |
|
212 data2 = data2 + d + d |
|
213 if audioop.tomono(data2, 1, 0.5, 0.5) != data[0]: |
|
214 return 0 |
|
215 return 1 |
|
216 |
|
217 def testtostereo(data): |
|
218 if verbose: |
|
219 print 'tostereo' |
|
220 data2 = '' |
|
221 for d in data[0]: |
|
222 data2 = data2 + d + d |
|
223 if audioop.tostereo(data[0], 1, 1, 1) != data2: |
|
224 return 0 |
|
225 return 1 |
|
226 |
|
227 def testfindfactor(data): |
|
228 if verbose: |
|
229 print 'findfactor' |
|
230 if audioop.findfactor(data[1], data[1]) != 1.0: |
|
231 return 0 |
|
232 return 1 |
|
233 |
|
234 def testfindfit(data): |
|
235 if verbose: |
|
236 print 'findfit' |
|
237 if audioop.findfit(data[1], data[1]) != (0, 1.0): |
|
238 return 0 |
|
239 return 1 |
|
240 |
|
241 def testfindmax(data): |
|
242 if verbose: |
|
243 print 'findmax' |
|
244 if audioop.findmax(data[1], 1) != 2: |
|
245 return 0 |
|
246 return 1 |
|
247 |
|
248 def testgetsample(data): |
|
249 if verbose: |
|
250 print 'getsample' |
|
251 for i in range(3): |
|
252 if audioop.getsample(data[0], 1, i) != i or \ |
|
253 audioop.getsample(data[1], 2, i) != i or \ |
|
254 audioop.getsample(data[2], 4, i) != i: |
|
255 return 0 |
|
256 return 1 |
|
257 |
|
258 def testone(name, data): |
|
259 try: |
|
260 func = eval('test'+name) |
|
261 except NameError: |
|
262 print 'No test found for audioop.'+name+'()' |
|
263 return |
|
264 try: |
|
265 rv = func(data) |
|
266 except 'xx': |
|
267 print 'Test FAILED for audioop.'+name+'() (with an exception)' |
|
268 return |
|
269 if not rv: |
|
270 print 'Test FAILED for audioop.'+name+'()' |
|
271 |
|
272 def testall(): |
|
273 data = [gendata1(), gendata2(), gendata4()] |
|
274 names = dir(audioop) |
|
275 # We know there is a routine 'add' |
|
276 routines = [] |
|
277 for n in names: |
|
278 if type(eval('audioop.'+n)) == type(audioop.add): |
|
279 routines.append(n) |
|
280 for n in routines: |
|
281 testone(n, data) |
|
282 testall() |