5
from py.test import raises
7
from pypy import conftest
8
from pypy.translator.test import snippet
9
from pypy.translator.translator import TranslationContext
10
from pypy.rlib.rarithmetic import r_uint, r_ulonglong, r_longlong, intmask
12
# XXX this tries to make compiling faster for full-scale testing
13
from pypy.translator.tool import cbuild
14
cbuild.enable_fast_compilation()
17
class CompilationTestCase:
19
def annotatefunc(self, func, argtypes=None):
20
from pypy.config.pypyoption import get_pypy_config
21
config = get_pypy_config(translating=True)
22
config.translation.gc = "ref"
23
config.translation.simplifying = True
24
t = TranslationContext(config=config)
27
a = t.buildannotator()
28
a.build_types(func, argtypes)
32
def compilefunc(self, t, func):
33
from pypy.translator.c import genc
34
builder = genc.CExtModuleBuilder(t, func, config=t.config)
35
builder.generate_source()
37
builder.import_module()
38
return builder.get_entry_point()
40
def getcompiled(self, func, argtypes=None, view=False):
41
from pypy.translator.transform import insert_ll_stackcheck
42
t = self.annotatefunc(func, argtypes)
44
if view or conftest.option.view:
47
insert_ll_stackcheck(t)
48
return self.compilefunc(t, func)
51
t.buildrtyper().specialize()
52
#raisingop2direct_call(t)
55
class TestTypedTestCase(CompilationTestCase):
57
def test_set_attr(self):
58
set_attr = self.getcompiled(snippet.set_attr)
59
assert set_attr() == 2
61
def test_inheritance2(self):
62
inheritance2 = self.getcompiled(snippet.inheritance2)
63
assert inheritance2() == ((-12, -12), (3, "world"))
65
def test_factorial2(self):
66
factorial2 = self.getcompiled(snippet.factorial2, [int])
67
assert factorial2(5) == 120
69
def test_factorial(self):
70
factorial = self.getcompiled(snippet.factorial, [int])
71
assert factorial(5) == 120
73
def test_simple_method(self):
74
simple_method = self.getcompiled(snippet.simple_method, [int])
75
assert simple_method(55) == 55
77
def test_sieve_of_eratosthenes(self):
78
sieve_of_eratosthenes = self.getcompiled(snippet.sieve_of_eratosthenes)
79
assert sieve_of_eratosthenes() == 1028
81
def test_nested_whiles(self):
82
nested_whiles = self.getcompiled(snippet.nested_whiles, [int, int])
83
assert nested_whiles(5,3) == '!!!!!'
85
def test_call_five(self):
86
call_five = self.getcompiled(snippet.call_five, [int])
90
def test_call_unpack_56(self):
91
call_unpack_56 = self.getcompiled(snippet.call_unpack_56, [])
92
result = call_unpack_56()
93
assert result == (2, 5, 6)
95
def test_class_defaultattr(self):
98
def class_defaultattr():
102
fn = self.getcompiled(class_defaultattr)
103
assert fn() == "hello world"
105
def test_tuple_repr(self):
106
def tuple_repr(x, y):
111
fn = self.getcompiled(tuple_repr, [int, str])
112
assert fn(6,'a') == (6,'a')
114
def test_classattribute(self):
115
fn = self.getcompiled(snippet.classattribute, [int])
120
assert fn(5) == 101112
122
def test_get_set_del_slice(self):
123
fn = self.getcompiled(snippet.get_set_del_slice, [list])
124
l = list('abcdefghij')
126
assert l == [3, 'c', 8, 11, 'h', 9]
127
assert result == ([3, 'c'], [9], [11, 'h'])
129
def test_type_conversion(self):
130
# obfuscated test case specially for typer.insert_link_conversions()
131
def type_conversion(n):
138
fn = self.getcompiled(type_conversion, [int])
141
assert abs(fn(7) + 0.8584) < 1E-5
143
def test_do_try_raise_choose(self):
144
fn = self.getcompiled(snippet.try_raise_choose, [int])
148
assert result == [-1,0,1,2]
150
def test_is_perfect_number(self):
151
fn = self.getcompiled(snippet.is_perfect_number, [int])
152
for i in range(1, 33):
154
assert perfect is (i in (6,28))
156
def test_prime(self):
157
fn = self.getcompiled(snippet.prime, [int])
158
result = [fn(i) for i in range(1, 21)]
159
assert result == [False, True, True, False, True, False, True, False,
160
False, False, True, False, True, False, False, False,
161
True, False, True, False]
163
def test_mutate_global(self):
166
g1 = Stuff(); g1.value = 1
167
g2 = Stuff(); g2.value = 2
168
g3 = Stuff(); g3.value = 3
176
return g3.next.next.value
177
fn = self.getcompiled(do_things)
180
def test_float_ops(self):
182
return abs((-x) ** 3 + 1)
183
fn = self.getcompiled(f, [float])
184
assert fn(-4.5) == 92.125
185
assert fn(4.5) == 90.125
187
def test_memoryerror(self):
192
fn = self.getcompiled(f, [int])
195
py.test.raises(MemoryError, fn, sys.maxint//2+1)
196
py.test.raises(MemoryError, fn, sys.maxint)
201
return 'Yes ' + chr(x)
204
fn = self.getcompiled(f, [int])
205
assert fn(65) == 'Yes A'
206
assert fn(256) == 'No'
207
assert fn(-1) == 'No'
209
def test_unichr(self):
212
return ord(unichr(x))
215
fn = self.getcompiled(f, [int])
217
assert fn(-12) == -42
218
assert fn(sys.maxint) == -42
220
def test_list_indexerror(self):
228
fn = self.getcompiled(f, [int])
234
def test_long_long(self):
237
fn = self.getcompiled(f, [r_ulonglong], view=False)
238
assert fn(sys.maxint) == 4*sys.maxint
242
gn = self.getcompiled(g, [r_longlong], view=False)
243
assert gn(sys.maxint) == 4*sys.maxint
245
def test_specializing_int_functions(self):
248
f._annspecialcase_ = "specialize:argtype(0)"
251
return f(r_longlong(0))
255
fn = self.getcompiled(g, [int])
259
def test_downcast_int(self):
262
fn = self.getcompiled(f, [r_longlong])
265
def test_function_ptr(self):
276
fn = self.getcompiled(g, [int])
280
def test_call_five(self):
281
# -- the result of call_five() isn't a real list, but an rlist
282
# that can't be converted to a PyListObject
284
lst = snippet.call_five()
285
return len(lst), lst[0]
286
call_five = self.getcompiled(wrapper)
288
assert result == (1, 5)
290
def test_get_set_del_slice(self):
291
def get_set_del_nonneg_slice(): # no neg slices for now!
292
l = [ord('a'), ord('b'), ord('c'), ord('d'), ord('e'), ord('f'), ord('g'), ord('h'), ord('i'), ord('j')]
301
#l[bound:] = [9] no setting slice into lists for now
303
l[0], l[-1], l[2], l[3] =3, 9, 8, 11
308
return (len(l), l[0], l[1], l[2], l[3], l[4], l[5],
309
len(list_3_c), list_3_c[0], list_3_c[1],
310
len(list_9), list_9[0],
311
len(list_11_h), list_11_h[0], list_11_h[1])
312
fn = self.getcompiled(get_set_del_nonneg_slice)
314
assert result == (6, 3, ord('c'), 8, 11, ord('h'), 9,
323
fn = self.getcompiled(testfn)
325
assert result is True
329
fn = self.getcompiled(testfn)
331
assert result is False
333
def test_str_compare(self):
336
s2 = ['one', 'two', 'o', 'on', 'twos', 'foobar']
337
return s1[i] == s2[j]
338
fn = self.getcompiled(testfn, [int, int])
342
assert res is testfn(i, j)
346
s2 = ['one', 'two', 'o', 'on', 'twos', 'foobar']
347
return s1[i] != s2[j]
348
fn = self.getcompiled(testfn, [int, int])
352
assert res is testfn(i, j)
356
s2 = ['one', 'two', 'o', 'on', 'twos', 'foobar']
358
fn = self.getcompiled(testfn, [int, int])
362
assert res is testfn(i, j)
366
s2 = ['one', 'two', 'o', 'on', 'twos', 'foobar']
367
return s1[i] <= s2[j]
368
fn = self.getcompiled(testfn, [int, int])
372
assert res is testfn(i, j)
376
s2 = ['one', 'two', 'o', 'on', 'twos', 'foobar']
378
fn = self.getcompiled(testfn, [int, int])
382
assert res is testfn(i, j)
386
s2 = ['one', 'two', 'o', 'on', 'twos', 'foobar']
387
return s1[i] >= s2[j]
388
fn = self.getcompiled(testfn, [int, int])
392
assert res is testfn(i, j)
394
def test_str_methods(self):
397
s2 = ['one', 'two', 'o', 'on', 'ne', 'e', 'twos', 'foobar', 'fortytwo']
398
return s1[i].startswith(s2[j])
399
fn = self.getcompiled(testfn, [int, int])
403
assert res is testfn(i, j)
406
s2 = ['one', 'two', 'o', 'on', 'ne', 'e', 'twos', 'foobar', 'fortytwo']
407
return s1[i].endswith(s2[j])
408
fn = self.getcompiled(testfn, [int, int])
412
assert res is testfn(i, j)
414
def test_str_join(self):
416
s1 = [ '', ',', ' and ']
417
s2 = [ [], ['foo'], ['bar', 'baz', 'bazz']]
418
return s1[i].join(s2[j])
419
fn = self.getcompiled(testfn, [int, int])
423
assert res == fn(i, j)
425
def test_unichr_eq(self):
426
l = list(u'Hello world')
429
fn = self.getcompiled(f, [int, int])
430
for i in range(len(l)):
431
for j in range(len(l)):
435
def test_unichr_ne(self):
436
l = list(u'Hello world')
439
fn = self.getcompiled(f, [int, int])
440
for i in range(len(l)):
441
for j in range(len(l)):
445
def test_unichr_ord(self):
446
l = list(u'Hello world')
449
fn = self.getcompiled(f, [int])
450
for i in range(len(l)):
454
def test_unichr_unichr(self):
455
l = list(u'Hello world')
457
return l[i] == unichr(j)
458
fn = self.getcompiled(f, [int, int])
459
for i in range(len(l)):
460
for j in range(len(l)):
461
res = fn(i, ord(l[j]))
462
assert res == f(i, ord(l[j]))
464
def test_int_overflow(self):
465
fn = self.getcompiled(snippet.add_func, [int])
466
raises(OverflowError, fn, sys.maxint)
468
def test_int_floordiv_ovf_zer(self):
469
fn = self.getcompiled(snippet.div_func, [int])
470
raises(OverflowError, fn, -1)
471
raises(ZeroDivisionError, fn, 0)
473
def test_int_mul_ovf(self):
474
fn = self.getcompiled(snippet.mul_func, [int, int])
475
for y in range(-5, 5):
476
for x in range(-5, 5):
477
assert fn(x, y) == snippet.mul_func(x, y)
479
assert fn(n, 3) == snippet.mul_func(n, 3)
480
assert fn(n, 4) == snippet.mul_func(n, 4)
481
raises(OverflowError, fn, n, 5)
483
def test_int_mod_ovf_zer(self):
484
fn = self.getcompiled(snippet.mod_func, [int])
485
raises(OverflowError, fn, -1)
486
raises(ZeroDivisionError, fn, 0)
488
def test_int_rshift_val(self):
489
fn = self.getcompiled(snippet.rshift_func, [int])
490
raises(ValueError, fn, -1)
492
def test_int_lshift_ovf_val(self):
493
fn = self.getcompiled(snippet.lshift_func, [int])
494
raises(ValueError, fn, -1)
495
raises(OverflowError, fn, 1)
497
def test_int_unary_ovf(self):
498
fn = self.getcompiled(snippet.unary_func, [int])
499
for i in range(-3,3):
500
assert fn(i) == (-(i), abs(i-1))
501
raises (OverflowError, fn, -sys.maxint-1)
502
raises (OverflowError, fn, -sys.maxint)
505
def test_float_operations(self):
509
z = math.fmod(z, 60.0)
514
fn = self.getcompiled(func, [float, float])
515
assert fn(5.0, 6.0) == func(5.0, 6.0)
517
def test_rpbc_bound_method_static_call(self):
525
res = self.getcompiled(fn)()
528
def test_constant_return_disagreement(self):
535
res = self.getcompiled(fn)()
539
def test_stringformatting(self):
541
return "you said %d, you did"%i
542
f = self.getcompiled(fn, [int])
545
def test_int2str(self):
548
f = self.getcompiled(fn, [int])
551
def test_float2str(self):
554
f = self.getcompiled(fn, [float])
556
assert type(res) is str and float(res) == 1.0
558
def test_uint_arith(self):
561
return ~(i*(i+1))/(i-1)
562
except ZeroDivisionError:
563
return r_uint(91872331)
564
f = self.getcompiled(fn, [r_uint])
565
for value in range(15):
569
def test_ord_returns_a_positive(self):
572
f = self.getcompiled(fn, [int])
575
def test_hash_preservation(self):
584
# xxx check for this CPython peculiarity for now:
585
x = (hash(d2) & sys.maxint) == (id(d2) & sys.maxint)
586
return x, hash(c)+hash(d)
588
f = self.getcompiled(fn)
592
assert res[0] == True
593
assert res[1] == intmask(hash(c)+hash(d))
595
def test_list_basic_ops(self):
596
def list_basic_ops(i, j):
607
f = self.getcompiled(list_basic_ops, [int, int])
610
assert f(i,j) == list_basic_ops(i,j)
612
def test_range2list(self):
617
f = self.getcompiled(fn)
620
def test_range_idx(self):
626
f = self.getcompiled(fn, [int])
628
assert f(-1) == fn(-1)
629
raises(IndexError, f, 42)
631
def test_range_step(self):
633
r = range(10, 37, step)
634
# we always raise on step = 0
636
f = self.getcompiled(fn, [int])
639
raises(ValueError, f, 0)
641
def test_range_iter(self):
642
def fn(start, stop, step):
646
r = range(start, stop, 1)
648
r = range(start, stop, -1)
650
r = range(start, stop, step)
654
f = self.getcompiled(fn, [int, int, int])
655
for args in [2, 7, 0], [7, 2, 0], [10, 50, 7], [50, -10, -3]:
656
assert f(*args) == intmask(fn(*args))
658
def test_recursion_detection(self):
663
return f(n-1, accum*n)
664
fn = self.getcompiled(f, [int, int])
665
assert fn(7, 1) == 5040
666
assert fn(7, 1) == 5040 # detection must work several times, too
667
assert fn(7, 1) == 5040
668
py.test.raises(RuntimeError, fn, -1, 0)
670
def test_list_len_is_true(self):
678
def append_to_list(self, e):
681
def check_list_is_true(self):
692
a1.append_to_list(X())
693
return a1.check_list_is_true()
694
fn = self.getcompiled(f)
697
def test_infinite_recursion(self):
708
fn = self.getcompiled(g, [int])
712
def test_r_dict_exceptions(self):
713
from pypy.rlib.objectmodel import r_dict
715
def raising_hash(obj):
716
if obj.startswith("bla"):
722
d1 = r_dict(eq, raising_hash)
729
fn = self.getcompiled(f)
734
d1 = r_dict(eq, raising_hash)
741
fn = self.getcompiled(f)
746
d1 = r_dict(eq, raising_hash)
753
fn = self.getcompiled(f)
757
def test_float(self):
758
ex = ['', ' ', '0', '1', '-1.5', '1.5E2', '2.5e-1', ' 0 ', '?']
766
fn = self.getcompiled(f, [int])
768
for i in range(len(ex)):
771
assert res == expected