blob: 339ab576d5cb8c62c1177de763855e2367bd71a1 [file] [log] [blame]
Victor Stinner7f3652e2010-06-07 20:14:04 +00001from test.support import run_unittest, TESTFN
2import unittest
3import os
4
5import sunau
6
7nchannels = 2
8sampwidth = 2
9framerate = 8000
10nframes = 100
11
12class SunAUTest(unittest.TestCase):
13
14 def setUp(self):
15 self.f = None
16
17 def tearDown(self):
18 if self.f is not None:
19 self.f.close()
20 try:
21 os.remove(TESTFN)
22 except OSError:
23 pass
24
25 def test_lin(self):
26 self.f = sunau.open(TESTFN, 'w')
27 self.f.setnchannels(nchannels)
28 self.f.setsampwidth(sampwidth)
29 self.f.setframerate(framerate)
30 self.f.setcomptype('NONE', 'not compressed')
31 output = b'\xff\x00\x12\xcc' * (nframes * nchannels * sampwidth // 4)
32 self.f.writeframes(output)
33 self.f.close()
34
35 self.f = sunau.open(TESTFN, 'rb')
36 self.assertEqual(nchannels, self.f.getnchannels())
37 self.assertEqual(sampwidth, self.f.getsampwidth())
38 self.assertEqual(framerate, self.f.getframerate())
39 self.assertEqual(nframes, self.f.getnframes())
40 self.assertEqual('NONE', self.f.getcomptype())
41 self.assertEqual(self.f.readframes(nframes), output)
42 self.f.close()
43
44 def test_ulaw(self):
45 self.f = sunau.open(TESTFN, 'w')
46 self.f.setnchannels(nchannels)
47 self.f.setsampwidth(sampwidth)
48 self.f.setframerate(framerate)
49 self.f.setcomptype('ULAW', '')
50 # u-law compression is lossy, therefore we can't expect non-zero data
51 # to come back unchanged.
52 output = b'\0' * nframes * nchannels * sampwidth
53 self.f.writeframes(output)
54 self.f.close()
55
56 self.f = sunau.open(TESTFN, 'rb')
57 self.assertEqual(nchannels, self.f.getnchannels())
58 self.assertEqual(sampwidth, self.f.getsampwidth())
59 self.assertEqual(framerate, self.f.getframerate())
60 self.assertEqual(nframes, self.f.getnframes())
61 self.assertEqual('ULAW', self.f.getcomptype())
62 self.assertEqual(self.f.readframes(nframes), output)
63 self.f.close()
64
65
66def test_main():
67 run_unittest(SunAUTest)
68
69if __name__ == "__main__":
70 unittest.main()