pypy / rpython / jit / metainterp / test / test_rawmem.py

The branch 'jit-ordereddict' does not exist.
from rpython.jit.metainterp.test.support import LLJitMixin
from rpython.rtyper.lltypesystem import lltype, rffi
from rpython.rlib.rawstorage import (alloc_raw_storage, raw_storage_setitem,
                                  free_raw_storage, raw_storage_getitem)

class RawMemTests(object):
    def test_cast_void_ptr(self):
        TP = lltype.Array(lltype.Float, hints={"nolength": True})
        VOID_TP = lltype.Array(lltype.Void, hints={"nolength": True, "uncast_on_llgraph": True})
        class A(object):
            def __init__(self, x):
                self.storage = rffi.cast(lltype.Ptr(VOID_TP), x)

        def f(n):
            x = lltype.malloc(TP, n, flavor="raw", zero=True)
            a = A(x)
            s = 0.0
            rffi.cast(lltype.Ptr(TP), a.storage)[0] = 1.0
            s += rffi.cast(lltype.Ptr(TP), a.storage)[0]
            lltype.free(x, flavor="raw")
            return s
        self.interp_operations(f, [10])

    def test_fixed_size_malloc(self):
        TIMEVAL = lltype.Struct('dummy', ('tv_sec', rffi.LONG), ('tv_usec', rffi.LONG))
        def f():
            p = lltype.malloc(TIMEVAL, flavor='raw')
            lltype.free(p, flavor='raw')
            return 42
        res = self.interp_operations(f, [])
        assert res == 42
        self.check_operations_history({'call': 2, 'guard_no_exception': 1,
                                       'finish': 1})

    def test_raw_storage_int(self):
        def f():
            p = alloc_raw_storage(15)
            raw_storage_setitem(p, 3, 24)
            res = raw_storage_getitem(lltype.Signed, p, 3)
            free_raw_storage(p)
            return res
        res = self.interp_operations(f, [])
        assert res == 24
        self.check_operations_history({'call': 2, 'guard_no_exception': 1,
                                       'raw_store': 1, 'raw_load': 1,
                                       'finish': 1})

    def test_raw_storage_float(self):
        def f():
            p = alloc_raw_storage(15)
            raw_storage_setitem(p, 4, 2.4e15)
            res = raw_storage_getitem(lltype.Float, p, 4)
            free_raw_storage(p)
            return res
        res = self.interp_operations(f, [])
        assert res == 2.4e15
        self.check_operations_history({'call': 2, 'guard_no_exception': 1,
                                       'raw_store': 1, 'raw_load': 1,
                                       'finish': 1})

    def test_raw_storage_byte(self):
        def f():
            p = alloc_raw_storage(15)
            raw_storage_setitem(p, 5, rffi.cast(rffi.UCHAR, 254))
            res = raw_storage_getitem(rffi.UCHAR, p, 5)
            free_raw_storage(p)
            return rffi.cast(lltype.Signed, res)
        res = self.interp_operations(f, [])
        assert res == 254
        self.check_operations_history({'call': 2, 'guard_no_exception': 1,
                                       'raw_store': 1, 'raw_load': 1,
                                       'finish': 1})

    def test_getarraysubstruct(self):
        A2 = lltype.Array(('a', lltype.Signed), ('b', lltype.Signed),
                          hints={'nolength': True})
        p = lltype.malloc(A2, 10, flavor='raw', immortal=True, zero=True)
        p[2].b = 689
        def f(n, m):
            p[n].a = 55
            p[n].b = 44
            p[4].b = 66
            return p[m].b

        # run with 'disable_optimizations' to prevent an error
        # 'Symbolics cannot be compared!' in the optimizer for int_mul
        res = self.interp_operations(f, [7, 2], disable_optimizations=True)
        assert res == 689
        res = self.interp_operations(f, [7, 4], disable_optimizations=True)
        assert res == 66
        res = self.interp_operations(f, [2, 2], disable_optimizations=True)
        assert res == 44

class TestRawMem(RawMemTests, LLJitMixin):
    pass
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.