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 | |
Benjamin Peterson | 550b945 | 2014-06-23 20:12:27 -0700 | [diff] [blame] | 7 | import sys |
Thomas Wouters | 3ccec68 | 2007-08-28 15:28:19 +0000 | [diff] [blame] | 8 | import unittest |
| 9 | from test import test_support |
| 10 | |
| 11 | class BufferTests(unittest.TestCase): |
| 12 | |
| 13 | def test_extended_getslice(self): |
| 14 | # Test extended slicing by comparing with list slicing. |
| 15 | s = "".join(chr(c) for c in list(range(255, -1, -1))) |
| 16 | b = buffer(s) |
| 17 | indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300) |
| 18 | for start in indices: |
| 19 | for stop in indices: |
| 20 | # Skip step 0 (invalid) |
| 21 | for step in indices[1:]: |
| 22 | self.assertEqual(b[start:stop:step], |
| 23 | s[start:stop:step]) |
| 24 | |
Kristján Valur Jónsson | 1d108bc | 2013-03-19 16:50:51 -0700 | [diff] [blame] | 25 | def test_newbuffer_interface(self): |
| 26 | # Test that the buffer object has the new buffer interface |
| 27 | # as used by the memoryview object |
| 28 | s = "".join(chr(c) for c in list(range(255, -1, -1))) |
| 29 | b = buffer(s) |
| 30 | m = memoryview(b) # Should not raise an exception |
| 31 | self.assertEqual(m.tobytes(), s) |
| 32 | |
Benjamin Peterson | 550b945 | 2014-06-23 20:12:27 -0700 | [diff] [blame] | 33 | def test_large_buffer_size_and_offset(self): |
| 34 | data = bytearray('hola mundo') |
| 35 | buf = buffer(data, sys.maxsize, sys.maxsize) |
| 36 | self.assertEqual(buf[:4096], "") |
| 37 | |
Thomas Wouters | 3ccec68 | 2007-08-28 15:28:19 +0000 | [diff] [blame] | 38 | |
| 39 | def test_main(): |
Florent Xicluna | 0762788 | 2010-03-21 01:14:24 +0000 | [diff] [blame] | 40 | with test_support.check_py3k_warnings(("buffer.. not supported", |
| 41 | DeprecationWarning)): |
| 42 | test_support.run_unittest(BufferTests) |
Thomas Wouters | 3ccec68 | 2007-08-28 15:28:19 +0000 | [diff] [blame] | 43 | |
| 44 | if __name__ == "__main__": |
| 45 | test_main() |