Thomas Wouters | 3ccec68 | 2007-08-28 15:28:19 +0000 | [diff] [blame] | 1 | """Unit tests for buffer objects. |
| 2 | |
| 3 | For now, tests just new or changed functionality. |
| 4 | |
| 5 | """ |
| 6 | |
Serhiy Storchaka | b8e54dd | 2015-12-30 20:43:29 +0200 | [diff] [blame] | 7 | import copy |
| 8 | import pickle |
Benjamin Peterson | 550b945 | 2014-06-23 20:12:27 -0700 | [diff] [blame] | 9 | import sys |
Thomas Wouters | 3ccec68 | 2007-08-28 15:28:19 +0000 | [diff] [blame] | 10 | import unittest |
Serhiy Storchaka | 3dd1ccb | 2017-08-02 11:33:33 +0300 | [diff] [blame^] | 11 | import warnings |
Thomas Wouters | 3ccec68 | 2007-08-28 15:28:19 +0000 | [diff] [blame] | 12 | from test import test_support |
| 13 | |
| 14 | class BufferTests(unittest.TestCase): |
| 15 | |
| 16 | def test_extended_getslice(self): |
| 17 | # Test extended slicing by comparing with list slicing. |
| 18 | s = "".join(chr(c) for c in list(range(255, -1, -1))) |
| 19 | b = buffer(s) |
| 20 | indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300) |
| 21 | for start in indices: |
| 22 | for stop in indices: |
| 23 | # Skip step 0 (invalid) |
| 24 | for step in indices[1:]: |
| 25 | self.assertEqual(b[start:stop:step], |
| 26 | s[start:stop:step]) |
| 27 | |
Kristján Valur Jónsson | 1d108bc | 2013-03-19 16:50:51 -0700 | [diff] [blame] | 28 | def test_newbuffer_interface(self): |
| 29 | # Test that the buffer object has the new buffer interface |
| 30 | # as used by the memoryview object |
| 31 | s = "".join(chr(c) for c in list(range(255, -1, -1))) |
| 32 | b = buffer(s) |
| 33 | m = memoryview(b) # Should not raise an exception |
| 34 | self.assertEqual(m.tobytes(), s) |
| 35 | |
Benjamin Peterson | 550b945 | 2014-06-23 20:12:27 -0700 | [diff] [blame] | 36 | def test_large_buffer_size_and_offset(self): |
| 37 | data = bytearray('hola mundo') |
| 38 | buf = buffer(data, sys.maxsize, sys.maxsize) |
| 39 | self.assertEqual(buf[:4096], "") |
| 40 | |
Serhiy Storchaka | b8e54dd | 2015-12-30 20:43:29 +0200 | [diff] [blame] | 41 | def test_copy(self): |
| 42 | buf = buffer(b'abc') |
Serhiy Storchaka | 3dd1ccb | 2017-08-02 11:33:33 +0300 | [diff] [blame^] | 43 | with self.assertRaises(TypeError), warnings.catch_warnings(): |
| 44 | warnings.filterwarnings('ignore', ".*buffer", DeprecationWarning) |
Serhiy Storchaka | b8e54dd | 2015-12-30 20:43:29 +0200 | [diff] [blame] | 45 | copy.copy(buf) |
| 46 | |
Serhiy Storchaka | 3dd1ccb | 2017-08-02 11:33:33 +0300 | [diff] [blame^] | 47 | @test_support.cpython_only |
| 48 | def test_pickle(self): |
| 49 | buf = buffer(b'abc') |
| 50 | for proto in range(2): |
| 51 | with self.assertRaises(TypeError): |
| 52 | pickle.dumps(buf, proto) |
| 53 | with test_support.check_py3k_warnings( |
| 54 | (".*buffer", DeprecationWarning)): |
| 55 | pickle.dumps(buf, 2) |
Serhiy Storchaka | b8e54dd | 2015-12-30 20:43:29 +0200 | [diff] [blame] | 56 | |
Thomas Wouters | 3ccec68 | 2007-08-28 15:28:19 +0000 | [diff] [blame] | 57 | |
| 58 | def test_main(): |
Florent Xicluna | 0762788 | 2010-03-21 01:14:24 +0000 | [diff] [blame] | 59 | with test_support.check_py3k_warnings(("buffer.. not supported", |
| 60 | DeprecationWarning)): |
| 61 | test_support.run_unittest(BufferTests) |
Thomas Wouters | 3ccec68 | 2007-08-28 15:28:19 +0000 | [diff] [blame] | 62 | |
| 63 | if __name__ == "__main__": |
| 64 | test_main() |