~ubuntu-branches/ubuntu/karmic/pypy/karmic

« back to all changes in this revision

Viewing changes to pypy/rpython/memory/test/test_convertlltype.py

  • Committer: Bazaar Package Importer
  • Author(s): Alexandre Fayolle
  • Date: 2007-04-13 09:33:09 UTC
  • Revision ID: james.westby@ubuntu.com-20070413093309-yoojh4jcoocu2krz
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from pypy.rpython.memory.lltypesimulation import *
 
2
from pypy.rpython.memory.convertlltype import LLTypeConverter
 
3
 
 
4
def test_convert_primitives():
 
5
    cvter = LLTypeConverter(lladdress.NULL)
 
6
    c1 = cvter.convert(1)
 
7
    c = cvter.convert("c")
 
8
    assert c1 == 1
 
9
    assert c == "c"
 
10
 
 
11
def test_convert_array_of_primitives():
 
12
    cvter = LLTypeConverter(lladdress.raw_malloc(1000))
 
13
    A = lltype.GcArray(lltype.Signed)
 
14
    lls = lltype.malloc(A, 3)
 
15
    lls[0] = 1
 
16
    lls[1] = 2
 
17
    a = cvter.convert(lls)
 
18
    assert a[0] == 1
 
19
    assert a[1] == 2
 
20
    b = cvter.convert(lls)
 
21
    assert a == b
 
22
    
 
23
def test_convert_array_of_structs():
 
24
    cvter = LLTypeConverter(lladdress.raw_malloc(1000))
 
25
    S = lltype.Struct("test", ("v1", lltype.Signed), ("v2", lltype.Signed))
 
26
    Ar =  lltype.GcArray(S)
 
27
    llx = lltype.malloc(Ar, 3)
 
28
    llx[0].v1 = 1
 
29
    llx[1].v1 = 2
 
30
    llx[2].v1 = 3    
 
31
    x = cvter.convert(llx)
 
32
    assert [x[z].v1 for z in range(3)] == [1, 2, 3]
 
33
    assert [x[z].v2 for z in range(3)] == [0, 0, 0]
 
34
 
 
35
def test_convert_array_of_ptrs():
 
36
    cvter = LLTypeConverter(lladdress.raw_malloc(1000))
 
37
    S = lltype.GcStruct("name", ("v", lltype.Signed))
 
38
    A = lltype.GcArray(lltype.Ptr(S))
 
39
    lla = lltype.malloc(A, 3)
 
40
    lla[0] = lltype.malloc(S)
 
41
    lla[0].v = 1
 
42
    lla[1] = lltype.malloc(S)
 
43
    lla[1].v = 2
 
44
    lla[2] = lltype.malloc(S)
 
45
    lla[2].v = 3
 
46
    assert [lla[z].v for z in range(3)] == [1, 2, 3]
 
47
    print lla
 
48
    print [lla[z] for z in range(3)]
 
49
    x = cvter.convert(lla)
 
50
    print x
 
51
    print [x[z] for z in range(3)]
 
52
    print x._address._load("iiiiiii")
 
53
    assert [x[z].v for z in range(3)] == [1, 2, 3]
 
54
    
 
55
 
 
56
def test_circular_struct():
 
57
    cvter = LLTypeConverter(lladdress.raw_malloc(100))
 
58
    F = lltype.GcForwardReference()
 
59
    S = lltype.GcStruct('abc', ('x', lltype.Ptr(F)))
 
60
    F.become(S)
 
61
    lls = lltype.malloc(S)
 
62
    lls.x = lls
 
63
    s = cvter.convert(lls)
 
64
    assert s.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x.x == s
 
65
 
 
66
def test_circular_array():
 
67
    cvter = LLTypeConverter(lladdress.raw_malloc(1000))
 
68
    F = lltype.GcForwardReference()
 
69
    A = lltype.GcArray(lltype.Ptr(F))
 
70
    S = lltype.GcStruct("name", ("a", lltype.Ptr(A)), ("b", lltype.Signed))
 
71
    F.become(S)
 
72
    lla = lltype.malloc(A, 3)
 
73
    lla[0] = lltype.malloc(S)
 
74
    lla[1] = lltype.malloc(S)
 
75
    lla[2] = lltype.malloc(S)
 
76
    lla[0].a = lla
 
77
    lla[1].a = lla
 
78
    lla[2].a = lla
 
79
    lla[0].b = 1
 
80
    lla[1].b = 2
 
81
    lla[2].b = 3
 
82
    assert lla[0].a[1].a[2].a == lla
 
83
    assert [lla[i].b for i in range(3)] == [1, 2, 3]
 
84
    a = cvter.convert(lla)
 
85
    assert a[0].a[1].a[2].a == a
 
86
    assert [a[i].b for i in range(3)] == [1, 2, 3]
 
87
 
 
88
def test_varsize_struct():
 
89
    cvter = LLTypeConverter(lladdress.raw_malloc(1000))
 
90
    A = lltype.Array(lltype.Signed)
 
91
    S = lltype.GcStruct("name", ("v", lltype.Signed), ("a", A))
 
92
    lls = lltype.malloc(S, 3)
 
93
    lls.a[0] = 1
 
94
    lls.a[1] = 2
 
95
    lls.a[2] = 3
 
96
    lls.v = 4
 
97
    s = cvter.convert(lls)
 
98
    assert [s.a[i] for i in range(3)] == [1, 2, 3]
 
99
    assert s.v == 4
 
100
    
 
101
def test_nullptr():
 
102
    cvter = LLTypeConverter(lladdress.raw_malloc(10))
 
103
    S = lltype.GcStruct("name", ("v", lltype.Signed))
 
104
    llptr = lltype.nullptr(S)
 
105
    s = cvter.convert(llptr)
 
106
    assert not s
 
107
 
 
108
def test_funcptr():
 
109
    def f(x, y):
 
110
        return x + y
 
111
    F = lltype.FuncType((lltype.Signed, lltype.Signed), lltype.Signed)
 
112
    llfuncptr = lltype.functionptr(F, "add", _callable=f)
 
113
    assert llfuncptr(1, 2) == 3
 
114
    cvter = LLTypeConverter(lladdress.raw_malloc(10))
 
115
    fpter = cvter.convert(llfuncptr)
 
116
    assert fpter(1, 2) == 3
 
117
 
 
118
def test_convertsubstructure():
 
119
    cvter = LLTypeConverter(lladdress.raw_malloc(100))
 
120
    S1 = lltype.GcStruct("s1", ("v1", lltype.Signed))
 
121
    S2 = lltype.GcStruct("s2", ("s", S1), ("v2", lltype.Signed))
 
122
    lls2 = lltype.malloc(S2)
 
123
    lls1 = lltype.cast_pointer(lltype.Ptr(S1), lls2)
 
124
    s1 = cvter.convert(lls1)
 
125
    s2 = lltype.cast_pointer(lltype.Ptr(S2), s1)
 
126
    assert s2.v2 == 0
 
127
 
 
128
def test_convertsubstructure_of_array():
 
129
    cvter = LLTypeConverter(lladdress.raw_malloc(100))
 
130
    S1 = lltype.Struct("s1", ("v1", lltype.Signed))
 
131
    A = lltype.GcArray(S1)
 
132
    lla = lltype.malloc(A, 3)
 
133
    s1 = cvter.convert(lla[0])