test_buffer.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. """Unit tests for buffer objects.
  2. For now, tests just new or changed functionality.
  3. """
  4. import copy
  5. import pickle
  6. import sys
  7. import unittest
  8. from test import test_support
  9. class BufferTests(unittest.TestCase):
  10. def test_extended_getslice(self):
  11. # Test extended slicing by comparing with list slicing.
  12. s = "".join(chr(c) for c in list(range(255, -1, -1)))
  13. b = buffer(s)
  14. indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300)
  15. for start in indices:
  16. for stop in indices:
  17. # Skip step 0 (invalid)
  18. for step in indices[1:]:
  19. self.assertEqual(b[start:stop:step],
  20. s[start:stop:step])
  21. def test_newbuffer_interface(self):
  22. # Test that the buffer object has the new buffer interface
  23. # as used by the memoryview object
  24. s = "".join(chr(c) for c in list(range(255, -1, -1)))
  25. b = buffer(s)
  26. m = memoryview(b) # Should not raise an exception
  27. self.assertEqual(m.tobytes(), s)
  28. def test_large_buffer_size_and_offset(self):
  29. data = bytearray('hola mundo')
  30. buf = buffer(data, sys.maxsize, sys.maxsize)
  31. self.assertEqual(buf[:4096], "")
  32. def test_copy(self):
  33. buf = buffer(b'abc')
  34. with self.assertRaises(TypeError):
  35. copy.copy(buf)
  36. # See issue #22995
  37. ## def test_pickle(self):
  38. ## buf = buffer(b'abc')
  39. ## for proto in range(pickle.HIGHEST_PROTOCOL + 1):
  40. ## with self.assertRaises(TypeError):
  41. ## pickle.dumps(buf, proto)
  42. def test_main():
  43. with test_support.check_py3k_warnings(("buffer.. not supported",
  44. DeprecationWarning)):
  45. test_support.run_unittest(BufferTests)
  46. if __name__ == "__main__":
  47. test_main()