blob: ccfd1e91509a4fa14fb4d155f1c799c2ccc6d832 [file] [log] [blame]
Thomas Wouters3ccec682007-08-28 15:28:19 +00001"""Unit tests for buffer objects.
2
3For now, tests just new or changed functionality.
4
5"""
6
Serhiy Storchakab8e54dd2015-12-30 20:43:29 +02007import copy
8import pickle
Benjamin Peterson550b9452014-06-23 20:12:27 -07009import sys
Thomas Wouters3ccec682007-08-28 15:28:19 +000010import 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
Kristján Valur Jónsson1d108bc2013-03-19 16:50:51 -070027 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
Benjamin Peterson550b9452014-06-23 20:12:27 -070035 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
Serhiy Storchakab8e54dd2015-12-30 20:43:29 +020040 def test_copy(self):
41 buf = buffer(b'abc')
42 with self.assertRaises(TypeError):
43 copy.copy(buf)
44
45 def test_pickle(self):
46 buf = buffer(b'abc')
47 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
48 with self.assertRaises(TypeError):
49 pickle.dumps(buf, proto)
50
Thomas Wouters3ccec682007-08-28 15:28:19 +000051
52def test_main():
Florent Xicluna07627882010-03-21 01:14:24 +000053 with test_support.check_py3k_warnings(("buffer.. not supported",
54 DeprecationWarning)):
55 test_support.run_unittest(BufferTests)
Thomas Wouters3ccec682007-08-28 15:28:19 +000056
57if __name__ == "__main__":
58 test_main()