| 1 |
import py, sys |
| 2 |
from rpython.rtyper.lltypesystem.llmemory import * |
| 3 |
from rpython.translator.c.test.test_genc import compile |
| 4 |
from rpython.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_memory_access_zero(): |
| 37 |
def f(): |
| 38 |
blocks = [] |
| 39 |
for i in range(1000): |
| 40 |
addr = raw_malloc(16, zero=False) |
| 41 |
addr.signed[1] = 10000 + i |
| 42 |
blocks.append(addr) |
| 43 |
for addr in blocks: |
| 44 |
raw_free(addr) |
| 45 |
result = 0 |
| 46 |
blocks = [] |
| 47 |
for i in range(1000): |
| 48 |
addr = raw_malloc(16, zero=True) |
| 49 |
result |= addr.signed[1] |
| 50 |
blocks.append(addr) |
| 51 |
for addr in blocks: |
| 52 |
raw_free(addr) |
| 53 |
return result |
| 54 |
fc = compile(f, []) |
| 55 |
res = fc() |
| 56 |
assert res == 0 |
| 57 |
|
| 58 |
def test_memory_float(): |
| 59 |
S = lltype.GcStruct("S", ("x", lltype.Float), ("y", lltype.Float)) |
| 60 |
offset = FieldOffset(S, 'x') |
| 61 |
offsety = FieldOffset(S, 'y') |
| 62 |
def f(value): |
| 63 |
s = lltype.malloc(S) |
| 64 |
s.x = 123.2 |
| 65 |
a = cast_ptr_to_adr(s) |
| 66 |
b = a + offset |
| 67 |
assert b.float[0] == 123.2 |
| 68 |
b.float[0] = 234.1 |
| 69 |
(a + offsety).float[0] = value |
| 70 |
assert s.x == 234.1 |
| 71 |
return s.x + value |
| 72 |
fc = compile(f, [float]) |
| 73 |
res = fc(42.42) |
| 74 |
assert res == f(42.42) |
| 75 |
|
| 76 |
def test_offset_inside_fixed_array(): |
| 77 |
S = lltype.FixedSizeArray(lltype.Signed, 10) |
| 78 |
offset = FieldOffset(S, 'item4') |
| 79 |
def f(value): |
| 80 |
s = lltype.malloc(S, flavor='raw') |
| 81 |
s[4] = value |
| 82 |
res = (cast_ptr_to_adr(s) + offset).signed[0] |
| 83 |
lltype.free(s, flavor='raw') |
| 84 |
return res |
| 85 |
fc = compile(f, [int]) |
| 86 |
res = fc(42) |
| 87 |
assert res == 42 |
| 88 |
|
| 89 |
def test_pointer_arithmetic(): |
| 90 |
def f(offset, char): |
| 91 |
char = chr(char) |
| 92 |
addr = raw_malloc(10000) |
| 93 |
same_offset = (addr + 2 * offset - offset) - addr |
| 94 |
addr.char[offset] = char |
| 95 |
result = (addr + same_offset).char[0] |
| 96 |
raw_free(addr) |
| 97 |
return result |
| 98 |
fc = compile(f, [int, int]) |
| 99 |
res = fc(10, ord("c")) |
| 100 |
assert res == "c" |
| 101 |
res = fc(12, ord("x")) |
| 102 |
assert res == "x" |
| 103 |
|
| 104 |
def test_pointer_arithmetic_inplace(): |
| 105 |
def f(offset, char): |
| 106 |
char = chr(char) |
| 107 |
addr = raw_malloc(10000) |
| 108 |
addr += offset |
| 109 |
addr.char[-offset] = char |
| 110 |
addr -= offset |
| 111 |
result = addr.char[0] |
| 112 |
raw_free(addr) |
| 113 |
return result |
| 114 |
fc = compile(f, [int, int]) |
| 115 |
res = fc(10, ord("c")) |
| 116 |
assert res == "c" |
| 117 |
|
| 118 |
def test_raw_memcopy(): |
| 119 |
def f(): |
| 120 |
addr = raw_malloc(100) |
| 121 |
addr.signed[0] = 12 |
| 122 |
(addr + 10).signed[0] = 42 |
| 123 |
(addr + 20).char[0] = "a" |
| 124 |
addr1 = raw_malloc(100) |
| 125 |
raw_memcopy(addr, addr1, 100) |
| 126 |
result = addr1.signed[0] == 12 |
| 127 |
result = result and (addr1 + 10).signed[0] == 42 |
| 128 |
result = result and (addr1 + 20).char[0] == "a" |
| 129 |
raw_free(addr) |
| 130 |
raw_free(addr1) |
| 131 |
return result |
| 132 |
fc = compile(f, []) |
| 133 |
res = fc() |
| 134 |
assert res |
| 135 |
|
| 136 |
def test_raw_memmove(): |
| 137 |
def f(): |
| 138 |
addr = raw_malloc(100) |
| 139 |
addr.signed[0] = 12 |
| 140 |
(addr + 10).signed[0] = 42 |
| 141 |
raw_memmove(addr, addr + 4, 96) |
| 142 |
result = (addr + 4).signed[0] + (addr + 14).signed[0] |
| 143 |
raw_free(addr) |
| 144 |
return result |
| 145 |
fc = compile(f, []) |
| 146 |
res = fc() |
| 147 |
assert res |
| 148 |
|
| 149 |
def test_pointer_comparison(): |
| 150 |
def f(): |
| 151 |
result = 0 |
| 152 |
addresses = [raw_malloc(1), NULL] |
| 153 |
for addr1 in addresses: |
| 154 |
addr2 = addr1 + 1 |
| 155 |
result = result * 2 + int(addr1 == addr2) |
| 156 |
result = result * 2 + int(addr1 != addr2) |
| 157 |
result = result * 2 + int(addr1 < addr2) |
| 158 |
result = result * 2 + int(addr1 <= addr2) |
| 159 |
result = result * 2 + int(addr1 > addr2) |
| 160 |
result = result * 2 + int(addr1 >= addr2) |
| 161 |
raw_free(addresses[0]) |
| 162 |
return result |
| 163 |
fc = compile(f, []) |
| 164 |
res = fc() |
| 165 |
assert res == int('011100' * 2, 2) |
| 166 |
|
| 167 |
def test_flavored_malloc_raw(): |
| 168 |
class A(object): |
| 169 |
_alloc_flavor_ = "raw" |
| 170 |
def __init__(self, val): |
| 171 |
self.val = val |
| 172 |
def f(x): |
| 173 |
a = A(x + 1) |
| 174 |
result = a.val |
| 175 |
free_non_gc_object(a) |
| 176 |
return result |
| 177 |
fn = compile(f, [int]) |
| 178 |
assert fn(1) == 2 |
| 179 |
|
| 180 |
def test_gcref(): |
| 181 |
if sys.platform == 'darwin': |
| 182 |
py.test.skip("'boehm' may crash") |
| 183 |
S = lltype.GcStruct("S", ("x", lltype.Signed)) |
| 184 |
s = lltype.malloc(S) |
| 185 |
s.x = 123 |
| 186 |
g1 = lltype.cast_opaque_ptr(GCREF, s) |
| 187 |
g2 = lltype.cast_opaque_ptr(GCREF, lltype.nullptr(S)) |
| 188 |
def f2(x): |
| 189 |
if x > 0: |
| 190 |
return g1 |
| 191 |
else: |
| 192 |
return g2 |
| 193 |
def f(x): |
| 194 |
gref = f2(x) |
| 195 |
g = lltype.cast_opaque_ptr(lltype.Ptr(S), gref) |
| 196 |
if g: |
| 197 |
return g.x |
| 198 |
else: |
| 199 |
return -42 |
| 200 |
fn = compile(f, [int], gcpolicy='boehm') |
| 201 |
assert fn(3) == 123 |
| 202 |
assert fn(-3) == -42 |
| 203 |
|
| 204 |
def test_cast_adr_to_int(): |
| 205 |
S = lltype.Struct("S", ("x", lltype.Signed)) |
| 206 |
s = lltype.malloc(S, immortal=True) |
| 207 |
adr = cast_ptr_to_adr(s) |
| 208 |
def f(n): |
| 209 |
i = cast_adr_to_int(adr) |
| 210 |
if n > 10: |
| 211 |
adr2 = adr |
| 212 |
else: |
| 213 |
adr2 = NULL |
| 214 |
print "hello world" |
| 215 |
j = cast_adr_to_int(adr2) |
| 216 |
return i - j |
| 217 |
fc = compile(f, [int]) |
| 218 |
res = fc(42) |
| 219 |
assert res == 0 |
| 220 |
|
| 221 |
def test_cast_int_to_adr(): |
| 222 |
S = lltype.Struct("S", ("x", lltype.Signed)) |
| 223 |
s = lltype.malloc(S, immortal=True) |
| 224 |
s.x = 42 |
| 225 |
integer = cast_adr_to_int(cast_ptr_to_adr(s), mode="symbolic") |
| 226 |
def f(n): |
| 227 |
if n > 1: |
| 228 |
i = integer |
| 229 |
else: |
| 230 |
i = 123 |
| 231 |
print "hello world" |
| 232 |
adr = cast_int_to_adr(i) |
| 233 |
s = cast_adr_to_ptr(adr, lltype.Ptr(S)) |
| 234 |
return s.x |
| 235 |
fc = compile(f, [int]) |
| 236 |
res = fc(5) |
| 237 |
assert res == 42 |
| 238 |
|
| 239 |
def test_dict_of_addresses(): |
| 240 |
TP = lltype.Struct('x') |
| 241 |
a = lltype.malloc(TP, flavor='raw', immortal=True) |
| 242 |
b = lltype.malloc(TP, flavor='raw', immortal=True) |
| 243 |
|
| 244 |
def f(i): |
| 245 |
d = {} |
| 246 |
d[cast_ptr_to_adr(a)] = 123 |
| 247 |
d[cast_ptr_to_adr(b)] = 456 |
| 248 |
if i > 5: |
| 249 |
key = cast_ptr_to_adr(a) |
| 250 |
else: |
| 251 |
key = cast_ptr_to_adr(b) |
| 252 |
return d[key] |
| 253 |
|
| 254 |
fc = compile(f, [int]) |
| 255 |
res = fc(-77) |
| 256 |
assert res == 456 |
| 257 |
res = fc(77) |
| 258 |
assert res == 123 |
| 259 |
|
| 260 |
def test_gcarray_length(): |
| 261 |
A = lltype.GcArray(lltype.Char) |
| 262 |
def f(): |
| 263 |
a = lltype.malloc(A, 117) |
| 264 |
p = lltype.cast_opaque_ptr(GCREF, a) |
| 265 |
return lltype.length_of_simple_gcarray_from_opaque(p) |
| 266 |
fc = compile(f, []) |
| 267 |
res = fc() |
| 268 |
assert res == 117 |