0001 # Test audioop. 0002 import audioop 0003 from test.test_support import verbose 0004 0005 def gendata1(): 0006 return '\0\1\2' 0007 0008 def gendata2(): 0009 if verbose: 0010 print 'getsample' 0011 if audioop.getsample('\0\1', 2, 0) == 1: 0012 return '\0\0\0\1\0\2' 0013 else: 0014 return '\0\0\1\0\2\0' 0015 0016 def gendata4(): 0017 if verbose: 0018 print 'getsample' 0019 if audioop.getsample('\0\0\0\1', 4, 0) == 1: 0020 return '\0\0\0\0\0\0\0\1\0\0\0\2' 0021 else: 0022 return '\0\0\0\0\1\0\0\0\2\0\0\0' 0023 0024 def testmax(data): 0025 if verbose: 0026 print 'max' 0027 if audioop.max(data[0], 1) != 2 or \ 0028 audioop.max(data[1], 2) != 2 or \ 0029 audioop.max(data[2], 4) != 2: 0030 return 0 0031 return 1 0032 0033 def testminmax(data): 0034 if verbose: 0035 print 'minmax' 0036 if audioop.minmax(data[0], 1) != (0, 2) or \ 0037 audioop.minmax(data[1], 2) != (0, 2) or \ 0038 audioop.minmax(data[2], 4) != (0, 2): 0039 return 0 0040 return 1 0041 0042 def testmaxpp(data): 0043 if verbose: 0044 print 'maxpp' 0045 if audioop.maxpp(data[0], 1) != 0 or \ 0046 audioop.maxpp(data[1], 2) != 0 or \ 0047 audioop.maxpp(data[2], 4) != 0: 0048 return 0 0049 return 1 0050 0051 def testavg(data): 0052 if verbose: 0053 print 'avg' 0054 if audioop.avg(data[0], 1) != 1 or \ 0055 audioop.avg(data[1], 2) != 1 or \ 0056 audioop.avg(data[2], 4) != 1: 0057 return 0 0058 return 1 0059 0060 def testavgpp(data): 0061 if verbose: 0062 print 'avgpp' 0063 if audioop.avgpp(data[0], 1) != 0 or \ 0064 audioop.avgpp(data[1], 2) != 0 or \ 0065 audioop.avgpp(data[2], 4) != 0: 0066 return 0 0067 return 1 0068 0069 def testrms(data): 0070 if audioop.rms(data[0], 1) != 1 or \ 0071 audioop.rms(data[1], 2) != 1 or \ 0072 audioop.rms(data[2], 4) != 1: 0073 return 0 0074 return 1 0075 0076 def testcross(data): 0077 if verbose: 0078 print 'cross' 0079 if audioop.cross(data[0], 1) != 0 or \ 0080 audioop.cross(data[1], 2) != 0 or \ 0081 audioop.cross(data[2], 4) != 0: 0082 return 0 0083 return 1 0084 0085 def testadd(data): 0086 if verbose: 0087 print 'add' 0088 data2 = [] 0089 for d in data: 0090 str = '' 0091 for s in d: 0092 str = str + chr(ord(s)*2) 0093 data2.append(str) 0094 if audioop.add(data[0], data[0], 1) != data2[0] or \ 0095 audioop.add(data[1], data[1], 2) != data2[1] or \ 0096 audioop.add(data[2], data[2], 4) != data2[2]: 0097 return 0 0098 return 1 0099 0100 def testbias(data): 0101 if verbose: 0102 print 'bias' 0103 # Note: this test assumes that avg() works 0104 d1 = audioop.bias(data[0], 1, 100) 0105 d2 = audioop.bias(data[1], 2, 100) 0106 d4 = audioop.bias(data[2], 4, 100) 0107 if audioop.avg(d1, 1) != 101 or \ 0108 audioop.avg(d2, 2) != 101 or \ 0109 audioop.avg(d4, 4) != 101: 0110 return 0 0111 return 1 0112 0113 def testlin2lin(data): 0114 if verbose: 0115 print 'lin2lin' 0116 # too simple: we test only the size 0117 for d1 in data: 0118 for d2 in data: 0119 got = len(d1)//3 0120 wtd = len(d2)//3 0121 if len(audioop.lin2lin(d1, got, wtd)) != len(d2): 0122 return 0 0123 return 1 0124 0125 def testadpcm2lin(data): 0126 # Very cursory test 0127 if audioop.adpcm2lin('\0\0', 1, None) != ('\0\0\0\0', (0,0)): 0128 return 0 0129 return 1 0130 0131 def testlin2adpcm(data): 0132 if verbose: 0133 print 'lin2adpcm' 0134 # Very cursory test 0135 if audioop.lin2adpcm('\0\0\0\0', 1, None) != ('\0\0', (0,0)): 0136 return 0 0137 return 1 0138 0139 def testlin2ulaw(data): 0140 if verbose: 0141 print 'lin2ulaw' 0142 if audioop.lin2ulaw(data[0], 1) != '\377\347\333' or \ 0143 audioop.lin2ulaw(data[1], 2) != '\377\377\377' or \ 0144 audioop.lin2ulaw(data[2], 4) != '\377\377\377': 0145 return 0 0146 return 1 0147 0148 def testulaw2lin(data): 0149 if verbose: 0150 print 'ulaw2lin' 0151 # Cursory 0152 d = audioop.lin2ulaw(data[0], 1) 0153 if audioop.ulaw2lin(d, 1) != data[0]: 0154 return 0 0155 return 1 0156 0157 def testmul(data): 0158 if verbose: 0159 print 'mul' 0160 data2 = [] 0161 for d in data: 0162 str = '' 0163 for s in d: 0164 str = str + chr(ord(s)*2) 0165 data2.append(str) 0166 if audioop.mul(data[0], 1, 2) != data2[0] or \ 0167 audioop.mul(data[1],2, 2) != data2[1] or \ 0168 audioop.mul(data[2], 4, 2) != data2[2]: 0169 return 0 0170 return 1 0171 0172 def testratecv(data): 0173 if verbose: 0174 print 'ratecv' 0175 state = None 0176 d1, state = audioop.ratecv(data[0], 1, 1, 8000, 16000, state) 0177 d2, state = audioop.ratecv(data[0], 1, 1, 8000, 16000, state) 0178 if d1 + d2 != '\000\000\001\001\002\001\000\000\001\001\002': 0179 return 0 0180 return 1 0181 0182 def testreverse(data): 0183 if verbose: 0184 print 'reverse' 0185 if audioop.reverse(data[0], 1) != '\2\1\0': 0186 return 0 0187 return 1 0188 0189 def testtomono(data): 0190 if verbose: 0191 print 'tomono' 0192 data2 = '' 0193 for d in data[0]: 0194 data2 = data2 + d + d 0195 if audioop.tomono(data2, 1, 0.5, 0.5) != data[0]: 0196 return 0 0197 return 1 0198 0199 def testtostereo(data): 0200 if verbose: 0201 print 'tostereo' 0202 data2 = '' 0203 for d in data[0]: 0204 data2 = data2 + d + d 0205 if audioop.tostereo(data[0], 1, 1, 1) != data2: 0206 return 0 0207 return 1 0208 0209 def testfindfactor(data): 0210 if verbose: 0211 print 'findfactor' 0212 if audioop.findfactor(data[1], data[1]) != 1.0: 0213 return 0 0214 return 1 0215 0216 def testfindfit(data): 0217 if verbose: 0218 print 'findfit' 0219 if audioop.findfit(data[1], data[1]) != (0, 1.0): 0220 return 0 0221 return 1 0222 0223 def testfindmax(data): 0224 if verbose: 0225 print 'findmax' 0226 if audioop.findmax(data[1], 1) != 2: 0227 return 0 0228 return 1 0229 0230 def testgetsample(data): 0231 if verbose: 0232 print 'getsample' 0233 for i in range(3): 0234 if audioop.getsample(data[0], 1, i) != i or \ 0235 audioop.getsample(data[1], 2, i) != i or \ 0236 audioop.getsample(data[2], 4, i) != i: 0237 return 0 0238 return 1 0239 0240 def testone(name, data): 0241 try: 0242 func = eval('test'+name) 0243 except NameError: 0244 print 'No test found for audioop.'+name+'()' 0245 return 0246 try: 0247 rv = func(data) 0248 except 'xx': 0249 print 'Test FAILED for audioop.'+name+'() (with an exception)' 0250 return 0251 if not rv: 0252 print 'Test FAILED for audioop.'+name+'()' 0253 0254 def testall(): 0255 data = [gendata1(), gendata2(), gendata4()] 0256 names = dir(audioop) 0257 # We know there is a routine 'add' 0258 routines = [] 0259 for n in names: 0260 if type(eval('audioop.'+n)) == type(audioop.add): 0261 routines.append(n) 0262 for n in routines: 0263 testone(n, data) 0264 testall() 0265
Generated by PyXR 0.9.4