| 1 |
import py |
| 2 |
from rpython.jit.backend.llsupport.symbolic import * |
| 3 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 4 |
from rpython.jit.backend.x86.arch import WORD |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
class FakeStats(object): |
| 10 |
pass |
| 11 |
|
| 12 |
S = lltype.GcStruct('S', ('x', lltype.Signed), |
| 13 |
('y', lltype.Signed), |
| 14 |
('z', lltype.Signed)) |
| 15 |
|
| 16 |
|
| 17 |
def test_field_token(): |
| 18 |
ofs_x, size_x = get_field_token(S, 'x', False) |
| 19 |
ofs_y, size_y = get_field_token(S, 'y', False) |
| 20 |
ofs_z, size_z = get_field_token(S, 'z', False) |
| 21 |
|
| 22 |
|
| 23 |
assert size_x == size_y == size_z == WORD |
| 24 |
assert ofs_x >= 0 |
| 25 |
assert ofs_y == ofs_x + WORD |
| 26 |
assert ofs_z == ofs_x + (WORD*2) |
| 27 |
|
| 28 |
def test_struct_size(): |
| 29 |
ofs_z, size_z = get_field_token(S, 'z', False) |
| 30 |
totalsize = get_size(S, False) |
| 31 |
assert totalsize == ofs_z + WORD |
| 32 |
|
| 33 |
def test_primitive_size(): |
| 34 |
assert get_size(lltype.Signed, False) == WORD |
| 35 |
assert get_size(lltype.Char, False) == 1 |
| 36 |
assert get_size(lltype.Ptr(S), False) == WORD |
| 37 |
|
| 38 |
def test_array_token(): |
| 39 |
A = lltype.GcArray(lltype.Char) |
| 40 |
basesize, itemsize, ofs_length = get_array_token(A, False) |
| 41 |
assert basesize >= WORD |
| 42 |
assert itemsize == 1 |
| 43 |
assert ofs_length == basesize - WORD |
| 44 |
A = lltype.GcArray(lltype.Signed) |
| 45 |
basesize, itemsize, ofs_length = get_array_token(A, False) |
| 46 |
assert basesize >= WORD |
| 47 |
assert itemsize == WORD |
| 48 |
assert ofs_length == basesize - WORD |
| 49 |
|
| 50 |
def test_varsized_struct_size(): |
| 51 |
S1 = lltype.GcStruct('S1', ('parent', S), |
| 52 |
('extra', lltype.Signed), |
| 53 |
('chars', lltype.Array(lltype.Char))) |
| 54 |
size_parent = get_size(S, False) |
| 55 |
ofs_extra, size_extra = get_field_token(S1, 'extra', False) |
| 56 |
basesize, itemsize, ofs_length = get_array_token(S1, False) |
| 57 |
assert size_parent == ofs_extra |
| 58 |
assert size_extra == WORD |
| 59 |
assert ofs_length == ofs_extra + WORD |
| 60 |
assert basesize == ofs_length + WORD |
| 61 |
assert itemsize == 1 |
| 62 |
|
| 63 |
def test_string(): |
| 64 |
STR = lltype.GcStruct('String', ('hash', lltype.Signed), |
| 65 |
('chars', lltype.Array(lltype.Char))) |
| 66 |
basesize, itemsize, ofs_length = get_array_token(STR, False) |
| 67 |
assert itemsize == 1 |
| 68 |
s1 = lltype.malloc(STR, 4) |
| 69 |
s1.chars[0] = 's' |
| 70 |
s1.chars[1] = 'p' |
| 71 |
s1.chars[2] = 'a' |
| 72 |
s1.chars[3] = 'm' |
| 73 |
x = ll2ctypes.lltype2ctypes(s1) |
| 74 |
rawbytes = ctypes.cast(x, ctypes.POINTER(ctypes.c_char)) |
| 75 |
assert rawbytes[basesize+0] == 's' |
| 76 |
assert rawbytes[basesize+1] == 'p' |
| 77 |
assert rawbytes[basesize+2] == 'a' |
| 78 |
assert rawbytes[basesize+3] == 'm' |
| 79 |
assert rawbytes[ofs_length+0] == chr(4) |
| 80 |
assert rawbytes[ofs_length+1] == chr(0) |
| 81 |
assert rawbytes[ofs_length+2] == chr(0) |
| 82 |
assert rawbytes[ofs_length+3] == chr(0) |