| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
from array import array |
| 4 |
|
| 5 |
from msgpack import packb, unpackb |
| 6 |
|
| 7 |
|
| 8 |
def make_array(f, data): |
| 9 |
a = array(f) |
| 10 |
a.frombytes(data) |
| 11 |
return a |
| 12 |
|
| 13 |
|
| 14 |
def _runtest(format, nbytes, expected_header, expected_prefix, use_bin_type): |
| 15 |
# create a new array |
| 16 |
original_array = array(format) |
| 17 |
original_array.fromlist([255] * (nbytes // original_array.itemsize)) |
| 18 |
original_data = original_array.tobytes() |
| 19 |
view = memoryview(original_array) |
| 20 |
|
| 21 |
# pack, unpack, and reconstruct array |
| 22 |
packed = packb(view, use_bin_type=use_bin_type) |
| 23 |
unpacked = unpackb(packed, raw=(not use_bin_type)) |
| 24 |
reconstructed_array = make_array(format, unpacked) |
| 25 |
|
| 26 |
# check that we got the right amount of data |
| 27 |
assert len(original_data) == nbytes |
| 28 |
# check packed header |
| 29 |
assert packed[:1] == expected_header |
| 30 |
# check packed length prefix, if any |
| 31 |
assert packed[1 : 1 + len(expected_prefix)] == expected_prefix |
| 32 |
# check packed data |
| 33 |
assert packed[1 + len(expected_prefix) :] == original_data |
| 34 |
# check array unpacked correctly |
| 35 |
assert original_array == reconstructed_array |
| 36 |
|
| 37 |
|
| 38 |
def test_fixstr_from_byte(): |
| 39 |
_runtest("B", 1, b"\xa1", b"", False) |
| 40 |
_runtest("B", 31, b"\xbf", b"", False) |
| 41 |
|
| 42 |
|
| 43 |
def test_fixstr_from_float(): |
| 44 |
_runtest("f", 4, b"\xa4", b"", False) |
| 45 |
_runtest("f", 28, b"\xbc", b"", False) |
| 46 |
|
| 47 |
|
| 48 |
def test_str16_from_byte(): |
| 49 |
_runtest("B", 2**8, b"\xda", b"\x01\x00", False) |
| 50 |
_runtest("B", 2**16 - 1, b"\xda", b"\xff\xff", False) |
| 51 |
|
| 52 |
|
| 53 |
def test_str16_from_float(): |
| 54 |
_runtest("f", 2**8, b"\xda", b"\x01\x00", False) |
| 55 |
_runtest("f", 2**16 - 4, b"\xda", b"\xff\xfc", False) |
| 56 |
|
| 57 |
|
| 58 |
def test_str32_from_byte(): |
| 59 |
_runtest("B", 2**16, b"\xdb", b"\x00\x01\x00\x00", False) |
| 60 |
|
| 61 |
|
| 62 |
def test_str32_from_float(): |
| 63 |
_runtest("f", 2**16, b"\xdb", b"\x00\x01\x00\x00", False) |
| 64 |
|
| 65 |
|
| 66 |
def test_bin8_from_byte(): |
| 67 |
_runtest("B", 1, b"\xc4", b"\x01", True) |
| 68 |
_runtest("B", 2**8 - 1, b"\xc4", b"\xff", True) |
| 69 |
|
| 70 |
|
| 71 |
def test_bin8_from_float(): |
| 72 |
_runtest("f", 4, b"\xc4", b"\x04", True) |
| 73 |
_runtest("f", 2**8 - 4, b"\xc4", b"\xfc", True) |
| 74 |
|
| 75 |
|
| 76 |
def test_bin16_from_byte(): |
| 77 |
_runtest("B", 2**8, b"\xc5", b"\x01\x00", True) |
| 78 |
_runtest("B", 2**16 - 1, b"\xc5", b"\xff\xff", True) |
| 79 |
|
| 80 |
|
| 81 |
def test_bin16_from_float(): |
| 82 |
_runtest("f", 2**8, b"\xc5", b"\x01\x00", True) |
| 83 |
_runtest("f", 2**16 - 4, b"\xc5", b"\xff\xfc", True) |
| 84 |
|
| 85 |
|
| 86 |
def test_bin32_from_byte(): |
| 87 |
_runtest("B", 2**16, b"\xc6", b"\x00\x01\x00\x00", True) |
| 88 |
|
| 89 |
|
| 90 |
def test_bin32_from_float(): |
| 91 |
_runtest("f", 2**16, b"\xc6", b"\x00\x01\x00\x00", True) |
| 92 |
|
| 93 |
|
| 94 |
def test_multidim_memoryview(): |
| 95 |
# See https://github.com/msgpack/msgpack-python/issues/526 |
| 96 |
view = memoryview(b"\00" * 6) |
| 97 |
data = view.cast(view.format, (3, 2)) |
| 98 |
packed = packb(data) |
| 99 |
assert packed == b"\xc4\x06\x00\x00\x00\x00\x00\x00" |
| 100 |
|
| 101 |
|
| 102 |
def test_unpack_noncontiguous_memoryview(): |
| 103 |
# Use a multi-byte value so the padded stride-2 view is non-contiguous. |
| 104 |
packed = packb(2**32) |
| 105 |
padded = bytearray() |
| 106 |
for byte in packed: |
| 107 |
padded.append(byte) |
| 108 |
padded.append(0) |
| 109 |
noncont = memoryview(bytes(padded))[::2] |
| 110 |
assert not noncont.c_contiguous |
| 111 |
assert unpackb(noncont) == 2**32 |