| 1 | import py |
| 2 | from rpython.jit.metainterp.test.support import LLJitMixin |
| 3 | from rpython.rlib.jit import JitDriver, dont_look_inside |
| 4 | |
| 5 | class TestByteArray(LLJitMixin): |
| 6 | |
| 7 | def test_getitem(self): |
| 8 | x = bytearray("foobar") |
| 9 | def fn(n): |
| 10 | assert n >= 0 |
| 11 | return x[n] |
| 12 | res = self.interp_operations(fn, [3]) |
| 13 | assert res == ord('b') |
| 14 | |
| 15 | def test_getitem_negative(self): |
| 16 | x = bytearray("foobar") |
| 17 | def fn(n): |
| 18 | return x[n] |
| 19 | res = self.interp_operations(fn, [-2]) |
| 20 | assert res == ord('a') |
| 21 | |
| 22 | def test_len(self): |
| 23 | x = bytearray("foobar") |
| 24 | def fn(n): |
| 25 | return len(x) |
| 26 | res = self.interp_operations(fn, [3]) |
| 27 | assert res == 6 |
| 28 | |
| 29 | def test_setitem(self): |
| 30 | @dont_look_inside |
| 31 | def make_me(): |
| 32 | return bytearray("foobar") |
| 33 | def fn(n): |
| 34 | assert n >= 0 |
| 35 | x = make_me() |
| 36 | x[n] = 3 |
| 37 | return x[3] + 1000 * x[4] |
| 38 | |
| 39 | res = self.interp_operations(fn, [3]) |
| 40 | assert res == 3 + 1000 * ord('a') |
| 41 | |
| 42 | def test_setitem_negative(self): |
| 43 | @dont_look_inside |
| 44 | def make_me(): |
| 45 | return bytearray("foobar") |
| 46 | def fn(n): |
| 47 | x = make_me() |
| 48 | x[n] = 3 |
| 49 | return x[3] + 1000 * x[4] |
| 50 | |
| 51 | res = self.interp_operations(fn, [-2]) |
| 52 | assert res == ord('b') + 1000 * 3 |
| 53 | |
| 54 | def test_new_bytearray(self): |
| 55 | def fn(n, m): |
| 56 | x = bytearray(str(n)) |
| 57 | x[m] = 0x34 |
| 58 | return int(str(x)) |
| 59 | |
| 60 | assert fn(610978, 3) == 610478 |
| 61 | res = self.interp_operations(fn, [610978, 3]) |
| 62 | assert res == 610478 |
| 63 | |
| 64 | def test_slice(self): |
| 65 | py.test.skip("XXX later") |
| 66 | def fn(n, m): |
| 67 | x = bytearray(str(n)) |
| 68 | x = x[1:5] |
| 69 | x[m] = 0x35 |
| 70 | return int(str(x)) |
| 71 | res = self.interp_operations(fn, [610978, 1]) |
| 72 | assert res == 1597 |
| 73 | |
| 74 | def test_bytearray_from_bytearray(self): |
| 75 | def fn(n, m): |
| 76 | x = bytearray(str(n)) |
| 77 | y = bytearray(x) |
| 78 | x[m] = 0x34 |
| 79 | return int(str(x)) + int(str(y)) |
| 80 | |
| 81 | res = self.interp_operations(fn, [610978, 3]) |
| 82 | assert res == 610478 + 610978 |