Debian Codegraph @

rpython/rtyper/test/test_annlowlevel.py

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