| 1 | import py |
| 2 | import sys |
| 3 | from rpython.rtyper.lltypesystem import lltype |
| 4 | from rpython.rlib import rawstorage |
| 5 | from rpython.rlib.rawstorage import alloc_raw_storage, free_raw_storage,\ |
| 6 | raw_storage_setitem, raw_storage_getitem, AlignmentError,\ |
| 7 | raw_storage_setitem_unaligned, raw_storage_getitem_unaligned |
| 8 | from rpython.rtyper.test.tool import BaseRtypingTest |
| 9 | from rpython.translator.c.test.test_genc import compile |
| 10 | |
| 11 | |
| 12 | def test_untranslated_storage(): |
| 13 | r = alloc_raw_storage(37) |
| 14 | raw_storage_setitem(r, 8, 1<<30) |
| 15 | res = raw_storage_getitem(lltype.Signed, r, 8) |
| 16 | assert res == 1<<30 |
| 17 | raw_storage_setitem(r, 8, 3.14) |
| 18 | res = raw_storage_getitem(lltype.Float, r, 8) |
| 19 | assert res == 3.14 |
| 20 | py.test.raises(AlignmentError, raw_storage_getitem, lltype.Signed, r, 3) |
| 21 | py.test.raises(AlignmentError, raw_storage_setitem, r, 3, 42.5) |
| 22 | free_raw_storage(r) |
| 23 | |
| 24 | def test_untranslated_storage_unaligned(monkeypatch): |
| 25 | monkeypatch.setattr(rawstorage, 'misaligned_is_fine', False) |
| 26 | r = alloc_raw_storage(15) |
| 27 | raw_storage_setitem_unaligned(r, 3, 1<<30) |
| 28 | res = raw_storage_getitem_unaligned(lltype.Signed, r, 3) |
| 29 | assert res == 1<<30 |
| 30 | raw_storage_setitem_unaligned(r, 3, 3.14) |
| 31 | res = raw_storage_getitem_unaligned(lltype.Float, r, 3) |
| 32 | assert res == 3.14 |
| 33 | free_raw_storage(r) |
| 34 | |
| 35 | class TestRawStorage(BaseRtypingTest): |
| 36 | |
| 37 | def test_storage_int(self): |
| 38 | def f(i): |
| 39 | r = alloc_raw_storage(24) |
| 40 | raw_storage_setitem(r, 8, i) |
| 41 | res = raw_storage_getitem(lltype.Signed, r, 8) |
| 42 | free_raw_storage(r) |
| 43 | return res |
| 44 | |
| 45 | x = self.interpret(f, [1<<30]) |
| 46 | assert x == 1 << 30 |
| 47 | |
| 48 | def test_storage_float_unaligned(self, monkeypatch): |
| 49 | def f(v): |
| 50 | r = alloc_raw_storage(24) |
| 51 | raw_storage_setitem_unaligned(r, 3, v) |
| 52 | res = raw_storage_getitem_unaligned(lltype.Float, r, 3) |
| 53 | free_raw_storage(r) |
| 54 | return res |
| 55 | |
| 56 | monkeypatch.setattr(rawstorage, 'misaligned_is_fine', False) |
| 57 | x = self.interpret(f, [3.14]) |
| 58 | assert x == 3.14 |
| 59 | |
| 60 | |
| 61 | class TestCBackend(object): |
| 62 | |
| 63 | def test_backend_int(self): |
| 64 | def f(i): |
| 65 | r = alloc_raw_storage(24) |
| 66 | raw_storage_setitem(r, 8, i) |
| 67 | res = raw_storage_getitem(lltype.Signed, r, 8) |
| 68 | free_raw_storage(r) |
| 69 | return res != i |
| 70 | |
| 71 | fc = compile(f, [int]) |
| 72 | x = fc(-sys.maxint // 3) |
| 73 | assert x == 0 |
| 74 | |
| 75 | def test_backend_float_unaligned(self, monkeypatch): |
| 76 | def f(v): |
| 77 | r = alloc_raw_storage(24) |
| 78 | raw_storage_setitem_unaligned(r, 3, v) |
| 79 | res = raw_storage_getitem_unaligned(lltype.Float, r, 3) |
| 80 | free_raw_storage(r) |
| 81 | return res != v |
| 82 | |
| 83 | if monkeypatch is not None: |
| 84 | monkeypatch.setattr(rawstorage, 'misaligned_is_fine', False) |
| 85 | fc = compile(f, [float]) |
| 86 | x = fc(-3.14) |
| 87 | assert x == 0 |
| 88 | |
| 89 | def test_backend_float_unaligned_allow_misalign(self): |
| 90 | self.test_backend_float_unaligned(monkeypatch=None) |