| 1 | import sys |
| 2 | from pypy.module._rawffi.structure import size_alignment_pos |
| 3 | from pypy.module._rawffi.interp_rawffi import TYPEMAP, letter2tp |
| 4 | |
| 5 | sizeof = lambda x : size_alignment_pos(x)[0] |
| 6 | |
| 7 | def unpack(desc): |
| 8 | return [('x', letter2tp('space', i), 0) for i in desc] |
| 9 | |
| 10 | def test_sizeof(): |
| 11 | s_c = sizeof(unpack('c')) |
| 12 | s_l = sizeof(unpack('l')) |
| 13 | s_q = sizeof(unpack('q')) |
| 14 | alignment_of_q = TYPEMAP['q'].c_alignment |
| 15 | assert alignment_of_q >= 4 |
| 16 | assert sizeof(unpack('cl')) == 2*s_l |
| 17 | assert sizeof(unpack('cq')) == alignment_of_q + s_q |
| 18 | assert sizeof(unpack('ccq')) == alignment_of_q + s_q |
| 19 | assert sizeof(unpack('cccq')) == alignment_of_q + s_q |
| 20 | assert sizeof(unpack('ccccq')) == alignment_of_q + s_q |
| 21 | assert sizeof(unpack('qc')) == s_q + alignment_of_q |
| 22 | assert sizeof(unpack('qcc')) == s_q + alignment_of_q |
| 23 | assert sizeof(unpack('qccc')) == s_q + alignment_of_q |
| 24 | assert sizeof(unpack('qcccc')) == s_q + alignment_of_q |
| 25 | |
| 26 | def test_bitsizes(): |
| 27 | fields = [("A", 'i', 1), |
| 28 | ("B", 'i', 2), |
| 29 | ("C", 'i', 3), |
| 30 | ("D", 'i', 4), |
| 31 | ("E", 'i', 5), |
| 32 | ("F", 'i', 6), |
| 33 | ("G", 'i', 7), |
| 34 | ("H", 'i', 8), |
| 35 | ("I", 'i', 9), |
| 36 | |
| 37 | ("M", 'h', 1), |
| 38 | ("N", 'h', 2), |
| 39 | ("O", 'h', 3), |
| 40 | ("P", 'h', 4), |
| 41 | ("Q", 'h', 5), |
| 42 | ("R", 'h', 6), |
| 43 | ("S", 'h', 7)] |
| 44 | size, alignment, pos, bitsizes = size_alignment_pos( |
| 45 | [(name, letter2tp('space', t), size) |
| 46 | for (name, t, size) in fields]) |
| 47 | assert size == 12 |
| 48 | |
| 49 | import ctypes |
| 50 | class X(ctypes.Structure): |
| 51 | _fields_ = [(name, {'i':ctypes.c_int, 'h': ctypes.c_short}[t], size) |
| 52 | for (name, t, size) in fields] |
| 53 | |
| 54 | assert pos == [getattr(X, name).offset for (name, _, _) in fields] |
| 55 | assert bitsizes == [getattr(X, name).size for (name, _, _) in fields] |
| 56 | |
| 57 | def test_bitsizes_longlong(): |
| 58 | fields = [("a", 'q', 1), |
| 59 | ("b", 'q', 62), |
| 60 | ("c", 'q', 1)] |
| 61 | size, alignment, pos, bitsizes = size_alignment_pos( |
| 62 | [(name, letter2tp('space', t), size) |
| 63 | for (name, t, size) in fields]) |
| 64 | assert size == 8 |
| 65 | assert pos == [0, 0, 0] |
| 66 | if sys.byteorder == 'little': |
| 67 | assert bitsizes == [0x10000, 0x3e0001, 0x1003f] |
| 68 | else: |
| 69 | assert bitsizes == [0x1003f, 0x3e0001, 0x10000] |