blob: ac8e636ba4011f7355075e562fe6065c4c06bbcb [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
7import unittest
8from test import test_support
9
10class BufferTests(unittest.TestCase):
11
12 def test_extended_getslice(self):
13 # Test extended slicing by comparing with list slicing.
14 s = "".join(chr(c) for c in list(range(255, -1, -1)))
15 b = buffer(s)
16 indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300)
17 for start in indices:
18 for stop in indices:
19 # Skip step 0 (invalid)
20 for step in indices[1:]:
21 self.assertEqual(b[start:stop:step],
22 s[start:stop:step])
23
Kristján Valur Jónsson1d108bc2013-03-19 16:50:51 -070024 def test_newbuffer_interface(self):
25 # Test that the buffer object has the new buffer interface
26 # as used by the memoryview object
27 s = "".join(chr(c) for c in list(range(255, -1, -1)))
28 b = buffer(s)
29 m = memoryview(b) # Should not raise an exception
30 self.assertEqual(m.tobytes(), s)
31
Thomas Wouters3ccec682007-08-28 15:28:19 +000032
33def test_main():
Florent Xicluna07627882010-03-21 01:14:24 +000034 with test_support.check_py3k_warnings(("buffer.. not supported",
35 DeprecationWarning)):
36 test_support.run_unittest(BufferTests)
Thomas Wouters3ccec682007-08-28 15:28:19 +000037
38if __name__ == "__main__":
39 test_main()