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

« back to all changes in this revision

Viewing changes to pypy/translator/pyrex/test/test_pyrextrans.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 autopath
 
2
import py
 
3
from pypy.tool.udir import udir
 
4
from pypy.translator.pyrex.genpyrex import GenPyrex
 
5
from pypy.objspace.flow.model import *
 
6
from pypy.translator.tool.cbuild import build_cfunc
 
7
from pypy.translator.tool.cbuild import make_module_from_pyxstring
 
8
from pypy.translator.translator import TranslationContext
 
9
from pypy.objspace.flow import FlowObjSpace
 
10
 
 
11
from pypy import conftest 
 
12
#from pypy.conftest import option
 
13
 
 
14
from pypy.translator.test import snippet 
 
15
 
 
16
# XXX this tries to make compiling faster for full-scale testing
 
17
from pypy.translator.tool import cbuild
 
18
cbuild.enable_fast_compilation()
 
19
 
 
20
 
 
21
class TestNoTypePyrexGenTestCase:
 
22
    def setup_class(cls): 
 
23
        cls.space = FlowObjSpace() 
 
24
 
 
25
    def build_cfunc(self, func):
 
26
        try: func = func.im_func
 
27
        except AttributeError: pass
 
28
 
 
29
        dot = conftest.option.verbose > 0 and 1 or 0 
 
30
        options = {
 
31
            'simplify' : 1,
 
32
            'dot' : dot,
 
33
            }
 
34
        return build_cfunc(func, **options)
 
35
 
 
36
    def test_simple_func(self):
 
37
        cfunc = self.build_cfunc(snippet.simple_func)
 
38
        assert cfunc(1) == 2
 
39
 
 
40
    def test_while_func(self):
 
41
        while_func = self.build_cfunc(snippet.while_func)
 
42
        assert while_func(10) == 55
 
43
 
 
44
    def test_nested_whiles(self):
 
45
        nested_whiles = self.build_cfunc(snippet.nested_whiles)
 
46
        assert nested_whiles(111, 114) == (
 
47
                          '...!...!...!...!...!')
 
48
 
 
49
    def test_my_contains(self):
 
50
        my_contains = self.build_cfunc(snippet.my_contains)
 
51
        assert my_contains([1, 2, 3], 1)
 
52
 
 
53
    def test_poor_man_range(self):
 
54
        poor_man_range = self.build_cfunc(snippet.poor_man_range)
 
55
        assert poor_man_range(10) == range(10)
 
56
 
 
57
    def poor_man_rev_range(self):
 
58
        poor_man_rev_range = self.build_cfunc(snippet.poor_man_rev_range)
 
59
        assert poor_man_rev_range(10) == range(9,-1,-1)
 
60
 
 
61
    def test_simple_id(self):
 
62
        #we just want to see, if renaming of parameter works correctly
 
63
        #if the first branch is the end branch
 
64
        simple_id = self.build_cfunc(snippet.simple_id)
 
65
        assert simple_id(9) == 9
 
66
 
 
67
    def test_branch_id(self):
 
68
        branch_id = self.build_cfunc(snippet.branch_id)
 
69
        assert branch_id(1, 2, 3) == 2
 
70
        assert branch_id(0, 2, 3) == 3
 
71
 
 
72
    def test_int_id(self):
 
73
        int_id = self.build_cfunc(snippet.int_id)
 
74
        assert int_id(3) == 3
 
75
 
 
76
    def dont_test_attrs(self):
 
77
        attrs = self.build_cfunc(snippet.attrs)
 
78
        assert attrs() == 9
 
79
 
 
80
    def test_builtinusage(self):
 
81
        fun = self.build_cfunc(snippet.builtinusage)
 
82
        assert fun() == 4
 
83
 
 
84
    def test_sieve(self):
 
85
        sieve = self.build_cfunc(snippet.sieve_of_eratosthenes)
 
86
        assert sieve() == 1028
 
87
 
 
88
    def test_slice(self):
 
89
        half = self.build_cfunc(snippet.half_of_n)
 
90
        assert half(10) == 5
 
91
 
 
92
    def test_poly_branch(self):
 
93
        poly_branch = self.build_cfunc(snippet.poly_branch)
 
94
        assert poly_branch(10) == [1,2,3]*2
 
95
        assert poly_branch(0) == ['a','b','c']*2
 
96
 
 
97
    def test_and(self):
 
98
        sand = self.build_cfunc(snippet.s_and)
 
99
        assert sand(5, 6) == "yes"
 
100
        assert sand(5, 0) == "no"
 
101
        assert sand(0, 6) == "no"
 
102
        assert sand(0, 0) == "no"
 
103
 
 
104
# -- the following test doesn't really work right now --
 
105
##    def test_call_very_complex(self):
 
106
##        call_very_complex = self.build_cfunc(snippet.call_very_complex,
 
107
##                                             snippet.default_args)
 
108
##        assert call_very_complex(5, (3,), {}) == -12
 
109
##        assert call_very_complex(5, (), {'y': 3}) == -12
 
110
##        py.test.raises("call_very_complex(5, (3,), {'y': 4})")
 
111
 
 
112
 
 
113
class TestTypedTestCase:
 
114
 
 
115
    def getcompiled(self, func):
 
116
        t = TranslationContext() 
 
117
        # builds starting-types from func_defs 
 
118
        argstypelist = []
 
119
        if func.func_defaults:
 
120
            for spec in func.func_defaults:
 
121
                if isinstance(spec, tuple):
 
122
                    spec = spec[0] # use the first type only for the tests
 
123
                argstypelist.append(spec)
 
124
        t.buildannotator().build_types(func, argstypelist) 
 
125
        name = func.func_name
 
126
 
 
127
        blobs = []
 
128
        for graph in t.graphs:
 
129
            g = GenPyrex(graph)
 
130
            g.by_the_way_the_function_was = graph.func   # XXX
 
131
            g.setannotator(t.annotator)
 
132
            blobs.append(g.emitcode())
 
133
        code = g.globaldeclarations()  # any 'g' is fine here...
 
134
        if code:
 
135
            blobs.insert(0, code)
 
136
        pyxcode = '\n\n#_________________\n\n'.join(blobs)
 
137
 
 
138
        mod = make_module_from_pyxstring(name, udir, pyxcode)
 
139
        return getattr(mod, name)
 
140
 
 
141
    def test_set_attr(self):
 
142
        set_attr = self.getcompiled(snippet.set_attr)
 
143
        assert set_attr() == 2
 
144
 
 
145
    def test_inheritance2(self):
 
146
        inheritance2 = self.getcompiled(snippet.inheritance2)
 
147
        assert inheritance2() == ((-12, -12), (3, "world"))
 
148
 
 
149
    def test_factorial2(self):
 
150
        factorial2 = self.getcompiled(snippet.factorial2)
 
151
        assert factorial2(5) == 120
 
152
 
 
153
    def test_factorial(self):
 
154
        factorial = self.getcompiled(snippet.factorial)
 
155
        assert factorial(5) == 120
 
156
 
 
157
    def test_simple_method(self):
 
158
        simple_method = self.getcompiled(snippet.simple_method)
 
159
        assert simple_method(55) == 55
 
160
 
 
161
    def test_sieve_of_eratosthenes(self):
 
162
        sieve_of_eratosthenes = self.getcompiled(snippet.sieve_of_eratosthenes)
 
163
        assert sieve_of_eratosthenes() == 1028
 
164
 
 
165
    def test_nested_whiles(self):
 
166
        nested_whiles = self.getcompiled(snippet.nested_whiles)
 
167
        assert nested_whiles(5,3) == '!!!!!'
 
168
 
 
169
    def test_call_five(self):
 
170
        call_five = self.getcompiled(snippet.call_five)
 
171
        assert call_five() == [5]