test_rpython_list.py 6.15 KB
Newer Older
1
from rpython.rtyper.lltypesystem import rffi, lltype
2
from rpython.rlib.rmu import zebu
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
from rpython.translator.platform import platform
from util import fncptr_from_rpy_func, fncptr_from_py_script, may_spawn_proc
import ctypes, py, stat
import pytest

from test_rpython import run_boot_image

c_exit = rffi.llexternal('exit', [rffi.INT], lltype.Void, _nowrapper=True)

@may_spawn_proc
def test_rpython_list_new_empty():
    def main(argv):
        a = []
        c_exit(rffi.cast(rffi.INT, len(a)))
        return 0
    
    res = run_boot_image(main, '/tmp/test_rpython_list_new_empty')
    
    assert res.returncode == 0, res.err

@may_spawn_proc
def test_rpython_list_new_5():
    def main(argv):
        a = [1, 2, 3, 4, 5]
        c_exit(rffi.cast(rffi.INT, len(a)))
        return 0
    
    res = run_boot_image(main, '/tmp/test_rpython_list_new_5')
    
    assert res.returncode == 5, res.err

@may_spawn_proc
def test_rpython_list_append():
    def main(argv):
        a = []
        for i in range(0, 10):
            a.append(i)
        
        c_exit(rffi.cast(rffi.INT, len(a)))
        return 0
    
    res = run_boot_image(main, '/tmp/test_rpython_list_append')
    
    assert res.returncode == 10, res.err

@may_spawn_proc
def test_rpython_list_iter():
    def main(argv):
        a = []
        for i in range(0, 10):
            a.append(i)
        
        sum = 0
        for n in a:
            sum += n
        
        c_exit(rffi.cast(rffi.INT, sum))
        return 0
    
    res = run_boot_image(main, '/tmp/test_rpython_list_iter')
    
    assert res.returncode == 45, res.err

qinsoon's avatar
qinsoon committed
66 67
@may_spawn_proc
def test_rpython_list_addr_check_length1():
qinsoon's avatar
qinsoon committed
68
    Int64Ptr = lltype.Ptr(lltype.Array(rffi.LONGLONG, hints={'nolength': True}))
qinsoon's avatar
qinsoon committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

    def check(actual, expect):
        if actual != expect:
            c_exit(rffi.cast(rffi.INT, actual))
    
    def main(argv):
        a = []
        a.append(42)
        
        from rpython.rtyper.lltypesystem.llmemory import cast_ptr_to_adr
        from rpython.rlib.objectmodel import keepalive_until_here
        
        addr = cast_ptr_to_adr(a)
        mem  = rffi.cast(Int64Ptr, addr)
        # ignore mem[0]
        check(mem[1], 1)
qinsoon's avatar
qinsoon committed
85
        keepalive_until_here(a)
qinsoon's avatar
qinsoon committed
86 87 88 89 90 91 92 93 94
        
        return 0
        
    res = run_boot_image(main, '/tmp/test_rpython_list_addr_check_length1')
    
    assert res.returncode == 0, 'returncode = %d\n%s' % (res.returncode, res.err)

@may_spawn_proc
def test_rpython_list_addr_check_length2():
qinsoon's avatar
qinsoon committed
95
    Int64Ptr = lltype.Ptr(lltype.Array(rffi.LONGLONG, hints={'nolength': True}))
qinsoon's avatar
qinsoon committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

    def check(actual, expect):
        if actual != expect:
            c_exit(rffi.cast(rffi.INT, actual))
    
    def main(argv):
        a = []
        a.append(42)
        a.append(43)
        
        from rpython.rtyper.lltypesystem.llmemory import cast_ptr_to_adr
        from rpython.rlib.objectmodel import keepalive_until_here
        
        addr = cast_ptr_to_adr(a)
        mem  = rffi.cast(Int64Ptr, addr)
        # ignore mem[0]
        check(mem[1], 2)
qinsoon's avatar
qinsoon committed
113
        keepalive_until_here(a)
qinsoon's avatar
qinsoon committed
114 115 116 117 118 119
        
        return 0
        
    res = run_boot_image(main, '/tmp/test_rpython_list_addr_check_length2')
    
    assert res.returncode == 0, 'returncode = %d\n%s' % (res.returncode, res.err)
qinsoon's avatar
qinsoon committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
    
@may_spawn_proc
def test_rpython_list_addr_check_length100():
    Int64Ptr = lltype.Ptr(lltype.Array(rffi.LONGLONG, hints={'nolength': True}))

    def check(actual, expect):
        if actual != expect:
            c_exit(rffi.cast(rffi.INT, actual))
    
    def main(argv):
        a = []
        for i in range(0, 100):
            a.append(i)
        
        from rpython.rtyper.lltypesystem.llmemory import cast_ptr_to_adr
        from rpython.rlib.objectmodel import keepalive_until_here
        
        addr = cast_ptr_to_adr(a)
        mem  = rffi.cast(Int64Ptr, addr)
        # ignore mem[0]
        check(mem[1], 100)
        keepalive_until_here(a)
        
        return 0
        
    res = run_boot_image(main, '/tmp/test_rpython_list_addr_check_length2')
    
    assert res.returncode == 0, 'returncode = %d\n%s' % (res.returncode, res.err)
qinsoon's avatar
qinsoon committed
148

149
@may_spawn_proc
qinsoon's avatar
qinsoon committed
150
def test_rpython_list_addr_check_all10():
qinsoon's avatar
qinsoon committed
151
    Int64Ptr = lltype.Ptr(lltype.Array(rffi.LONGLONG, hints={'nolength': True}))
152

qinsoon's avatar
qinsoon committed
153 154
    N = 10

qinsoon's avatar
qinsoon committed
155 156 157
    def check(actual, expect):
        if actual != expect:
            c_exit(rffi.cast(rffi.INT, actual))
158 159 160
    
    def main(argv):
        a = []
qinsoon's avatar
qinsoon committed
161
        for i in range(0, N):
162 163 164 165 166 167 168 169
            a.append(i)
        
        from rpython.rtyper.lltypesystem.llmemory import cast_ptr_to_adr
        from rpython.rlib.objectmodel import keepalive_until_here
        
        addr = cast_ptr_to_adr(a)
        mem  = rffi.cast(Int64Ptr, addr)
        # ignore mem[0]
qinsoon's avatar
qinsoon committed
170
        check(mem[1], N)
qinsoon's avatar
qinsoon committed
171 172 173 174

        inner_addr = mem[2]
        inner = rffi.cast(Int64Ptr, inner_addr)
        # inner[0], inner[1] is ignored
qinsoon's avatar
qinsoon committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
        for i in range(0, N):
            check(inner[2 + i], i)
        
        keepalive_until_here(a)
        
        return 0
        
    res = run_boot_image(main, '/tmp/test_rpython_list_addr_check_all10')
    
    assert res.returncode == 0, 'returncode = %d\n%s' % (res.returncode, res.err)

@pytest.mark.xfail(reason='unknown')
@may_spawn_proc
def test_rpython_list_addr_check_all100():
    Int64Ptr = lltype.Ptr(lltype.Array(rffi.LONGLONG, hints={'nolength': True}))

    N = 100

    def check(actual, expect):
        if actual != expect:
            c_exit(rffi.cast(rffi.INT, actual))
    
    def main(argv):
        a = []
        for i in range(0, N):
            a.append(i)
        
        from rpython.rtyper.lltypesystem.llmemory import cast_ptr_to_adr
        from rpython.rlib.objectmodel import keepalive_until_here
        
        addr = cast_ptr_to_adr(a)
        mem  = rffi.cast(Int64Ptr, addr)
        # ignore mem[0]
        check(mem[1], N)

        inner_addr = mem[2]
        inner = rffi.cast(Int64Ptr, inner_addr)
        # inner[0], inner[1] is ignored
        for i in range(0, N):
qinsoon's avatar
qinsoon committed
214 215 216
            check(inner[2 + i], i)
        
        keepalive_until_here(a)
217 218 219
        
        return 0
        
qinsoon's avatar
qinsoon committed
220
    res = run_boot_image(main, '/tmp/test_rpython_list_addr_check_all100')
221 222
    
    assert res.returncode == 0, 'returncode = %d\n%s' % (res.returncode, res.err)