PageRenderTime 45ms CodeModel.GetById 2ms app.highlight 40ms RepoModel.GetById 1ms app.codeStats 0ms

/Lib/test/test_audioop.py

http://unladen-swallow.googlecode.com/
Python | 173 lines | 158 code | 15 blank | 0 comment | 4 complexity | 0b0020a20086d393786154be9694bcdb MD5 | raw file
  1import audioop
  2import unittest
  3from test.test_support import run_unittest
  4
  5
  6def gendata1():
  7    return '\0\1\2'
  8
  9def gendata2():
 10    if audioop.getsample('\0\1', 2, 0) == 1:
 11        return '\0\0\0\1\0\2'
 12    else:
 13        return '\0\0\1\0\2\0'
 14
 15def gendata4():
 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'
 18    else:
 19        return '\0\0\0\0\1\0\0\0\2\0\0\0'
 20
 21data = [gendata1(), gendata2(), gendata4()]
 22
 23
 24class TestAudioop(unittest.TestCase):
 25
 26    def test_max(self):
 27        self.assertEqual(audioop.max(data[0], 1), 2)
 28        self.assertEqual(audioop.max(data[1], 2), 2)
 29        self.assertEqual(audioop.max(data[2], 4), 2)
 30
 31    def test_minmax(self):
 32        self.assertEqual(audioop.minmax(data[0], 1), (0, 2))
 33        self.assertEqual(audioop.minmax(data[1], 2), (0, 2))
 34        self.assertEqual(audioop.minmax(data[2], 4), (0, 2))
 35
 36    def test_maxpp(self):
 37        self.assertEqual(audioop.maxpp(data[0], 1), 0)
 38        self.assertEqual(audioop.maxpp(data[1], 2), 0)
 39        self.assertEqual(audioop.maxpp(data[2], 4), 0)
 40
 41    def test_avg(self):
 42        self.assertEqual(audioop.avg(data[0], 1), 1)
 43        self.assertEqual(audioop.avg(data[1], 2), 1)
 44        self.assertEqual(audioop.avg(data[2], 4), 1)
 45
 46    def test_avgpp(self):
 47        self.assertEqual(audioop.avgpp(data[0], 1), 0)
 48        self.assertEqual(audioop.avgpp(data[1], 2), 0)
 49        self.assertEqual(audioop.avgpp(data[2], 4), 0)
 50
 51    def test_rms(self):
 52        self.assertEqual(audioop.rms(data[0], 1), 1)
 53        self.assertEqual(audioop.rms(data[1], 2), 1)
 54        self.assertEqual(audioop.rms(data[2], 4), 1)
 55
 56    def test_cross(self):
 57        self.assertEqual(audioop.cross(data[0], 1), 0)
 58        self.assertEqual(audioop.cross(data[1], 2), 0)
 59        self.assertEqual(audioop.cross(data[2], 4), 0)
 60
 61    def test_add(self):
 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        self.assertEqual(audioop.add(data[0], data[0], 1), data2[0])
 69        self.assertEqual(audioop.add(data[1], data[1], 2), data2[1])
 70        self.assertEqual(audioop.add(data[2], data[2], 4), data2[2])
 71
 72    def test_bias(self):
 73        # Note: this test assumes that avg() works
 74        d1 = audioop.bias(data[0], 1, 100)
 75        d2 = audioop.bias(data[1], 2, 100)
 76        d4 = audioop.bias(data[2], 4, 100)
 77        self.assertEqual(audioop.avg(d1, 1), 101)
 78        self.assertEqual(audioop.avg(d2, 2), 101)
 79        self.assertEqual(audioop.avg(d4, 4), 101)
 80
 81    def test_lin2lin(self):
 82        # too simple: we test only the size
 83        for d1 in data:
 84            for d2 in data:
 85                got = len(d1)//3
 86                wtd = len(d2)//3
 87                self.assertEqual(len(audioop.lin2lin(d1, got, wtd)), len(d2))
 88
 89    def test_adpcm2lin(self):
 90        # Very cursory test
 91        self.assertEqual(audioop.adpcm2lin('\0\0', 1, None), ('\0\0\0\0', (0,0)))
 92
 93    def test_lin2adpcm(self):
 94        # Very cursory test
 95        self.assertEqual(audioop.lin2adpcm('\0\0\0\0', 1, None), ('\0\0', (0,0)))
 96
 97    def test_lin2alaw(self):
 98        self.assertEqual(audioop.lin2alaw(data[0], 1), '\xd5\xc5\xf5')
 99        self.assertEqual(audioop.lin2alaw(data[1], 2), '\xd5\xd5\xd5')
100        self.assertEqual(audioop.lin2alaw(data[2], 4), '\xd5\xd5\xd5')
101
102    def test_alaw2lin(self):
103        # Cursory
104        d = audioop.lin2alaw(data[0], 1)
105        self.assertEqual(audioop.alaw2lin(d, 1), data[0])
106
107    def test_lin2ulaw(self):
108        self.assertEqual(audioop.lin2ulaw(data[0], 1), '\xff\xe7\xdb')
109        self.assertEqual(audioop.lin2ulaw(data[1], 2), '\xff\xff\xff')
110        self.assertEqual(audioop.lin2ulaw(data[2], 4), '\xff\xff\xff')
111
112    def test_ulaw2lin(self):
113        # Cursory
114        d = audioop.lin2ulaw(data[0], 1)
115        self.assertEqual(audioop.ulaw2lin(d, 1), data[0])
116
117    def test_mul(self):
118        data2 = []
119        for d in data:
120            str = ''
121            for s in d:
122                str = str + chr(ord(s)*2)
123            data2.append(str)
124        self.assertEqual(audioop.mul(data[0], 1, 2), data2[0])
125        self.assertEqual(audioop.mul(data[1],2, 2), data2[1])
126        self.assertEqual(audioop.mul(data[2], 4, 2), data2[2])
127
128    def test_ratecv(self):
129        state = None
130        d1, state = audioop.ratecv(data[0], 1, 1, 8000, 16000, state)
131        d2, state = audioop.ratecv(data[0], 1, 1, 8000, 16000, state)
132        self.assertEqual(d1 + d2, '\000\000\001\001\002\001\000\000\001\001\002')
133
134    def test_reverse(self):
135        self.assertEqual(audioop.reverse(data[0], 1), '\2\1\0')
136
137    def test_tomono(self):
138        data2 = ''
139        for d in data[0]:
140            data2 = data2 + d + d
141        self.assertEqual(audioop.tomono(data2, 1, 0.5, 0.5), data[0])
142
143    def test_tostereo(self):
144        data2 = ''
145        for d in data[0]:
146            data2 = data2 + d + d
147        self.assertEqual(audioop.tostereo(data[0], 1, 1, 1), data2)
148
149    def test_findfactor(self):
150        self.assertEqual(audioop.findfactor(data[1], data[1]), 1.0)
151
152    def test_findfit(self):
153        self.assertEqual(audioop.findfit(data[1], data[1]), (0, 1.0))
154
155    def test_findmax(self):
156        self.assertEqual(audioop.findmax(data[1], 1), 2)
157
158    def test_getsample(self):
159        for i in range(3):
160            self.assertEqual(audioop.getsample(data[0], 1, i), i)
161            self.assertEqual(audioop.getsample(data[1], 2, i), i)
162            self.assertEqual(audioop.getsample(data[2], 4, i), i)
163
164    def test_negativelen(self):
165        # from issue 3306, previously it segfaulted
166        self.assertRaises(audioop.error,
167            audioop.findmax, ''.join( chr(x) for x in xrange(256)), -2392392)
168
169def test_main():
170    run_unittest(TestAudioop)
171
172if __name__ == '__main__':
173    test_main()