blob: 62b3d1801842d9e8bf85769ad4c0a59afb5d37ea [file] [log] [blame]
R. David Murray0f457e52009-04-29 20:15:18 +00001from test.test_support import findfile, run_unittest, TESTFN
R. David Murray25b4add2009-04-29 13:17:37 +00002import unittest
R. David Murray0f457e52009-04-29 20:15:18 +00003import os
Antoine Pitrou3b6a3142012-01-17 17:13:04 +01004import io
R. David Murray25b4add2009-04-29 13:17:37 +00005
6import aifc
7
8
9class AIFCTest(unittest.TestCase):
10
11 def setUp(self):
R. David Murray0f457e52009-04-29 20:15:18 +000012 self.f = self.fout = None
R. David Murray25b4add2009-04-29 13:17:37 +000013 self.sndfilepath = findfile('Sine-1000Hz-300ms.aif')
14
R. David Murray0f457e52009-04-29 20:15:18 +000015 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 Murray25b4add2009-04-29 13:17:37 +000028 def test_skipunknown(self):
29 #Issue 2245
30 #This file contains chunk types aifc doesn't recognize.
Benjamin Peterson7c7250d2009-04-30 00:06:33 +000031 self.f = aifc.open(self.sndfilepath)
R. David Murray25b4add2009-04-29 13:17:37 +000032
R. David Murray971b1b12009-04-29 13:51:44 +000033 def test_params(self):
R. David Murray0f457e52009-04-29 20:15:18 +000034 f = self.f = aifc.open(self.sndfilepath)
R. David Murray971b1b12009-04-29 13:51:44 +000035 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 Murray971b1b12009-04-29 13:51:44 +000042
43 def test_read(self):
R. David Murray0f457e52009-04-29 20:15:18 +000044 f = self.f = aifc.open(self.sndfilepath)
R. David Murray971b1b12009-04-29 13:51:44 +000045 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 Murray971b1b12009-04-29 13:51:44 +000058
R. David Murray0f457e52009-04-29 20:15:18 +000059 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 Murray971b1b12009-04-29 13:51:44 +000094
R. David Murray8fd522f2009-05-07 16:27:02 +000095 def test_close(self):
96 class Wrapfile(object):
97 def __init__(self, file):
R. David Murrayf7e7bab2009-05-07 18:09:58 +000098 self.file = open(file, 'rb')
R. David Murray8fd522f2009-05-07 16:27:02 +000099 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 Storchaka4ed797e2012-12-29 22:25:59 +0200109 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 Murray8fd522f2009-05-07 16:27:02 +0000116
R. David Murray25b4add2009-04-29 13:17:37 +0000117
Antoine Pitrou3b6a3142012-01-17 17:13:04 +0100118class 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 Murray25b4add2009-04-29 13:17:37 +0000154def test_main():
155 run_unittest(AIFCTest)
Antoine Pitrou3b6a3142012-01-17 17:13:04 +0100156 run_unittest(AIFCLowLevelTest)
R. David Murray25b4add2009-04-29 13:17:37 +0000157
158
159if __name__ == "__main__":
160 unittest.main()