| 1 |
"""Unit tests for buffer objects. |
| 2 |
|
| 3 |
For now, tests just new or changed functionality. |
| 4 |
|
| 5 |
""" |
| 6 |
|
| 7 |
import copy |
| 8 |
import pickle |
| 9 |
import sys |
| 10 |
import unittest |
| 11 |
import warnings |
| 12 |
from test import test_support |
| 13 |
|
| 14 |
class BufferTests(unittest.TestCase): |
| 15 |
|
| 16 |
def test_extended_getslice(self): |
| 17 |
|
| 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 |
|
| 24 |
for step in indices[1:]: |
| 25 |
self.assertEqual(b[start:stop:step], |
| 26 |
s[start:stop:step]) |
| 27 |
|
| 28 |
def test_newbuffer_interface(self): |
| 29 |
|
| 30 |
|
| 31 |
s = "".join(chr(c) for c in list(range(255, -1, -1))) |
| 32 |
b = buffer(s) |
| 33 |
m = memoryview(b) |
| 34 |
self.assertEqual(m.tobytes(), s) |
| 35 |
|
| 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 |
|
| 41 |
def test_copy(self): |
| 42 |
buf = buffer(b'abc') |
| 43 |
with self.assertRaises(TypeError), warnings.catch_warnings(): |
| 44 |
warnings.filterwarnings('ignore', ".*buffer", DeprecationWarning) |
| 45 |
copy.copy(buf) |
| 46 |
|
| 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) |
| 56 |
|
| 57 |
|
| 58 |
def test_main(): |
| 59 |
with test_support.check_py3k_warnings(("buffer.. not supported", |
| 60 |
DeprecationWarning)): |
| 61 |
test_support.run_unittest(BufferTests) |
| 62 |
|
| 63 |
if __name__ == "__main__": |
| 64 |
test_main() |