blob: a02c5f7e36feba7343aef461212a4294da9b0c60 [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
Benjamin Peterson550b9452014-06-23 20:12:27 -07007import sys
Thomas Wouters3ccec682007-08-28 15:28:19 +00008import unittest
9from test import test_support
10
11class 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ónsson1d108bc2013-03-19 16:50:51 -070025 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 Peterson550b9452014-06-23 20:12:27 -070033 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 Wouters3ccec682007-08-28 15:28:19 +000038
39def test_main():
Florent Xicluna07627882010-03-21 01:14:24 +000040 with test_support.check_py3k_warnings(("buffer.. not supported",
41 DeprecationWarning)):
42 test_support.run_unittest(BufferTests)
Thomas Wouters3ccec682007-08-28 15:28:19 +000043
44if __name__ == "__main__":
45 test_main()