~ubuntu-branches/ubuntu/karmic/pypy/karmic

« back to all changes in this revision

Viewing changes to pypy/rlib/rctypes/test/test_rctypesobject.py

  • Committer: Bazaar Package Importer
  • Author(s): Alexandre Fayolle
  • Date: 2007-04-13 09:33:09 UTC
  • Revision ID: james.westby@ubuntu.com-20070413093309-yoojh4jcoocu2krz
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import py
 
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
 
7
 
 
8
 
 
9
class TestBasic:
 
10
 
 
11
    def do(self, func):
 
12
        return func()
 
13
 
 
14
    def test_primitive(self):
 
15
        def func():
 
16
            x = rc_int.allocate()
 
17
            assert x.get_value() == 0
 
18
            x.set_value(17)
 
19
            return x.get_value()
 
20
        res = self.do(func)
 
21
        assert res == 17
 
22
 
 
23
    def test_ptr(self):
 
24
        def func():
 
25
            x = rc_int.allocate()
 
26
            p1 = pointer(x)
 
27
            p2 = pointer(x)
 
28
            x.set_value(17)
 
29
            assert p1.get_contents().get_value() == 17
 
30
            p2.get_contents().set_value(18)
 
31
            assert x.get_value() == 18
 
32
            del x
 
33
            return p1.get_contents().get_value()
 
34
        res = self.do(func)
 
35
        assert res == 18
 
36
 
 
37
    def test_struct(self):
 
38
        S1 = RStruct('S1', [('x', rc_int),
 
39
                            ('y', RPointer(rc_int))])
 
40
        def func():
 
41
            x = rc_int.allocate()
 
42
            x.set_value(42)
 
43
            s = S1.allocate()
 
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()
 
48
        res = self.do(func)
 
49
        assert res == 42
 
50
 
 
51
    def test_copyfrom(self):
 
52
        def func():
 
53
            x1 = rc_int.allocate()
 
54
            x1.set_value(101)
 
55
            p1 = pointer(x1)
 
56
            x2 = rc_int.allocate()
 
57
            x2.set_value(202)
 
58
            p2 = pointer(x2)
 
59
            del x1, x2
 
60
            p1.copyfrom(p2)
 
61
            assert p1.get_contents().sameaddr(p2.get_contents())
 
62
            p1.get_contents().set_value(303)
 
63
            assert p2.get_contents().get_value() == 303
 
64
            del p2
 
65
            return p1.get_contents().get_value()
 
66
        res = self.do(func)
 
67
        assert res == 303
 
68
 
 
69
    def test_copyfrom_2(self):
 
70
        def func():
 
71
            x1 = rc_int.allocate()
 
72
            x1.set_value(11)
 
73
            x2 = rc_int.allocate()
 
74
            x2.set_value(7)
 
75
            p1 = pointer(x1)
 
76
            p1.get_contents().copyfrom(x2)
 
77
            return x1.get_value()
 
78
        res = self.do(func)
 
79
        assert res == 7
 
80
 
 
81
    def test_fixedarray(self):
 
82
        def func():
 
83
            a = RFixedArray(rc_int, 10).allocate()
 
84
            for i in range(10):
 
85
                a.ref(i).set_value(5 * i)
 
86
            for i in range(10):
 
87
                assert a.ref(i).get_value() == 5 * i
 
88
            return a.length
 
89
        res = self.do(func)
 
90
        assert res == 10
 
91
 
 
92
    def test_vararray(self):
 
93
        def func():
 
94
            a = RVarArray(rc_int).allocate(10)
 
95
            for i in range(10):
 
96
                a.ref(i).set_value(5 * i)
 
97
            for i in range(10):
 
98
                assert a.ref(i).get_value() == 5 * i
 
99
            return a.length
 
100
        res = self.do(func)
 
101
        assert res == 10
 
102
 
 
103
    def test_vararray_cast(self):
 
104
        def func():
 
105
            a = RVarArray(rc_int).allocate(10)
 
106
            for i in range(10):
 
107
                a.ref(i).set_value(100 + 5 * i)
 
108
            p = pointer(a.ref(0))
 
109
            del a
 
110
            assert p.get_contents().get_value() == 100
 
111
            a1 = RVarArray(rc_int).fromitem(p.get_contents(), 8)
 
112
            del p
 
113
            for i in range(8):
 
114
                assert a1.ref(i).get_value() == 100 + 5 * i
 
115
            return a1.length
 
116
        res = self.do(func)
 
117
        assert res == 8
 
118
 
 
119
    def test_varstructarray_cast(self):
 
120
        S1 = RStruct('S1', [('x', rc_int),
 
121
                            ('y', rc_int)])
 
122
        def func():
 
123
            a = RVarArray(S1).allocate(10)
 
124
            for i in range(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))
 
128
            del a
 
129
            a1 = RVarArray(S1).fromitem(p.get_contents(), 8)
 
130
            del p
 
131
            return a1.ref(4).ref_y().get_value()
 
132
        res = self.do(func)
 
133
        assert res == 208
 
134
 
 
135
    def test_char_p(self):
 
136
        def func():
 
137
            p = rc_char_p.allocate()
 
138
            s = ''
 
139
            for i in range(65, 91):
 
140
                s += chr(i)
 
141
            p.set_value(s)
 
142
            del s
 
143
            s = p.get_value()
 
144
            for i in range(26):
 
145
                assert ord(s[i]) == 65 + i
 
146
            return len(s)
 
147
        res = self.do(func)
 
148
        assert res == 26
 
149
 
 
150
    def test_char_p_in_struct(self):
 
151
        S2 = RStruct('S2', [('p', rc_char_p)])
 
152
        def func():
 
153
            s = S2.allocate()
 
154
            for test in ["abc", "hello world"]:
 
155
                s.ref_p().set_value(test)
 
156
            assert s.ref_p().get_value() == "hello world"
 
157
            return 1
 
158
        res = self.do(func)
 
159
        assert res == 1
 
160
 
 
161
    def test_char_p_None(self):
 
162
        def func():
 
163
            p = rc_char_p.allocate()
 
164
            assert p.get_value() is None
 
165
            p.set_value("")
 
166
            assert p.get_value() == ""
 
167
            p.set_value("abc")
 
168
            assert p.get_value() == "abc"
 
169
            p.set_value(None)
 
170
            assert p.get_value() is None
 
171
        self.do(func)
 
172
 
 
173
    def test_char_array(self):
 
174
        def func():
 
175
            a = RFixedArray(rc_char, 10).allocate()
 
176
            for i in range(6):
 
177
                a.ref(i).set_value("hello!"[i])
 
178
            assert a.get_value() == "hello!"
 
179
            a.set_value("foo")
 
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"
 
188
            return 1
 
189
        res = self.do(func)
 
190
        assert res == 1
 
191
 
 
192
    def test_string_buffer(self):
 
193
        def func():
 
194
            a = create_string_buffer(10)
 
195
            for i in range(6):
 
196
                a.ref(i).set_value("hello!"[i])
 
197
            assert a.get_value() == "hello!"
 
198
            a.set_value("foo")
 
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"
 
207
            return 1
 
208
        res = self.do(func)
 
209
        assert res == 1
 
210
 
 
211
    def test_func(self):
 
212
        def g(x, y):
 
213
            return x - y
 
214
        def func():
 
215
            a = RFuncType((rc_int, rc_int), rc_int).fromrpython(g)
 
216
            return a.call(50, 8)
 
217
        res = self.do(func)
 
218
        assert res == 42
 
219
 
 
220
    def test_labs(self):
 
221
        def ll_labs(n):
 
222
            return abs(n)
 
223
        labs = RFuncType((rc_int,), rc_int).fromlib(LIBC, 'labs', ll_labs)
 
224
        def func():
 
225
            return labs.call(-7)
 
226
        res = self.do(func)
 
227
        assert res == 7
 
228
 
 
229
    def test_pointer_indexing(self):
 
230
        def func():
 
231
            a = RFixedArray(rc_int, 10).allocate()
 
232
            for i in range(10):
 
233
                a.ref(i).set_value(100 + 5 * i)
 
234
            p = pointer(a.ref(0))
 
235
            del a
 
236
            return p.ref(7).get_value()
 
237
        res = self.do(func)
 
238
        assert res == 135
 
239
 
 
240
    def test_structpointer_indexing(self):
 
241
        S1 = RStruct('S1', [('x', rc_int),
 
242
                            ('y', rc_int)])
 
243
        def func():
 
244
            a = RFixedArray(S1, 10).allocate()
 
245
            for i in range(10):
 
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))
 
249
            del a
 
250
            s1 = p.ref(3)
 
251
            return s1.ref_x().get_value() + s1.ref_y().get_value()
 
252
        res = self.do(func)
 
253
        assert res == 115 + 206
 
254
 
 
255
    def test_null_pointer(self):
 
256
        P = RPointer(rc_int)
 
257
        def func():
 
258
            x = rc_int.allocate()
 
259
            p = P.allocate()
 
260
            res1 = p.is_null()
 
261
            p.set_contents(x)
 
262
            res2 = p.is_null()
 
263
            p.set_null()
 
264
            res3 = p.is_null()
 
265
            return res1 * 100 + res2 * 10 + res3
 
266
        res = self.do(func)
 
267
        assert res == 101
 
268
 
 
269
    def test_recursive_structure(self):
 
270
        P1 = RPointer(None)
 
271
        S1 = RStruct('S1', [('next', P1)])
 
272
        P1.setpointertype(S1)
 
273
        def func():
 
274
            s1 = S1.allocate()
 
275
            s2 = S1.allocate()
 
276
            s2.ref_next().set_contents(s1)
 
277
            return s2.ref_next().get_contents().sameaddr(s1)
 
278
        res = self.do(func)
 
279
        assert res == True
 
280
 
 
281
POLICY = AnnotatorPolicy()
 
282
POLICY.allow_someobjects = False
 
283
 
 
284
class TestLLInterpreted(TestBasic):
 
285
 
 
286
    def do(self, func):
 
287
        return interpret(func, [], policy=POLICY, backendopt=True)
 
288
 
 
289
    def test_simple_struct(self):
 
290
        S0 = RStruct('S0', [('x', rc_int)])
 
291
        def func():
 
292
            s = S0.allocate()
 
293
            s.ref_x().set_value(12)
 
294
            return s.ref_x().get_value()
 
295
 
 
296
        interp, graph = get_interpreter(func, [], policy=POLICY,
 
297
                                        backendopt=True)
 
298
        res = interp.eval_graph(graph, [])
 
299
        assert res == 12
 
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'
 
306
 
 
307
 
 
308
class TestCompiled(TestBasic):
 
309
 
 
310
    def do(self, func):
 
311
        fn = compile(func, [], annotatorpolicy=POLICY)
 
312
        return fn()