summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_sunau.py
blob: 339ab576d5cb8c62c1177de763855e2367bd71a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from test.support import run_unittest, TESTFN
import unittest
import os

import sunau

nchannels = 2
sampwidth = 2
framerate = 8000
nframes = 100

class SunAUTest(unittest.TestCase):

    def setUp(self):
        self.f = None

    def tearDown(self):
        if self.f is not None:
            self.f.close()
        try:
            os.remove(TESTFN)
        except OSError:
            pass

    def test_lin(self):
        self.f = sunau.open(TESTFN, 'w')
        self.f.setnchannels(nchannels)
        self.f.setsampwidth(sampwidth)
        self.f.setframerate(framerate)
        self.f.setcomptype('NONE', 'not compressed')
        output = b'\xff\x00\x12\xcc' * (nframes * nchannels * sampwidth // 4)
        self.f.writeframes(output)
        self.f.close()

        self.f = sunau.open(TESTFN, 'rb')
        self.assertEqual(nchannels, self.f.getnchannels())
        self.assertEqual(sampwidth, self.f.getsampwidth())
        self.assertEqual(framerate, self.f.getframerate())
        self.assertEqual(nframes, self.f.getnframes())
        self.assertEqual('NONE', self.f.getcomptype())
        self.assertEqual(self.f.readframes(nframes), output)
        self.f.close()

    def test_ulaw(self):
        self.f = sunau.open(TESTFN, 'w')
        self.f.setnchannels(nchannels)
        self.f.setsampwidth(sampwidth)
        self.f.setframerate(framerate)
        self.f.setcomptype('ULAW', '')
        # u-law compression is lossy, therefore we can't expect non-zero data
        # to come back unchanged.
        output = b'\0' * nframes * nchannels * sampwidth
        self.f.writeframes(output)
        self.f.close()

        self.f = sunau.open(TESTFN, 'rb')
        self.assertEqual(nchannels, self.f.getnchannels())
        self.assertEqual(sampwidth, self.f.getsampwidth())
        self.assertEqual(framerate, self.f.getframerate())
        self.assertEqual(nframes, self.f.getnframes())
        self.assertEqual('ULAW', self.f.getcomptype())
        self.assertEqual(self.f.readframes(nframes), output)
        self.f.close()


def test_main():
    run_unittest(SunAUTest)

if __name__ == "__main__":
    unittest.main()