| 1 |
import py |
| 2 |
from rpython.jit.tl.tla import tla |
| 3 |
|
| 4 |
def test_stack(): |
| 5 |
f = tla.Frame('') |
| 6 |
f.push(1) |
| 7 |
f.push(2) |
| 8 |
f.push(3) |
| 9 |
assert f.pop() == 3 |
| 10 |
assert f.pop() == 2 |
| 11 |
assert f.pop() == 1 |
| 12 |
py.test.raises(AssertionError, f.pop) |
| 13 |
|
| 14 |
|
| 15 |
def test_W_IntObject(): |
| 16 |
w_a = tla.W_IntObject(0) |
| 17 |
w_b = tla.W_IntObject(10) |
| 18 |
w_c = tla.W_IntObject(32) |
| 19 |
assert not w_a.is_true() |
| 20 |
assert w_b.is_true() |
| 21 |
assert w_c.is_true() |
| 22 |
assert w_b.add(w_c).intvalue == 42 |
| 23 |
assert w_b.getrepr() == '10' |
| 24 |
|
| 25 |
|
| 26 |
def assemble(mylist): |
| 27 |
return ''.join([chr(x) for x in mylist]) |
| 28 |
|
| 29 |
def interp(mylist, w_arg): |
| 30 |
bytecode = assemble(mylist) |
| 31 |
return tla.run(bytecode, w_arg) |
| 32 |
|
| 33 |
def test_interp(): |
| 34 |
code = [ |
| 35 |
tla.RETURN |
| 36 |
] |
| 37 |
res = interp(code, tla.W_IntObject(42)) |
| 38 |
assert res.intvalue == 42 |
| 39 |
|
| 40 |
def test_pop(): |
| 41 |
code = [ |
| 42 |
tla.CONST_INT, 99, |
| 43 |
tla.POP, |
| 44 |
tla.RETURN |
| 45 |
] |
| 46 |
res = interp(code, tla.W_IntObject(42)) |
| 47 |
assert res.intvalue == 42 |
| 48 |
|
| 49 |
def test_dup(): |
| 50 |
code = [ |
| 51 |
tla.DUP, |
| 52 |
tla.ADD, |
| 53 |
tla.RETURN |
| 54 |
] |
| 55 |
res = interp(code, tla.W_IntObject(41)) |
| 56 |
assert res.intvalue == 2 * 41 |
| 57 |
|
| 58 |
def test_bogus_return(): |
| 59 |
code = [ |
| 60 |
tla.CONST_INT, 123, |
| 61 |
tla.RETURN |
| 62 |
] |
| 63 |
py.test.raises(AssertionError, "interp(code, tla.W_IntObject(234))") |
| 64 |
|
| 65 |
def test_add(): |
| 66 |
code = [ |
| 67 |
tla.CONST_INT, 20, |
| 68 |
tla.ADD, |
| 69 |
tla.RETURN |
| 70 |
] |
| 71 |
res = interp(code, tla.W_IntObject(22)) |
| 72 |
assert res.intvalue == 42 |
| 73 |
|
| 74 |
def test_sub(): |
| 75 |
code = [ |
| 76 |
tla.CONST_INT, 20, |
| 77 |
tla.SUB, |
| 78 |
tla.RETURN |
| 79 |
] |
| 80 |
res = interp(code, tla.W_IntObject(22)) |
| 81 |
assert res.intvalue == 2 |
| 82 |
|
| 83 |
def test_jump_if(): |
| 84 |
code = [ |
| 85 |
tla.JUMP_IF, 5, |
| 86 |
tla.CONST_INT, 123, |
| 87 |
tla.RETURN, |
| 88 |
tla.CONST_INT, 234, |
| 89 |
tla.RETURN |
| 90 |
] |
| 91 |
res = interp(code, tla.W_IntObject(0)) |
| 92 |
assert res.intvalue == 123 |
| 93 |
|
| 94 |
res = interp(code, tla.W_IntObject(1)) |
| 95 |
assert res.intvalue == 234 |
| 96 |
|
| 97 |
|
| 98 |
def test_newstr(): |
| 99 |
code = [ |
| 100 |
tla.POP, |
| 101 |
tla.NEWSTR, ord('x'), |
| 102 |
tla.RETURN |
| 103 |
] |
| 104 |
res = interp(code, tla.W_IntObject(0)) |
| 105 |
assert isinstance(res, tla.W_StringObject) |
| 106 |
assert res.strvalue == 'x' |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
def test_add_strings(): |
| 114 |
py.test.skip('exercise!') |
| 115 |
code = [ |
| 116 |
tla.NEWSTR, ord('d'), |
| 117 |
tla.ADD, |
| 118 |
tla.NEWSTR, ord('!'), |
| 119 |
tla.ADD, |
| 120 |
tla.RETURN |
| 121 |
] |
| 122 |
res = interp(code, tla.W_StringObject('Hello worl')) |
| 123 |
assert res.strvalue == 'Hello world!' |
| 124 |
|
| 125 |
def test_mul(): |
| 126 |
py.test.skip('exercise!') |
| 127 |
code = [ |
| 128 |
tla.CONST_INT, 2, |
| 129 |
tla.MUL, |
| 130 |
tla.RETURN |
| 131 |
] |
| 132 |
res = interp(code, tla.W_IntObject(21)) |
| 133 |
assert res.intvalue == 42 |
| 134 |
|
| 135 |
def test_mul_strings(): |
| 136 |
py.test.skip('exercise!') |
| 137 |
code = [ |
| 138 |
tla.CONST_INT, 3, |
| 139 |
tla.MUL, |
| 140 |
tla.RETURN |
| 141 |
] |
| 142 |
res = interp(code, tla.W_StringObject('foo ')) |
| 143 |
assert res.strvalue == 'foo foo foo ' |
| 144 |
|
| 145 |
def test_div_float(): |
| 146 |
py.test.skip('exercise!') |
| 147 |
code = [ |
| 148 |
tla.CONST_INT, 2, |
| 149 |
tla.DIV, |
| 150 |
tla.RETURN |
| 151 |
] |
| 152 |
res = interp(code, tla.W_IntObject(5)) |
| 153 |
assert isinstance(res, tla.W_FloatObject) |
| 154 |
assert res.floatval == 2.5 |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
from rpython.jit.metainterp.test.support import LLJitMixin |
| 159 |
|
| 160 |
class TestLLtype(LLJitMixin): |
| 161 |
def test_loop(self): |
| 162 |
code = [ |
| 163 |
tla.DUP, |
| 164 |
tla.CONST_INT, 1, |
| 165 |
tla.SUB, |
| 166 |
tla.DUP, |
| 167 |
tla.JUMP_IF, 1, |
| 168 |
tla.POP, |
| 169 |
tla.CONST_INT, 1, |
| 170 |
tla.SUB, |
| 171 |
tla.DUP, |
| 172 |
tla.JUMP_IF, 0, |
| 173 |
tla.RETURN |
| 174 |
] |
| 175 |
def interp_w(intvalue): |
| 176 |
w_result = interp(code, tla.W_IntObject(intvalue)) |
| 177 |
assert isinstance(w_result, tla.W_IntObject) |
| 178 |
return w_result.intvalue |
| 179 |
res = self.meta_interp(interp_w, [42], listops=True) |
| 180 |
assert res == 0 |