Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 1 | import audioop |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 2 | import unittest |
| 3 | from test.support import run_unittest |
| 4 | |
Antoine Pitrou | f99f333 | 2012-02-04 16:44:21 +0100 | [diff] [blame] | 5 | endian = 'big' if audioop.getsample(b'\0\1', 2, 0) == 1 else 'little' |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 6 | |
| 7 | def gendata1(): |
Martin v. Löwis | 3a2e5ce | 2007-07-20 07:36:26 +0000 | [diff] [blame] | 8 | return b'\0\1\2' |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 9 | |
| 10 | def gendata2(): |
Antoine Pitrou | f99f333 | 2012-02-04 16:44:21 +0100 | [diff] [blame] | 11 | if endian == 'big': |
Martin v. Löwis | 3a2e5ce | 2007-07-20 07:36:26 +0000 | [diff] [blame] | 12 | return b'\0\0\0\1\0\2' |
Roger E. Masse | 7a61f87 | 1997-01-03 19:20:17 +0000 | [diff] [blame] | 13 | else: |
Martin v. Löwis | 3a2e5ce | 2007-07-20 07:36:26 +0000 | [diff] [blame] | 14 | return b'\0\0\1\0\2\0' |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 15 | |
| 16 | def gendata4(): |
Antoine Pitrou | f99f333 | 2012-02-04 16:44:21 +0100 | [diff] [blame] | 17 | if endian == 'big': |
Martin v. Löwis | 3a2e5ce | 2007-07-20 07:36:26 +0000 | [diff] [blame] | 18 | return b'\0\0\0\0\0\0\0\1\0\0\0\2' |
Roger E. Masse | 7a61f87 | 1997-01-03 19:20:17 +0000 | [diff] [blame] | 19 | else: |
Martin v. Löwis | 3a2e5ce | 2007-07-20 07:36:26 +0000 | [diff] [blame] | 20 | return b'\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] | 21 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 22 | data = [gendata1(), gendata2(), gendata4()] |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 23 | |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 24 | INVALID_DATA = [ |
Antoine Pitrou | 75ff65e | 2012-01-28 22:01:59 +0100 | [diff] [blame] | 25 | (b'abc', 0), |
| 26 | (b'abc', 2), |
| 27 | (b'abc', 4), |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 28 | ] |
| 29 | |
Guido van Rossum | 4fdb744 | 1996-12-20 03:13:06 +0000 | [diff] [blame] | 30 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 31 | class TestAudioop(unittest.TestCase): |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 32 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 33 | def test_max(self): |
| 34 | self.assertEqual(audioop.max(data[0], 1), 2) |
| 35 | self.assertEqual(audioop.max(data[1], 2), 2) |
| 36 | self.assertEqual(audioop.max(data[2], 4), 2) |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 37 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 38 | def test_minmax(self): |
| 39 | self.assertEqual(audioop.minmax(data[0], 1), (0, 2)) |
| 40 | self.assertEqual(audioop.minmax(data[1], 2), (0, 2)) |
| 41 | self.assertEqual(audioop.minmax(data[2], 4), (0, 2)) |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 42 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 43 | def test_maxpp(self): |
| 44 | self.assertEqual(audioop.maxpp(data[0], 1), 0) |
| 45 | self.assertEqual(audioop.maxpp(data[1], 2), 0) |
| 46 | self.assertEqual(audioop.maxpp(data[2], 4), 0) |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 47 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 48 | def test_avg(self): |
| 49 | self.assertEqual(audioop.avg(data[0], 1), 1) |
| 50 | self.assertEqual(audioop.avg(data[1], 2), 1) |
| 51 | self.assertEqual(audioop.avg(data[2], 4), 1) |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 52 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 53 | def test_avgpp(self): |
| 54 | self.assertEqual(audioop.avgpp(data[0], 1), 0) |
| 55 | self.assertEqual(audioop.avgpp(data[1], 2), 0) |
| 56 | self.assertEqual(audioop.avgpp(data[2], 4), 0) |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 57 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 58 | def test_rms(self): |
| 59 | self.assertEqual(audioop.rms(data[0], 1), 1) |
| 60 | self.assertEqual(audioop.rms(data[1], 2), 1) |
| 61 | self.assertEqual(audioop.rms(data[2], 4), 1) |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 62 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 63 | def test_cross(self): |
| 64 | self.assertEqual(audioop.cross(data[0], 1), 0) |
| 65 | self.assertEqual(audioop.cross(data[1], 2), 0) |
| 66 | self.assertEqual(audioop.cross(data[2], 4), 0) |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 67 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 68 | def test_add(self): |
| 69 | data2 = [] |
| 70 | for d in data: |
| 71 | str = bytearray(len(d)) |
| 72 | for i,b in enumerate(d): |
| 73 | str[i] = 2*b |
| 74 | data2.append(str) |
| 75 | self.assertEqual(audioop.add(data[0], data[0], 1), data2[0]) |
| 76 | self.assertEqual(audioop.add(data[1], data[1], 2), data2[1]) |
| 77 | self.assertEqual(audioop.add(data[2], data[2], 4), data2[2]) |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 78 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 79 | def test_bias(self): |
| 80 | # Note: this test assumes that avg() works |
| 81 | d1 = audioop.bias(data[0], 1, 100) |
| 82 | d2 = audioop.bias(data[1], 2, 100) |
| 83 | d4 = audioop.bias(data[2], 4, 100) |
| 84 | self.assertEqual(audioop.avg(d1, 1), 101) |
| 85 | self.assertEqual(audioop.avg(d2, 2), 101) |
| 86 | self.assertEqual(audioop.avg(d4, 4), 101) |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 87 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 88 | def test_lin2lin(self): |
| 89 | # too simple: we test only the size |
| 90 | for d1 in data: |
| 91 | for d2 in data: |
| 92 | got = len(d1)//3 |
| 93 | wtd = len(d2)//3 |
| 94 | self.assertEqual(len(audioop.lin2lin(d1, got, wtd)), len(d2)) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 95 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 96 | def test_adpcm2lin(self): |
| 97 | # Very cursory test |
Antoine Pitrou | 75ff65e | 2012-01-28 22:01:59 +0100 | [diff] [blame] | 98 | self.assertEqual(audioop.adpcm2lin(b'\0\0', 1, None), (b'\0' * 4, (0,0))) |
| 99 | self.assertEqual(audioop.adpcm2lin(b'\0\0', 2, None), (b'\0' * 8, (0,0))) |
| 100 | self.assertEqual(audioop.adpcm2lin(b'\0\0', 4, None), (b'\0' * 16, (0,0))) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 101 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 102 | def test_lin2adpcm(self): |
| 103 | # Very cursory test |
| 104 | self.assertEqual(audioop.lin2adpcm(b'\0\0\0\0', 1, None), (b'\0\0', (0,0))) |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 105 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 106 | def test_lin2alaw(self): |
| 107 | self.assertEqual(audioop.lin2alaw(data[0], 1), b'\xd5\xc5\xf5') |
| 108 | self.assertEqual(audioop.lin2alaw(data[1], 2), b'\xd5\xd5\xd5') |
| 109 | self.assertEqual(audioop.lin2alaw(data[2], 4), b'\xd5\xd5\xd5') |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 110 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 111 | def test_alaw2lin(self): |
| 112 | # Cursory |
| 113 | d = audioop.lin2alaw(data[0], 1) |
| 114 | self.assertEqual(audioop.alaw2lin(d, 1), data[0]) |
Antoine Pitrou | f99f333 | 2012-02-04 16:44:21 +0100 | [diff] [blame] | 115 | if endian == 'big': |
| 116 | self.assertEqual(audioop.alaw2lin(d, 2), |
| 117 | b'\x00\x08\x01\x08\x02\x10') |
| 118 | self.assertEqual(audioop.alaw2lin(d, 4), |
| 119 | b'\x00\x08\x00\x00\x01\x08\x00\x00\x02\x10\x00\x00') |
| 120 | else: |
| 121 | self.assertEqual(audioop.alaw2lin(d, 2), |
| 122 | b'\x08\x00\x08\x01\x10\x02') |
| 123 | self.assertEqual(audioop.alaw2lin(d, 4), |
| 124 | b'\x00\x00\x08\x00\x00\x00\x08\x01\x00\x00\x10\x02') |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 125 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 126 | def test_lin2ulaw(self): |
| 127 | self.assertEqual(audioop.lin2ulaw(data[0], 1), b'\xff\xe7\xdb') |
| 128 | self.assertEqual(audioop.lin2ulaw(data[1], 2), b'\xff\xff\xff') |
| 129 | self.assertEqual(audioop.lin2ulaw(data[2], 4), b'\xff\xff\xff') |
Guido van Rossum | c8504e2 | 1997-02-14 15:58:00 +0000 | [diff] [blame] | 130 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 131 | def test_ulaw2lin(self): |
| 132 | # Cursory |
| 133 | d = audioop.lin2ulaw(data[0], 1) |
| 134 | self.assertEqual(audioop.ulaw2lin(d, 1), data[0]) |
Antoine Pitrou | f99f333 | 2012-02-04 16:44:21 +0100 | [diff] [blame] | 135 | if endian == 'big': |
| 136 | self.assertEqual(audioop.ulaw2lin(d, 2), |
| 137 | b'\x00\x00\x01\x04\x02\x0c') |
| 138 | self.assertEqual(audioop.ulaw2lin(d, 4), |
| 139 | b'\x00\x00\x00\x00\x01\x04\x00\x00\x02\x0c\x00\x00') |
| 140 | else: |
| 141 | self.assertEqual(audioop.ulaw2lin(d, 2), |
| 142 | b'\x00\x00\x04\x01\x0c\x02') |
| 143 | self.assertEqual(audioop.ulaw2lin(d, 4), |
| 144 | b'\x00\x00\x00\x00\x00\x00\x04\x01\x00\x00\x0c\x02') |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 145 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 146 | def test_mul(self): |
| 147 | data2 = [] |
| 148 | for d in data: |
| 149 | str = bytearray(len(d)) |
| 150 | for i,b in enumerate(d): |
| 151 | str[i] = 2*b |
| 152 | data2.append(str) |
| 153 | self.assertEqual(audioop.mul(data[0], 1, 2), data2[0]) |
| 154 | self.assertEqual(audioop.mul(data[1],2, 2), data2[1]) |
| 155 | self.assertEqual(audioop.mul(data[2], 4, 2), data2[2]) |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 156 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 157 | def test_ratecv(self): |
| 158 | state = None |
| 159 | d1, state = audioop.ratecv(data[0], 1, 1, 8000, 16000, state) |
| 160 | d2, state = audioop.ratecv(data[0], 1, 1, 8000, 16000, state) |
| 161 | self.assertEqual(d1 + d2, b'\000\000\001\001\002\001\000\000\001\001\002') |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 162 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 163 | def test_reverse(self): |
| 164 | self.assertEqual(audioop.reverse(data[0], 1), b'\2\1\0') |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 165 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 166 | def test_tomono(self): |
| 167 | data2 = bytearray() |
| 168 | for d in data[0]: |
| 169 | data2.append(d) |
| 170 | data2.append(d) |
| 171 | self.assertEqual(audioop.tomono(data2, 1, 0.5, 0.5), data[0]) |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 172 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 173 | def test_tostereo(self): |
| 174 | data2 = bytearray() |
| 175 | for d in data[0]: |
| 176 | data2.append(d) |
| 177 | data2.append(d) |
| 178 | self.assertEqual(audioop.tostereo(data[0], 1, 1, 1), data2) |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 179 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 180 | def test_findfactor(self): |
| 181 | self.assertEqual(audioop.findfactor(data[1], data[1]), 1.0) |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 182 | |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 183 | def test_findfit(self): |
| 184 | self.assertEqual(audioop.findfit(data[1], data[1]), (0, 1.0)) |
| 185 | |
| 186 | def test_findmax(self): |
| 187 | self.assertEqual(audioop.findmax(data[1], 1), 2) |
| 188 | |
| 189 | def test_getsample(self): |
| 190 | for i in range(3): |
| 191 | self.assertEqual(audioop.getsample(data[0], 1, i), i) |
| 192 | self.assertEqual(audioop.getsample(data[1], 2, i), i) |
| 193 | self.assertEqual(audioop.getsample(data[2], 4, i), i) |
| 194 | |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 195 | def test_negativelen(self): |
Georg Brandl | 86b2fb9 | 2008-07-16 03:43:04 +0000 | [diff] [blame] | 196 | # from issue 3306, previously it segfaulted |
| 197 | self.assertRaises(audioop.error, |
| 198 | audioop.findmax, ''.join(chr(x) for x in range(256)), -2392392) |
Guido van Rossum | fa7fcb9 | 1994-01-12 09:55:11 +0000 | [diff] [blame] | 199 | |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 200 | def test_issue7673(self): |
| 201 | state = None |
| 202 | for data, size in INVALID_DATA: |
| 203 | size2 = size |
| 204 | self.assertRaises(audioop.error, audioop.getsample, data, size, 0) |
| 205 | self.assertRaises(audioop.error, audioop.max, data, size) |
| 206 | self.assertRaises(audioop.error, audioop.minmax, data, size) |
| 207 | self.assertRaises(audioop.error, audioop.avg, data, size) |
| 208 | self.assertRaises(audioop.error, audioop.rms, data, size) |
| 209 | self.assertRaises(audioop.error, audioop.avgpp, data, size) |
| 210 | self.assertRaises(audioop.error, audioop.maxpp, data, size) |
| 211 | self.assertRaises(audioop.error, audioop.cross, data, size) |
| 212 | self.assertRaises(audioop.error, audioop.mul, data, size, 1.0) |
| 213 | self.assertRaises(audioop.error, audioop.tomono, data, size, 0.5, 0.5) |
| 214 | self.assertRaises(audioop.error, audioop.tostereo, data, size, 0.5, 0.5) |
| 215 | self.assertRaises(audioop.error, audioop.add, data, data, size) |
| 216 | self.assertRaises(audioop.error, audioop.bias, data, size, 0) |
| 217 | self.assertRaises(audioop.error, audioop.reverse, data, size) |
| 218 | self.assertRaises(audioop.error, audioop.lin2lin, data, size, size2) |
| 219 | self.assertRaises(audioop.error, audioop.ratecv, data, size, 1, 1, 1, state) |
| 220 | self.assertRaises(audioop.error, audioop.lin2ulaw, data, size) |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 221 | self.assertRaises(audioop.error, audioop.lin2alaw, data, size) |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 222 | self.assertRaises(audioop.error, audioop.lin2adpcm, data, size, state) |
Antoine Pitrou | 75ff65e | 2012-01-28 22:01:59 +0100 | [diff] [blame] | 223 | |
| 224 | def test_wrongsize(self): |
| 225 | data = b'abc' |
| 226 | state = None |
| 227 | for size in (-1, 3, 5): |
| 228 | self.assertRaises(audioop.error, audioop.ulaw2lin, data, size) |
| 229 | self.assertRaises(audioop.error, audioop.alaw2lin, data, size) |
Victor Stinner | bc5c54b | 2010-07-03 13:44:22 +0000 | [diff] [blame] | 230 | self.assertRaises(audioop.error, audioop.adpcm2lin, data, size, state) |
| 231 | |
Christian Heimes | 180510d | 2008-03-03 19:15:45 +0000 | [diff] [blame] | 232 | def test_main(): |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 233 | run_unittest(TestAudioop) |
Christian Heimes | 180510d | 2008-03-03 19:15:45 +0000 | [diff] [blame] | 234 | |
| 235 | if __name__ == '__main__': |
| 236 | test_main() |