2
from pypy.rpython.rctypes.rcarithmetic import *
3
from pypy.rpython.lltypesystem import lltype
4
from pypy.rlib import rarithmetic
5
from pypy.annotation import model as annmodel
6
from pypy.annotation.annrpython import RPythonAnnotator
7
from pypy.rpython.test.test_llinterp import interpret
8
from pypy.rpython.error import TyperError
9
from pypy.translator.translator import TranslationContext
11
def specialize(func, types):
12
t = TranslationContext()
13
t.buildannotator().build_types(func, types)
14
t.buildrtyper().specialize()
17
def test_signedness():
19
assert rcubyte(-1) > 0
22
assert type(rcbyte(1) + 1) is rcbyte
23
assert type(1 + rcbyte(1)) is rcbyte
25
assert type(rcbyte(1) + rcshort(1)) is rcshort
26
assert type(rcshort(1) + rcbyte(1)) is rcshort
28
assert type(rcubyte(1) + rcshort(1)) is rcushort
29
assert type(rcshort(1) + rcubyte(1)) is rcushort
33
assert lltype.typeOf(rarithmetic.r_int(0)) == lltype.Signed
34
assert lltype.typeOf(rclong(0)) == lltype.Signed
35
assert lltype.Signed == CLong
36
assert lltype.typeOf(rarithmetic.r_uint(0)) == lltype.Unsigned
37
assert lltype.typeOf(rculong(0)) == lltype.Unsigned
38
assert lltype.Unsigned == CULong
40
assert lltype.typeOf(rcbyte(0)) == CByte
41
assert lltype.typeOf(rcshort(0)) == CShort
43
assert lltype.typeOf(rcushort(0)) == CUShort
45
inttypes = [rcbyte, rcubyte, rcshort, rcushort, rcint, rcuint,
46
rclong, rculong, rclonglong, rculonglong]
49
for inttype in inttypes:
53
a = RPythonAnnotator()
54
s = a.build_types(f, [])
55
assert isinstance(s, annmodel.SomeInteger)
56
assert s.knowntype == inttype
57
assert s.unsigned == (inttype(-1) > 0)
59
for inttype in inttypes:
62
a = RPythonAnnotator()
63
s = a.build_types(f, [])
64
assert isinstance(s, annmodel.SomeInteger)
65
assert s.knowntype == inttype
66
assert s.unsigned == (inttype(-1) > 0)
68
for inttype in inttypes:
71
a = RPythonAnnotator()
72
s = a.build_types(f, [inttype])
73
assert isinstance(s, annmodel.SomeInteger)
74
assert s.knowntype == inttype
75
assert s.unsigned == (inttype(-1) > 0)
77
def test_specialize():
78
for inttype in inttypes:
82
res = interpret(f, [])
84
assert lltype.typeOf(res) == lltype.build_number(None, inttype)
86
for inttype in inttypes:
89
res = interpret(f, [])
91
assert lltype.typeOf(res) == lltype.build_number(None, inttype)
93
for inttype in inttypes:
96
res = interpret(f, [inttype(0)])
97
assert res == f(inttype(0))
98
assert lltype.typeOf(res) == lltype.build_number(None, inttype)
101
def test_unsupported_op():
105
py.test.raises(TyperError, specialize, f, [rcbyte, rcbyte])