1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
""" Few tests for annlowlevel helpers
"""
from rpython.rtyper.test.tool import BaseRtypingTest
from rpython.rtyper.lltypesystem.rstr import mallocstr, mallocunicode
from rpython.rtyper.lltypesystem import lltype, llmemory
from rpython.rtyper.annlowlevel import hlstr, llstr
from rpython.rtyper.annlowlevel import hlunicode, llunicode
from rpython.rtyper import annlowlevel
from rpython.rtyper.rclass import OBJECTPTR
class TestLLType(BaseRtypingTest):
def test_hlstr(self):
s = mallocstr(3)
s.chars[0] = "a"
s.chars[1] = "b"
s.chars[2] = "c"
assert hlstr(s) == "abc"
def test_llstr(self):
s = llstr("abc")
assert len(s.chars) == 3
assert s.chars[0] == "a"
assert s.chars[1] == "b"
assert s.chars[2] == "c"
def test_llstr_compile(self):
def f(arg):
s = llstr(hlstr(arg))
return len(s.chars)
res = self.interpret(f, [self.string_to_ll("abc")])
assert res == 3
def test_llstr_const_char(self):
def f(arg):
s = llstr(hlstr(arg)[0])
return len(s.chars)
res = self.interpret(f, [self.string_to_ll("abc")])
assert res == 1
def test_hlunicode(self):
s = mallocunicode(3)
s.chars[0] = u"a"
s.chars[1] = u"b"
s.chars[2] = u"c"
assert hlunicode(s) == u"abc"
def test_llunicode(self):
s = llunicode(u"abc")
assert len(s.chars) == 3
assert s.chars[0] == u"a"
assert s.chars[1] == u"b"
assert s.chars[2] == u"c"
def test_llunicode_compile(self):
def f(arg):
s = llunicode(hlunicode(arg))
return len(s.chars)
res = self.interpret(f, [self.unicode_to_ll(u"abc")])
assert res == 3
def test_cast_instance_to_base_ptr(self):
class X(object):
pass
x = X()
ptr = annlowlevel.cast_instance_to_base_ptr(x)
assert lltype.typeOf(ptr) == OBJECTPTR
y = annlowlevel.cast_base_ptr_to_instance(X, ptr)
assert y is x
def test_cast_instance_to_gcref(self):
class X(object):
pass
x = X()
ptr = annlowlevel.cast_instance_to_gcref(x)
assert lltype.typeOf(ptr) == llmemory.GCREF
y = annlowlevel.cast_gcref_to_instance(X, ptr)
assert y is x
def test_delayedptr(self):
FUNCTYPE = lltype.FuncType([], lltype.Signed)
name = "delayed!myfunc"
delayedptr1 = lltype._ptr(lltype.Ptr(FUNCTYPE), name, solid=True)
delayedptr2 = lltype._ptr(lltype.Ptr(FUNCTYPE), name, solid=True)
assert delayedptr1 == delayedptr1
assert delayedptr1 != delayedptr2
assert bool(delayedptr1)
assert delayedptr1 != lltype.nullptr(FUNCTYPE)
|