| 1 |
import struct |
| 2 |
from rpython.rtyper.test.tool import BaseRtypingTest |
| 3 |
from rpython.rtyper.lltypesystem import lltype, llmemory, rffi |
| 4 |
from rpython.rtyper.lltypesystem.lloperation import llop |
| 5 |
from rpython.rtyper.lltypesystem.rstr import STR |
| 6 |
from rpython.rtyper.annlowlevel import llstr |
| 7 |
from rpython.rlib.rarithmetic import r_singlefloat |
| 8 |
from rpython.rlib.rgc import (resizable_list_supporting_raw_ptr, |
| 9 |
ll_for_resizable_list) |
| 10 |
|
| 11 |
|
| 12 |
def str_gc_load(TYPE, buf, offset): |
| 13 |
base_ofs = (llmemory.offsetof(STR, 'chars') + |
| 14 |
llmemory.itemoffsetof(STR.chars, 0)) |
| 15 |
scale_factor = llmemory.sizeof(lltype.Char) |
| 16 |
lls = llstr(buf) |
| 17 |
return llop.gc_load_indexed(TYPE, lls, offset, |
| 18 |
scale_factor, base_ofs) |
| 19 |
|
| 20 |
|
| 21 |
def newlist_and_gc_store(TYPE, value): |
| 22 |
size = rffi.sizeof(TYPE) |
| 23 |
lst = resizable_list_supporting_raw_ptr(['\x00']*size) |
| 24 |
ll_data = ll_for_resizable_list(lst) |
| 25 |
ll_items = ll_data.items |
| 26 |
LIST = lltype.typeOf(ll_data).TO |
| 27 |
base_ofs = llmemory.itemoffsetof(LIST.items.TO, 0) |
| 28 |
scale_factor = llmemory.sizeof(lltype.Char) |
| 29 |
value = lltype.cast_primitive(TYPE, value) |
| 30 |
llop.gc_store_indexed(lltype.Void, ll_items, 0, |
| 31 |
value, scale_factor, base_ofs) |
| 32 |
return lst |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
class BaseLLOpTest(object): |
| 37 |
|
| 38 |
def test_gc_load_indexed(self): |
| 39 |
buf = struct.pack('dfi', 123.456, 123.456, 0x12345678) |
| 40 |
val = self.gc_load_from_string(rffi.DOUBLE, buf, 0) |
| 41 |
assert val == 123.456 |
| 42 |
|
| 43 |
val = self.gc_load_from_string(rffi.FLOAT, buf, 8) |
| 44 |
assert val == r_singlefloat(123.456) |
| 45 |
|
| 46 |
val = self.gc_load_from_string(rffi.INT, buf, 12) |
| 47 |
assert val == 0x12345678 |
| 48 |
|
| 49 |
def test_gc_store_indexed_int(self): |
| 50 |
expected = struct.pack('i', 0x12345678) |
| 51 |
self.newlist_and_gc_store(rffi.INT, 0x12345678, expected) |
| 52 |
|
| 53 |
def test_gc_store_indexed_double(self): |
| 54 |
expected = struct.pack('d', 123.456) |
| 55 |
self.newlist_and_gc_store(rffi.DOUBLE, 123.456, expected) |
| 56 |
|
| 57 |
def test_gc_store_indexed_float(self): |
| 58 |
expected = struct.pack('f', 123.456) |
| 59 |
self.newlist_and_gc_store(rffi.FLOAT, 123.456, expected) |
| 60 |
|
| 61 |
|
| 62 |
class TestDirect(BaseLLOpTest): |
| 63 |
|
| 64 |
def gc_load_from_string(self, TYPE, buf, offset): |
| 65 |
return str_gc_load(TYPE, buf, offset) |
| 66 |
|
| 67 |
def newlist_and_gc_store(self, TYPE, value, expected): |
| 68 |
got = newlist_and_gc_store(TYPE, value) |
| 69 |
got = ''.join(got) |
| 70 |
assert got == expected |
| 71 |
|
| 72 |
class TestRTyping(BaseLLOpTest, BaseRtypingTest): |
| 73 |
|
| 74 |
def gc_load_from_string(self, TYPE, buf, offset): |
| 75 |
def fn(offset): |
| 76 |
return str_gc_load(TYPE, buf, offset) |
| 77 |
return self.interpret(fn, [offset]) |
| 78 |
|
| 79 |
def newlist_and_gc_store(self, TYPE, value, expected): |
| 80 |
def fn(value): |
| 81 |
return newlist_and_gc_store(TYPE, value) |
| 82 |
ll_res = self.interpret(fn, [value]) |
| 83 |
got = ''.join(ll_res.items) |
| 84 |
assert got == expected |