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
|
import py
from rpython.jit.metainterp.test.support import LLJitMixin
from rpython.rlib.jit import JitDriver, dont_look_inside
class TestByteArray(LLJitMixin):
def test_getitem(self):
x = bytearray("foobar")
def fn(n):
assert n >= 0
return x[n]
res = self.interp_operations(fn, [3])
assert res == ord('b')
def test_getitem_negative(self):
x = bytearray("foobar")
def fn(n):
return x[n]
res = self.interp_operations(fn, [-2])
assert res == ord('a')
def test_len(self):
x = bytearray("foobar")
def fn(n):
return len(x)
res = self.interp_operations(fn, [3])
assert res == 6
def test_setitem(self):
@dont_look_inside
def make_me():
return bytearray("foobar")
def fn(n):
assert n >= 0
x = make_me()
x[n] = 3
return x[3] + 1000 * x[4]
res = self.interp_operations(fn, [3])
assert res == 3 + 1000 * ord('a')
def test_setitem_negative(self):
@dont_look_inside
def make_me():
return bytearray("foobar")
def fn(n):
x = make_me()
x[n] = 3
return x[3] + 1000 * x[4]
res = self.interp_operations(fn, [-2])
assert res == ord('b') + 1000 * 3
def test_new_bytearray(self):
def fn(n, m):
x = bytearray(str(n))
x[m] = 0x34
return int(str(x))
assert fn(610978, 3) == 610478
res = self.interp_operations(fn, [610978, 3])
assert res == 610478
def test_slice(self):
py.test.skip("XXX later")
def fn(n, m):
x = bytearray(str(n))
x = x[1:5]
x[m] = 0x35
return int(str(x))
res = self.interp_operations(fn, [610978, 1])
assert res == 1597
def test_bytearray_from_bytearray(self):
def fn(n, m):
x = bytearray(str(n))
y = bytearray(x)
x[m] = 0x34
return int(str(x)) + int(str(y))
res = self.interp_operations(fn, [610978, 3])
assert res == 610478 + 610978
|