Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 1 | # Test audioop. |
| 2 | import audioop |
Roger E. Masse | 7a61f87 | 1997-01-03 19:20:17 +0000 | [diff] [blame] | 3 | from test_support import verbose |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 4 | |
| 5 | def gendata1(): |
Roger E. Masse | 7a61f87 | 1997-01-03 19:20:17 +0000 | [diff] [blame] | 6 | return '\0\1\2' |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 7 | |
| 8 | def gendata2(): |
Roger E. Masse | 7a61f87 | 1997-01-03 19:20:17 +0000 | [diff] [blame] | 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' |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 15 | |
| 16 | def gendata4(): |
Roger E. Masse | 7a61f87 | 1997-01-03 19:20:17 +0000 | [diff] [blame] | 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' |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 23 | |
| 24 | def testmax(data): |
Roger E. Masse | 7a61f87 | 1997-01-03 19:20:17 +0000 | [diff] [blame] | 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 |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 32 | |
Guido van Rossum | 4fdb744 | 1996-12-20 03:13:06 +0000 | [diff] [blame] | 33 | def testminmax(data): |
Roger E. Masse | 7a61f87 | 1997-01-03 19:20:17 +0000 | [diff] [blame] | 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 |
Guido van Rossum | 4fdb744 | 1996-12-20 03:13:06 +0000 | [diff] [blame] | 41 | |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 42 | def testmaxpp(data): |
Roger E. Masse | 7a61f87 | 1997-01-03 19:20:17 +0000 | [diff] [blame] | 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 |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 50 | |
| 51 | def testavg(data): |
Roger E. Masse | 7a61f87 | 1997-01-03 19:20:17 +0000 | [diff] [blame] | 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 |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 59 | |
| 60 | def testavgpp(data): |
Roger E. Masse | 7a61f87 | 1997-01-03 19:20:17 +0000 | [diff] [blame] | 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 |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 68 | |
| 69 | def testrms(data): |
Roger E. Masse | 7a61f87 | 1997-01-03 19:20:17 +0000 | [diff] [blame] | 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 |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 75 | |
| 76 | def testcross(data): |
Roger E. Masse | 7a61f87 | 1997-01-03 19:20:17 +0000 | [diff] [blame] | 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 |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 84 | |
| 85 | def testadd(data): |
Roger E. Masse | 7a61f87 | 1997-01-03 19:20:17 +0000 | [diff] [blame] | 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 |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 99 | |
| 100 | def testbias(data): |
Roger E. Masse | 7a61f87 | 1997-01-03 19:20:17 +0000 | [diff] [blame] | 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 |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 112 | |
| 113 | def testlin2lin(data): |
Roger E. Masse | 7a61f87 | 1997-01-03 19:20:17 +0000 | [diff] [blame] | 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 |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 124 | |
| 125 | def testadpcm2lin(data): |
Roger E. Masse | 7a61f87 | 1997-01-03 19:20:17 +0000 | [diff] [blame] | 126 | # Very cursory test |
| 127 | if audioop.adpcm2lin('\0\0', 1, None) <> ('\0\0\0\0', (0,0)): |
| 128 | return 0 |
| 129 | return 1 |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 130 | |
| 131 | def testlin2adpcm(data): |
Roger E. Masse | 7a61f87 | 1997-01-03 19:20:17 +0000 | [diff] [blame] | 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 |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 138 | |
| 139 | def testlin2ulaw(data): |
Roger E. Masse | 7a61f87 | 1997-01-03 19:20:17 +0000 | [diff] [blame] | 140 | if verbose: |
| 141 | print 'lin2ulaw' |
| 142 | if audioop.lin2ulaw(data[0], 1) <> '\377\347\333' or \ |
| 143 | audioop.lin2ulaw(data[1], 2) <> '\377\377\377' or \ |
| 144 | audioop.lin2ulaw(data[2], 4) <> '\377\377\377': |
| 145 | return 0 |
| 146 | return 1 |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 147 | |
| 148 | def testulaw2lin(data): |
Roger E. Masse | 7a61f87 | 1997-01-03 19:20:17 +0000 | [diff] [blame] | 149 | if verbose: |
| 150 | print 'ulaw2lin' |
| 151 | # Cursory |
| 152 | d = audioop.lin2ulaw(data[0], 1) |
| 153 | if audioop.ulaw2lin(d, 1) <> data[0]: |
| 154 | return 0 |
| 155 | return 1 |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 156 | |
| 157 | def testmul(data): |
Roger E. Masse | 7a61f87 | 1997-01-03 19:20:17 +0000 | [diff] [blame] | 158 | if verbose: |
| 159 | print 'mul' |
| 160 | data2 = [] |
| 161 | for d in data: |
| 162 | str = '' |
| 163 | for s in d: |
| 164 | str = str + chr(ord(s)*2) |
| 165 | data2.append(str) |
| 166 | if audioop.mul(data[0], 1, 2) <> data2[0] or \ |
| 167 | audioop.mul(data[1],2, 2) <> data2[1] or \ |
| 168 | audioop.mul(data[2], 4, 2) <> data2[2]: |
| 169 | return 0 |
| 170 | return 1 |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 171 | |
Guido van Rossum | c8504e2 | 1997-02-14 15:58:00 +0000 | [diff] [blame] | 172 | def testratecv(data): |
| 173 | if verbose: |
Guido van Rossum | 05ba2ab | 1997-05-21 14:27:20 +0000 | [diff] [blame] | 174 | print 'ratecv' |
| 175 | state = None |
| 176 | d1, state = audioop.ratecv(data[0], 1, 1, 8000, 16000, state) |
| 177 | d2, state = audioop.ratecv(data[0], 1, 1, 8000, 16000, state) |
| 178 | if d1 + d2 != '\000\000\001\001\002\001\000\000\001\001\002': |
| 179 | return 0 |
Guido van Rossum | c8504e2 | 1997-02-14 15:58:00 +0000 | [diff] [blame] | 180 | return 1 |
| 181 | |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 182 | def testreverse(data): |
Roger E. Masse | 7a61f87 | 1997-01-03 19:20:17 +0000 | [diff] [blame] | 183 | if verbose: |
| 184 | print 'reverse' |
| 185 | if audioop.reverse(data[0], 1) <> '\2\1\0': |
| 186 | return 0 |
| 187 | return 1 |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 188 | |
| 189 | def testtomono(data): |
Roger E. Masse | 7a61f87 | 1997-01-03 19:20:17 +0000 | [diff] [blame] | 190 | if verbose: |
| 191 | print 'tomono' |
| 192 | data2 = '' |
| 193 | for d in data[0]: |
| 194 | data2 = data2 + d + d |
| 195 | if audioop.tomono(data2, 1, 0.5, 0.5) <> data[0]: |
| 196 | return 0 |
| 197 | return 1 |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 198 | |
| 199 | def testtostereo(data): |
Roger E. Masse | 7a61f87 | 1997-01-03 19:20:17 +0000 | [diff] [blame] | 200 | if verbose: |
| 201 | print 'tostereo' |
| 202 | data2 = '' |
| 203 | for d in data[0]: |
| 204 | data2 = data2 + d + d |
| 205 | if audioop.tostereo(data[0], 1, 1, 1) <> data2: |
| 206 | return 0 |
| 207 | return 1 |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 208 | |
| 209 | def testfindfactor(data): |
Roger E. Masse | 7a61f87 | 1997-01-03 19:20:17 +0000 | [diff] [blame] | 210 | if verbose: |
| 211 | print 'findfactor' |
| 212 | if audioop.findfactor(data[1], data[1]) <> 1.0: |
| 213 | return 0 |
| 214 | return 1 |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 215 | |
| 216 | def testfindfit(data): |
Roger E. Masse | 7a61f87 | 1997-01-03 19:20:17 +0000 | [diff] [blame] | 217 | if verbose: |
| 218 | print 'findfit' |
| 219 | if audioop.findfit(data[1], data[1]) <> (0, 1.0): |
| 220 | return 0 |
| 221 | return 1 |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 222 | |
| 223 | def testfindmax(data): |
Roger E. Masse | 7a61f87 | 1997-01-03 19:20:17 +0000 | [diff] [blame] | 224 | if verbose: |
| 225 | print 'findmax' |
| 226 | if audioop.findmax(data[1], 1) <> 2: |
| 227 | return 0 |
| 228 | return 1 |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 229 | |
| 230 | def testgetsample(data): |
Roger E. Masse | 7a61f87 | 1997-01-03 19:20:17 +0000 | [diff] [blame] | 231 | if verbose: |
| 232 | print 'getsample' |
| 233 | for i in range(3): |
| 234 | if audioop.getsample(data[0], 1, i) <> i or \ |
| 235 | audioop.getsample(data[1], 2, i) <> i or \ |
| 236 | audioop.getsample(data[2], 4, i) <> i: |
| 237 | return 0 |
| 238 | return 1 |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 239 | |
| 240 | def testone(name, data): |
Roger E. Masse | 7a61f87 | 1997-01-03 19:20:17 +0000 | [diff] [blame] | 241 | try: |
| 242 | func = eval('test'+name) |
| 243 | except NameError: |
| 244 | print 'No test found for audioop.'+name+'()' |
| 245 | return |
| 246 | try: |
| 247 | rv = func(data) |
| 248 | except 'xx': |
| 249 | print 'Test FAILED for audioop.'+name+'() (with an exception)' |
| 250 | return |
| 251 | if not rv: |
| 252 | print 'Test FAILED for audioop.'+name+'()' |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 253 | |
| 254 | def testall(): |
Roger E. Masse | 7a61f87 | 1997-01-03 19:20:17 +0000 | [diff] [blame] | 255 | data = [gendata1(), gendata2(), gendata4()] |
| 256 | names = dir(audioop) |
| 257 | # We know there is a routine 'add' |
| 258 | routines = [] |
| 259 | for n in names: |
| 260 | if type(eval('audioop.'+n)) == type(audioop.add): |
| 261 | routines.append(n) |
| 262 | for n in routines: |
| 263 | testone(n, data) |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 264 | testall() |