| 1 |
import py.test |
| 2 |
from rpython.annotator.model import ( |
| 3 |
SomeInteger, SomeBool, SomeChar, union, SomeImpossibleValue, |
| 4 |
UnionError, SomeInstance, SomeSingleFloat) |
| 5 |
from rpython.rlib.rarithmetic import r_uint, r_singlefloat |
| 6 |
from rpython.rtyper.llannotation import ( |
| 7 |
SomePtr, annotation_to_lltype, ll_to_annotation) |
| 8 |
from rpython.rtyper.lltypesystem import lltype |
| 9 |
import rpython.rtyper.rtyper |
| 10 |
|
| 11 |
class C(object): |
| 12 |
pass |
| 13 |
|
| 14 |
class DummyClassDef: |
| 15 |
def __init__(self, cls=C): |
| 16 |
self.cls = cls |
| 17 |
self.name = cls.__name__ |
| 18 |
self.classdesc = cls |
| 19 |
|
| 20 |
def test_ll_to_annotation(): |
| 21 |
s_z = ll_to_annotation(lltype.Signed._defl()) |
| 22 |
s_s = SomeInteger() |
| 23 |
s_u = SomeInteger(nonneg=True, unsigned=True) |
| 24 |
assert s_z.contains(s_s) |
| 25 |
assert not s_z.contains(s_u) |
| 26 |
s_uz = ll_to_annotation(lltype.Unsigned._defl()) |
| 27 |
assert s_uz.contains(s_u) |
| 28 |
assert ll_to_annotation(lltype.Bool._defl()).contains(SomeBool()) |
| 29 |
assert ll_to_annotation(lltype.Char._defl()).contains(SomeChar()) |
| 30 |
S = lltype.GcStruct('s') |
| 31 |
A = lltype.GcArray() |
| 32 |
s_p = ll_to_annotation(lltype.malloc(S)) |
| 33 |
assert isinstance(s_p, SomePtr) and s_p.ll_ptrtype == lltype.Ptr(S) |
| 34 |
s_p = ll_to_annotation(lltype.malloc(A, 0)) |
| 35 |
assert isinstance(s_p, SomePtr) and s_p.ll_ptrtype == lltype.Ptr(A) |
| 36 |
|
| 37 |
def test_annotation_to_lltype(): |
| 38 |
s_i = SomeInteger() |
| 39 |
s_pos = SomeInteger(nonneg=True) |
| 40 |
s_1 = SomeInteger(nonneg=True) |
| 41 |
s_1.const = 1 |
| 42 |
s_m1 = SomeInteger(nonneg=False) |
| 43 |
s_m1.const = -1 |
| 44 |
s_u = SomeInteger(nonneg=True, unsigned=True) |
| 45 |
s_u1 = SomeInteger(nonneg=True, unsigned=True) |
| 46 |
s_u1.const = r_uint(1) |
| 47 |
assert annotation_to_lltype(s_i) == lltype.Signed |
| 48 |
assert annotation_to_lltype(s_pos) == lltype.Signed |
| 49 |
assert annotation_to_lltype(s_1) == lltype.Signed |
| 50 |
assert annotation_to_lltype(s_m1) == lltype.Signed |
| 51 |
assert annotation_to_lltype(s_u) == lltype.Unsigned |
| 52 |
assert annotation_to_lltype(s_u1) == lltype.Unsigned |
| 53 |
assert annotation_to_lltype(SomeBool()) == lltype.Bool |
| 54 |
assert annotation_to_lltype(SomeChar()) == lltype.Char |
| 55 |
PS = lltype.Ptr(lltype.GcStruct('s')) |
| 56 |
s_p = SomePtr(ll_ptrtype=PS) |
| 57 |
assert annotation_to_lltype(s_p) == PS |
| 58 |
si0 = SomeInstance(DummyClassDef(), True) |
| 59 |
with py.test.raises(ValueError): |
| 60 |
annotation_to_lltype(si0) |
| 61 |
s_singlefloat = SomeSingleFloat() |
| 62 |
s_singlefloat.const = r_singlefloat(0.0) |
| 63 |
assert annotation_to_lltype(s_singlefloat) == lltype.SingleFloat |
| 64 |
|
| 65 |
def test_ll_union(): |
| 66 |
PS1 = lltype.Ptr(lltype.GcStruct('s')) |
| 67 |
PS2 = lltype.Ptr(lltype.GcStruct('s')) |
| 68 |
PS3 = lltype.Ptr(lltype.GcStruct('s3')) |
| 69 |
PA1 = lltype.Ptr(lltype.GcArray()) |
| 70 |
PA2 = lltype.Ptr(lltype.GcArray()) |
| 71 |
|
| 72 |
assert union(SomePtr(PS1), SomePtr(PS1)) == SomePtr(PS1) |
| 73 |
assert union(SomePtr(PS1), SomePtr(PS2)) == SomePtr(PS2) |
| 74 |
assert union(SomePtr(PS1), SomePtr(PS2)) == SomePtr(PS1) |
| 75 |
|
| 76 |
assert union(SomePtr(PA1), SomePtr(PA1)) == SomePtr(PA1) |
| 77 |
assert union(SomePtr(PA1), SomePtr(PA2)) == SomePtr(PA2) |
| 78 |
assert union(SomePtr(PA1), SomePtr(PA2)) == SomePtr(PA1) |
| 79 |
|
| 80 |
assert union(SomePtr(PS1), SomeImpossibleValue()) == SomePtr(PS1) |
| 81 |
assert union(SomeImpossibleValue(), SomePtr(PS1)) == SomePtr(PS1) |
| 82 |
|
| 83 |
with py.test.raises(UnionError): |
| 84 |
union(SomePtr(PA1), SomePtr(PS1)) |
| 85 |
with py.test.raises(UnionError): |
| 86 |
union(SomePtr(PS1), SomePtr(PS3)) |
| 87 |
with py.test.raises(UnionError): |
| 88 |
union(SomePtr(PS1), SomeInteger()) |
| 89 |
with py.test.raises(UnionError): |
| 90 |
union(SomeInteger(), SomePtr(PS1)) |