2
from pypy.rlib.rctypes.rctypesobject import *
3
from pypy.rpython.test.test_llinterp import interpret, get_interpreter
4
from pypy.translator.c.test.test_genc import compile
5
from pypy.annotation.policy import AnnotatorPolicy
6
from pypy.objspace.flow.model import mkentrymap
14
def test_primitive(self):
17
assert x.get_value() == 0
29
assert p1.get_contents().get_value() == 17
30
p2.get_contents().set_value(18)
31
assert x.get_value() == 18
33
return p1.get_contents().get_value()
37
def test_struct(self):
38
S1 = RStruct('S1', [('x', rc_int),
39
('y', RPointer(rc_int))])
44
s.ref_x().set_value(12)
45
s.ref_y().set_contents(x)
46
assert s.ref_x().get_value() == 12
47
return s.ref_y().get_contents().get_value()
51
def test_copyfrom(self):
53
x1 = rc_int.allocate()
56
x2 = rc_int.allocate()
61
assert p1.get_contents().sameaddr(p2.get_contents())
62
p1.get_contents().set_value(303)
63
assert p2.get_contents().get_value() == 303
65
return p1.get_contents().get_value()
69
def test_copyfrom_2(self):
71
x1 = rc_int.allocate()
73
x2 = rc_int.allocate()
76
p1.get_contents().copyfrom(x2)
81
def test_fixedarray(self):
83
a = RFixedArray(rc_int, 10).allocate()
85
a.ref(i).set_value(5 * i)
87
assert a.ref(i).get_value() == 5 * i
92
def test_vararray(self):
94
a = RVarArray(rc_int).allocate(10)
96
a.ref(i).set_value(5 * i)
98
assert a.ref(i).get_value() == 5 * i
103
def test_vararray_cast(self):
105
a = RVarArray(rc_int).allocate(10)
107
a.ref(i).set_value(100 + 5 * i)
108
p = pointer(a.ref(0))
110
assert p.get_contents().get_value() == 100
111
a1 = RVarArray(rc_int).fromitem(p.get_contents(), 8)
114
assert a1.ref(i).get_value() == 100 + 5 * i
119
def test_varstructarray_cast(self):
120
S1 = RStruct('S1', [('x', rc_int),
123
a = RVarArray(S1).allocate(10)
125
a.ref(i).ref_x().set_value(100 + 5 * i)
126
a.ref(i).ref_y().set_value(200 + 2 * i)
127
p = pointer(a.ref(0))
129
a1 = RVarArray(S1).fromitem(p.get_contents(), 8)
131
return a1.ref(4).ref_y().get_value()
135
def test_char_p(self):
137
p = rc_char_p.allocate()
139
for i in range(65, 91):
145
assert ord(s[i]) == 65 + i
150
def test_char_p_in_struct(self):
151
S2 = RStruct('S2', [('p', rc_char_p)])
154
for test in ["abc", "hello world"]:
155
s.ref_p().set_value(test)
156
assert s.ref_p().get_value() == "hello world"
161
def test_char_p_None(self):
163
p = rc_char_p.allocate()
164
assert p.get_value() is None
166
assert p.get_value() == ""
168
assert p.get_value() == "abc"
170
assert p.get_value() is None
173
def test_char_array(self):
175
a = RFixedArray(rc_char, 10).allocate()
177
a.ref(i).set_value("hello!"[i])
178
assert a.get_value() == "hello!"
180
assert a.get_value() == "foo"
181
raw = ''.join([a.ref(i).get_value() for i in range(10)])
182
assert raw == "foo\x00o!\x00\x00\x00\x00"
183
assert raw == a.get_raw()
184
a.set_value("0123456789")
185
assert a.get_raw() == "0123456789"
186
assert a.get_value() == "0123456789"
187
assert a.get_substring(2, 5) == "23456"
192
def test_string_buffer(self):
194
a = create_string_buffer(10)
196
a.ref(i).set_value("hello!"[i])
197
assert a.get_value() == "hello!"
199
assert a.get_value() == "foo"
200
raw = ''.join([a.ref(i).get_value() for i in range(10)])
201
assert raw == "foo\x00o!\x00\x00\x00\x00"
202
assert raw == a.get_raw()
203
a.set_value("0123456789")
204
assert a.get_raw() == "0123456789"
205
assert a.get_value() == "0123456789"
206
assert a.get_substring(2, 5) == "23456"
215
a = RFuncType((rc_int, rc_int), rc_int).fromrpython(g)
223
labs = RFuncType((rc_int,), rc_int).fromlib(LIBC, 'labs', ll_labs)
229
def test_pointer_indexing(self):
231
a = RFixedArray(rc_int, 10).allocate()
233
a.ref(i).set_value(100 + 5 * i)
234
p = pointer(a.ref(0))
236
return p.ref(7).get_value()
240
def test_structpointer_indexing(self):
241
S1 = RStruct('S1', [('x', rc_int),
244
a = RFixedArray(S1, 10).allocate()
246
a.ref(i).ref_x().set_value(100 + 5 * i)
247
a.ref(i).ref_y().set_value(200 + 2 * i)
248
p = pointer(a.ref(0))
251
return s1.ref_x().get_value() + s1.ref_y().get_value()
253
assert res == 115 + 206
255
def test_null_pointer(self):
258
x = rc_int.allocate()
265
return res1 * 100 + res2 * 10 + res3
269
def test_recursive_structure(self):
271
S1 = RStruct('S1', [('next', P1)])
272
P1.setpointertype(S1)
276
s2.ref_next().set_contents(s1)
277
return s2.ref_next().get_contents().sameaddr(s1)
281
POLICY = AnnotatorPolicy()
282
POLICY.allow_someobjects = False
284
class TestLLInterpreted(TestBasic):
287
return interpret(func, [], policy=POLICY, backendopt=True)
289
def test_simple_struct(self):
290
S0 = RStruct('S0', [('x', rc_int)])
293
s.ref_x().set_value(12)
294
return s.ref_x().get_value()
296
interp, graph = get_interpreter(func, [], policy=POLICY,
298
res = interp.eval_graph(graph, [])
300
# after inlining the get_value() call, there is a getarrayitem
301
# at the end of the main graph. However, the memory it accesses
302
# must be protected by a following keepalive...
303
entrymap = mkentrymap(graph)
304
[link] = entrymap[graph.returnblock]
305
assert link.prevblock.operations[-1].opname == 'keepalive'
308
class TestCompiled(TestBasic):
311
fn = compile(func, [], annotatorpolicy=POLICY)