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

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

Issue 18919: Unify audio modules tests
Left Patch Set: Created 5 years, 8 months ago
Right Patch Set: Created 5 years, 8 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 = 'pluck-pcm8.aiff' 15 sndfilename = 'pluck-pcm8.aiff'
16 sndfilenframes = 3307 16 sndfilenframes = 3307
17 nchannels = 2 17 nchannels = 2
18 sampwidth = 1 18 sampwidth = 1
19 framerate = 11025 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 02FF 4B00 3104 8008 CB06 4803 BF01 03FE B8FA B4F3 29EB 1AE6 \ 24 02FF 4B00 3104 8008 CB06 4803 BF01 03FE B8FA B4F3 29EB 1AE6 \
25 EDE4 C6E2 0EE0 EFE0 57E2 FBE8 13EF D8F7 97FB F5FC 08FB DFFB \ 25 EDE4 C6E2 0EE0 EFE0 57E2 FBE8 13EF D8F7 97FB F5FC 08FB DFFB \
26 11FA 3EFB BCFC 66FF CF04 4309 C10E 5112 EE17 8216 7F14 8012 \ 26 11FA 3EFB BCFC 66FF CF04 4309 C10E 5112 EE17 8216 7F14 8012 \
27 490E 520D EF0F CE0F E40C 630A 080A 2B0B 510E 8B11 B60E 440A \ 27 490E 520D EF0F CE0F E40C 630A 080A 2B0B 510E 8B11 B60E 440A \
28 """) 28 """)
29 close_fd = True 29 close_fd = True
30 30
31 31
32 class AifcPCM16Test(audiotests.AudioWriteTests, 32 class AifcPCM16Test(audiotests.AudioWriteTests,
33 audiotests.AudioTestsWithSourceFile, 33 audiotests.AudioTestsWithSourceFile,
34 unittest.TestCase): 34 unittest.TestCase):
35 module = aifc 35 module = aifc
36 sndfilename = 'pluck-pcm16.aiff' 36 sndfilename = 'pluck-pcm16.aiff'
37 sndfilenframes = 3307 37 sndfilenframes = 3307
38 nchannels = 2 38 nchannels = 2
39 sampwidth = 2 39 sampwidth = 2
40 framerate = 11025 40 framerate = 11025
41 nframes = 48 41 nframes = 48
42 comptype = b'NONE' 42 comptype = 'NONE'
43 compname = b'not compressed' 43 compname = 'not compressed'
44 frames = bytes.fromhex("""\ 44 frames = audiotests.fromhex("""\
45 022EFFEA 4B5D00F6 311804EA 80E10840 CBE106B1 48A903F5 BFE601B2 036CFE7B \ 45 022EFFEA 4B5D00F6 311804EA 80E10840 CBE106B1 48A903F5 BFE601B2 036CFE7B \
46 B858FA3E B4B1F34F 299AEBCA 1A5DE6DA EDFAE491 C628E275 0E09E0B5 EF2AE029 \ 46 B858FA3E B4B1F34F 299AEBCA 1A5DE6DA EDFAE491 C628E275 0E09E0B5 EF2AE029 \
47 5758E271 FB35E83F 1376EF86 D82BF727 9790FB76 F5FAFC0F 0867FB9C DF30FB43 \ 47 5758E271 FB35E83F 1376EF86 D82BF727 9790FB76 F5FAFC0F 0867FB9C DF30FB43 \
48 117EFA36 3EE5FB5B BC79FCB1 66D9FF5D CF150412 431D097C C1BA0EC8 512112A1 \ 48 117EFA36 3EE5FB5B BC79FCB1 66D9FF5D CF150412 431D097C C1BA0EC8 512112A1 \
49 EEE21753 82071665 7FFF1443 8004128F 49A20EAF 52BB0DBA EFB40F60 CE3C0FBF \ 49 EEE21753 82071665 7FFF1443 8004128F 49A20EAF 52BB0DBA EFB40F60 CE3C0FBF \
50 E4B30CEC 63430A5C 08C80A20 2BBB0B08 514A0E43 8BCF1139 B6F60EEB 44120A5E \ 50 E4B30CEC 63430A5C 08C80A20 2BBB0B08 514A0E43 8BCF1139 B6F60EEB 44120A5E \
51 """) 51 """)
52 close_fd = True 52 close_fd = True
53 53
54 54
55 class AifcPCM24Test(audiotests.AudioWriteTests, 55 class AifcPCM24Test(audiotests.AudioWriteTests,
56 audiotests.AudioTestsWithSourceFile, 56 audiotests.AudioTestsWithSourceFile,
57 unittest.TestCase): 57 unittest.TestCase):
58 module = aifc 58 module = aifc
59 sndfilename = 'pluck-pcm24.aiff' 59 sndfilename = 'pluck-pcm24.aiff'
60 sndfilenframes = 3307 60 sndfilenframes = 3307
61 nchannels = 2 61 nchannels = 2
62 sampwidth = 3 62 sampwidth = 3
63 framerate = 11025 63 framerate = 11025
64 nframes = 48 64 nframes = 48
65 comptype = b'NONE' 65 comptype = 'NONE'
66 compname = b'not compressed' 66 compname = 'not compressed'
67 frames = bytes.fromhex("""\ 67 frames = audiotests.fromhex("""\
68 022D65FFEB9D 4B5A0F00FA54 3113C304EE2B 80DCD6084303 \ 68 022D65FFEB9D 4B5A0F00FA54 3113C304EE2B 80DCD6084303 \
69 CBDEC006B261 48A99803F2F8 BFE82401B07D 036BFBFE7B5D \ 69 CBDEC006B261 48A99803F2F8 BFE82401B07D 036BFBFE7B5D \
70 B85756FA3EC9 B4B055F3502B 299830EBCB62 1A5CA7E6D99A \ 70 B85756FA3EC9 B4B055F3502B 299830EBCB62 1A5CA7E6D99A \
71 EDFA3EE491BD C625EBE27884 0E05A9E0B6CF EF2929E02922 \ 71 EDFA3EE491BD C625EBE27884 0E05A9E0B6CF EF2929E02922 \
72 5758D8E27067 FB3557E83E16 1377BFEF8402 D82C5BF7272A \ 72 5758D8E27067 FB3557E83E16 1377BFEF8402 D82C5BF7272A \
73 978F16FB7745 F5F865FC1013 086635FB9C4E DF30FCFB40EE \ 73 978F16FB7745 F5F865FC1013 086635FB9C4E DF30FCFB40EE \
74 117FE0FA3438 3EE6B8FB5AC3 BC77A3FCB2F4 66D6DAFF5F32 \ 74 117FE0FA3438 3EE6B8FB5AC3 BC77A3FCB2F4 66D6DAFF5F32 \
75 CF13B9041275 431D69097A8C C1BB600EC74E 5120B912A2BA \ 75 CF13B9041275 431D69097A8C C1BB600EC74E 5120B912A2BA \
76 EEDF641754C0 8207001664B7 7FFFFF14453F 8000001294E6 \ 76 EEDF641754C0 8207001664B7 7FFFFF14453F 8000001294E6 \
77 499C1B0EB3B2 52B73E0DBCA0 EFB2B20F5FD8 CE3CDB0FBE12 \ 77 499C1B0EB3B2 52B73E0DBCA0 EFB2B20F5FD8 CE3CDB0FBE12 \
78 E4B49C0CEA2D 6344A80A5A7C 08C8FE0A1FFE 2BB9860B0A0E \ 78 E4B49C0CEA2D 6344A80A5A7C 08C8FE0A1FFE 2BB9860B0A0E \
79 51486F0E44E1 8BCC64113B05 B6F4EC0EEB36 4413170A5B48 \ 79 51486F0E44E1 8BCC64113B05 B6F4EC0EEB36 4413170A5B48 \
80 """) 80 """)
81 close_fd = True 81 close_fd = True
82 82
83 83
84 class AifcPCM32Test(audiotests.AudioWriteTests, 84 class AifcPCM32Test(audiotests.AudioWriteTests,
85 audiotests.AudioTestsWithSourceFile, 85 audiotests.AudioTestsWithSourceFile,
86 unittest.TestCase): 86 unittest.TestCase):
87 module = aifc 87 module = aifc
88 sndfilename = 'pluck-pcm32.aiff' 88 sndfilename = 'pluck-pcm32.aiff'
89 sndfilenframes = 3307 89 sndfilenframes = 3307
90 nchannels = 2 90 nchannels = 2
91 sampwidth = 4 91 sampwidth = 4
92 framerate = 11025 92 framerate = 11025
93 nframes = 48 93 nframes = 48
94 comptype = b'NONE' 94 comptype = 'NONE'
95 compname = b'not compressed' 95 compname = 'not compressed'
96 frames = bytes.fromhex("""\ 96 frames = audiotests.fromhex("""\
97 022D65BCFFEB9D92 4B5A0F8000FA549C 3113C34004EE2BC0 80DCD680084303E0 \ 97 022D65BCFFEB9D92 4B5A0F8000FA549C 3113C34004EE2BC0 80DCD680084303E0 \
98 CBDEC0C006B26140 48A9980003F2F8FC BFE8248001B07D92 036BFB60FE7B5D34 \ 98 CBDEC0C006B26140 48A9980003F2F8FC BFE8248001B07D92 036BFB60FE7B5D34 \
99 B8575600FA3EC920 B4B05500F3502BC0 29983000EBCB6240 1A5CA7A0E6D99A60 \ 99 B8575600FA3EC920 B4B05500F3502BC0 29983000EBCB6240 1A5CA7A0E6D99A60 \
100 EDFA3E80E491BD40 C625EB80E27884A0 0E05A9A0E0B6CFE0 EF292940E0292280 \ 100 EDFA3E80E491BD40 C625EB80E27884A0 0E05A9A0E0B6CFE0 EF292940E0292280 \
101 5758D800E2706700 FB3557D8E83E1640 1377BF00EF840280 D82C5B80F7272A80 \ 101 5758D800E2706700 FB3557D8E83E1640 1377BF00EF840280 D82C5B80F7272A80 \
102 978F1600FB774560 F5F86510FC101364 086635A0FB9C4E20 DF30FC40FB40EE28 \ 102 978F1600FB774560 F5F86510FC101364 086635A0FB9C4E20 DF30FC40FB40EE28 \
103 117FE0A0FA3438B0 3EE6B840FB5AC3F0 BC77A380FCB2F454 66D6DA80FF5F32B4 \ 103 117FE0A0FA3438B0 3EE6B840FB5AC3F0 BC77A380FCB2F454 66D6DA80FF5F32B4 \
104 CF13B980041275B0 431D6980097A8C00 C1BB60000EC74E00 5120B98012A2BAA0 \ 104 CF13B980041275B0 431D6980097A8C00 C1BB60000EC74E00 5120B98012A2BAA0 \
105 EEDF64C01754C060 820700001664B780 7FFFFFFF14453F40 800000001294E6E0 \ 105 EEDF64C01754C060 820700001664B780 7FFFFFFF14453F40 800000001294E6E0 \
106 499C1B000EB3B270 52B73E000DBCA020 EFB2B2E00F5FD880 CE3CDB400FBE1270 \ 106 499C1B000EB3B270 52B73E000DBCA020 EFB2B2E00F5FD880 CE3CDB400FBE1270 \
107 E4B49CC00CEA2D90 6344A8800A5A7CA0 08C8FE800A1FFEE0 2BB986C00B0A0E00 \ 107 E4B49CC00CEA2D90 6344A8800A5A7CA0 08C8FE800A1FFEE0 2BB986C00B0A0E00 \
108 51486F800E44E190 8BCC6480113B0580 B6F4EC000EEB3630 441317800A5B48A0 \ 108 51486F800E44E190 8BCC6480113B0580 B6F4EC000EEB3630 441317800A5B48A0 \
109 """) 109 """)
110 close_fd = True 110 close_fd = True
111 111
112 112
113 class AifcULAWTest(audiotests.AudioWriteTests, 113 class AifcULAWTest(audiotests.AudioWriteTests,
114 audiotests.AudioTestsWithSourceFile, 114 audiotests.AudioTestsWithSourceFile,
115 unittest.TestCase): 115 unittest.TestCase):
116 module = aifc 116 module = aifc
117 sndfilename = 'pluck-ulaw.aifc' 117 sndfilename = 'pluck-ulaw.aifc'
118 sndfilenframes = 3307 118 sndfilenframes = 3307
119 nchannels = 2 119 nchannels = 2
120 sampwidth = 2 120 sampwidth = 2
121 framerate = 11025 121 framerate = 11025
122 nframes = 48 122 nframes = 48
123 comptype = b'ulaw' 123 comptype = 'ulaw'
124 compname = b'' 124 compname = ''
125 frames = bytes.fromhex("""\ 125 frames = audiotests.fromhex("""\
126 022CFFE8 497C0104 307C04DC 8284083C CB84069C 497C03DC BE8401AC 036CFE74 \ 126 022CFFE8 497C0104 307C04DC 8284083C CB84069C 497C03DC BE8401AC 036CFE74 \
127 B684FA24 B684F344 2A7CEC04 19FCE704 EE04E504 C584E204 0E3CE104 EF04DF84 \ 127 B684FA24 B684F344 2A7CEC04 19FCE704 EE04E504 C584E204 0E3CE104 EF04DF84 \
128 557CE204 FB24E804 12FCEF04 D784F744 9684FB64 F5C4FC24 083CFBA4 DF84FB24 \ 128 557CE204 FB24E804 12FCEF04 D784F744 9684FB64 F5C4FC24 083CFBA4 DF84FB24 \
129 11FCFA24 3E7CFB64 BA84FCB4 657CFF5C CF84041C 417C093C C1840EBC 517C12FC \ 129 11FCFA24 3E7CFB64 BA84FCB4 657CFF5C CF84041C 417C093C C1840EBC 517C12FC \
130 EF0416FC 828415FC 7D7C13FC 828412FC 497C0EBC 517C0DBC F0040F3C CD840FFC \ 130 EF0416FC 828415FC 7D7C13FC 828412FC 497C0EBC 517C0DBC F0040F3C CD840FFC \
131 E5040CBC 617C0A3C 08BC0A3C 2C7C0B3C 517C0E3C 8A8410FC B6840EBC 457C0A3C \ 131 E5040CBC 617C0A3C 08BC0A3C 2C7C0B3C 517C0E3C 8A8410FC B6840EBC 457C0A3C \
132 """) 132 """)
133 if sys.byteorder != 'big': 133 if sys.byteorder != 'big':
134 frames = audiotests.byteswap2(frames) 134 frames = audiotests.byteswap2(frames)
135 close_fd = True 135 close_fd = True
136 136
137 137
138 class AifcALAWTest(audiotests.AudioWriteTests,
139 audiotests.AudioTestsWithSourceFile,
140 unittest.TestCase):
141 module = aifc
142 sndfilename = 'pluck-alaw.aifc'
143 sndfilenframes = 3307
144 nchannels = 2
145 sampwidth = 2
146 framerate = 11025
147 nframes = 48
148 comptype = b'alaw'
149 compname = b''
150 frames = bytes.fromhex("""\
151 0230FFE8 4A0000F8 310004E0 82000840 CB0006A0 4A0003F0 BE0001A8 0370FE78 \
152 BA00FA20 B600F340 2900EB80 1A80E680 ED80E480 C700E280 0E40E080 EF80E080 \
153 5600E280 FB20E880 1380EF80 D900F740 9600FB60 F5C0FC10 0840FBA0 DF00FB20 \
154 1180FA20 3F00FB60 BE00FCB0 6600FF58 CF000420 42000940 C1000EC0 52001280 \
155 EE801780 82001680 7E001480 82001280 4A000EC0 52000DC0 EF800F40 CF000FC0 \
156 E4800CC0 62000A40 08C00A40 2B000B40 52000E40 8A001180 B6000EC0 46000A40 \
157 """)
158 if sys.byteorder != 'big':
159 frames = audiotests.byteswap2(frames)
160 close_fd = True
161
162
163 class AifcMiscTest(audiotests.AudioTests, unittest.TestCase): 138 class AifcMiscTest(audiotests.AudioTests, unittest.TestCase):
164 def test_skipunknown(self): 139 def test_skipunknown(self):
165 #Issue 2245 140 #Issue 2245
166 #This file contains chunk types aifc doesn't recognize. 141 #This file contains chunk types aifc doesn't recognize.
167 self.f = aifc.open(findfile('Sine-1000Hz-300ms.aif')) 142 self.f = aifc.open(findfile('Sine-1000Hz-300ms.aif'))
168 143
169 def test_params_added(self):
170 f = self.f = aifc.open(TESTFN, 'wb')
171 f.aiff()
172 f.setparams((1, 1, 1, 1, b'NONE', b''))
173 f.close()
174
175 f = self.f = aifc.open(TESTFN, 'rb')
176 params = f.getparams()
177 self.assertEqual(params.nchannels, f.getnchannels())
178 self.assertEqual(params.sampwidth, f.getsampwidth())
179 self.assertEqual(params.framerate, f.getframerate())
180 self.assertEqual(params.nframes, f.getnframes())
181 self.assertEqual(params.comptype, f.getcomptype())
182 self.assertEqual(params.compname, f.getcompname())
183
184 def test_write_header_comptype_sampwidth(self):
185 for comptype in (b'ULAW', b'ulaw', b'ALAW', b'alaw', b'G722'):
186 fout = aifc.open(io.BytesIO(), 'wb')
187 fout.setnchannels(1)
188 fout.setframerate(1)
189 fout.setcomptype(comptype, b'')
190 fout.close()
191 self.assertEqual(fout.getsampwidth(), 2)
192 fout.initfp(None)
193
194 def test_write_markers_values(self): 144 def test_write_markers_values(self):
195 fout = aifc.open(io.BytesIO(), 'wb') 145 fout = aifc.open(io.BytesIO(), 'wb')
196 self.assertEqual(fout.getmarkers(), None) 146 self.assertEqual(fout.getmarkers(), None)
197 fout.setmark(1, 0, b'foo1') 147 fout.setmark(1, 0, 'foo1')
198 fout.setmark(1, 1, b'foo2') 148 fout.setmark(1, 1, 'foo2')
199 self.assertEqual(fout.getmark(1), (1, 1, b'foo2')) 149 self.assertEqual(fout.getmark(1), (1, 1, 'foo2'))
200 self.assertEqual(fout.getmarkers(), [(1, 1, b'foo2')]) 150 self.assertEqual(fout.getmarkers(), [(1, 1, 'foo2')])
201 fout.initfp(None) 151 fout.initfp(None)
202 152
203 def test_read_markers(self): 153 def test_read_markers(self):
204 fout = self.fout = aifc.open(TESTFN, 'wb') 154 fout = self.fout = aifc.open(TESTFN, 'wb')
205 fout.aiff() 155 fout.aiff()
206 fout.setparams((1, 1, 1, 1, b'NONE', b'')) 156 fout.setparams((1, 1, 1, 1, 'NONE', ''))
207 fout.setmark(1, 0, b'odd') 157 fout.setmark(1, 0, 'odd')
208 fout.setmark(2, 0, b'even') 158 fout.setmark(2, 0, 'even')
209 fout.writeframes(b'\x00') 159 fout.writeframes('\x00')
210 fout.close() 160 fout.close()
211 f = self.f = aifc.open(TESTFN, 'rb') 161 f = self.f = aifc.open(TESTFN, 'rb')
212 self.assertEqual(f.getmarkers(), [(1, 0, b'odd'), (2, 0, b'even')]) 162 self.assertEqual(f.getmarkers(), [(1, 0, 'odd'), (2, 0, 'even')])
213 self.assertEqual(f.getmark(1), (1, 0, b'odd')) 163 self.assertEqual(f.getmark(1), (1, 0, 'odd'))
214 self.assertEqual(f.getmark(2), (2, 0, b'even')) 164 self.assertEqual(f.getmark(2), (2, 0, 'even'))
215 self.assertRaises(aifc.Error, f.getmark, 3) 165 self.assertRaises(aifc.Error, f.getmark, 3)
216 166
217 167
218 class AIFCLowLevelTest(unittest.TestCase): 168 class AIFCLowLevelTest(unittest.TestCase):
219 169
220 def test_read_written(self): 170 def test_read_written(self):
221 def read_written(self, what): 171 def read_written(self, what):
222 f = io.BytesIO() 172 f = io.BytesIO()
223 getattr(aifc, '_write_' + what)(f, x) 173 getattr(aifc, '_write_' + what)(f, x)
224 f.seek(0) 174 f.seek(0)
225 return getattr(aifc, '_read_' + what)(f) 175 return getattr(aifc, '_read_' + what)(f)
226 for x in (-1, 0, 0.1, 1): 176 for x in (-1, 0, 0.1, 1):
227 self.assertEqual(read_written(x, 'float'), x) 177 self.assertEqual(read_written(x, 'float'), x)
228 for x in (float('NaN'), float('Inf')): 178 for x in (float('NaN'), float('Inf')):
229 self.assertEqual(read_written(x, 'float'), aifc._HUGE_VAL) 179 self.assertEqual(read_written(x, 'float'), aifc._HUGE_VAL)
230 for x in (b'', b'foo', b'a' * 255): 180 for x in ('', 'foo', 'a' * 255):
231 self.assertEqual(read_written(x, 'string'), x) 181 self.assertEqual(read_written(x, 'string'), x)
232 for x in (-0x7FFFFFFF, -1, 0, 1, 0x7FFFFFFF): 182 for x in (-0x7FFFFFFF, -1, 0, 1, 0x7FFFFFFF):
233 self.assertEqual(read_written(x, 'long'), x) 183 self.assertEqual(read_written(x, 'long'), x)
234 for x in (0, 1, 0xFFFFFFFF): 184 for x in (0, 1, 0xFFFFFFFF):
235 self.assertEqual(read_written(x, 'ulong'), x) 185 self.assertEqual(read_written(x, 'ulong'), x)
236 for x in (-0x7FFF, -1, 0, 1, 0x7FFF): 186 for x in (-0x7FFF, -1, 0, 1, 0x7FFF):
237 self.assertEqual(read_written(x, 'short'), x) 187 self.assertEqual(read_written(x, 'short'), x)
238 for x in (0, 1, 0xFFFF): 188 for x in (0, 1, 0xFFFF):
239 self.assertEqual(read_written(x, 'ushort'), x) 189 self.assertEqual(read_written(x, 'ushort'), x)
240 190
241 def test_read_raises(self): 191 def test_read_raises(self):
242 f = io.BytesIO(b'\x00') 192 f = io.BytesIO('\x00')
243 self.assertRaises(EOFError, aifc._read_ulong, f) 193 self.assertRaises(EOFError, aifc._read_ulong, f)
244 self.assertRaises(EOFError, aifc._read_long, f) 194 self.assertRaises(EOFError, aifc._read_long, f)
245 self.assertRaises(EOFError, aifc._read_ushort, f) 195 self.assertRaises(EOFError, aifc._read_ushort, f)
246 self.assertRaises(EOFError, aifc._read_short, f) 196 self.assertRaises(EOFError, aifc._read_short, f)
247 197
248 def test_write_long_string_raises(self): 198 def test_write_long_string_raises(self):
249 f = io.BytesIO() 199 f = io.BytesIO()
250 with self.assertRaises(ValueError): 200 with self.assertRaises(ValueError):
251 aifc._write_string(f, b'too long' * 255) 201 aifc._write_string(f, 'too long' * 255)
252 202
253 def test_wrong_open_mode(self): 203 def test_wrong_open_mode(self):
254 with self.assertRaises(aifc.Error): 204 with self.assertRaises(aifc.Error):
255 aifc.open(TESTFN, 'wrong_mode') 205 aifc.open(TESTFN, 'wrong_mode')
256 206
257 def test_read_wrong_form(self): 207 def test_read_wrong_form(self):
258 b1 = io.BytesIO(b'WRNG' + struct.pack('>L', 0)) 208 b1 = io.BytesIO('WRNG' + struct.pack('>L', 0))
259 b2 = io.BytesIO(b'FORM' + struct.pack('>L', 4) + b'WRNG') 209 b2 = io.BytesIO('FORM' + struct.pack('>L', 4) + 'WRNG')
260 self.assertRaises(aifc.Error, aifc.open, b1) 210 self.assertRaises(aifc.Error, aifc.open, b1)
261 self.assertRaises(aifc.Error, aifc.open, b2) 211 self.assertRaises(aifc.Error, aifc.open, b2)
262 212
263 def test_read_no_comm_chunk(self): 213 def test_read_no_comm_chunk(self):
264 b = io.BytesIO(b'FORM' + struct.pack('>L', 4) + b'AIFF') 214 b = io.BytesIO('FORM' + struct.pack('>L', 4) + 'AIFF')
265 self.assertRaises(aifc.Error, aifc.open, b) 215 self.assertRaises(aifc.Error, aifc.open, b)
266 216
267 def test_read_wrong_compression_type(self): 217 def test_read_wrong_compression_type(self):
268 b = b'FORM' + struct.pack('>L', 4) + b'AIFC' 218 b = 'FORM' + struct.pack('>L', 4) + 'AIFC'
269 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)
270 b += b'WRNG' + struct.pack('B', 0) 220 b += 'WRNG' + struct.pack('B', 0)
271 self.assertRaises(aifc.Error, aifc.open, io.BytesIO(b)) 221 self.assertRaises(aifc.Error, aifc.open, io.BytesIO(b))
272 222
273 def test_read_wrong_marks(self): 223 def test_read_wrong_marks(self):
274 b = b'FORM' + struct.pack('>L', 4) + b'AIFF' 224 b = 'FORM' + struct.pack('>L', 4) + 'AIFF'
275 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)
276 b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8 226 b += 'SSND' + struct.pack('>L', 8) + '\x00' * 8
277 b += b'MARK' + struct.pack('>LhB', 3, 1, 1) 227 b += 'MARK' + struct.pack('>LhB', 3, 1, 1)
278 with self.assertWarns(UserWarning): 228 with captured_stdout() as s:
279 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')
280 self.assertEqual(f.getmarkers(), None) 232 self.assertEqual(f.getmarkers(), None)
281 233
282 def test_read_comm_kludge_compname_even(self): 234 def test_read_comm_kludge_compname_even(self):
283 b = b'FORM' + struct.pack('>L', 4) + b'AIFC' 235 b = 'FORM' + struct.pack('>L', 4) + 'AIFC'
284 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)
285 b += b'NONE' + struct.pack('B', 4) + b'even' + b'\x00' 237 b += 'NONE' + struct.pack('B', 4) + 'even' + '\x00'
286 b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8 238 b += 'SSND' + struct.pack('>L', 8) + '\x00' * 8
287 with self.assertWarns(UserWarning): 239 with captured_stdout() as s:
288 f = aifc.open(io.BytesIO(b)) 240 f = aifc.open(io.BytesIO(b))
289 self.assertEqual(f.getcompname(), b'even') 241 self.assertEqual(s.getvalue(), 'Warning: bad COMM chunk size\n')
242 self.assertEqual(f.getcompname(), 'even')
290 243
291 def test_read_comm_kludge_compname_odd(self): 244 def test_read_comm_kludge_compname_odd(self):
292 b = b'FORM' + struct.pack('>L', 4) + b'AIFC' 245 b = 'FORM' + struct.pack('>L', 4) + 'AIFC'
293 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)
294 b += b'NONE' + struct.pack('B', 3) + b'odd' 247 b += 'NONE' + struct.pack('B', 3) + 'odd'
295 b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8 248 b += 'SSND' + struct.pack('>L', 8) + '\x00' * 8
296 with self.assertWarns(UserWarning): 249 with captured_stdout() as s:
297 f = aifc.open(io.BytesIO(b)) 250 f = aifc.open(io.BytesIO(b))
298 self.assertEqual(f.getcompname(), b'odd') 251 self.assertEqual(s.getvalue(), 'Warning: bad COMM chunk size\n')
252 self.assertEqual(f.getcompname(), 'odd')
299 253
300 def test_write_params_raises(self): 254 def test_write_params_raises(self):
301 fout = aifc.open(io.BytesIO(), 'wb') 255 fout = aifc.open(io.BytesIO(), 'wb')
302 wrong_params = (0, 0, 0, 0, b'WRNG', '') 256 wrong_params = (0, 0, 0, 0, 'WRNG', '')
303 self.assertRaises(aifc.Error, fout.setparams, wrong_params) 257 self.assertRaises(aifc.Error, fout.setparams, wrong_params)
304 self.assertRaises(aifc.Error, fout.getparams) 258 self.assertRaises(aifc.Error, fout.getparams)
305 self.assertRaises(aifc.Error, fout.setnchannels, 0) 259 self.assertRaises(aifc.Error, fout.setnchannels, 0)
306 self.assertRaises(aifc.Error, fout.getnchannels) 260 self.assertRaises(aifc.Error, fout.getnchannels)
307 self.assertRaises(aifc.Error, fout.setsampwidth, 0) 261 self.assertRaises(aifc.Error, fout.setsampwidth, 0)
308 self.assertRaises(aifc.Error, fout.getsampwidth) 262 self.assertRaises(aifc.Error, fout.getsampwidth)
309 self.assertRaises(aifc.Error, fout.setframerate, 0) 263 self.assertRaises(aifc.Error, fout.setframerate, 0)
310 self.assertRaises(aifc.Error, fout.getframerate) 264 self.assertRaises(aifc.Error, fout.getframerate)
311 self.assertRaises(aifc.Error, fout.setcomptype, b'WRNG', '') 265 self.assertRaises(aifc.Error, fout.setcomptype, 'WRNG', '')
312 fout.aiff() 266 fout.aiff()
313 fout.setnchannels(1) 267 fout.setnchannels(1)
314 fout.setsampwidth(1) 268 fout.setsampwidth(1)
315 fout.setframerate(1) 269 fout.setframerate(1)
316 fout.setnframes(1) 270 fout.setnframes(1)
317 fout.writeframes(b'\x00') 271 fout.writeframes('\x00')
318 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))
319 self.assertRaises(aifc.Error, fout.setnchannels, 1) 273 self.assertRaises(aifc.Error, fout.setnchannels, 1)
320 self.assertRaises(aifc.Error, fout.setsampwidth, 1) 274 self.assertRaises(aifc.Error, fout.setsampwidth, 1)
321 self.assertRaises(aifc.Error, fout.setframerate, 1) 275 self.assertRaises(aifc.Error, fout.setframerate, 1)
322 self.assertRaises(aifc.Error, fout.setnframes, 1) 276 self.assertRaises(aifc.Error, fout.setnframes, 1)
323 self.assertRaises(aifc.Error, fout.setcomptype, b'NONE', '') 277 self.assertRaises(aifc.Error, fout.setcomptype, 'NONE', '')
324 self.assertRaises(aifc.Error, fout.aiff) 278 self.assertRaises(aifc.Error, fout.aiff)
325 self.assertRaises(aifc.Error, fout.aifc) 279 self.assertRaises(aifc.Error, fout.aifc)
326 280
327 def test_write_params_singles(self): 281 def test_write_params_singles(self):
328 fout = aifc.open(io.BytesIO(), 'wb') 282 fout = aifc.open(io.BytesIO(), 'wb')
329 fout.aifc() 283 fout.aifc()
330 fout.setnchannels(1) 284 fout.setnchannels(1)
331 fout.setsampwidth(2) 285 fout.setsampwidth(2)
332 fout.setframerate(3) 286 fout.setframerate(3)
333 fout.setnframes(4) 287 fout.setnframes(4)
334 fout.setcomptype(b'NONE', b'name') 288 fout.setcomptype('NONE', 'name')
335 self.assertEqual(fout.getnchannels(), 1) 289 self.assertEqual(fout.getnchannels(), 1)
336 self.assertEqual(fout.getsampwidth(), 2) 290 self.assertEqual(fout.getsampwidth(), 2)
337 self.assertEqual(fout.getframerate(), 3) 291 self.assertEqual(fout.getframerate(), 3)
338 self.assertEqual(fout.getnframes(), 0) 292 self.assertEqual(fout.getnframes(), 0)
339 self.assertEqual(fout.tell(), 0) 293 self.assertEqual(fout.tell(), 0)
340 self.assertEqual(fout.getcomptype(), b'NONE') 294 self.assertEqual(fout.getcomptype(), 'NONE')
341 self.assertEqual(fout.getcompname(), b'name') 295 self.assertEqual(fout.getcompname(), 'name')
342 fout.writeframes(b'\x00' * 4 * fout.getsampwidth() * fout.getnchannels() ) 296 fout.writeframes('\x00' * 4 * fout.getsampwidth() * fout.getnchannels())
343 self.assertEqual(fout.getnframes(), 4) 297 self.assertEqual(fout.getnframes(), 4)
344 self.assertEqual(fout.tell(), 4) 298 self.assertEqual(fout.tell(), 4)
345 299
346 def test_write_params_bunch(self): 300 def test_write_params_bunch(self):
347 fout = aifc.open(io.BytesIO(), 'wb') 301 fout = aifc.open(io.BytesIO(), 'wb')
348 fout.aifc() 302 fout.aifc()
349 p = (1, 2, 3, 4, b'NONE', b'name') 303 p = (1, 2, 3, 4, 'NONE', 'name')
350 fout.setparams(p) 304 fout.setparams(p)
351 self.assertEqual(fout.getparams(), p) 305 self.assertEqual(fout.getparams(), p)
352 fout.initfp(None) 306 fout.initfp(None)
353 307
354 def test_write_header_raises(self): 308 def test_write_header_raises(self):
355 fout = aifc.open(io.BytesIO(), 'wb') 309 fout = aifc.open(io.BytesIO(), 'wb')
356 self.assertRaises(aifc.Error, fout.close) 310 self.assertRaises(aifc.Error, fout.close)
357 fout = aifc.open(io.BytesIO(), 'wb') 311 fout = aifc.open(io.BytesIO(), 'wb')
358 fout.setnchannels(1) 312 fout.setnchannels(1)
359 self.assertRaises(aifc.Error, fout.close) 313 self.assertRaises(aifc.Error, fout.close)
360 fout = aifc.open(io.BytesIO(), 'wb') 314 fout = aifc.open(io.BytesIO(), 'wb')
361 fout.setnchannels(1) 315 fout.setnchannels(1)
362 fout.setsampwidth(1) 316 fout.setsampwidth(1)
363 self.assertRaises(aifc.Error, fout.close) 317 self.assertRaises(aifc.Error, fout.close)
364 318
365 def test_write_header_comptype_raises(self): 319 def test_write_header_comptype_raises(self):
366 for comptype in (b'ULAW', b'ulaw', b'ALAW', b'alaw', b'G722'): 320 for comptype in ('ULAW', 'ulaw', 'ALAW', 'alaw', 'G722'):
367 fout = aifc.open(io.BytesIO(), 'wb') 321 fout = aifc.open(io.BytesIO(), 'wb')
368 fout.setsampwidth(1) 322 fout.setsampwidth(1)
369 fout.setcomptype(comptype, b'') 323 fout.setcomptype(comptype, '')
370 self.assertRaises(aifc.Error, fout.close) 324 self.assertRaises(aifc.Error, fout.close)
371 fout.initfp(None) 325 fout.initfp(None)
372 326
373 def test_write_markers_raises(self): 327 def test_write_markers_raises(self):
374 fout = aifc.open(io.BytesIO(), 'wb') 328 fout = aifc.open(io.BytesIO(), 'wb')
375 self.assertRaises(aifc.Error, fout.setmark, 0, 0, b'') 329 self.assertRaises(aifc.Error, fout.setmark, 0, 0, '')
376 self.assertRaises(aifc.Error, fout.setmark, 1, -1, b'') 330 self.assertRaises(aifc.Error, fout.setmark, 1, -1, '')
377 self.assertRaises(aifc.Error, fout.setmark, 1, 0, None) 331 self.assertRaises(aifc.Error, fout.setmark, 1, 0, None)
378 self.assertRaises(aifc.Error, fout.getmark, 1) 332 self.assertRaises(aifc.Error, fout.getmark, 1)
379 fout.initfp(None) 333 fout.initfp(None)
380 334
381 def test_write_aiff_by_extension(self): 335 def test_write_aiff_by_extension(self):
382 sampwidth = 2 336 sampwidth = 2
383 filename = TESTFN + '.aiff' 337 fout = self.fout = aifc.open(TESTFN + '.aiff', 'wb')
384 fout = self.fout = aifc.open(filename, 'wb') 338 fout.setparams((1, sampwidth, 1, 1, 'ULAW', ''))
385 self.addCleanup(unlink, filename) 339 frames = '\x00' * fout.getnchannels() * sampwidth
386 fout.setparams((1, sampwidth, 1, 1, b'ULAW', b''))
387 frames = b'\x00' * fout.getnchannels() * sampwidth
388 fout.writeframes(frames) 340 fout.writeframes(frames)
389 fout.close() 341 fout.close()
390 f = self.f = aifc.open(filename, 'rb') 342 f = self.f = aifc.open(TESTFN + '.aiff', 'rb')
391 self.assertEqual(f.getcomptype(), b'NONE') 343 self.assertEqual(f.getcomptype(), 'NONE')
392 f.close() 344 f.close()
393 345
394 346
347 def test_main():
348 run_unittest(AifcPCM8Test, AifcPCM16Test, AifcPCM16Test, AifcPCM24Test,
349 AifcPCM32Test, AifcULAWTest,
350 AifcMiscTest, AIFCLowLevelTest)
351
395 if __name__ == "__main__": 352 if __name__ == "__main__":
396 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+