Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(257431)

Delta Between Two Patch Sets: Lib/test/test_aifc.py

Issue 18919: Unify audio modules tests
Left Patch Set: Created 5 years, 11 months ago
Right Patch Set: Created 5 years, 10 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 from test.support import findfile, TESTFN, unlink 1 from test.test_support import findfile, TESTFN, unlink, captured_stdout, run_uni ttest
2 import unittest 2 import unittest
3 from test import audiotests 3 from test import audiotests
4 import os 4 import os
5 import io 5 import io
6 import sys 6 import sys
7 import struct 7 import struct
8 import aifc 8 import aifc
9 9
10 10
11 class AifcPCM8Test(audiotests.AudioWriteTests, 11 class AifcPCM8Test(audiotests.AudioWriteTests,
12 audiotests.AudioTestsWithSourceFile, 12 audiotests.AudioTestsWithSourceFile,
13 unittest.TestCase): 13 unittest.TestCase):
14 module = aifc 14 module = aifc
15 sndfilename = 'pcm8.aiff' 15 sndfilename = 'pluck-pcm8.aiff'
16 sndfilenframes = 48 16 sndfilenframes = 3307
17 nchannels = 2 17 nchannels = 2
18 sampwidth = 1 18 sampwidth = 1
19 framerate = 48000 19 framerate = 11025
20 nframes = 48 20 nframes = 48
21 comptype = b'NONE' 21 comptype = 'NONE'
22 compname = b'not compressed' 22 compname = 'not compressed'
23 frames = bytes.fromhex( 23 frames = audiotests.fromhex("""\
24 '0000 0C0C 1717 2222 2D2D 3737 3F3F 4747 4E4E 5353 5757 5959 5A5A 5959'\ 24 02FF 4B00 3104 8008 CB06 4803 BF01 03FE B8FA B4F3 29EB 1AE6 \
25 '5757 5353 4E4E 4747 4040 3737 2D2D 2222 1717 0C0C 0000 F4F4 E9E9 DEDE'\ 25 EDE4 C6E2 0EE0 EFE0 57E2 FBE8 13EF D8F7 97FB F5FC 08FB DFFB \
26 'D3D3 C9C9 C1C1 B9B9 B2B2 ADAD A9A9 A7A7 A6A6 A7A7 A9A9 ADAD B2B2 B9B9'\ 26 11FA 3EFB BCFC 66FF CF04 4309 C10E 5112 EE17 8216 7F14 8012 \
27 'C0C0 C9C9 D3D3 DEDE E9E9 F4F4') 27 490E 520D EF0F CE0F E40C 630A 080A 2B0B 510E 8B11 B60E 440A \
28 """)
28 close_fd = True 29 close_fd = True
29 30
30 31
31 class AifcPCM16Test(audiotests.AudioWriteTests, 32 class AifcPCM16Test(audiotests.AudioWriteTests,
32 audiotests.AudioTestsWithSourceFile, 33 audiotests.AudioTestsWithSourceFile,
33 unittest.TestCase): 34 unittest.TestCase):
34 module = aifc 35 module = aifc
35 sndfilename = 'pcm16.aiff' 36 sndfilename = 'pluck-pcm16.aiff'
36 sndfilenframes = 48 37 sndfilenframes = 3307
37 nchannels = 2 38 nchannels = 2
38 sampwidth = 2 39 sampwidth = 2
39 framerate = 48000 40 framerate = 11025
40 nframes = 48 41 nframes = 48
41 comptype = b'NONE' 42 comptype = 'NONE'
42 compname = b'not compressed' 43 compname = 'not compressed'
43 frames = bytes.fromhex( 44 frames = audiotests.fromhex("""\
44 '00000000 0BD00BD0 176D176D 22A322A3 2D412D41 37193719 3FFF3FFF 47CE47CE'\ 45 022EFFEA 4B5D00F6 311804EA 80E10840 CBE106B1 48A903F5 BFE601B2 036CFE7B \
45 '4E624E62 539E539E 576C576C 59BC59BC 5A825A82 59BC59BC 576C576C 539E539E'\ 46 B858FA3E B4B1F34F 299AEBCA 1A5DE6DA EDFAE491 C628E275 0E09E0B5 EF2AE029 \
46 '4E624E62 47CE47CE 40004000 37193719 2D412D41 22A322A3 176D176D 0BD00BD0'\ 47 5758E271 FB35E83F 1376EF86 D82BF727 9790FB76 F5FAFC0F 0867FB9C DF30FB43 \
47 '00000000 F430F430 E893E893 DD5DDD5D D2BFD2BF C8E7C8E7 C001C001 B832B832'\ 48 117EFA36 3EE5FB5B BC79FCB1 66D9FF5D CF150412 431D097C C1BA0EC8 512112A1 \
48 'B19EB19E AC62AC62 A894A894 A644A644 A57EA57E A644A644 A894A894 AC62AC62'\ 49 EEE21753 82071665 7FFF1443 8004128F 49A20EAF 52BB0DBA EFB40F60 CE3C0FBF \
49 'B19EB19E B832B832 C000C000 C8E7C8E7 D2BFD2BF DD5DDD5D E893E893 F430F430') 50 E4B30CEC 63430A5C 08C80A20 2BBB0B08 514A0E43 8BCF1139 B6F60EEB 44120A5E \
51 """)
50 close_fd = True 52 close_fd = True
51 53
52 54
53 class AifcPCM24Test(audiotests.AudioWriteTests, 55 class AifcPCM24Test(audiotests.AudioWriteTests,
54 audiotests.AudioTestsWithSourceFile, 56 audiotests.AudioTestsWithSourceFile,
55 unittest.TestCase): 57 unittest.TestCase):
56 module = aifc 58 module = aifc
57 sndfilename = 'pcm24.aiff' 59 sndfilename = 'pluck-pcm24.aiff'
58 sndfilenframes = 48 60 sndfilenframes = 3307
59 nchannels = 2 61 nchannels = 2
60 sampwidth = 3 62 sampwidth = 3
61 framerate = 48000 63 framerate = 11025
62 nframes = 48 64 nframes = 48
63 comptype = b'NONE' 65 comptype = 'NONE'
64 compname = b'not compressed' 66 compname = 'not compressed'
65 frames = bytes.fromhex( 67 frames = audiotests.fromhex("""\
66 '000000000000 0BD05A0BD05A 176CF6176CF6 22A2F522A2F5 2D413C2D413C'\ 68 022D65FFEB9D 4B5A0F00FA54 3113C304EE2B 80DCD6084303 \
67 '37194A37194A 3FFFFF3FFFFF 47CE5F47CE5F 4E62384E6238 539EBA539EBA'\ 69 CBDEC006B261 48A99803F2F8 BFE82401B07D 036BFBFE7B5D \
68 '576CF5576CF5 59BC3F59BC3F 5A82795A8279 59BC3F59BC3F 576CF5576CF5'\ 70 B85756FA3EC9 B4B055F3502B 299830EBCB62 1A5CA7E6D99A \
69 '539EBA539EBA 4E62384E6238 47CE5F47CE5F 400000400000 37194A37194A'\ 71 EDFA3EE491BD C625EBE27884 0E05A9E0B6CF EF2929E02922 \
70 '2D413C2D413C 22A2F522A2F5 176CF6176CF6 0BD05A0BD05A 000000000000'\ 72 5758D8E27067 FB3557E83E16 1377BFEF8402 D82C5BF7272A \
71 'F42FA6F42FA6 E8930AE8930A DD5D0BDD5D0B D2BEC4D2BEC4 C8E6B6C8E6B6'\ 73 978F16FB7745 F5F865FC1013 086635FB9C4E DF30FCFB40EE \
72 'C00001C00001 B831A1B831A1 B19DC8B19DC8 AC6146AC6146 A8930BA8930B'\ 74 117FE0FA3438 3EE6B8FB5AC3 BC77A3FCB2F4 66D6DAFF5F32 \
73 'A643C1A643C1 A57D87A57D87 A643C1A643C1 A8930BA8930B AC6146AC6146'\ 75 CF13B9041275 431D69097A8C C1BB600EC74E 5120B912A2BA \
74 'B19DC8B19DC8 B831A1B831A1 C00000C00000 C8E6B6C8E6B6 D2BEC4D2BEC4'\ 76 EEDF641754C0 8207001664B7 7FFFFF14453F 8000001294E6 \
75 'DD5D0BDD5D0B E8930AE8930A F42FA6F42FA6') 77 499C1B0EB3B2 52B73E0DBCA0 EFB2B20F5FD8 CE3CDB0FBE12 \
78 E4B49C0CEA2D 6344A80A5A7C 08C8FE0A1FFE 2BB9860B0A0E \
79 51486F0E44E1 8BCC64113B05 B6F4EC0EEB36 4413170A5B48 \
80 """)
76 close_fd = True 81 close_fd = True
77 82
78 83
79 class AifcPCM32Test(audiotests.AudioWriteTests, 84 class AifcPCM32Test(audiotests.AudioWriteTests,
80 audiotests.AudioTestsWithSourceFile, 85 audiotests.AudioTestsWithSourceFile,
81 unittest.TestCase): 86 unittest.TestCase):
82 module = aifc 87 module = aifc
83 sndfilename = 'pcm32.aiff' 88 sndfilename = 'pluck-pcm32.aiff'
84 sndfilenframes = 48 89 sndfilenframes = 3307
85 nchannels = 2 90 nchannels = 2
86 sampwidth = 4 91 sampwidth = 4
87 framerate = 48000 92 framerate = 11025
88 nframes = 48 93 nframes = 48
89 comptype = b'NONE' 94 comptype = 'NONE'
90 compname = b'not compressed' 95 compname = 'not compressed'
91 frames = bytes.fromhex( 96 frames = audiotests.fromhex("""\
92 '0000000000000000 0BD05A970BD05A97 176CF5D1176CF5D1 22A2F4F722A2F4F7'\ 97 022D65BCFFEB9D92 4B5A0F8000FA549C 3113C34004EE2BC0 80DCD680084303E0 \
93 '2D413CCD2D413CCD 37194A9E37194A9E 3FFFFFFF3FFFFFFF 47CE5FAD47CE5FAD'\ 98 CBDEC0C006B26140 48A9980003F2F8FC BFE8248001B07D92 036BFB60FE7B5D34 \
94 '4E6238504E623850 539EBA44539EBA44 576CF5D0576CF5D0 59BC3F9559BC3F95'\ 99 B8575600FA3EC920 B4B05500F3502BC0 29983000EBCB6240 1A5CA7A0E6D99A60 \
95 '5A8279995A827999 59BC3F9559BC3F95 576CF5D0576CF5D0 539EBA44539EBA44'\ 100 EDFA3E80E491BD40 C625EB80E27884A0 0E05A9A0E0B6CFE0 EF292940E0292280 \
96 '4E6238504E623850 47CE5FAD47CE5FAD 4000000040000000 37194A9E37194A9E'\ 101 5758D800E2706700 FB3557D8E83E1640 1377BF00EF840280 D82C5B80F7272A80 \
97 '2D413CCD2D413CCD 22A2F4F722A2F4F7 176CF5D1176CF5D1 0BD05A970BD05A97'\ 102 978F1600FB774560 F5F86510FC101364 086635A0FB9C4E20 DF30FC40FB40EE28 \
98 '0000000000000000 F42FA569F42FA569 E8930A2FE8930A2F DD5D0B09DD5D0B09'\ 103 117FE0A0FA3438B0 3EE6B840FB5AC3F0 BC77A380FCB2F454 66D6DA80FF5F32B4 \
99 'D2BEC333D2BEC333 C8E6B562C8E6B562 C0000001C0000001 B831A053B831A053'\ 104 CF13B980041275B0 431D6980097A8C00 C1BB60000EC74E00 5120B98012A2BAA0 \
100 'B19DC7B0B19DC7B0 AC6145BCAC6145BC A8930A30A8930A30 A643C06BA643C06B'\ 105 EEDF64C01754C060 820700001664B780 7FFFFFFF14453F40 800000001294E6E0 \
101 'A57D8667A57D8667 A643C06BA643C06B A8930A30A8930A30 AC6145BCAC6145BC'\ 106 499C1B000EB3B270 52B73E000DBCA020 EFB2B2E00F5FD880 CE3CDB400FBE1270 \
102 'B19DC7B0B19DC7B0 B831A053B831A053 C0000000C0000000 C8E6B562C8E6B562'\ 107 E4B49CC00CEA2D90 6344A8800A5A7CA0 08C8FE800A1FFEE0 2BB986C00B0A0E00 \
103 'D2BEC333D2BEC333 DD5D0B09DD5D0B09 E8930A2FE8930A2F F42FA569F42FA569') 108 51486F800E44E190 8BCC6480113B0580 B6F4EC000EEB3630 441317800A5B48A0 \
104 close_fd = True 109 """)
105 110 close_fd = True
106 111
107 class AifcALAWTest(audiotests.AudioWriteTests, 112
108 audiotests.AudioTestsWithSourceFile, 113 class AifcULAWTest(audiotests.AudioWriteTests,
109 unittest.TestCase): 114 audiotests.AudioTestsWithSourceFile,
110 module = aifc 115 unittest.TestCase):
111 sndfilename = 'alaw.aifc' 116 module = aifc
112 sndfilenframes = 48 117 sndfilename = 'pluck-ulaw.aifc'
118 sndfilenframes = 3307
113 nchannels = 2 119 nchannels = 2
114 sampwidth = 2 120 sampwidth = 2
115 framerate = 48000 121 framerate = 11025
116 nframes = 48 122 nframes = 48
117 comptype = b'alaw' 123 comptype = 'ulaw'
118 compname = b'ALaw 2:1' 124 compname = ''
119 frames = bytes.fromhex( 125 frames = audiotests.fromhex("""\
120 '00080008 0BC00BC0 17801780 23002300 2D002D00 37003700 3F003F00 46004600'\ 126 022CFFE8 497C0104 307C04DC 8284083C CB84069C 497C03DC BE8401AC 036CFE74 \
121 '4E004E00 52005200 56005600 5A005A00 5A005A00 5A005A00 56005600 52005200'\ 127 B684FA24 B684F344 2A7CEC04 19FCE704 EE04E504 C584E204 0E3CE104 EF04DF84 \
122 '4E004E00 46004600 42004200 37003700 2D002D00 23002300 17801780 0BC00BC0'\ 128 557CE204 FB24E804 12FCEF04 D784F744 9684FB64 F5C4FC24 083CFBA4 DF84FB24 \
123 '00080008 F440F440 E880E880 DD00DD00 D300D300 C900C900 C100C100 BA00BA00'\ 129 11FCFA24 3E7CFB64 BA84FCB4 657CFF5C CF84041C 417C093C C1840EBC 517C12FC \
124 'B200B200 AE00AE00 AA00AA00 A600A600 A600A600 A600A600 AA00AA00 AE00AE00'\ 130 EF0416FC 828415FC 7D7C13FC 828412FC 497C0EBC 517C0DBC F0040F3C CD840FFC \
125 'B200B200 BA00BA00 C100C100 C900C900 D300D300 DD00DD00 E880E880 F440F440') 131 E5040CBC 617C0A3C 08BC0A3C 2C7C0B3C 517C0E3C 8A8410FC B6840EBC 457C0A3C \
126 if sys.byteorder != 'big': 132 """)
127 frames = audiotests.byteswap2(frames)
128 close_fd = True
129
130
131 class AifcULAWTest(audiotests.AudioWriteTests,
132 audiotests.AudioTestsWithSourceFile,
133 unittest.TestCase):
134 module = aifc
135 sndfilename = 'ulaw.aifc'
136 sndfilenframes = 48
137 nchannels = 2
138 sampwidth = 2
139 framerate = 48000
140 nframes = 48
141 comptype = b'ulaw'
142 compname = b'\xb5Law 2:1'
143 frames = bytes.fromhex(
144 '00000000 0BBC0BBC 16FC16FC 227C227C 2C7C2C7C 367C367C 417C417C 497C497C'\
145 '4D7C4D7C 557C557C 557C557C 597C597C 597C597C 597C597C 557C557C 557C557C'\
146 '4D7C4D7C 497C497C 417C417C 367C367C 2C7C2C7C 227C227C 16FC16FC 0BBC0BBC'\
147 '00000000 F444F444 E904E904 DD84DD84 D384D384 C984C984 BE84BE84 B684B684'\
148 'B284B284 AA84AA84 AA84AA84 A684A684 A684A684 A684A684 AA84AA84 AA84AA84'\
149 'B284B284 B684B684 BE84BE84 C984C984 D384D384 DD84DD84 E904E904 F444F444')
150 if sys.byteorder != 'big': 133 if sys.byteorder != 'big':
151 frames = audiotests.byteswap2(frames) 134 frames = audiotests.byteswap2(frames)
152 close_fd = True 135 close_fd = True
153 136
154 137
155 class AifcMiscTest(audiotests.AudioTests, unittest.TestCase): 138 class AifcMiscTest(audiotests.AudioTests, unittest.TestCase):
156 def test_skipunknown(self): 139 def test_skipunknown(self):
157 #Issue 2245 140 #Issue 2245
158 #This file contains chunk types aifc doesn't recognize. 141 #This file contains chunk types aifc doesn't recognize.
159 self.f = aifc.open(findfile('Sine-1000Hz-300ms.aif')) 142 self.f = aifc.open(findfile('Sine-1000Hz-300ms.aif'))
160 143
161 def test_params_added(self):
162 f = self.f = aifc.open(TESTFN, 'wb')
163 f.aiff()
164 f.setparams((1, 1, 1, 1, b'NONE', b''))
165 f.close()
166
167 f = self.f = aifc.open(TESTFN, 'rb')
168 params = f.getparams()
169 self.assertEqual(params.nchannels, f.getnchannels())
170 self.assertEqual(params.sampwidth, f.getsampwidth())
171 self.assertEqual(params.framerate, f.getframerate())
172 self.assertEqual(params.nframes, f.getnframes())
173 self.assertEqual(params.comptype, f.getcomptype())
174 self.assertEqual(params.compname, f.getcompname())
175
176 def test_write_header_comptype_sampwidth(self):
177 for comptype in (b'ULAW', b'ulaw', b'ALAW', b'alaw', b'G722'):
178 fout = aifc.open(io.BytesIO(), 'wb')
179 fout.setnchannels(1)
180 fout.setframerate(1)
181 fout.setcomptype(comptype, b'')
182 fout.close()
183 self.assertEqual(fout.getsampwidth(), 2)
184 fout.initfp(None)
185
186 def test_write_markers_values(self): 144 def test_write_markers_values(self):
187 fout = aifc.open(io.BytesIO(), 'wb') 145 fout = aifc.open(io.BytesIO(), 'wb')
188 self.assertEqual(fout.getmarkers(), None) 146 self.assertEqual(fout.getmarkers(), None)
189 fout.setmark(1, 0, b'foo1') 147 fout.setmark(1, 0, 'foo1')
190 fout.setmark(1, 1, b'foo2') 148 fout.setmark(1, 1, 'foo2')
191 self.assertEqual(fout.getmark(1), (1, 1, b'foo2')) 149 self.assertEqual(fout.getmark(1), (1, 1, 'foo2'))
192 self.assertEqual(fout.getmarkers(), [(1, 1, b'foo2')]) 150 self.assertEqual(fout.getmarkers(), [(1, 1, 'foo2')])
193 fout.initfp(None) 151 fout.initfp(None)
194 152
195 def test_read_markers(self): 153 def test_read_markers(self):
196 fout = self.fout = aifc.open(TESTFN, 'wb') 154 fout = self.fout = aifc.open(TESTFN, 'wb')
197 fout.aiff() 155 fout.aiff()
198 fout.setparams((1, 1, 1, 1, b'NONE', b'')) 156 fout.setparams((1, 1, 1, 1, 'NONE', ''))
199 fout.setmark(1, 0, b'odd') 157 fout.setmark(1, 0, 'odd')
200 fout.setmark(2, 0, b'even') 158 fout.setmark(2, 0, 'even')
201 fout.writeframes(b'\x00') 159 fout.writeframes('\x00')
202 fout.close() 160 fout.close()
203 f = self.f = aifc.open(TESTFN, 'rb') 161 f = self.f = aifc.open(TESTFN, 'rb')
204 self.assertEqual(f.getmarkers(), [(1, 0, b'odd'), (2, 0, b'even')]) 162 self.assertEqual(f.getmarkers(), [(1, 0, 'odd'), (2, 0, 'even')])
205 self.assertEqual(f.getmark(1), (1, 0, b'odd')) 163 self.assertEqual(f.getmark(1), (1, 0, 'odd'))
206 self.assertEqual(f.getmark(2), (2, 0, b'even')) 164 self.assertEqual(f.getmark(2), (2, 0, 'even'))
207 self.assertRaises(aifc.Error, f.getmark, 3) 165 self.assertRaises(aifc.Error, f.getmark, 3)
208 166
209 167
210 class AIFCLowLevelTest(unittest.TestCase): 168 class AIFCLowLevelTest(unittest.TestCase):
211 169
212 def test_read_written(self): 170 def test_read_written(self):
213 def read_written(self, what): 171 def read_written(self, what):
214 f = io.BytesIO() 172 f = io.BytesIO()
215 getattr(aifc, '_write_' + what)(f, x) 173 getattr(aifc, '_write_' + what)(f, x)
216 f.seek(0) 174 f.seek(0)
217 return getattr(aifc, '_read_' + what)(f) 175 return getattr(aifc, '_read_' + what)(f)
218 for x in (-1, 0, 0.1, 1): 176 for x in (-1, 0, 0.1, 1):
219 self.assertEqual(read_written(x, 'float'), x) 177 self.assertEqual(read_written(x, 'float'), x)
220 for x in (float('NaN'), float('Inf')): 178 for x in (float('NaN'), float('Inf')):
221 self.assertEqual(read_written(x, 'float'), aifc._HUGE_VAL) 179 self.assertEqual(read_written(x, 'float'), aifc._HUGE_VAL)
222 for x in (b'', b'foo', b'a' * 255): 180 for x in ('', 'foo', 'a' * 255):
223 self.assertEqual(read_written(x, 'string'), x) 181 self.assertEqual(read_written(x, 'string'), x)
224 for x in (-0x7FFFFFFF, -1, 0, 1, 0x7FFFFFFF): 182 for x in (-0x7FFFFFFF, -1, 0, 1, 0x7FFFFFFF):
225 self.assertEqual(read_written(x, 'long'), x) 183 self.assertEqual(read_written(x, 'long'), x)
226 for x in (0, 1, 0xFFFFFFFF): 184 for x in (0, 1, 0xFFFFFFFF):
227 self.assertEqual(read_written(x, 'ulong'), x) 185 self.assertEqual(read_written(x, 'ulong'), x)
228 for x in (-0x7FFF, -1, 0, 1, 0x7FFF): 186 for x in (-0x7FFF, -1, 0, 1, 0x7FFF):
229 self.assertEqual(read_written(x, 'short'), x) 187 self.assertEqual(read_written(x, 'short'), x)
230 for x in (0, 1, 0xFFFF): 188 for x in (0, 1, 0xFFFF):
231 self.assertEqual(read_written(x, 'ushort'), x) 189 self.assertEqual(read_written(x, 'ushort'), x)
232 190
233 def test_read_raises(self): 191 def test_read_raises(self):
234 f = io.BytesIO(b'\x00') 192 f = io.BytesIO('\x00')
235 self.assertRaises(EOFError, aifc._read_ulong, f) 193 self.assertRaises(EOFError, aifc._read_ulong, f)
236 self.assertRaises(EOFError, aifc._read_long, f) 194 self.assertRaises(EOFError, aifc._read_long, f)
237 self.assertRaises(EOFError, aifc._read_ushort, f) 195 self.assertRaises(EOFError, aifc._read_ushort, f)
238 self.assertRaises(EOFError, aifc._read_short, f) 196 self.assertRaises(EOFError, aifc._read_short, f)
239 197
240 def test_write_long_string_raises(self): 198 def test_write_long_string_raises(self):
241 f = io.BytesIO() 199 f = io.BytesIO()
242 with self.assertRaises(ValueError): 200 with self.assertRaises(ValueError):
243 aifc._write_string(f, b'too long' * 255) 201 aifc._write_string(f, 'too long' * 255)
244 202
245 def test_wrong_open_mode(self): 203 def test_wrong_open_mode(self):
246 with self.assertRaises(aifc.Error): 204 with self.assertRaises(aifc.Error):
247 aifc.open(TESTFN, 'wrong_mode') 205 aifc.open(TESTFN, 'wrong_mode')
248 206
249 def test_read_wrong_form(self): 207 def test_read_wrong_form(self):
250 b1 = io.BytesIO(b'WRNG' + struct.pack('>L', 0)) 208 b1 = io.BytesIO('WRNG' + struct.pack('>L', 0))
251 b2 = io.BytesIO(b'FORM' + struct.pack('>L', 4) + b'WRNG') 209 b2 = io.BytesIO('FORM' + struct.pack('>L', 4) + 'WRNG')
252 self.assertRaises(aifc.Error, aifc.open, b1) 210 self.assertRaises(aifc.Error, aifc.open, b1)
253 self.assertRaises(aifc.Error, aifc.open, b2) 211 self.assertRaises(aifc.Error, aifc.open, b2)
254 212
255 def test_read_no_comm_chunk(self): 213 def test_read_no_comm_chunk(self):
256 b = io.BytesIO(b'FORM' + struct.pack('>L', 4) + b'AIFF') 214 b = io.BytesIO('FORM' + struct.pack('>L', 4) + 'AIFF')
257 self.assertRaises(aifc.Error, aifc.open, b) 215 self.assertRaises(aifc.Error, aifc.open, b)
258 216
259 def test_read_wrong_compression_type(self): 217 def test_read_wrong_compression_type(self):
260 b = b'FORM' + struct.pack('>L', 4) + b'AIFC' 218 b = 'FORM' + struct.pack('>L', 4) + 'AIFC'
261 b += b'COMM' + struct.pack('>LhlhhLL', 23, 0, 0, 0, 0, 0, 0) 219 b += 'COMM' + struct.pack('>LhlhhLL', 23, 0, 0, 0, 0, 0, 0)
262 b += b'WRNG' + struct.pack('B', 0) 220 b += 'WRNG' + struct.pack('B', 0)
263 self.assertRaises(aifc.Error, aifc.open, io.BytesIO(b)) 221 self.assertRaises(aifc.Error, aifc.open, io.BytesIO(b))
264 222
265 def test_read_wrong_marks(self): 223 def test_read_wrong_marks(self):
266 b = b'FORM' + struct.pack('>L', 4) + b'AIFF' 224 b = 'FORM' + struct.pack('>L', 4) + 'AIFF'
267 b += b'COMM' + struct.pack('>LhlhhLL', 18, 0, 0, 0, 0, 0, 0) 225 b += 'COMM' + struct.pack('>LhlhhLL', 18, 0, 0, 0, 0, 0, 0)
268 b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8 226 b += 'SSND' + struct.pack('>L', 8) + '\x00' * 8
269 b += b'MARK' + struct.pack('>LhB', 3, 1, 1) 227 b += 'MARK' + struct.pack('>LhB', 3, 1, 1)
270 with self.assertWarns(UserWarning): 228 with captured_stdout() as s:
271 f = aifc.open(io.BytesIO(b)) 229 f = aifc.open(io.BytesIO(b))
230 self.assertEqual(s.getvalue(), 'Warning: MARK chunk contains only '
231 '0 markers instead of 1\n')
272 self.assertEqual(f.getmarkers(), None) 232 self.assertEqual(f.getmarkers(), None)
273 233
274 def test_read_comm_kludge_compname_even(self): 234 def test_read_comm_kludge_compname_even(self):
275 b = b'FORM' + struct.pack('>L', 4) + b'AIFC' 235 b = 'FORM' + struct.pack('>L', 4) + 'AIFC'
276 b += b'COMM' + struct.pack('>LhlhhLL', 18, 0, 0, 0, 0, 0, 0) 236 b += 'COMM' + struct.pack('>LhlhhLL', 18, 0, 0, 0, 0, 0, 0)
277 b += b'NONE' + struct.pack('B', 4) + b'even' + b'\x00' 237 b += 'NONE' + struct.pack('B', 4) + 'even' + '\x00'
278 b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8 238 b += 'SSND' + struct.pack('>L', 8) + '\x00' * 8
279 with self.assertWarns(UserWarning): 239 with captured_stdout() as s:
280 f = aifc.open(io.BytesIO(b)) 240 f = aifc.open(io.BytesIO(b))
281 self.assertEqual(f.getcompname(), b'even') 241 self.assertEqual(s.getvalue(), 'Warning: bad COMM chunk size\n')
242 self.assertEqual(f.getcompname(), 'even')
282 243
283 def test_read_comm_kludge_compname_odd(self): 244 def test_read_comm_kludge_compname_odd(self):
284 b = b'FORM' + struct.pack('>L', 4) + b'AIFC' 245 b = 'FORM' + struct.pack('>L', 4) + 'AIFC'
285 b += b'COMM' + struct.pack('>LhlhhLL', 18, 0, 0, 0, 0, 0, 0) 246 b += 'COMM' + struct.pack('>LhlhhLL', 18, 0, 0, 0, 0, 0, 0)
286 b += b'NONE' + struct.pack('B', 3) + b'odd' 247 b += 'NONE' + struct.pack('B', 3) + 'odd'
287 b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8 248 b += 'SSND' + struct.pack('>L', 8) + '\x00' * 8
288 with self.assertWarns(UserWarning): 249 with captured_stdout() as s:
289 f = aifc.open(io.BytesIO(b)) 250 f = aifc.open(io.BytesIO(b))
290 self.assertEqual(f.getcompname(), b'odd') 251 self.assertEqual(s.getvalue(), 'Warning: bad COMM chunk size\n')
252 self.assertEqual(f.getcompname(), 'odd')
291 253
292 def test_write_params_raises(self): 254 def test_write_params_raises(self):
293 fout = aifc.open(io.BytesIO(), 'wb') 255 fout = aifc.open(io.BytesIO(), 'wb')
294 wrong_params = (0, 0, 0, 0, b'WRNG', '') 256 wrong_params = (0, 0, 0, 0, 'WRNG', '')
295 self.assertRaises(aifc.Error, fout.setparams, wrong_params) 257 self.assertRaises(aifc.Error, fout.setparams, wrong_params)
296 self.assertRaises(aifc.Error, fout.getparams) 258 self.assertRaises(aifc.Error, fout.getparams)
297 self.assertRaises(aifc.Error, fout.setnchannels, 0) 259 self.assertRaises(aifc.Error, fout.setnchannels, 0)
298 self.assertRaises(aifc.Error, fout.getnchannels) 260 self.assertRaises(aifc.Error, fout.getnchannels)
299 self.assertRaises(aifc.Error, fout.setsampwidth, 0) 261 self.assertRaises(aifc.Error, fout.setsampwidth, 0)
300 self.assertRaises(aifc.Error, fout.getsampwidth) 262 self.assertRaises(aifc.Error, fout.getsampwidth)
301 self.assertRaises(aifc.Error, fout.setframerate, 0) 263 self.assertRaises(aifc.Error, fout.setframerate, 0)
302 self.assertRaises(aifc.Error, fout.getframerate) 264 self.assertRaises(aifc.Error, fout.getframerate)
303 self.assertRaises(aifc.Error, fout.setcomptype, b'WRNG', '') 265 self.assertRaises(aifc.Error, fout.setcomptype, 'WRNG', '')
304 fout.aiff() 266 fout.aiff()
305 fout.setnchannels(1) 267 fout.setnchannels(1)
306 fout.setsampwidth(1) 268 fout.setsampwidth(1)
307 fout.setframerate(1) 269 fout.setframerate(1)
308 fout.setnframes(1) 270 fout.setnframes(1)
309 fout.writeframes(b'\x00') 271 fout.writeframes('\x00')
310 self.assertRaises(aifc.Error, fout.setparams, (1, 1, 1, 1, 1, 1)) 272 self.assertRaises(aifc.Error, fout.setparams, (1, 1, 1, 1, 1, 1))
311 self.assertRaises(aifc.Error, fout.setnchannels, 1) 273 self.assertRaises(aifc.Error, fout.setnchannels, 1)
312 self.assertRaises(aifc.Error, fout.setsampwidth, 1) 274 self.assertRaises(aifc.Error, fout.setsampwidth, 1)
313 self.assertRaises(aifc.Error, fout.setframerate, 1) 275 self.assertRaises(aifc.Error, fout.setframerate, 1)
314 self.assertRaises(aifc.Error, fout.setnframes, 1) 276 self.assertRaises(aifc.Error, fout.setnframes, 1)
315 self.assertRaises(aifc.Error, fout.setcomptype, b'NONE', '') 277 self.assertRaises(aifc.Error, fout.setcomptype, 'NONE', '')
316 self.assertRaises(aifc.Error, fout.aiff) 278 self.assertRaises(aifc.Error, fout.aiff)
317 self.assertRaises(aifc.Error, fout.aifc) 279 self.assertRaises(aifc.Error, fout.aifc)
318 280
319 def test_write_params_singles(self): 281 def test_write_params_singles(self):
320 fout = aifc.open(io.BytesIO(), 'wb') 282 fout = aifc.open(io.BytesIO(), 'wb')
321 fout.aifc() 283 fout.aifc()
322 fout.setnchannels(1) 284 fout.setnchannels(1)
323 fout.setsampwidth(2) 285 fout.setsampwidth(2)
324 fout.setframerate(3) 286 fout.setframerate(3)
325 fout.setnframes(4) 287 fout.setnframes(4)
326 fout.setcomptype(b'NONE', b'name') 288 fout.setcomptype('NONE', 'name')
327 self.assertEqual(fout.getnchannels(), 1) 289 self.assertEqual(fout.getnchannels(), 1)
328 self.assertEqual(fout.getsampwidth(), 2) 290 self.assertEqual(fout.getsampwidth(), 2)
329 self.assertEqual(fout.getframerate(), 3) 291 self.assertEqual(fout.getframerate(), 3)
330 self.assertEqual(fout.getnframes(), 0) 292 self.assertEqual(fout.getnframes(), 0)
331 self.assertEqual(fout.tell(), 0) 293 self.assertEqual(fout.tell(), 0)
332 self.assertEqual(fout.getcomptype(), b'NONE') 294 self.assertEqual(fout.getcomptype(), 'NONE')
333 self.assertEqual(fout.getcompname(), b'name') 295 self.assertEqual(fout.getcompname(), 'name')
334 fout.writeframes(b'\x00' * 4 * fout.getsampwidth() * fout.getnchannels() ) 296 fout.writeframes('\x00' * 4 * fout.getsampwidth() * fout.getnchannels())
335 self.assertEqual(fout.getnframes(), 4) 297 self.assertEqual(fout.getnframes(), 4)
336 self.assertEqual(fout.tell(), 4) 298 self.assertEqual(fout.tell(), 4)
337 299
338 def test_write_params_bunch(self): 300 def test_write_params_bunch(self):
339 fout = aifc.open(io.BytesIO(), 'wb') 301 fout = aifc.open(io.BytesIO(), 'wb')
340 fout.aifc() 302 fout.aifc()
341 p = (1, 2, 3, 4, b'NONE', b'name') 303 p = (1, 2, 3, 4, 'NONE', 'name')
342 fout.setparams(p) 304 fout.setparams(p)
343 self.assertEqual(fout.getparams(), p) 305 self.assertEqual(fout.getparams(), p)
344 fout.initfp(None) 306 fout.initfp(None)
345 307
346 def test_write_header_raises(self): 308 def test_write_header_raises(self):
347 fout = aifc.open(io.BytesIO(), 'wb') 309 fout = aifc.open(io.BytesIO(), 'wb')
348 self.assertRaises(aifc.Error, fout.close) 310 self.assertRaises(aifc.Error, fout.close)
349 fout = aifc.open(io.BytesIO(), 'wb') 311 fout = aifc.open(io.BytesIO(), 'wb')
350 fout.setnchannels(1) 312 fout.setnchannels(1)
351 self.assertRaises(aifc.Error, fout.close) 313 self.assertRaises(aifc.Error, fout.close)
352 fout = aifc.open(io.BytesIO(), 'wb') 314 fout = aifc.open(io.BytesIO(), 'wb')
353 fout.setnchannels(1) 315 fout.setnchannels(1)
354 fout.setsampwidth(1) 316 fout.setsampwidth(1)
355 self.assertRaises(aifc.Error, fout.close) 317 self.assertRaises(aifc.Error, fout.close)
356 318
357 def test_write_header_comptype_raises(self): 319 def test_write_header_comptype_raises(self):
358 for comptype in (b'ULAW', b'ulaw', b'ALAW', b'alaw', b'G722'): 320 for comptype in ('ULAW', 'ulaw', 'ALAW', 'alaw', 'G722'):
359 fout = aifc.open(io.BytesIO(), 'wb') 321 fout = aifc.open(io.BytesIO(), 'wb')
360 fout.setsampwidth(1) 322 fout.setsampwidth(1)
361 fout.setcomptype(comptype, b'') 323 fout.setcomptype(comptype, '')
362 self.assertRaises(aifc.Error, fout.close) 324 self.assertRaises(aifc.Error, fout.close)
363 fout.initfp(None) 325 fout.initfp(None)
364 326
365 def test_write_markers_raises(self): 327 def test_write_markers_raises(self):
366 fout = aifc.open(io.BytesIO(), 'wb') 328 fout = aifc.open(io.BytesIO(), 'wb')
367 self.assertRaises(aifc.Error, fout.setmark, 0, 0, b'') 329 self.assertRaises(aifc.Error, fout.setmark, 0, 0, '')
368 self.assertRaises(aifc.Error, fout.setmark, 1, -1, b'') 330 self.assertRaises(aifc.Error, fout.setmark, 1, -1, '')
369 self.assertRaises(aifc.Error, fout.setmark, 1, 0, None) 331 self.assertRaises(aifc.Error, fout.setmark, 1, 0, None)
370 self.assertRaises(aifc.Error, fout.getmark, 1) 332 self.assertRaises(aifc.Error, fout.getmark, 1)
371 fout.initfp(None) 333 fout.initfp(None)
372 334
373 def test_write_aiff_by_extension(self): 335 def test_write_aiff_by_extension(self):
374 sampwidth = 2 336 sampwidth = 2
375 filename = TESTFN + '.aiff' 337 fout = self.fout = aifc.open(TESTFN + '.aiff', 'wb')
376 fout = self.fout = aifc.open(filename, 'wb') 338 fout.setparams((1, sampwidth, 1, 1, 'ULAW', ''))
377 self.addCleanup(unlink, filename) 339 frames = '\x00' * fout.getnchannels() * sampwidth
378 fout.setparams((1, sampwidth, 1, 1, b'ULAW', b''))
379 frames = b'\x00' * fout.getnchannels() * sampwidth
380 fout.writeframes(frames) 340 fout.writeframes(frames)
381 fout.close() 341 fout.close()
382 f = self.f = aifc.open(filename, 'rb') 342 f = self.f = aifc.open(TESTFN + '.aiff', 'rb')
383 self.assertEqual(f.getcomptype(), b'NONE') 343 self.assertEqual(f.getcomptype(), 'NONE')
384 f.close() 344 f.close()
385 345
386 346
347 def test_main():
348 run_unittest(AifcPCM8Test, AifcPCM16Test, AifcPCM16Test, AifcPCM24Test,
349 AifcPCM32Test, AifcULAWTest,
350 AifcMiscTest, AIFCLowLevelTest)
351
387 if __name__ == "__main__": 352 if __name__ == "__main__":
388 unittest.main() 353 test_main()
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+