Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_aifc.py @ 54294:dba767bc3155 legacy-trunk
remove mention of old ctypes version
| author | Benjamin Peterson <benjamin@python.org> |
|---|---|
| date | Sat, 23 May 2009 20:59:09 +0000 |
| parents | 6c8b380db727 |
| children | b36cb4602e21 |
| rev | line source |
|---|---|
| 53607 | 1 from test.test_support import findfile, run_unittest, TESTFN |
|
53595
4ef7f93d61a1
Fix issue 2245. aifc now skips any chunk type it doesn't actually
R. David Murray <rdmurray@bitdance.com>
parents:
diff
changeset
|
2 import unittest |
| 53607 | 3 import os |
|
53595
4ef7f93d61a1
Fix issue 2245. aifc now skips any chunk type it doesn't actually
R. David Murray <rdmurray@bitdance.com>
parents:
diff
changeset
|
4 |
|
4ef7f93d61a1
Fix issue 2245. aifc now skips any chunk type it doesn't actually
R. David Murray <rdmurray@bitdance.com>
parents:
diff
changeset
|
5 import aifc |
|
4ef7f93d61a1
Fix issue 2245. aifc now skips any chunk type it doesn't actually
R. David Murray <rdmurray@bitdance.com>
parents:
diff
changeset
|
6 |
|
4ef7f93d61a1
Fix issue 2245. aifc now skips any chunk type it doesn't actually
R. David Murray <rdmurray@bitdance.com>
parents:
diff
changeset
|
7 |
|
4ef7f93d61a1
Fix issue 2245. aifc now skips any chunk type it doesn't actually
R. David Murray <rdmurray@bitdance.com>
parents:
diff
changeset
|
8 class AIFCTest(unittest.TestCase): |
|
4ef7f93d61a1
Fix issue 2245. aifc now skips any chunk type it doesn't actually
R. David Murray <rdmurray@bitdance.com>
parents:
diff
changeset
|
9 |
|
4ef7f93d61a1
Fix issue 2245. aifc now skips any chunk type it doesn't actually
R. David Murray <rdmurray@bitdance.com>
parents:
diff
changeset
|
10 def setUp(self): |
| 53607 | 11 self.f = self.fout = None |
|
53595
4ef7f93d61a1
Fix issue 2245. aifc now skips any chunk type it doesn't actually
R. David Murray <rdmurray@bitdance.com>
parents:
diff
changeset
|
12 self.sndfilepath = findfile('Sine-1000Hz-300ms.aif') |
|
4ef7f93d61a1
Fix issue 2245. aifc now skips any chunk type it doesn't actually
R. David Murray <rdmurray@bitdance.com>
parents:
diff
changeset
|
13 |
| 53607 | 14 def tearDown(self): |
| 15 if self.f is not None: | |
| 16 self.f.close() | |
| 17 if self.fout is not None: | |
| 18 try: | |
| 19 self.fout.close() | |
| 20 except (aifc.Error, AttributeError): | |
| 21 pass | |
| 22 try: | |
| 23 os.remove(TESTFN) | |
| 24 except OSError: | |
| 25 pass | |
| 26 | |
|
53595
4ef7f93d61a1
Fix issue 2245. aifc now skips any chunk type it doesn't actually
R. David Murray <rdmurray@bitdance.com>
parents:
diff
changeset
|
27 def test_skipunknown(self): |
|
4ef7f93d61a1
Fix issue 2245. aifc now skips any chunk type it doesn't actually
R. David Murray <rdmurray@bitdance.com>
parents:
diff
changeset
|
28 #Issue 2245 |
|
4ef7f93d61a1
Fix issue 2245. aifc now skips any chunk type it doesn't actually
R. David Murray <rdmurray@bitdance.com>
parents:
diff
changeset
|
29 #This file contains chunk types aifc doesn't recognize. |
|
53626
79dc8d588eb3
make sure to close file
Benjamin Peterson <benjamin@python.org>
parents:
53607
diff
changeset
|
30 self.f = aifc.open(self.sndfilepath) |
|
53595
4ef7f93d61a1
Fix issue 2245. aifc now skips any chunk type it doesn't actually
R. David Murray <rdmurray@bitdance.com>
parents:
diff
changeset
|
31 |
|
53596
8617e81016b6
Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents:
53595
diff
changeset
|
32 def test_params(self): |
| 53607 | 33 f = self.f = aifc.open(self.sndfilepath) |
|
53596
8617e81016b6
Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents:
53595
diff
changeset
|
34 self.assertEqual(f.getnchannels(), 2) |
|
8617e81016b6
Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents:
53595
diff
changeset
|
35 self.assertEqual(f.getsampwidth(), 2) |
|
8617e81016b6
Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents:
53595
diff
changeset
|
36 self.assertEqual(f.getframerate(), 48000) |
|
8617e81016b6
Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents:
53595
diff
changeset
|
37 self.assertEqual(f.getnframes(), 14400) |
|
8617e81016b6
Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents:
53595
diff
changeset
|
38 self.assertEqual(f.getcomptype(), 'NONE') |
|
8617e81016b6
Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents:
53595
diff
changeset
|
39 self.assertEqual(f.getcompname(), 'not compressed') |
|
8617e81016b6
Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents:
53595
diff
changeset
|
40 self.assertEqual(f.getparams(), (2, 2, 48000, 14400, 'NONE', 'not compressed')) |
|
8617e81016b6
Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents:
53595
diff
changeset
|
41 |
|
8617e81016b6
Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents:
53595
diff
changeset
|
42 def test_read(self): |
| 53607 | 43 f = self.f = aifc.open(self.sndfilepath) |
|
53596
8617e81016b6
Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents:
53595
diff
changeset
|
44 self.assertEqual(f.tell(), 0) |
|
8617e81016b6
Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents:
53595
diff
changeset
|
45 self.assertEqual(f.readframes(2), '\x00\x00\x00\x00\x0b\xd4\x0b\xd4') |
|
8617e81016b6
Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents:
53595
diff
changeset
|
46 f.rewind() |
|
8617e81016b6
Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents:
53595
diff
changeset
|
47 pos0 = f.tell() |
|
8617e81016b6
Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents:
53595
diff
changeset
|
48 self.assertEqual(pos0, 0) |
|
8617e81016b6
Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents:
53595
diff
changeset
|
49 self.assertEqual(f.readframes(2), '\x00\x00\x00\x00\x0b\xd4\x0b\xd4') |
|
8617e81016b6
Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents:
53595
diff
changeset
|
50 pos2 = f.tell() |
|
8617e81016b6
Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents:
53595
diff
changeset
|
51 self.assertEqual(pos2, 2) |
|
8617e81016b6
Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents:
53595
diff
changeset
|
52 self.assertEqual(f.readframes(2), '\x17t\x17t"\xad"\xad') |
|
8617e81016b6
Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents:
53595
diff
changeset
|
53 f.setpos(pos2) |
|
8617e81016b6
Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents:
53595
diff
changeset
|
54 self.assertEqual(f.readframes(2), '\x17t\x17t"\xad"\xad') |
|
8617e81016b6
Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents:
53595
diff
changeset
|
55 f.setpos(pos0) |
|
8617e81016b6
Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents:
53595
diff
changeset
|
56 self.assertEqual(f.readframes(2), '\x00\x00\x00\x00\x0b\xd4\x0b\xd4') |
| 53607 | 57 |
| 58 def test_write(self): | |
| 59 f = self.f = aifc.open(self.sndfilepath) | |
| 60 fout = self.fout = aifc.open(TESTFN, 'wb') | |
| 61 fout.aifc() | |
| 62 fout.setparams(f.getparams()) | |
| 63 for frame in range(f.getnframes()): | |
| 64 fout.writeframes(f.readframes(1)) | |
| 65 fout.close() | |
| 66 fout = self.fout = aifc.open(TESTFN, 'rb') | |
| 67 f.rewind() | |
| 68 self.assertEqual(f.getparams(), fout.getparams()) | |
| 69 self.assertEqual(f.readframes(5), fout.readframes(5)) | |
|
53596
8617e81016b6
Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents:
53595
diff
changeset
|
70 |
| 53607 | 71 def test_compress(self): |
| 72 f = self.f = aifc.open(self.sndfilepath) | |
| 73 fout = self.fout = aifc.open(TESTFN, 'wb') | |
| 74 fout.aifc() | |
| 75 fout.setnchannels(f.getnchannels()) | |
| 76 fout.setsampwidth(f.getsampwidth()) | |
| 77 fout.setframerate(f.getframerate()) | |
| 78 fout.setcomptype('ULAW', 'foo') | |
| 79 for frame in range(f.getnframes()): | |
| 80 fout.writeframes(f.readframes(1)) | |
| 81 fout.close() | |
| 82 self.assertLess( | |
| 83 os.stat(TESTFN).st_size, | |
| 84 os.stat(self.sndfilepath).st_size*0.75, | |
| 85 ) | |
| 86 fout = self.fout = aifc.open(TESTFN, 'rb') | |
| 87 f.rewind() | |
| 88 self.assertEqual(f.getparams()[0:3], fout.getparams()[0:3]) | |
| 89 self.assertEqual(fout.getcomptype(), 'ULAW') | |
| 90 self.assertEqual(fout.getcompname(), 'foo') | |
| 91 # XXX: this test fails, not sure if it should succeed or not | |
| 92 # self.assertEqual(f.readframes(5), fout.readframes(5)) | |
|
53596
8617e81016b6
Now that we've got a test_aifc, add a few tests.
R. David Murray <rdmurray@bitdance.com>
parents:
53595
diff
changeset
|
93 |
|
53872
85b9df525e10
Issue5955: aifc's close method did not close the file it wrapped,
R. David Murray <rdmurray@bitdance.com>
parents:
53626
diff
changeset
|
94 def test_close(self): |
|
85b9df525e10
Issue5955: aifc's close method did not close the file it wrapped,
R. David Murray <rdmurray@bitdance.com>
parents:
53626
diff
changeset
|
95 class Wrapfile(object): |
|
85b9df525e10
Issue5955: aifc's close method did not close the file it wrapped,
R. David Murray <rdmurray@bitdance.com>
parents:
53626
diff
changeset
|
96 def __init__(self, file): |
|
53877
6c8b380db727
Pre-opened test file needs to be opened in binary mode.
R. David Murray <rdmurray@bitdance.com>
parents:
53872
diff
changeset
|
97 self.file = open(file, 'rb') |
|
53872
85b9df525e10
Issue5955: aifc's close method did not close the file it wrapped,
R. David Murray <rdmurray@bitdance.com>
parents:
53626
diff
changeset
|
98 self.closed = False |
|
85b9df525e10
Issue5955: aifc's close method did not close the file it wrapped,
R. David Murray <rdmurray@bitdance.com>
parents:
53626
diff
changeset
|
99 def close(self): |
|
85b9df525e10
Issue5955: aifc's close method did not close the file it wrapped,
R. David Murray <rdmurray@bitdance.com>
parents:
53626
diff
changeset
|
100 self.file.close() |
|
85b9df525e10
Issue5955: aifc's close method did not close the file it wrapped,
R. David Murray <rdmurray@bitdance.com>
parents:
53626
diff
changeset
|
101 self.closed = True |
|
85b9df525e10
Issue5955: aifc's close method did not close the file it wrapped,
R. David Murray <rdmurray@bitdance.com>
parents:
53626
diff
changeset
|
102 def __getattr__(self, attr): return getattr(self.file, attr) |
|
85b9df525e10
Issue5955: aifc's close method did not close the file it wrapped,
R. David Murray <rdmurray@bitdance.com>
parents:
53626
diff
changeset
|
103 testfile = Wrapfile(self.sndfilepath) |
|
85b9df525e10
Issue5955: aifc's close method did not close the file it wrapped,
R. David Murray <rdmurray@bitdance.com>
parents:
53626
diff
changeset
|
104 f = self.f = aifc.open(testfile) |
|
85b9df525e10
Issue5955: aifc's close method did not close the file it wrapped,
R. David Murray <rdmurray@bitdance.com>
parents:
53626
diff
changeset
|
105 self.assertEqual(testfile.closed, False) |
|
85b9df525e10
Issue5955: aifc's close method did not close the file it wrapped,
R. David Murray <rdmurray@bitdance.com>
parents:
53626
diff
changeset
|
106 f.close() |
|
85b9df525e10
Issue5955: aifc's close method did not close the file it wrapped,
R. David Murray <rdmurray@bitdance.com>
parents:
53626
diff
changeset
|
107 self.assertEqual(testfile.closed, True) |
|
85b9df525e10
Issue5955: aifc's close method did not close the file it wrapped,
R. David Murray <rdmurray@bitdance.com>
parents:
53626
diff
changeset
|
108 |
|
53595
4ef7f93d61a1
Fix issue 2245. aifc now skips any chunk type it doesn't actually
R. David Murray <rdmurray@bitdance.com>
parents:
diff
changeset
|
109 |
|
4ef7f93d61a1
Fix issue 2245. aifc now skips any chunk type it doesn't actually
R. David Murray <rdmurray@bitdance.com>
parents:
diff
changeset
|
110 def test_main(): |
|
4ef7f93d61a1
Fix issue 2245. aifc now skips any chunk type it doesn't actually
R. David Murray <rdmurray@bitdance.com>
parents:
diff
changeset
|
111 run_unittest(AIFCTest) |
|
4ef7f93d61a1
Fix issue 2245. aifc now skips any chunk type it doesn't actually
R. David Murray <rdmurray@bitdance.com>
parents:
diff
changeset
|
112 |
|
4ef7f93d61a1
Fix issue 2245. aifc now skips any chunk type it doesn't actually
R. David Murray <rdmurray@bitdance.com>
parents:
diff
changeset
|
113 |
|
4ef7f93d61a1
Fix issue 2245. aifc now skips any chunk type it doesn't actually
R. David Murray <rdmurray@bitdance.com>
parents:
diff
changeset
|
114 if __name__ == "__main__": |
|
4ef7f93d61a1
Fix issue 2245. aifc now skips any chunk type it doesn't actually
R. David Murray <rdmurray@bitdance.com>
parents:
diff
changeset
|
115 unittest.main() |
