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
11
def test_convert_to_bool():
17
fc = compile(f, [int])
19
assert isinstance(res, bool) and not res
21
assert isinstance(res, bool) and res
23
def test_memory_access():
26
addr.signed[0] = value
27
result = addr.signed[0]
30
fc = compile(f, [int])
36
def test_pointer_arithmetic():
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]
44
fc = compile(f, [int, SomeChar()])
50
def test_pointer_arithmetic_inplace():
52
addr = raw_malloc(10000)
54
addr.char[-offset] = char
59
fc = compile(f, [int, SomeChar()])
63
def test_raw_memcopy():
65
addr = raw_malloc(100)
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"
81
def test_pointer_comparison():
84
addresses = [raw_malloc(1), NULL]
85
for addr1 in addresses:
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])
97
assert res == int('011100' * 2, 2)
99
def test_flavored_malloc_raw():
101
_alloc_flavor_ = "raw"
102
def __init__(self, val):
107
free_non_gc_object(a)
109
fn = compile(f, [int])
112
def test_flavored_malloc_stack():
114
_alloc_flavor_ = "stack"
115
def __init__(self, val):
121
fn = compile(f, [int])
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
136
l2.append(cast_object_to_weakgcaddress(a))
137
return len(l1) == len(l2)
138
fn = compile(func, [int])
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
148
constant_weak_a = cast_object_to_weakgcaddress(constant_a)
149
l = [constant_weak_a]
158
l2.append(cast_object_to_weakgcaddress(a))
159
l3.append(constant_a)
161
return len(l1) == len(l2) == len(l3) == len(l4)
162
fn = compile(func, [int])