|
1 | 1 | #!/usr/bin/env python |
2 | 2 | # coding: utf-8 |
3 | 3 |
|
4 | | - |
| 4 | +from array import array |
5 | 5 | from msgpack import packb, unpackb |
| 6 | +import sys |
| 7 | + |
| 8 | + |
| 9 | +# For Python < 3: |
| 10 | +# - array type only supports old buffer interface |
| 11 | +# - array.frombytes is not available, must use deprecated array.fromstring |
| 12 | +if sys.version_info[0] < 3: |
| 13 | + def make_memoryview(obj): |
| 14 | + return memoryview(buffer(obj)) |
| 15 | + |
| 16 | + def make_array(f, data): |
| 17 | + a = array(f) |
| 18 | + a.fromstring(data) |
| 19 | + return a |
| 20 | + |
| 21 | + def get_data(a): |
| 22 | + return a.tostring() |
| 23 | +else: |
| 24 | + make_memoryview = memoryview |
| 25 | + |
| 26 | + def make_array(f, data): |
| 27 | + a = array(f) |
| 28 | + a.frombytes(data) |
| 29 | + return a |
| 30 | + |
| 31 | + def get_data(a): |
| 32 | + return a.tobytes() |
| 33 | + |
| 34 | + |
| 35 | +def _runtest(format, nbytes, expected_header, expected_prefix, use_bin_type): |
| 36 | + # create a new array |
| 37 | + original_array = array(format) |
| 38 | + original_array.fromlist([255] * (nbytes // original_array.itemsize)) |
| 39 | + original_data = get_data(original_array) |
| 40 | + view = make_memoryview(original_array) |
| 41 | + |
| 42 | + # pack, unpack, and reconstruct array |
| 43 | + packed = packb(view, use_bin_type=use_bin_type) |
| 44 | + unpacked = unpackb(packed) |
| 45 | + reconstructed_array = make_array(format, unpacked) |
| 46 | + |
| 47 | + # check that we got the right amount of data |
| 48 | + assert len(original_data) == nbytes |
| 49 | + # check packed header |
| 50 | + assert packed[:1] == expected_header |
| 51 | + # check packed length prefix, if any |
| 52 | + assert packed[1:1+len(expected_prefix)] == expected_prefix |
| 53 | + # check packed data |
| 54 | + assert packed[1+len(expected_prefix):] == original_data |
| 55 | + # check array unpacked correctly |
| 56 | + assert original_array == reconstructed_array |
| 57 | + |
| 58 | + |
| 59 | +def test_fixstr_from_byte(): |
| 60 | + _runtest('B', 1, b'\xa1', b'', False) |
| 61 | + _runtest('B', 31, b'\xbf', b'', False) |
| 62 | + |
| 63 | + |
| 64 | +def test_fixstr_from_float(): |
| 65 | + _runtest('f', 4, b'\xa4', b'', False) |
| 66 | + _runtest('f', 28, b'\xbc', b'', False) |
| 67 | + |
| 68 | + |
| 69 | +def test_str16_from_byte(): |
| 70 | + _runtest('B', 2**8, b'\xda', b'\x01\x00', False) |
| 71 | + _runtest('B', 2**16-1, b'\xda', b'\xff\xff', False) |
| 72 | + |
| 73 | + |
| 74 | +def test_str16_from_float(): |
| 75 | + _runtest('f', 2**8, b'\xda', b'\x01\x00', False) |
| 76 | + _runtest('f', 2**16-4, b'\xda', b'\xff\xfc', False) |
| 77 | + |
| 78 | + |
| 79 | +def test_str32_from_byte(): |
| 80 | + _runtest('B', 2**16, b'\xdb', b'\x00\x01\x00\x00', False) |
| 81 | + |
| 82 | + |
| 83 | +def test_str32_from_float(): |
| 84 | + _runtest('f', 2**16, b'\xdb', b'\x00\x01\x00\x00', False) |
| 85 | + |
| 86 | + |
| 87 | +def test_bin8_from_byte(): |
| 88 | + _runtest('B', 1, b'\xc4', b'\x01', True) |
| 89 | + _runtest('B', 2**8-1, b'\xc4', b'\xff', True) |
| 90 | + |
| 91 | + |
| 92 | +def test_bin8_from_float(): |
| 93 | + _runtest('f', 4, b'\xc4', b'\x04', True) |
| 94 | + _runtest('f', 2**8-4, b'\xc4', b'\xfc', True) |
| 95 | + |
| 96 | + |
| 97 | +def test_bin16_from_byte(): |
| 98 | + _runtest('B', 2**8, b'\xc5', b'\x01\x00', True) |
| 99 | + _runtest('B', 2**16-1, b'\xc5', b'\xff\xff', True) |
| 100 | + |
| 101 | + |
| 102 | +def test_bin16_from_float(): |
| 103 | + _runtest('f', 2**8, b'\xc5', b'\x01\x00', True) |
| 104 | + _runtest('f', 2**16-4, b'\xc5', b'\xff\xfc', True) |
| 105 | + |
| 106 | + |
| 107 | +def test_bin32_from_byte(): |
| 108 | + _runtest('B', 2**16, b'\xc6', b'\x00\x01\x00\x00', True) |
6 | 109 |
|
7 | 110 |
|
8 | | -def test_pack_memoryview(): |
9 | | - data = bytearray(range(256)) |
10 | | - view = memoryview(data) |
11 | | - unpacked = unpackb(packb(view)) |
12 | | - assert data == unpacked |
| 111 | +def test_bin32_from_float(): |
| 112 | + _runtest('f', 2**16, b'\xc6', b'\x00\x01\x00\x00', True) |
0 commit comments