test_rpython_list.py 4.22 KB
Newer Older
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
from rpython.rtyper.lltypesystem import rffi, lltype
from rpython.rlib import rmu_fast as rmu
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
120
        
        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)

121
@may_spawn_proc
qinsoon's avatar
qinsoon committed
122
def test_rpython_list_addr_check_all():
qinsoon's avatar
qinsoon committed
123
    Int64Ptr = lltype.Ptr(lltype.Array(rffi.LONGLONG, hints={'nolength': True}))
124

qinsoon's avatar
qinsoon committed
125
126
127
    def check(actual, expect):
        if actual != expect:
            c_exit(rffi.cast(rffi.INT, actual))
128
129
130
131
132
133
134
135
136
137
138
139
140
    
    def main(argv):
        a = []
        for i in range(0, 10):
            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], 10)
qinsoon's avatar
qinsoon committed
141
142
143
144
145
146
147
148

        inner_addr = mem[2]
        inner = rffi.cast(Int64Ptr, inner_addr)
        # inner[0], inner[1] is ignored
        for i in range(0, 10):
            check(inner[2 + i], i)
        
        keepalive_until_here(a)
149
150
151
        
        return 0
        
qinsoon's avatar
qinsoon committed
152
    res = run_boot_image(main, '/tmp/test_rpython_list_addr_check_all')
153
154
    
    assert res.returncode == 0, 'returncode = %d\n%s' % (res.returncode, res.err)