1"""Unit tests for buffer objects.
2
3For now, tests just new or changed functionality.
4
5"""
6
7import copy
8import pickle
9import sys
10import unittest
11from test import test_support
12
13class BufferTests(unittest.TestCase):
14
15    def test_extended_getslice(self):
16        # Test extended slicing by comparing with list slicing.
17        s = "".join(chr(c) for c in list(range(255, -1, -1)))
18        b = buffer(s)
19        indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300)
20        for start in indices:
21            for stop in indices:
22                # Skip step 0 (invalid)
23                for step in indices[1:]:
24                    self.assertEqual(b[start:stop:step],
25                                     s[start:stop:step])
26
27    def test_newbuffer_interface(self):
28        # Test that the buffer object has the new buffer interface
29        # as used by the memoryview object
30        s = "".join(chr(c) for c in list(range(255, -1, -1)))
31        b = buffer(s)
32        m = memoryview(b) # Should not raise an exception
33        self.assertEqual(m.tobytes(), s)
34
35    def test_large_buffer_size_and_offset(self):
36        data = bytearray('hola mundo')
37        buf = buffer(data, sys.maxsize, sys.maxsize)
38        self.assertEqual(buf[:4096], "")
39
40    def test_copy(self):
41        buf = buffer(b'abc')
42        with self.assertRaises(TypeError):
43            copy.copy(buf)
44
45    # See issue #22995
46    ## def test_pickle(self):
47    ##     buf = buffer(b'abc')
48    ##     for proto in range(pickle.HIGHEST_PROTOCOL + 1):
49    ##         with self.assertRaises(TypeError):
50    ##             pickle.dumps(buf, proto)
51
52
53def test_main():
54    with test_support.check_py3k_warnings(("buffer.. not supported",
55                                           DeprecationWarning)):
56        test_support.run_unittest(BufferTests)
57
58if __name__ == "__main__":
59    test_main()
60