~ubuntu-branches/ubuntu/karmic/pypy/karmic

« back to all changes in this revision

Viewing changes to pypy/rpython/rctypes/test/test_rcarithmetic.py

  • Committer: Bazaar Package Importer
  • Author(s): Alexandre Fayolle
  • Date: 2007-04-13 09:33:09 UTC
  • Revision ID: james.westby@ubuntu.com-20070413093309-yoojh4jcoocu2krz
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import py.test
 
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
 
10
 
 
11
def specialize(func, types):
 
12
    t = TranslationContext()
 
13
    t.buildannotator().build_types(func, types)
 
14
    t.buildrtyper().specialize()
 
15
    t.checkgraphs()    
 
16
 
 
17
def test_signedness():
 
18
    assert rcbyte(-1) < 0
 
19
    assert rcubyte(-1) > 0
 
20
 
 
21
def test_promotion():
 
22
    assert type(rcbyte(1) + 1) is rcbyte
 
23
    assert type(1 + rcbyte(1)) is rcbyte
 
24
    
 
25
    assert type(rcbyte(1) + rcshort(1)) is rcshort
 
26
    assert type(rcshort(1) + rcbyte(1)) is rcshort
 
27
 
 
28
    assert type(rcubyte(1) + rcshort(1)) is rcushort
 
29
    assert type(rcshort(1) + rcubyte(1)) is rcushort
 
30
 
 
31
    
 
32
def test_typeof():
 
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
 
39
 
 
40
    assert lltype.typeOf(rcbyte(0)) == CByte
 
41
    assert lltype.typeOf(rcshort(0)) == CShort
 
42
 
 
43
    assert lltype.typeOf(rcushort(0)) == CUShort
 
44
 
 
45
inttypes = [rcbyte, rcubyte, rcshort, rcushort, rcint, rcuint,
 
46
            rclong, rculong, rclonglong, rculonglong]
 
47
 
 
48
def test_annotate():
 
49
    for inttype in inttypes:
 
50
        c = inttype()
 
51
        def f():
 
52
            return c
 
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)
 
58
 
 
59
    for inttype in inttypes:
 
60
        def f():
 
61
            return inttype(0)
 
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)
 
67
 
 
68
    for inttype in inttypes:
 
69
        def f(x):
 
70
            return x
 
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)
 
76
 
 
77
def test_specialize():
 
78
    for inttype in inttypes:
 
79
        c = inttype()
 
80
        def f():
 
81
            return c
 
82
        res = interpret(f, [])
 
83
        assert res == f()
 
84
        assert lltype.typeOf(res) == lltype.build_number(None, inttype)
 
85
 
 
86
    for inttype in inttypes:
 
87
        def f():
 
88
            return inttype(0)
 
89
        res = interpret(f, [])
 
90
        assert res == f()
 
91
        assert lltype.typeOf(res) == lltype.build_number(None, inttype)        
 
92
 
 
93
    for inttype in inttypes:
 
94
        def f(x):
 
95
            return x
 
96
        res = interpret(f, [inttype(0)])
 
97
        assert res == f(inttype(0))
 
98
        assert lltype.typeOf(res) == lltype.build_number(None, inttype)
 
99
 
 
100
        
 
101
def test_unsupported_op():
 
102
    def f(x, y):
 
103
        return x + y
 
104
 
 
105
    py.test.raises(TyperError, specialize, f, [rcbyte, rcbyte])
 
106