Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1 | from test.support import TESTFN, run_unittest |
Guido van Rossum | 3b0a329 | 2002-08-09 16:38:32 +0000 | [diff] [blame] | 2 | import os |
Guido van Rossum | a00afc1 | 2000-10-09 20:05:59 +0000 | [diff] [blame] | 3 | import wave |
Benjamin Peterson | 5efea04 | 2010-01-13 03:49:50 +0000 | [diff] [blame] | 4 | import struct |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 5 | import unittest |
Guido van Rossum | a00afc1 | 2000-10-09 20:05:59 +0000 | [diff] [blame] | 6 | |
| 7 | nchannels = 2 |
| 8 | sampwidth = 2 |
| 9 | framerate = 8000 |
| 10 | nframes = 100 |
| 11 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 12 | class TestWave(unittest.TestCase): |
Guido van Rossum | a00afc1 | 2000-10-09 20:05:59 +0000 | [diff] [blame] | 13 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 14 | def setUp(self): |
| 15 | self.f = None |
Guido van Rossum | a00afc1 | 2000-10-09 20:05:59 +0000 | [diff] [blame] | 16 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 17 | def tearDown(self): |
| 18 | if self.f is not None: |
| 19 | self.f.close() |
| 20 | try: |
| 21 | os.remove(TESTFN) |
| 22 | except OSError: |
| 23 | pass |
| 24 | |
Mark Dickinson | 64a38c0 | 2010-08-28 17:22:16 +0000 | [diff] [blame] | 25 | def test_it(self, test_rounding=False): |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 26 | self.f = wave.open(TESTFN, 'wb') |
| 27 | self.f.setnchannels(nchannels) |
| 28 | self.f.setsampwidth(sampwidth) |
Mark Dickinson | 64a38c0 | 2010-08-28 17:22:16 +0000 | [diff] [blame] | 29 | if test_rounding: |
| 30 | self.f.setframerate(framerate - 0.1) |
| 31 | else: |
| 32 | self.f.setframerate(framerate) |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 33 | self.f.setnframes(nframes) |
| 34 | output = b'\0' * nframes * nchannels * sampwidth |
| 35 | self.f.writeframes(output) |
| 36 | self.f.close() |
| 37 | |
| 38 | self.f = wave.open(TESTFN, 'rb') |
| 39 | self.assertEqual(nchannels, self.f.getnchannels()) |
| 40 | self.assertEqual(sampwidth, self.f.getsampwidth()) |
| 41 | self.assertEqual(framerate, self.f.getframerate()) |
| 42 | self.assertEqual(nframes, self.f.getnframes()) |
| 43 | self.assertEqual(self.f.readframes(nframes), output) |
| 44 | |
Mark Dickinson | 64a38c0 | 2010-08-28 17:22:16 +0000 | [diff] [blame] | 45 | def test_fractional_framerate(self): |
| 46 | """ |
| 47 | Addresses [ 1512791 ] module wave does no rounding |
| 48 | Floating point framerates should be rounded, rather than truncated. |
| 49 | """ |
| 50 | self.test_it(test_rounding=True) |
| 51 | |
Benjamin Peterson | 5efea04 | 2010-01-13 03:49:50 +0000 | [diff] [blame] | 52 | def test_issue7681(self): |
| 53 | self.f = wave.open(TESTFN, 'wb') |
| 54 | self.f.setnchannels(nchannels) |
| 55 | self.f.setsampwidth(sampwidth) |
| 56 | self.f.setframerate(framerate) |
| 57 | # Don't call setnframes, make _write_header divide to figure it out |
| 58 | output = b'\0' * nframes * nchannels * sampwidth |
| 59 | self.f.writeframes(output) |
| 60 | |
| 61 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 62 | def test_main(): |
| 63 | run_unittest(TestWave) |
| 64 | |
| 65 | if __name__ == '__main__': |
| 66 | test_main() |