R. David Murray | 0f457e5 | 2009-04-29 20:15:18 +0000 | [diff] [blame] | 1 | from test.test_support import findfile, run_unittest, TESTFN |
R. David Murray | 25b4add | 2009-04-29 13:17:37 +0000 | [diff] [blame] | 2 | import unittest |
R. David Murray | 0f457e5 | 2009-04-29 20:15:18 +0000 | [diff] [blame] | 3 | import os |
Antoine Pitrou | 3b6a314 | 2012-01-17 17:13:04 +0100 | [diff] [blame] | 4 | import io |
R. David Murray | 25b4add | 2009-04-29 13:17:37 +0000 | [diff] [blame] | 5 | |
| 6 | import aifc |
| 7 | |
| 8 | |
| 9 | class AIFCTest(unittest.TestCase): |
| 10 | |
| 11 | def setUp(self): |
R. David Murray | 0f457e5 | 2009-04-29 20:15:18 +0000 | [diff] [blame] | 12 | self.f = self.fout = None |
R. David Murray | 25b4add | 2009-04-29 13:17:37 +0000 | [diff] [blame] | 13 | self.sndfilepath = findfile('Sine-1000Hz-300ms.aif') |
| 14 | |
R. David Murray | 0f457e5 | 2009-04-29 20:15:18 +0000 | [diff] [blame] | 15 | def tearDown(self): |
| 16 | if self.f is not None: |
| 17 | self.f.close() |
| 18 | if self.fout is not None: |
| 19 | try: |
| 20 | self.fout.close() |
| 21 | except (aifc.Error, AttributeError): |
| 22 | pass |
| 23 | try: |
| 24 | os.remove(TESTFN) |
| 25 | except OSError: |
| 26 | pass |
| 27 | |
R. David Murray | 25b4add | 2009-04-29 13:17:37 +0000 | [diff] [blame] | 28 | def test_skipunknown(self): |
| 29 | #Issue 2245 |
| 30 | #This file contains chunk types aifc doesn't recognize. |
Benjamin Peterson | 7c7250d | 2009-04-30 00:06:33 +0000 | [diff] [blame] | 31 | self.f = aifc.open(self.sndfilepath) |
R. David Murray | 25b4add | 2009-04-29 13:17:37 +0000 | [diff] [blame] | 32 | |
R. David Murray | 971b1b1 | 2009-04-29 13:51:44 +0000 | [diff] [blame] | 33 | def test_params(self): |
R. David Murray | 0f457e5 | 2009-04-29 20:15:18 +0000 | [diff] [blame] | 34 | f = self.f = aifc.open(self.sndfilepath) |
R. David Murray | 971b1b1 | 2009-04-29 13:51:44 +0000 | [diff] [blame] | 35 | self.assertEqual(f.getnchannels(), 2) |
| 36 | self.assertEqual(f.getsampwidth(), 2) |
| 37 | self.assertEqual(f.getframerate(), 48000) |
| 38 | self.assertEqual(f.getnframes(), 14400) |
| 39 | self.assertEqual(f.getcomptype(), 'NONE') |
| 40 | self.assertEqual(f.getcompname(), 'not compressed') |
| 41 | self.assertEqual(f.getparams(), (2, 2, 48000, 14400, 'NONE', 'not compressed')) |
R. David Murray | 971b1b1 | 2009-04-29 13:51:44 +0000 | [diff] [blame] | 42 | |
| 43 | def test_read(self): |
R. David Murray | 0f457e5 | 2009-04-29 20:15:18 +0000 | [diff] [blame] | 44 | f = self.f = aifc.open(self.sndfilepath) |
R. David Murray | 971b1b1 | 2009-04-29 13:51:44 +0000 | [diff] [blame] | 45 | self.assertEqual(f.tell(), 0) |
| 46 | self.assertEqual(f.readframes(2), '\x00\x00\x00\x00\x0b\xd4\x0b\xd4') |
| 47 | f.rewind() |
| 48 | pos0 = f.tell() |
| 49 | self.assertEqual(pos0, 0) |
| 50 | self.assertEqual(f.readframes(2), '\x00\x00\x00\x00\x0b\xd4\x0b\xd4') |
| 51 | pos2 = f.tell() |
| 52 | self.assertEqual(pos2, 2) |
| 53 | self.assertEqual(f.readframes(2), '\x17t\x17t"\xad"\xad') |
| 54 | f.setpos(pos2) |
| 55 | self.assertEqual(f.readframes(2), '\x17t\x17t"\xad"\xad') |
| 56 | f.setpos(pos0) |
| 57 | self.assertEqual(f.readframes(2), '\x00\x00\x00\x00\x0b\xd4\x0b\xd4') |
R. David Murray | 971b1b1 | 2009-04-29 13:51:44 +0000 | [diff] [blame] | 58 | |
R. David Murray | 0f457e5 | 2009-04-29 20:15:18 +0000 | [diff] [blame] | 59 | def test_write(self): |
| 60 | f = self.f = aifc.open(self.sndfilepath) |
| 61 | fout = self.fout = aifc.open(TESTFN, 'wb') |
| 62 | fout.aifc() |
| 63 | fout.setparams(f.getparams()) |
| 64 | for frame in range(f.getnframes()): |
| 65 | fout.writeframes(f.readframes(1)) |
| 66 | fout.close() |
| 67 | fout = self.fout = aifc.open(TESTFN, 'rb') |
| 68 | f.rewind() |
| 69 | self.assertEqual(f.getparams(), fout.getparams()) |
| 70 | self.assertEqual(f.readframes(5), fout.readframes(5)) |
| 71 | |
| 72 | def test_compress(self): |
| 73 | f = self.f = aifc.open(self.sndfilepath) |
| 74 | fout = self.fout = aifc.open(TESTFN, 'wb') |
| 75 | fout.aifc() |
| 76 | fout.setnchannels(f.getnchannels()) |
| 77 | fout.setsampwidth(f.getsampwidth()) |
| 78 | fout.setframerate(f.getframerate()) |
| 79 | fout.setcomptype('ULAW', 'foo') |
| 80 | for frame in range(f.getnframes()): |
| 81 | fout.writeframes(f.readframes(1)) |
| 82 | fout.close() |
| 83 | self.assertLess( |
| 84 | os.stat(TESTFN).st_size, |
| 85 | os.stat(self.sndfilepath).st_size*0.75, |
| 86 | ) |
| 87 | fout = self.fout = aifc.open(TESTFN, 'rb') |
| 88 | f.rewind() |
| 89 | self.assertEqual(f.getparams()[0:3], fout.getparams()[0:3]) |
| 90 | self.assertEqual(fout.getcomptype(), 'ULAW') |
| 91 | self.assertEqual(fout.getcompname(), 'foo') |
| 92 | # XXX: this test fails, not sure if it should succeed or not |
| 93 | # self.assertEqual(f.readframes(5), fout.readframes(5)) |
R. David Murray | 971b1b1 | 2009-04-29 13:51:44 +0000 | [diff] [blame] | 94 | |
R. David Murray | 8fd522f | 2009-05-07 16:27:02 +0000 | [diff] [blame] | 95 | def test_close(self): |
| 96 | class Wrapfile(object): |
| 97 | def __init__(self, file): |
R. David Murray | f7e7bab | 2009-05-07 18:09:58 +0000 | [diff] [blame] | 98 | self.file = open(file, 'rb') |
R. David Murray | 8fd522f | 2009-05-07 16:27:02 +0000 | [diff] [blame] | 99 | self.closed = False |
| 100 | def close(self): |
| 101 | self.file.close() |
| 102 | self.closed = True |
| 103 | def __getattr__(self, attr): return getattr(self.file, attr) |
| 104 | testfile = Wrapfile(self.sndfilepath) |
| 105 | f = self.f = aifc.open(testfile) |
| 106 | self.assertEqual(testfile.closed, False) |
| 107 | f.close() |
| 108 | self.assertEqual(testfile.closed, True) |
Serhiy Storchaka | 4ed797e | 2012-12-29 22:25:59 +0200 | [diff] [blame] | 109 | testfile = open(TESTFN, 'wb') |
| 110 | fout = aifc.open(testfile, 'wb') |
| 111 | self.assertFalse(testfile.closed) |
| 112 | with self.assertRaises(aifc.Error): |
| 113 | fout.close() |
| 114 | self.assertTrue(testfile.closed) |
| 115 | fout.close() # do nothing |
R. David Murray | 8fd522f | 2009-05-07 16:27:02 +0000 | [diff] [blame] | 116 | |
R. David Murray | 25b4add | 2009-04-29 13:17:37 +0000 | [diff] [blame] | 117 | |
Antoine Pitrou | 3b6a314 | 2012-01-17 17:13:04 +0100 | [diff] [blame] | 118 | class AIFCLowLevelTest(unittest.TestCase): |
| 119 | |
| 120 | def test_read_written(self): |
| 121 | def read_written(self, what): |
| 122 | f = io.BytesIO() |
| 123 | getattr(aifc, '_write_' + what)(f, x) |
| 124 | f.seek(0) |
| 125 | return getattr(aifc, '_read_' + what)(f) |
| 126 | for x in (-1, 0, 0.1, 1): |
| 127 | self.assertEqual(read_written(x, 'float'), x) |
| 128 | for x in (float('NaN'), float('Inf')): |
| 129 | self.assertEqual(read_written(x, 'float'), aifc._HUGE_VAL) |
| 130 | for x in (b'', b'foo', b'a' * 255): |
| 131 | self.assertEqual(read_written(x, 'string'), x) |
| 132 | for x in (-0x7FFFFFFF, -1, 0, 1, 0x7FFFFFFF): |
| 133 | self.assertEqual(read_written(x, 'long'), x) |
| 134 | for x in (0, 1, 0xFFFFFFFF): |
| 135 | self.assertEqual(read_written(x, 'ulong'), x) |
| 136 | for x in (-0x7FFF, -1, 0, 1, 0x7FFF): |
| 137 | self.assertEqual(read_written(x, 'short'), x) |
| 138 | for x in (0, 1, 0xFFFF): |
| 139 | self.assertEqual(read_written(x, 'ushort'), x) |
| 140 | |
| 141 | def test_read_raises(self): |
| 142 | f = io.BytesIO(b'\x00') |
| 143 | self.assertRaises(EOFError, aifc._read_ulong, f) |
| 144 | self.assertRaises(EOFError, aifc._read_long, f) |
| 145 | self.assertRaises(EOFError, aifc._read_ushort, f) |
| 146 | self.assertRaises(EOFError, aifc._read_short, f) |
| 147 | |
| 148 | def test_write_long_string_raises(self): |
| 149 | f = io.BytesIO() |
| 150 | with self.assertRaises(ValueError): |
| 151 | aifc._write_string(f, b'too long' * 255) |
| 152 | |
| 153 | |
R. David Murray | 25b4add | 2009-04-29 13:17:37 +0000 | [diff] [blame] | 154 | def test_main(): |
| 155 | run_unittest(AIFCTest) |
Antoine Pitrou | 3b6a314 | 2012-01-17 17:13:04 +0100 | [diff] [blame] | 156 | run_unittest(AIFCLowLevelTest) |
R. David Murray | 25b4add | 2009-04-29 13:17:37 +0000 | [diff] [blame] | 157 | |
| 158 | |
| 159 | if __name__ == "__main__": |
| 160 | unittest.main() |