| 1 |
from rpython.rlib.rrawarray import copy_list_to_raw_array, \ |
| 2 |
populate_list_from_raw_array |
| 3 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 4 |
from rpython.rtyper.test.tool import BaseRtypingTest |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
class TestRArray(BaseRtypingTest): |
| 9 |
|
| 10 |
def test_copy_list_to_raw_array(self): |
| 11 |
ARRAY = rffi.CArray(lltype.Signed) |
| 12 |
buf = lltype.malloc(ARRAY, 4, flavor='raw') |
| 13 |
lst = [1, 2, 3, 4] |
| 14 |
copy_list_to_raw_array(lst, buf) |
| 15 |
for i in range(4): |
| 16 |
assert buf[i] == i+1 |
| 17 |
lltype.free(buf, flavor='raw') |
| 18 |
|
| 19 |
|
| 20 |
def test_copy_list_to_raw_array_rtyped(self): |
| 21 |
INTARRAY = rffi.CArray(lltype.Signed) |
| 22 |
FLOATARRAY = rffi.CArray(lltype.Float) |
| 23 |
def fn(): |
| 24 |
buf = lltype.malloc(INTARRAY, 3, flavor='raw') |
| 25 |
lst = [1, 2, 3] |
| 26 |
copy_list_to_raw_array(lst, buf) |
| 27 |
for i in range(3): |
| 28 |
assert buf[i] == lst[i] |
| 29 |
|
| 30 |
buf2 = lltype.malloc(FLOATARRAY, 3, flavor='raw') |
| 31 |
lst = [1.1, 2.2, 3.3] |
| 32 |
copy_list_to_raw_array(lst, buf2) |
| 33 |
for i in range(3): |
| 34 |
assert buf2[i] == lst[i] |
| 35 |
|
| 36 |
lltype.free(buf, flavor='raw') |
| 37 |
lltype.free(buf2, flavor='raw') |
| 38 |
self.interpret(fn, []) |
| 39 |
|
| 40 |
def test_new_list_from_raw_array(self): |
| 41 |
INTARRAY = rffi.CArray(lltype.Signed) |
| 42 |
buf = lltype.malloc(INTARRAY, 4, flavor='raw') |
| 43 |
buf[0] = 1 |
| 44 |
buf[1] = 2 |
| 45 |
buf[2] = 3 |
| 46 |
buf[3] = 4 |
| 47 |
lst = [] |
| 48 |
populate_list_from_raw_array(lst, buf, 4) |
| 49 |
assert lst == [1, 2, 3, 4] |
| 50 |
lltype.free(buf, flavor='raw') |
| 51 |
|
| 52 |
def test_new_list_from_raw_array_rtyped(self): |
| 53 |
INTARRAY = rffi.CArray(lltype.Signed) |
| 54 |
def fn(): |
| 55 |
buf = lltype.malloc(INTARRAY, 4, flavor='raw') |
| 56 |
buf[0] = 1 |
| 57 |
buf[1] = 2 |
| 58 |
buf[2] = 3 |
| 59 |
buf[3] = 4 |
| 60 |
lst = [] |
| 61 |
populate_list_from_raw_array(lst, buf, 4) |
| 62 |
assert lst == [1, 2, 3, 4] |
| 63 |
lltype.free(buf, flavor='raw') |
| 64 |
|
| 65 |
self.interpret(fn, []) |