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
11
from pypy import conftest
12
#from pypy.conftest import option
14
from pypy.translator.test import snippet
16
# XXX this tries to make compiling faster for full-scale testing
17
from pypy.translator.tool import cbuild
18
cbuild.enable_fast_compilation()
21
class TestNoTypePyrexGenTestCase:
23
cls.space = FlowObjSpace()
25
def build_cfunc(self, func):
26
try: func = func.im_func
27
except AttributeError: pass
29
dot = conftest.option.verbose > 0 and 1 or 0
34
return build_cfunc(func, **options)
36
def test_simple_func(self):
37
cfunc = self.build_cfunc(snippet.simple_func)
40
def test_while_func(self):
41
while_func = self.build_cfunc(snippet.while_func)
42
assert while_func(10) == 55
44
def test_nested_whiles(self):
45
nested_whiles = self.build_cfunc(snippet.nested_whiles)
46
assert nested_whiles(111, 114) == (
47
'...!...!...!...!...!')
49
def test_my_contains(self):
50
my_contains = self.build_cfunc(snippet.my_contains)
51
assert my_contains([1, 2, 3], 1)
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)
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)
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
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
72
def test_int_id(self):
73
int_id = self.build_cfunc(snippet.int_id)
76
def dont_test_attrs(self):
77
attrs = self.build_cfunc(snippet.attrs)
80
def test_builtinusage(self):
81
fun = self.build_cfunc(snippet.builtinusage)
85
sieve = self.build_cfunc(snippet.sieve_of_eratosthenes)
86
assert sieve() == 1028
89
half = self.build_cfunc(snippet.half_of_n)
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
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"
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})")
113
class TestTypedTestCase:
115
def getcompiled(self, func):
116
t = TranslationContext()
117
# builds starting-types from func_defs
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
128
for graph in t.graphs:
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...
135
blobs.insert(0, code)
136
pyxcode = '\n\n#_________________\n\n'.join(blobs)
138
mod = make_module_from_pyxstring(name, udir, pyxcode)
139
return getattr(mod, name)
141
def test_set_attr(self):
142
set_attr = self.getcompiled(snippet.set_attr)
143
assert set_attr() == 2
145
def test_inheritance2(self):
146
inheritance2 = self.getcompiled(snippet.inheritance2)
147
assert inheritance2() == ((-12, -12), (3, "world"))
149
def test_factorial2(self):
150
factorial2 = self.getcompiled(snippet.factorial2)
151
assert factorial2(5) == 120
153
def test_factorial(self):
154
factorial = self.getcompiled(snippet.factorial)
155
assert factorial(5) == 120
157
def test_simple_method(self):
158
simple_method = self.getcompiled(snippet.simple_method)
159
assert simple_method(55) == 55
161
def test_sieve_of_eratosthenes(self):
162
sieve_of_eratosthenes = self.getcompiled(snippet.sieve_of_eratosthenes)
163
assert sieve_of_eratosthenes() == 1028
165
def test_nested_whiles(self):
166
nested_whiles = self.getcompiled(snippet.nested_whiles)
167
assert nested_whiles(5,3) == '!!!!!'
169
def test_call_five(self):
170
call_five = self.getcompiled(snippet.call_five)
171
assert call_five() == [5]