File: test_tlc.py

package info (click to toggle)
pypy 5.6.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 97,040 kB
  • ctags: 185,069
  • sloc: python: 1,147,862; ansic: 49,642; cpp: 5,245; asm: 5,169; makefile: 529; sh: 481; xml: 232; lisp: 45
file content (311 lines) | stat: -rw-r--r-- 7,975 bytes parent folder | download | duplicates (9)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import py
from rpython.jit.tl.tlopcode import compile, NEW, RETURN
from rpython.jit.tl.test import test_tl
from rpython.jit.tl.tlc import ConstantPool
    
def test_constant_pool():
    pool = ConstantPool()
    bytecode = compile("""
        NEW foo,bar,meth=f
      f:
        RETURN
    """, pool)
    expected = test_tl.list2bytecode([NEW, 0, RETURN])
    assert expected == bytecode
    assert len(pool.classdescrs) == 1
    descr = pool.classdescrs[0]
    assert descr.attributes == ['foo', 'bar']
    assert descr.methods == [('meth', 2)]

def test_serialization():
    from rpython.jit.tl.tlopcode import serialize_program, decode_program
    pool = ConstantPool()
    bytecode = compile("""
        NEW foo,bar,meth=f
        SETATTR foobar
      f:
        RETURN
    """, pool)
    s = serialize_program(bytecode, pool)
    bytecode2, pool2 = decode_program(s)
    assert bytecode == bytecode2
    assert pool == pool2

class TestTLC(object):
    @staticmethod
    def interp(code='', pc=0, inputarg=0):
        from rpython.jit.tl.tlc import interp
        return interp(code, pc, inputarg)

    def test_unconditional_branch(self):
        bytecode = compile("""
    main:
        BR target
        PUSH 123
        RETURN
    target:
        PUSH 42
        RETURN
    """)
        res = self.interp(bytecode, 0, 0)
        assert res == 42

    def test_basic_cons_cell(self):
        bytecode = compile("""
            NIL
            PUSHARG
            CONS
            PUSH 1
            CONS
            CDR
            CAR
        """)

        res = self.interp(bytecode, 0, 42)
        assert res == 42

    def test_nth(self):
        bytecode = compile("""
            NIL
            PUSH 4
            CONS
            PUSH 2
            CONS
            PUSH 1
            CONS
            PUSHARG
            DIV
        """)

        res = self.interp(bytecode, 0, 0)
        assert res == 1
        res = self.interp(bytecode, 0, 1)
        assert res == 2
        res = self.interp(bytecode, 0, 2)
        assert res == 4

        py.test.raises(IndexError, self.interp, bytecode, 0, 3)
            
    def test_concat(self):
        bytecode = compile("""
            NIL
            PUSH 4
            CONS
            PUSH 2
            CONS
            NIL
            PUSH 5
            CONS
            PUSH 3
            CONS
            PUSH 1
            CONS
            ADD
            PUSHARG
            DIV
        """)

        for i, n in enumerate([2, 4, 1, 3, 5]):
            res = self.interp(bytecode, 0, i)
            assert res == n

    def test_concat_errors(self):
        bytecode = compile("""
            NIL
            PUSH 4
            ADD
        """)
        py.test.raises(TypeError, self.interp, bytecode, 0, 0)

        bytecode = compile("""
            PUSH 4
            NIL
            ADD
        """)
        py.test.raises(TypeError, self.interp, bytecode, 0, 0)


        bytecode = compile("""
            NIL
            PUSH 1
            CONS
            PUSH 4
            ADD
        """)
        py.test.raises(TypeError, self.interp, bytecode, 0, 0)

        bytecode = compile("""
            PUSH 4
            NIL
            PUSH 1
            CONS
            ADD
        """)
        py.test.raises(TypeError, self.interp, bytecode, 0, 0)


        bytecode = compile("""
            PUSH 2
            PUSH 1
            CONS
            NIL
            ADD
        """)
        py.test.raises(TypeError, self.interp, bytecode, 0, 0)

    def test_new_obj(self):
        from rpython.jit.tl.tlc import interp_eval, InstanceObj, nil
        pool = ConstantPool()
        bytecode = compile("""
            NEW foo,bar
        """, pool)
        obj = interp_eval(bytecode, 0, [nil], pool)
        assert isinstance(obj, InstanceObj)
        assert len(obj.values) == 2
        assert sorted(obj.cls.attributes.keys()) == ['bar', 'foo']

    def test_setattr(self):
        from rpython.jit.tl.tlc import interp_eval, nil
        pool = ConstantPool()
        bytecode = compile("""
            NEW foo,bar
            PICK 0
            PUSH 42
            SETATTR foo
        """, pool)
        obj = interp_eval(bytecode, 0, [nil], pool)
        assert obj.values[0].int_o() == 42
        assert obj.values[1] is nil

    def test_getattr(self):
        from rpython.jit.tl.tlc import interp_eval, nil
        pool = ConstantPool()
        bytecode = compile("""
            NEW foo,bar
            PICK 0
            PUSH 42
            SETATTR bar
            GETATTR bar
        """, pool)
        res = interp_eval(bytecode, 0, [nil], pool)
        assert res.int_o() == 42

    def test_obj_truth(self):
        from rpython.jit.tl.tlc import interp_eval, nil
        pool = ConstantPool()
        bytecode = compile("""
            NEW foo,bar
            BR_COND true
            PUSH 12
            PUSH 1
            BR_COND exit
        true:
            PUSH 42
        exit:
            RETURN
        """, pool)
        res = interp_eval(bytecode, 0, [nil], pool)
        assert res.int_o() == 42

    def test_obj_equality(self):
        from rpython.jit.tl.tlc import interp_eval, nil
        pool = ConstantPool()
        bytecode = compile("""
            NEW foo,bar
            NEW foo,bar
            EQ
        """, pool)
        res = interp_eval(bytecode, 0, [nil], pool)
        assert res.int_o() == 0

    def test_method(self):
        from rpython.jit.tl.tlc import interp_eval, nil
        pool = ConstantPool()
        bytecode = compile("""
            NEW foo,meth=meth
            PICK 0
            PUSH 42
            SETATTR foo
            SEND meth/0
            RETURN
        meth:
            PUSHARG
            GETATTR foo
            RETURN
        """, pool)
        res = interp_eval(bytecode, 0, [nil], pool)
        assert res.int_o() == 42

    def test_method_arg(self):
        from rpython.jit.tl.tlc import interp_eval, nil
        pool = ConstantPool()
        bytecode = compile("""
            NEW foo,meth=meth
            PICK 0
            PUSH 40
            SETATTR foo
            PUSH 2
            SEND meth/1
            RETURN
        meth:
            PUSHARG
            GETATTR foo
            PUSHARGN 1
            ADD
            RETURN
        """, pool)
        res = interp_eval(bytecode, 0, [nil], pool)
        assert res.int_o() == 42

    def test_call_without_return_value(self):
        from rpython.jit.tl.tlc import interp_eval, nil
        pool = ConstantPool()
        bytecode = compile("""
            CALL foo
            PUSH 42
            RETURN
        foo:
            RETURN
        """, pool)
        res = interp_eval(bytecode, 0, [nil], pool)
        assert res.int_o() == 42

    def compile(self, filename):
        from rpython.jit.tl.tlc import interp_eval, IntObj
        pool = ConstantPool()
        path = py.path.local(__file__).join(filename)
        src = path.read()
        bytecode = compile(src, pool)
        def fn(n):
            obj = IntObj(n)
            res = interp_eval(bytecode, 0, [obj], pool)
            return res.int_o()
        return fn

    def test_binarytree(self):
        search = self.compile('../../binarytree.tlc.src')
        assert search(20)
        assert search(10)
        assert search(15)
        assert search(30)
        assert not search(1)
        assert not search(40)
        assert not search(12)
        assert not search(27)

    def test_fibo(self):
        fibo = self.compile('../../fibo.tlc.src')
        assert fibo(1) == 1
        assert fibo(2) == 1
        assert fibo(3) == 2
        assert fibo(7) == 13

    def test_accumulator(self):
        acc = self.compile('../../accumulator.tlc.src')
        assert acc(0) == 0
        assert acc(1) == 0
        assert acc(10) == sum(range(10))
        assert acc(20) == sum(range(20))
        assert acc(-1) == 1
        assert acc(-2) == 2
        assert acc(-10) == 10