File: test_tla.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 (180 lines) | stat: -rw-r--r-- 4,204 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
import py
from rpython.jit.tl.tla import tla

def test_stack():
    f = tla.Frame('')
    f.push(1)
    f.push(2)
    f.push(3)
    assert f.pop() == 3
    assert f.pop() == 2
    assert f.pop() == 1
    py.test.raises(AssertionError, f.pop)


def test_W_IntObject():
    w_a = tla.W_IntObject(0)
    w_b = tla.W_IntObject(10)
    w_c = tla.W_IntObject(32)
    assert not w_a.is_true()
    assert w_b.is_true()
    assert w_c.is_true()
    assert w_b.add(w_c).intvalue == 42
    assert w_b.getrepr() == '10'


def assemble(mylist):
    return ''.join([chr(x) for x in mylist])

def interp(mylist, w_arg):
    bytecode = assemble(mylist)
    return tla.run(bytecode, w_arg)

def test_interp():
    code = [
        tla.RETURN
        ]
    res = interp(code, tla.W_IntObject(42))
    assert res.intvalue == 42

def test_pop():
    code = [
        tla.CONST_INT, 99,
        tla.POP,
        tla.RETURN
        ]
    res = interp(code, tla.W_IntObject(42))
    assert res.intvalue == 42

def test_dup():
    code = [
        tla.DUP,
        tla.ADD,
        tla.RETURN
        ]
    res = interp(code, tla.W_IntObject(41))
    assert res.intvalue == 2 * 41

def test_bogus_return():
    code = [
        tla.CONST_INT, 123,
        tla.RETURN # stack depth == 2 here, error!
        ]
    py.test.raises(AssertionError, "interp(code, tla.W_IntObject(234))")

def test_add():
    code = [
        tla.CONST_INT, 20,
        tla.ADD,
        tla.RETURN
        ]
    res = interp(code, tla.W_IntObject(22))
    assert res.intvalue == 42

def test_sub():
    code = [
        tla.CONST_INT, 20,
        tla.SUB,
        tla.RETURN
        ]
    res = interp(code, tla.W_IntObject(22))
    assert res.intvalue == 2

def test_jump_if():
    code = [
        tla.JUMP_IF, 5,   # jump to target
        tla.CONST_INT, 123,
        tla.RETURN,
        tla.CONST_INT, 234,  # target
        tla.RETURN
        ]
    res = interp(code, tla.W_IntObject(0))
    assert res.intvalue == 123

    res = interp(code, tla.W_IntObject(1))
    assert res.intvalue == 234


def test_newstr():
    code = [
        tla.POP,
        tla.NEWSTR, ord('x'),
        tla.RETURN
        ]
    res = interp(code, tla.W_IntObject(0))
    assert isinstance(res, tla.W_StringObject)
    assert res.strvalue == 'x'

# ____________________________________________________________
# EXERCISES
# ____________________________________________________________


def test_add_strings():
    py.test.skip('exercise!')
    code = [
        tla.NEWSTR, ord('d'),
        tla.ADD,
        tla.NEWSTR, ord('!'),
        tla.ADD,
        tla.RETURN
        ]
    res = interp(code, tla.W_StringObject('Hello worl'))
    assert res.strvalue == 'Hello world!'

def test_mul():
    py.test.skip('exercise!')
    code = [
        tla.CONST_INT, 2,
        tla.MUL,
        tla.RETURN
        ]
    res = interp(code, tla.W_IntObject(21))
    assert res.intvalue == 42

def test_mul_strings():
    py.test.skip('exercise!')
    code = [
        tla.CONST_INT, 3,
        tla.MUL,
        tla.RETURN
        ]
    res = interp(code, tla.W_StringObject('foo '))
    assert res.strvalue == 'foo foo foo '

def test_div_float():
    py.test.skip('exercise!')
    code = [
        tla.CONST_INT, 2,
        tla.DIV,
        tla.RETURN
        ]
    res = interp(code, tla.W_IntObject(5))
    assert isinstance(res, tla.W_FloatObject)
    assert res.floatval == 2.5

# ____________________________________________________________

from rpython.jit.metainterp.test.support import LLJitMixin

class TestLLtype(LLJitMixin):
    def test_loop(self):
        code = [
                tla.DUP,
                tla.CONST_INT, 1,
                tla.SUB,
                tla.DUP,
                tla.JUMP_IF, 1,
                tla.POP,
                tla.CONST_INT, 1,
                tla.SUB,
                tla.DUP,
                tla.JUMP_IF, 0,
                tla.RETURN
            ]
        def interp_w(intvalue):
            w_result = interp(code, tla.W_IntObject(intvalue))
            assert isinstance(w_result, tla.W_IntObject)
            return w_result.intvalue
        res = self.meta_interp(interp_w, [42], listops=True)
        assert res == 0