Debian Codegraph @

rpython/jit/codewriter/test/test_support.py

raw symbols SCIP
1 import py, sys
2 from rpython.rtyper.lltypesystem import lltype
3 from rpython.rtyper.annlowlevel import llstr
4 from rpython.flowspace.model import Variable, Constant, SpaceOperation
5 from rpython.jit.codewriter.support import decode_builtin_call, LLtypeHelpers
6
7 def newconst(x):
8 return Constant(x, lltype.typeOf(x))
9
10 def voidconst(x):
11 return Constant(x, lltype.Void)
12
13 # ____________________________________________________________
14
15 def test_decode_builtin_call_nomethod():
16 def myfoobar(i, marker, c):
17 assert marker == 'mymarker'
18 return i * ord(c)
19 myfoobar.oopspec = 'foobar(2, c, i)'
20 TYPE = lltype.FuncType([lltype.Signed, lltype.Void, lltype.Char],
21 lltype.Signed)
22 fnobj = lltype.functionptr(TYPE, 'foobar', _callable=myfoobar)
23 vi = Variable('i')
24 vi.concretetype = lltype.Signed
25 vc = Variable('c')
26 vc.concretetype = lltype.Char
27 v_result = Variable('result')
28 v_result.concretetype = lltype.Signed
29 op = SpaceOperation('direct_call', [newconst(fnobj),
30 vi,
31 voidconst('mymarker'),
32 vc],
33 v_result)
34 oopspec, opargs = decode_builtin_call(op)
35 assert oopspec == 'foobar'
36 assert opargs == [newconst(2), vc, vi]
37 #impl = runner.get_oopspec_impl('foobar', lltype.Signed)
38 #assert impl(2, 'A', 5) == 5 * ord('A')
39
40 def test_decode_builtin_call_method():
41 A = lltype.GcArray(lltype.Signed)
42 def myfoobar(a, i, marker, c):
43 assert marker == 'mymarker'
44 return a[i] * ord(c)
45 myfoobar.oopspec = 'spam.foobar(a, 2, c, i)'
46 TYPE = lltype.FuncType([lltype.Ptr(A), lltype.Signed,
47 lltype.Void, lltype.Char],
48 lltype.Signed)
49 fnobj = lltype.functionptr(TYPE, 'foobar', _callable=myfoobar)
50 vi = Variable('i')
51 vi.concretetype = lltype.Signed
52 vc = Variable('c')
53 vc.concretetype = lltype.Char
54 v_result = Variable('result')
55 v_result.concretetype = lltype.Signed
56 myarray = lltype.malloc(A, 10)
57 myarray[5] = 42
58 op = SpaceOperation('direct_call', [newconst(fnobj),
59 newconst(myarray),
60 vi,
61 voidconst('mymarker'),
62 vc],
63 v_result)
64 oopspec, opargs = decode_builtin_call(op)
65 assert oopspec == 'spam.foobar'
66 assert opargs == [newconst(myarray), newconst(2), vc, vi]
67 #impl = runner.get_oopspec_impl('spam.foobar', lltype.Ptr(A))
68 #assert impl(myarray, 2, 'A', 5) == 42 * ord('A')
69
70 def test_streq_slice_checknull():
71 p1 = llstr("hello world")
72 p2 = llstr("wor")
73 func = LLtypeHelpers._ll_4_str_eq_slice_checknull.im_func
74 assert func(p1, 6, 3, p2) == True
75 assert func(p1, 6, 2, p2) == False
76 assert func(p1, 5, 3, p2) == False
77 assert func(p1, 2, 1, llstr(None)) == False
78
79 def test_streq_slice_nonnull():
80 p1 = llstr("hello world")
81 p2 = llstr("wor")
82 func = LLtypeHelpers._ll_4_str_eq_slice_nonnull.im_func
83 assert func(p1, 6, 3, p2) == True
84 assert func(p1, 6, 2, p2) == False
85 assert func(p1, 5, 3, p2) == False
86 py.test.raises(AttributeError, func, p1, 2, 1, llstr(None))
87
88 def test_streq_slice_char():
89 p1 = llstr("hello world")
90 func = LLtypeHelpers._ll_4_str_eq_slice_char.im_func
91 assert func(p1, 6, 3, "w") == False
92 assert func(p1, 6, 0, "w") == False
93 assert func(p1, 6, 1, "w") == True
94 assert func(p1, 6, 1, "x") == False
95
96 def test_streq_nonnull():
97 p1 = llstr("wor")
98 p2 = llstr("wor")
99 assert p1 != p2
100 func = LLtypeHelpers._ll_2_str_eq_nonnull.im_func
101 assert func(p1, p1) == True
102 assert func(p1, p2) == True
103 assert func(p1, llstr("wrl")) == False
104 assert func(p1, llstr("world")) == False
105 assert func(p1, llstr("w")) == False
106 py.test.raises(AttributeError, func, p1, llstr(None))
107 py.test.raises(AttributeError, func, llstr(None), p2)
108
109 def test_streq_nonnull_char():
110 func = LLtypeHelpers._ll_2_str_eq_nonnull_char.im_func
111 assert func(llstr("wor"), "x") == False
112 assert func(llstr("w"), "x") == False
113 assert func(llstr(""), "x") == False
114 assert func(llstr("x"), "x") == True
115 py.test.raises(AttributeError, func, llstr(None), "x")
116
117 def test_streq_checknull_char():
118 func = LLtypeHelpers._ll_2_str_eq_checknull_char.im_func
119 assert func(llstr("wor"), "x") == False
120 assert func(llstr("w"), "x") == False
121 assert func(llstr(""), "x") == False
122 assert func(llstr("x"), "x") == True
123 assert func(llstr(None), "x") == False
124
125 def test_streq_lengthok():
126 p1 = llstr("wor")
127 p2 = llstr("wor")
128 assert p1 != p2
129 func = LLtypeHelpers._ll_2_str_eq_lengthok.im_func
130 assert func(p1, p1) == True
131 assert func(p1, p2) == True
132 assert func(p1, llstr("wrl")) == False
133 py.test.raises(IndexError, func, p1, llstr("w"))
134 py.test.raises(AttributeError, func, p1, llstr(None))
135 py.test.raises(AttributeError, func, llstr(None), p2)
136
137 def test_int_abs():
138 from rpython.jit.codewriter.support import _ll_1_int_abs
139 assert _ll_1_int_abs(0) == 0
140 assert _ll_1_int_abs(1) == 1
141 assert _ll_1_int_abs(10) == 10
142 assert _ll_1_int_abs(sys.maxint) == sys.maxint
143 assert _ll_1_int_abs(-1) == 1
144 assert _ll_1_int_abs(-10) == 10
145 assert _ll_1_int_abs(-sys.maxint) == sys.maxint
146
147 def test_int_floordiv_mod():
148 from rpython.rtyper.lltypesystem.lloperation import llop
149 from rpython.jit.codewriter.support import _ll_2_int_floordiv, _ll_2_int_mod
150 for x in range(-6, 7):
151 for y in range(-3, 4):
152 if y != 0:
153 assert (_ll_2_int_floordiv(x, y) ==
154 llop.int_floordiv(lltype.Signed, x, y))
155 assert (_ll_2_int_mod(x, y) ==
156 llop.int_mod(lltype.Signed, x, y))