Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 1 | # Test audioop. |
| 2 | import audioop |
| 3 | |
| 4 | def gendata1(): |
| 5 | return '\0\1\2' |
| 6 | |
| 7 | def gendata2(): |
| 8 | if audioop.getsample('\0\1', 2, 0) == 1: |
| 9 | return '\0\0\0\1\0\2' |
| 10 | else: |
| 11 | return '\0\0\1\0\2\0' |
| 12 | |
| 13 | def gendata4(): |
| 14 | if audioop.getsample('\0\0\0\1', 4, 0) == 1: |
| 15 | return '\0\0\0\0\0\0\0\1\0\0\0\2' |
| 16 | else: |
| 17 | return '\0\0\0\0\1\0\0\0\2\0\0\0' |
| 18 | |
| 19 | def testmax(data): |
| 20 | if audioop.max(data[0], 1) <> 2 or \ |
| 21 | audioop.max(data[1], 2) <> 2 or \ |
| 22 | audioop.max(data[2], 4) <> 2: |
| 23 | return 0 |
| 24 | return 1 |
| 25 | |
| 26 | def testmaxpp(data): |
| 27 | if audioop.maxpp(data[0], 1) <> 0 or \ |
| 28 | audioop.maxpp(data[1], 2) <> 0 or \ |
| 29 | audioop.maxpp(data[2], 4) <> 0: |
| 30 | return 0 |
| 31 | return 1 |
| 32 | |
| 33 | def testavg(data): |
| 34 | if audioop.avg(data[0], 1) <> 1 or \ |
| 35 | audioop.avg(data[1], 2) <> 1 or \ |
| 36 | audioop.avg(data[2], 4) <> 1: |
| 37 | return 0 |
| 38 | return 1 |
| 39 | |
| 40 | def testavgpp(data): |
| 41 | if audioop.avgpp(data[0], 1) <> 0 or \ |
| 42 | audioop.avgpp(data[1], 2) <> 0 or \ |
| 43 | audioop.avgpp(data[2], 4) <> 0: |
| 44 | return 0 |
| 45 | return 1 |
| 46 | |
| 47 | def testrms(data): |
| 48 | if audioop.rms(data[0], 1) <> 1 or \ |
| 49 | audioop.rms(data[1], 2) <> 1 or \ |
| 50 | audioop.rms(data[2], 4) <> 1: |
| 51 | return 0 |
| 52 | return 1 |
| 53 | |
| 54 | def testcross(data): |
| 55 | if audioop.cross(data[0], 1) <> 0 or \ |
| 56 | audioop.cross(data[1], 2) <> 0 or \ |
| 57 | audioop.cross(data[2], 4) <> 0: |
| 58 | return 0 |
| 59 | return 1 |
| 60 | |
| 61 | def testadd(data): |
| 62 | data2 = [] |
| 63 | for d in data: |
| 64 | str = '' |
| 65 | for s in d: |
| 66 | str = str + chr(ord(s)*2) |
| 67 | data2.append(str) |
| 68 | if audioop.add(data[0], data[0], 1) <> data2[0] or \ |
| 69 | audioop.add(data[1], data[1], 2) <> data2[1] or \ |
| 70 | audioop.add(data[2], data[2], 4) <> data2[2]: |
| 71 | return 0 |
| 72 | return 1 |
| 73 | |
| 74 | def testbias(data): |
| 75 | # Note: this test assumes that avg() works |
| 76 | d1 = audioop.bias(data[0], 1, 100) |
| 77 | d2 = audioop.bias(data[1], 2, 100) |
| 78 | d4 = audioop.bias(data[2], 4, 100) |
| 79 | if audioop.avg(d1, 1) <> 101 or \ |
| 80 | audioop.avg(d2, 2) <> 101 or \ |
| 81 | audioop.avg(d4, 4) <> 101: |
| 82 | return 0 |
| 83 | return 1 |
| 84 | |
| 85 | def testlin2lin(data): |
| 86 | # too simple: we test only the size |
| 87 | for d1 in data: |
| 88 | for d2 in data: |
| 89 | got = len(d1)/3 |
| 90 | wtd = len(d2)/3 |
| 91 | if len(audioop.lin2lin(d1, got, wtd)) <> len(d2): |
| 92 | return 0 |
| 93 | return 1 |
| 94 | |
| 95 | def testadpcm2lin(data): |
| 96 | # Very cursory test |
| 97 | if audioop.adpcm2lin('\0\0', 1, None) <> ('\0\0\0\0', (0,0)): |
| 98 | return 0 |
| 99 | return 1 |
| 100 | |
| 101 | def testlin2adpcm(data): |
| 102 | # Very cursory test |
| 103 | if audioop.lin2adpcm('\0\0\0\0', 1, None) <> ('\0\0', (0,0)): |
| 104 | return 0 |
| 105 | return 1 |
| 106 | |
| 107 | def testlin2ulaw(data): |
| 108 | if audioop.lin2ulaw(data[0], 1) <> '\377\347\333' or \ |
| 109 | audioop.lin2ulaw(data[1], 2) <> '\377\377\377' or \ |
| 110 | audioop.lin2ulaw(data[2], 4) <> '\377\377\377': |
| 111 | return 0 |
| 112 | return 1 |
| 113 | |
| 114 | def testulaw2lin(data): |
| 115 | # Cursory |
| 116 | d = audioop.lin2ulaw(data[0], 1) |
| 117 | if audioop.ulaw2lin(d, 1) <> data[0]: |
| 118 | return 0 |
| 119 | return 1 |
| 120 | |
| 121 | def testmul(data): |
| 122 | data2 = [] |
| 123 | for d in data: |
| 124 | str = '' |
| 125 | for s in d: |
| 126 | str = str + chr(ord(s)*2) |
| 127 | data2.append(str) |
| 128 | if audioop.mul(data[0], 1, 2) <> data2[0] or \ |
| 129 | audioop.mul(data[1],2, 2) <> data2[1] or \ |
| 130 | audioop.mul(data[2], 4, 2) <> data2[2]: |
| 131 | return 0 |
| 132 | return 1 |
| 133 | |
| 134 | def testreverse(data): |
| 135 | if audioop.reverse(data[0], 1) <> '\2\1\0': |
| 136 | return 0 |
| 137 | return 1 |
| 138 | |
| 139 | def testtomono(data): |
| 140 | data2 = '' |
| 141 | for d in data[0]: |
| 142 | data2 = data2 + d + d |
| 143 | if audioop.tomono(data2, 1, 0.5, 0.5) <> data[0]: |
| 144 | return 0 |
| 145 | return 1 |
| 146 | |
| 147 | def testtostereo(data): |
| 148 | data2 = '' |
| 149 | for d in data[0]: |
| 150 | data2 = data2 + d + d |
| 151 | if audioop.tostereo(data[0], 1, 1, 1) <> data2: |
| 152 | return 0 |
| 153 | return 1 |
| 154 | |
| 155 | def testfindfactor(data): |
| 156 | if audioop.findfactor(data[1], data[1]) <> 1.0: |
| 157 | return 0 |
| 158 | return 1 |
| 159 | |
| 160 | def testfindfit(data): |
| 161 | if audioop.findfit(data[1], data[1]) <> (0, 1.0): |
| 162 | return 0 |
| 163 | return 1 |
| 164 | |
| 165 | def testfindmax(data): |
| 166 | if audioop.findmax(data[1], 1) <> 2: |
| 167 | return 0 |
| 168 | return 1 |
| 169 | |
| 170 | def testgetsample(data): |
| 171 | for i in range(3): |
| 172 | if audioop.getsample(data[0], 1, i) <> i or \ |
| 173 | audioop.getsample(data[1], 2, i) <> i or \ |
| 174 | audioop.getsample(data[2], 4, i) <> i: |
| 175 | return 0 |
| 176 | return 1 |
| 177 | |
| 178 | def testone(name, data): |
| 179 | try: |
| 180 | func = eval('test'+name) |
| 181 | except NameError: |
| 182 | print 'No test found for audioop.'+name+'()' |
| 183 | return |
| 184 | try: |
| 185 | rv = func(data) |
| 186 | except 'xx': |
| 187 | print 'Test FAILED for audioop.'+name+'() (with an exception)' |
| 188 | return |
| 189 | if not rv: |
| 190 | print 'Test FAILED for audioop.'+name+'()' |
| 191 | |
| 192 | def testall(): |
| 193 | data = [gendata1(), gendata2(), gendata4()] |
| 194 | names = dir(audioop) |
| 195 | # We know there is a routine 'add' |
| 196 | routines = [] |
| 197 | for n in names: |
| 198 | if type(eval('audioop.'+n)) == type(audioop.add): |
| 199 | routines.append(n) |
| 200 | for n in routines: |
| 201 | testone(n, data) |
| 202 | testall() |