blob: e4928382582b466e3872f78acb20b6384b51ae27 [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)
109
R. David Murray25b4add2009-04-29 13:17:37 +0000110
Antoine Pitrou3b6a3142012-01-17 17:13:04 +0100111class AIFCLowLevelTest(unittest.TestCase):
112
113 def test_read_written(self):
114 def read_written(self, what):
115 f = io.BytesIO()
116 getattr(aifc, '_write_' + what)(f, x)
117 f.seek(0)
118 return getattr(aifc, '_read_' + what)(f)
119 for x in (-1, 0, 0.1, 1):
120 self.assertEqual(read_written(x, 'float'), x)
121 for x in (float('NaN'), float('Inf')):
122 self.assertEqual(read_written(x, 'float'), aifc._HUGE_VAL)
123 for x in (b'', b'foo', b'a' * 255):
124 self.assertEqual(read_written(x, 'string'), x)
125 for x in (-0x7FFFFFFF, -1, 0, 1, 0x7FFFFFFF):
126 self.assertEqual(read_written(x, 'long'), x)
127 for x in (0, 1, 0xFFFFFFFF):
128 self.assertEqual(read_written(x, 'ulong'), x)
129 for x in (-0x7FFF, -1, 0, 1, 0x7FFF):
130 self.assertEqual(read_written(x, 'short'), x)
131 for x in (0, 1, 0xFFFF):
132 self.assertEqual(read_written(x, 'ushort'), x)
133
134 def test_read_raises(self):
135 f = io.BytesIO(b'\x00')
136 self.assertRaises(EOFError, aifc._read_ulong, f)
137 self.assertRaises(EOFError, aifc._read_long, f)
138 self.assertRaises(EOFError, aifc._read_ushort, f)
139 self.assertRaises(EOFError, aifc._read_short, f)
140
141 def test_write_long_string_raises(self):
142 f = io.BytesIO()
143 with self.assertRaises(ValueError):
144 aifc._write_string(f, b'too long' * 255)
145
146
R. David Murray25b4add2009-04-29 13:17:37 +0000147def test_main():
148 run_unittest(AIFCTest)
Antoine Pitrou3b6a3142012-01-17 17:13:04 +0100149 run_unittest(AIFCLowLevelTest)
R. David Murray25b4add2009-04-29 13:17:37 +0000150
151
152if __name__ == "__main__":
153 unittest.main()