move sections
[python/dscho.git] / Lib / test / test_aifc.py
blobcbf00e9a74d7a70281df1d2b08767985572b1b3f
1 from test.test_support import findfile, run_unittest, TESTFN
2 import unittest
3 import os
5 import aifc
8 class AIFCTest(unittest.TestCase):
10 def setUp(self):
11 self.f = self.fout = None
12 self.sndfilepath = findfile('Sine-1000Hz-300ms.aif')
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
27 def test_skipunknown(self):
28 #Issue 2245
29 #This file contains chunk types aifc doesn't recognize.
30 self.f = aifc.open(self.sndfilepath)
32 def test_params(self):
33 f = self.f = aifc.open(self.sndfilepath)
34 self.assertEqual(f.getnchannels(), 2)
35 self.assertEqual(f.getsampwidth(), 2)
36 self.assertEqual(f.getframerate(), 48000)
37 self.assertEqual(f.getnframes(), 14400)
38 self.assertEqual(f.getcomptype(), 'NONE')
39 self.assertEqual(f.getcompname(), 'not compressed')
40 self.assertEqual(f.getparams(), (2, 2, 48000, 14400, 'NONE', 'not compressed'))
42 def test_read(self):
43 f = self.f = aifc.open(self.sndfilepath)
44 self.assertEqual(f.tell(), 0)
45 self.assertEqual(f.readframes(2), '\x00\x00\x00\x00\x0b\xd4\x0b\xd4')
46 f.rewind()
47 pos0 = f.tell()
48 self.assertEqual(pos0, 0)
49 self.assertEqual(f.readframes(2), '\x00\x00\x00\x00\x0b\xd4\x0b\xd4')
50 pos2 = f.tell()
51 self.assertEqual(pos2, 2)
52 self.assertEqual(f.readframes(2), '\x17t\x17t"\xad"\xad')
53 f.setpos(pos2)
54 self.assertEqual(f.readframes(2), '\x17t\x17t"\xad"\xad')
55 f.setpos(pos0)
56 self.assertEqual(f.readframes(2), '\x00\x00\x00\x00\x0b\xd4\x0b\xd4')
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))
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,
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))
94 def test_close(self):
95 class Wrapfile(object):
96 def __init__(self, file):
97 self.file = open(file, 'rb')
98 self.closed = False
99 def close(self):
100 self.file.close()
101 self.closed = True
102 def __getattr__(self, attr): return getattr(self.file, attr)
103 testfile = Wrapfile(self.sndfilepath)
104 f = self.f = aifc.open(testfile)
105 self.assertEqual(testfile.closed, False)
106 f.close()
107 self.assertEqual(testfile.closed, True)
110 def test_main():
111 run_unittest(AIFCTest)
114 if __name__ == "__main__":
115 unittest.main()