blob: 6ff5bb81bbac408ce37d9e5920c6b37eaec3e8d7 [file] [log] [blame]
Serhiy Storchaka1b80e632013-10-13 17:55:07 +03001from test.support import findfile, TESTFN, unlink
2import unittest
3import array
4import io
5import pickle
6import sys
7
Serhiy Storchaka84d28b42013-12-14 20:35:04 +02008class UnseekableIO(io.FileIO):
9 def tell(self):
10 raise io.UnsupportedOperation
11
12 def seek(self, *args, **kwargs):
13 raise io.UnsupportedOperation
14
Serhiy Storchaka1b80e632013-10-13 17:55:07 +030015def byteswap2(data):
Serhiy Storchakad9a01822013-11-21 11:02:30 +020016 a = array.array('h')
17 a.frombytes(data)
Serhiy Storchaka1b80e632013-10-13 17:55:07 +030018 a.byteswap()
19 return a.tobytes()
20
21def byteswap3(data):
22 ba = bytearray(data)
23 ba[::3] = data[2::3]
24 ba[2::3] = data[::3]
25 return bytes(ba)
26
27def byteswap4(data):
Serhiy Storchakad9a01822013-11-21 11:02:30 +020028 a = array.array('i')
29 a.frombytes(data)
Serhiy Storchaka1b80e632013-10-13 17:55:07 +030030 a.byteswap()
31 return a.tobytes()
32
33
34class AudioTests:
35 close_fd = False
36
37 def setUp(self):
38 self.f = self.fout = None
39
40 def tearDown(self):
41 if self.f is not None:
42 self.f.close()
43 if self.fout is not None:
44 self.fout.close()
45 unlink(TESTFN)
46
47 def check_params(self, f, nchannels, sampwidth, framerate, nframes,
48 comptype, compname):
49 self.assertEqual(f.getnchannels(), nchannels)
50 self.assertEqual(f.getsampwidth(), sampwidth)
51 self.assertEqual(f.getframerate(), framerate)
52 self.assertEqual(f.getnframes(), nframes)
53 self.assertEqual(f.getcomptype(), comptype)
54 self.assertEqual(f.getcompname(), compname)
55
56 params = f.getparams()
57 self.assertEqual(params,
58 (nchannels, sampwidth, framerate, nframes, comptype, compname))
59
60 dump = pickle.dumps(params)
61 self.assertEqual(pickle.loads(dump), params)
62
63
64class AudioWriteTests(AudioTests):
65
66 def create_file(self, testfile):
67 f = self.fout = self.module.open(testfile, 'wb')
68 f.setnchannels(self.nchannels)
69 f.setsampwidth(self.sampwidth)
70 f.setframerate(self.framerate)
71 f.setcomptype(self.comptype, self.compname)
72 return f
73
74 def check_file(self, testfile, nframes, frames):
75 f = self.module.open(testfile, 'rb')
76 try:
77 self.assertEqual(f.getnchannels(), self.nchannels)
78 self.assertEqual(f.getsampwidth(), self.sampwidth)
79 self.assertEqual(f.getframerate(), self.framerate)
80 self.assertEqual(f.getnframes(), nframes)
81 self.assertEqual(f.readframes(nframes), frames)
82 finally:
83 f.close()
84
85 def test_write_params(self):
86 f = self.create_file(TESTFN)
87 f.setnframes(self.nframes)
88 f.writeframes(self.frames)
89 self.check_params(f, self.nchannels, self.sampwidth, self.framerate,
90 self.nframes, self.comptype, self.compname)
91 f.close()
92
93 def test_write(self):
94 f = self.create_file(TESTFN)
95 f.setnframes(self.nframes)
96 f.writeframes(self.frames)
97 f.close()
98
99 self.check_file(TESTFN, self.nframes, self.frames)
100
101 def test_incompleted_write(self):
102 with open(TESTFN, 'wb') as testfile:
103 testfile.write(b'ababagalamaga')
104 f = self.create_file(testfile)
105 f.setnframes(self.nframes + 1)
106 f.writeframes(self.frames)
107 f.close()
108
109 with open(TESTFN, 'rb') as testfile:
110 self.assertEqual(testfile.read(13), b'ababagalamaga')
111 self.check_file(testfile, self.nframes, self.frames)
112
113 def test_multiple_writes(self):
114 with open(TESTFN, 'wb') as testfile:
115 testfile.write(b'ababagalamaga')
116 f = self.create_file(testfile)
117 f.setnframes(self.nframes)
118 framesize = self.nchannels * self.sampwidth
119 f.writeframes(self.frames[:-framesize])
120 f.writeframes(self.frames[-framesize:])
121 f.close()
122
123 with open(TESTFN, 'rb') as testfile:
124 self.assertEqual(testfile.read(13), b'ababagalamaga')
125 self.check_file(testfile, self.nframes, self.frames)
126
127 def test_overflowed_write(self):
128 with open(TESTFN, 'wb') as testfile:
129 testfile.write(b'ababagalamaga')
130 f = self.create_file(testfile)
131 f.setnframes(self.nframes - 1)
132 f.writeframes(self.frames)
133 f.close()
134
135 with open(TESTFN, 'rb') as testfile:
136 self.assertEqual(testfile.read(13), b'ababagalamaga')
137 self.check_file(testfile, self.nframes, self.frames)
138
Serhiy Storchaka84d28b42013-12-14 20:35:04 +0200139 def test_unseekable_read(self):
140 f = self.create_file(TESTFN)
141 f.setnframes(self.nframes)
142 f.writeframes(self.frames)
143 f.close()
144
145 with UnseekableIO(TESTFN, 'rb') as testfile:
146 self.check_file(testfile, self.nframes, self.frames)
147
148 def test_unseekable_write(self):
149 with UnseekableIO(TESTFN, 'wb') as testfile:
150 f = self.create_file(testfile)
151 f.setnframes(self.nframes)
152 f.writeframes(self.frames)
153 f.close()
154
155 self.check_file(TESTFN, self.nframes, self.frames)
156
157 def test_unseekable_incompleted_write(self):
158 with UnseekableIO(TESTFN, 'wb') as testfile:
159 testfile.write(b'ababagalamaga')
160 f = self.create_file(testfile)
161 f.setnframes(self.nframes + 1)
162 try:
163 f.writeframes(self.frames)
164 except OSError:
165 pass
166 try:
167 f.close()
168 except OSError:
169 pass
170
171 with open(TESTFN, 'rb') as testfile:
172 self.assertEqual(testfile.read(13), b'ababagalamaga')
173 self.check_file(testfile, self.nframes + 1, self.frames)
174
175 def test_unseekable_overflowed_write(self):
176 with UnseekableIO(TESTFN, 'wb') as testfile:
177 testfile.write(b'ababagalamaga')
178 f = self.create_file(testfile)
179 f.setnframes(self.nframes - 1)
180 try:
181 f.writeframes(self.frames)
182 except OSError:
183 pass
184 try:
185 f.close()
186 except OSError:
187 pass
188
189 with open(TESTFN, 'rb') as testfile:
190 self.assertEqual(testfile.read(13), b'ababagalamaga')
191 framesize = self.nchannels * self.sampwidth
192 self.check_file(testfile, self.nframes - 1, self.frames[:-framesize])
193
Serhiy Storchaka1b80e632013-10-13 17:55:07 +0300194
195class AudioTestsWithSourceFile(AudioTests):
196
197 @classmethod
198 def setUpClass(cls):
199 cls.sndfilepath = findfile(cls.sndfilename, subdir='audiodata')
200
201 def test_read_params(self):
202 f = self.f = self.module.open(self.sndfilepath)
203 #self.assertEqual(f.getfp().name, self.sndfilepath)
204 self.check_params(f, self.nchannels, self.sampwidth, self.framerate,
205 self.sndfilenframes, self.comptype, self.compname)
206
207 def test_close(self):
Serhiy Storchaka85812bc2013-10-14 20:09:47 +0300208 with open(self.sndfilepath, 'rb') as testfile:
209 f = self.f = self.module.open(testfile)
210 self.assertFalse(testfile.closed)
211 f.close()
212 self.assertEqual(testfile.closed, self.close_fd)
213 with open(TESTFN, 'wb') as testfile:
214 fout = self.fout = self.module.open(testfile, 'wb')
215 self.assertFalse(testfile.closed)
216 with self.assertRaises(self.module.Error):
217 fout.close()
218 self.assertEqual(testfile.closed, self.close_fd)
219 fout.close() # do nothing
Serhiy Storchaka1b80e632013-10-13 17:55:07 +0300220
221 def test_read(self):
222 framesize = self.nchannels * self.sampwidth
223 chunk1 = self.frames[:2 * framesize]
224 chunk2 = self.frames[2 * framesize: 4 * framesize]
225 f = self.f = self.module.open(self.sndfilepath)
226 self.assertEqual(f.readframes(0), b'')
227 self.assertEqual(f.tell(), 0)
228 self.assertEqual(f.readframes(2), chunk1)
229 f.rewind()
230 pos0 = f.tell()
231 self.assertEqual(pos0, 0)
232 self.assertEqual(f.readframes(2), chunk1)
233 pos2 = f.tell()
234 self.assertEqual(pos2, 2)
235 self.assertEqual(f.readframes(2), chunk2)
236 f.setpos(pos2)
237 self.assertEqual(f.readframes(2), chunk2)
238 f.setpos(pos0)
239 self.assertEqual(f.readframes(2), chunk1)
240 with self.assertRaises(self.module.Error):
241 f.setpos(-1)
242 with self.assertRaises(self.module.Error):
243 f.setpos(f.getnframes() + 1)
244
245 def test_copy(self):
246 f = self.f = self.module.open(self.sndfilepath)
247 fout = self.fout = self.module.open(TESTFN, 'wb')
248 fout.setparams(f.getparams())
249 i = 0
250 n = f.getnframes()
251 while n > 0:
252 i += 1
253 fout.writeframes(f.readframes(i))
254 n -= i
255 fout.close()
256 fout = self.fout = self.module.open(TESTFN, 'rb')
257 f.rewind()
258 self.assertEqual(f.getparams(), fout.getparams())
259 self.assertEqual(f.readframes(f.getnframes()),
260 fout.readframes(fout.getnframes()))
261
262 def test_read_not_from_start(self):
263 with open(TESTFN, 'wb') as testfile:
264 testfile.write(b'ababagalamaga')
265 with open(self.sndfilepath, 'rb') as f:
266 testfile.write(f.read())
267
268 with open(TESTFN, 'rb') as testfile:
269 self.assertEqual(testfile.read(13), b'ababagalamaga')
270 f = self.module.open(testfile, 'rb')
271 try:
272 self.assertEqual(f.getnchannels(), self.nchannels)
273 self.assertEqual(f.getsampwidth(), self.sampwidth)
274 self.assertEqual(f.getframerate(), self.framerate)
275 self.assertEqual(f.getnframes(), self.sndfilenframes)
276 self.assertEqual(f.readframes(self.nframes), self.frames)
277 finally:
278 f.close()