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

« back to all changes in this revision

Viewing changes to pypy/translator/c/test/test_lladdresses.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.lladdress import *
 
2
from pypy.annotation.model import SomeAddress, SomeChar
 
3
from pypy.translator.c.test.test_genc import compile
 
4
from pypy.rlib.objectmodel import free_non_gc_object
 
5
 
 
6
def test_null():
 
7
    def f():
 
8
        return NULL - NULL
 
9
    fc = compile(f, [])
 
10
 
 
11
def test_convert_to_bool():
 
12
    def f(x):
 
13
        if x:
 
14
            return bool(NULL)
 
15
        else:
 
16
            return bool(NULL + 1)
 
17
    fc = compile(f, [int])
 
18
    res = fc(1)
 
19
    assert isinstance(res, bool) and not res
 
20
    res = fc(0)
 
21
    assert isinstance(res, bool) and res
 
22
 
 
23
def test_memory_access():
 
24
    def f(value):
 
25
        addr = raw_malloc(16)
 
26
        addr.signed[0] = value
 
27
        result = addr.signed[0]
 
28
        raw_free(addr)
 
29
        return result
 
30
    fc = compile(f, [int])
 
31
    res = fc(42)
 
32
    assert res == 42
 
33
    res = fc(1)
 
34
    assert res == 1
 
35
    
 
36
def test_pointer_arithmetic():
 
37
    def f(offset, char):
 
38
        addr = raw_malloc(10000)
 
39
        same_offset = (addr + 2 * offset - offset) - addr 
 
40
        addr.char[offset] = char
 
41
        result = (addr + same_offset).char[0]
 
42
        raw_free(addr)
 
43
        return result
 
44
    fc = compile(f, [int, SomeChar()])
 
45
    res = fc(10, "c")
 
46
    assert res == "c"
 
47
    res = fc(12, "x")
 
48
    assert res == "x"
 
49
 
 
50
def test_pointer_arithmetic_inplace():
 
51
    def f(offset, char):
 
52
        addr = raw_malloc(10000)
 
53
        addr += offset
 
54
        addr.char[-offset] = char
 
55
        addr -= offset
 
56
        result = addr.char[0]
 
57
        raw_free(addr)
 
58
        return result
 
59
    fc = compile(f, [int, SomeChar()])
 
60
    res = fc(10, "c")
 
61
    assert res == "c"
 
62
 
 
63
def test_raw_memcopy():
 
64
    def f():
 
65
        addr = raw_malloc(100)
 
66
        addr.signed[0] = 12
 
67
        (addr + 10).signed[0] = 42
 
68
        (addr + 20).char[0] = "a"
 
69
        addr1 = raw_malloc(100)
 
70
        raw_memcopy(addr, addr1, 100)
 
71
        result = addr1.signed[0] == 12
 
72
        result = result and (addr1 + 10).signed[0] == 42
 
73
        result = result and (addr1 + 20).char[0] == "a"
 
74
        raw_free(addr)
 
75
        raw_free(addr1)
 
76
        return result
 
77
    fc = compile(f, [])
 
78
    res = fc()
 
79
    assert res
 
80
 
 
81
def test_pointer_comparison():
 
82
    def f():
 
83
        result = 0
 
84
        addresses = [raw_malloc(1), NULL]
 
85
        for addr1 in addresses:
 
86
            addr2 = addr1 + 1
 
87
            result = result * 2 + int(addr1 == addr2)
 
88
            result = result * 2 + int(addr1 != addr2)
 
89
            result = result * 2 + int(addr1 <  addr2)
 
90
            result = result * 2 + int(addr1 <= addr2)
 
91
            result = result * 2 + int(addr1 >  addr2)
 
92
            result = result * 2 + int(addr1 >= addr2)
 
93
        raw_free(addresses[0])
 
94
        return result
 
95
    fc = compile(f, [])
 
96
    res = fc()
 
97
    assert res == int('011100' * 2, 2)
 
98
 
 
99
def test_flavored_malloc_raw():
 
100
    class A(object):
 
101
        _alloc_flavor_ = "raw"
 
102
        def __init__(self, val):
 
103
            self.val = val
 
104
    def f(x):
 
105
        a = A(x + 1)
 
106
        result = a.val
 
107
        free_non_gc_object(a)
 
108
        return result
 
109
    fn = compile(f, [int])
 
110
    assert fn(1) == 2
 
111
 
 
112
def test_flavored_malloc_stack():
 
113
    class A(object):
 
114
        _alloc_flavor_ = "stack"
 
115
        def __init__(self, val):
 
116
            self.val = val
 
117
    def f(x):
 
118
        a = A(x + 1)
 
119
        result = a.val
 
120
        return result
 
121
    fn = compile(f, [int])
 
122
    assert fn(1) == 2
 
123
 
 
124
def test_weakaddress():
 
125
    from pypy.rlib.objectmodel import cast_object_to_weakgcaddress
 
126
    from pypy.rlib.objectmodel import cast_weakgcaddress_to_object
 
127
    from pypy.rpython.lltypesystem.lloperation import llop
 
128
    class A(object):
 
129
        pass
 
130
    def func(i):
 
131
        l1 = []
 
132
        l2 = []
 
133
        for i in range(i):
 
134
            a = A()
 
135
            l1.append(a)
 
136
            l2.append(cast_object_to_weakgcaddress(a))
 
137
        return len(l1) == len(l2)
 
138
    fn = compile(func, [int])
 
139
    assert fn(10)
 
140
 
 
141
def test_constant_weakaddress():
 
142
    from pypy.rlib.objectmodel import cast_object_to_weakgcaddress
 
143
    from pypy.rlib.objectmodel import cast_weakgcaddress_to_object
 
144
    from pypy.rpython.lltypesystem.lloperation import llop
 
145
    class A(object):
 
146
        pass
 
147
    constant_a = A()
 
148
    constant_weak_a = cast_object_to_weakgcaddress(constant_a)
 
149
    l = [constant_weak_a]
 
150
    def func(i):
 
151
        l1 = []
 
152
        l2 = []
 
153
        l3 = []
 
154
        l4 = []
 
155
        for i in range(i):
 
156
            a = A()
 
157
            l1.append(a)
 
158
            l2.append(cast_object_to_weakgcaddress(a))
 
159
            l3.append(constant_a)
 
160
            l4.extend(l)
 
161
        return len(l1) == len(l2) == len(l3) == len(l4)
 
162
    fn = compile(func, [int])
 
163
    assert fn(10)