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

« back to all changes in this revision

Viewing changes to pypy/translator/c/test/test_typed.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 sys
 
3
import py
 
4
 
 
5
from py.test import raises
 
6
 
 
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
 
11
 
 
12
# XXX this tries to make compiling faster for full-scale testing
 
13
from pypy.translator.tool import cbuild
 
14
cbuild.enable_fast_compilation()
 
15
 
 
16
 
 
17
class CompilationTestCase:
 
18
 
 
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)
 
25
        if argtypes is None:
 
26
            argtypes = []
 
27
        a = t.buildannotator()
 
28
        a.build_types(func, argtypes)
 
29
        a.simplify()
 
30
        return t
 
31
 
 
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()
 
36
        builder.compile()
 
37
        builder.import_module()
 
38
        return builder.get_entry_point()
 
39
 
 
40
    def getcompiled(self, func, argtypes=None, view=False):
 
41
        from pypy.translator.transform import insert_ll_stackcheck
 
42
        t = self.annotatefunc(func, argtypes)
 
43
        self.process(t)
 
44
        if view or conftest.option.view:
 
45
            t.view()
 
46
        t.checkgraphs()
 
47
        insert_ll_stackcheck(t)
 
48
        return self.compilefunc(t, func)
 
49
 
 
50
    def process(self, t):
 
51
        t.buildrtyper().specialize()
 
52
        #raisingop2direct_call(t)
 
53
 
 
54
 
 
55
class TestTypedTestCase(CompilationTestCase):
 
56
 
 
57
    def test_set_attr(self):
 
58
        set_attr = self.getcompiled(snippet.set_attr)
 
59
        assert set_attr() == 2
 
60
 
 
61
    def test_inheritance2(self):
 
62
        inheritance2 = self.getcompiled(snippet.inheritance2)
 
63
        assert inheritance2() == ((-12, -12), (3, "world"))
 
64
 
 
65
    def test_factorial2(self):
 
66
        factorial2 = self.getcompiled(snippet.factorial2, [int])
 
67
        assert factorial2(5) == 120
 
68
 
 
69
    def test_factorial(self):
 
70
        factorial = self.getcompiled(snippet.factorial, [int])
 
71
        assert factorial(5) == 120
 
72
 
 
73
    def test_simple_method(self):
 
74
        simple_method = self.getcompiled(snippet.simple_method, [int])
 
75
        assert simple_method(55) == 55
 
76
 
 
77
    def test_sieve_of_eratosthenes(self):
 
78
        sieve_of_eratosthenes = self.getcompiled(snippet.sieve_of_eratosthenes)
 
79
        assert sieve_of_eratosthenes() == 1028
 
80
 
 
81
    def test_nested_whiles(self):
 
82
        nested_whiles = self.getcompiled(snippet.nested_whiles, [int, int])
 
83
        assert nested_whiles(5,3) == '!!!!!'
 
84
 
 
85
    def test_call_five(self):
 
86
        call_five = self.getcompiled(snippet.call_five, [int])
 
87
        result = call_five()
 
88
        assert result == [5]
 
89
 
 
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)
 
94
 
 
95
    def test_class_defaultattr(self):
 
96
        class K:
 
97
            n = "hello"
 
98
        def class_defaultattr():
 
99
            k = K()
 
100
            k.n += " world"
 
101
            return k.n
 
102
        fn = self.getcompiled(class_defaultattr)
 
103
        assert fn() == "hello world"
 
104
 
 
105
    def test_tuple_repr(self):
 
106
        def tuple_repr(x, y):
 
107
            z = x, y
 
108
            while x:
 
109
                x = x-1
 
110
            return z
 
111
        fn = self.getcompiled(tuple_repr, [int, str])
 
112
        assert fn(6,'a') == (6,'a')
 
113
 
 
114
    def test_classattribute(self):
 
115
        fn = self.getcompiled(snippet.classattribute, [int])
 
116
        assert fn(1) == 123
 
117
        assert fn(2) == 456
 
118
        assert fn(3) == 789
 
119
        assert fn(4) == 789
 
120
        assert fn(5) == 101112
 
121
 
 
122
    def test_get_set_del_slice(self):
 
123
        fn = self.getcompiled(snippet.get_set_del_slice, [list])
 
124
        l = list('abcdefghij')
 
125
        result = fn(l)
 
126
        assert l == [3, 'c', 8, 11, 'h', 9]
 
127
        assert result == ([3, 'c'], [9], [11, 'h'])
 
128
 
 
129
    def test_type_conversion(self):
 
130
        # obfuscated test case specially for typer.insert_link_conversions()
 
131
        def type_conversion(n):
 
132
            if n > 3:
 
133
                while n > 0:
 
134
                    n = n-1
 
135
                    if n == 5:
 
136
                        n += 3.1416
 
137
            return n
 
138
        fn = self.getcompiled(type_conversion, [int])
 
139
        assert fn(3) == 3
 
140
        assert fn(5) == 0
 
141
        assert abs(fn(7) + 0.8584) < 1E-5
 
142
 
 
143
    def test_do_try_raise_choose(self):
 
144
        fn = self.getcompiled(snippet.try_raise_choose, [int])
 
145
        result = []
 
146
        for n in [-1,0,1,2]:
 
147
            result.append(fn(n))
 
148
        assert result == [-1,0,1,2]
 
149
 
 
150
    def test_is_perfect_number(self):
 
151
        fn = self.getcompiled(snippet.is_perfect_number, [int])
 
152
        for i in range(1, 33):
 
153
            perfect = fn(i)
 
154
            assert perfect is (i in (6,28))
 
155
 
 
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]
 
162
 
 
163
    def test_mutate_global(self):
 
164
        class Stuff:
 
165
            pass
 
166
        g1 = Stuff(); g1.value = 1
 
167
        g2 = Stuff(); g2.value = 2
 
168
        g3 = Stuff(); g3.value = 3
 
169
        g1.next = g3
 
170
        g2.next = g3
 
171
        g3.next = g3
 
172
        def do_things():
 
173
            g1.next = g1
 
174
            g2.next = g1
 
175
            g3.next = g2
 
176
            return g3.next.next.value
 
177
        fn = self.getcompiled(do_things)
 
178
        assert fn() == 1
 
179
 
 
180
    def test_float_ops(self):
 
181
        def f(x):
 
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
 
186
 
 
187
    def test_memoryerror(self):
 
188
        def f(i):
 
189
            lst = [0]*i
 
190
            lst[-1] = 5
 
191
            return lst[0]
 
192
        fn = self.getcompiled(f, [int])
 
193
        assert fn(1) == 5
 
194
        assert fn(2) == 0
 
195
        py.test.raises(MemoryError, fn, sys.maxint//2+1)
 
196
        py.test.raises(MemoryError, fn, sys.maxint)
 
197
 
 
198
    def test_chr(self):
 
199
        def f(x):
 
200
            try:
 
201
                return 'Yes ' + chr(x)
 
202
            except ValueError:
 
203
                return 'No'
 
204
        fn = self.getcompiled(f, [int])
 
205
        assert fn(65) == 'Yes A'
 
206
        assert fn(256) == 'No'
 
207
        assert fn(-1) == 'No'
 
208
 
 
209
    def test_unichr(self):
 
210
        def f(x):
 
211
            try:
 
212
                return ord(unichr(x))
 
213
            except ValueError:
 
214
                return -42
 
215
        fn = self.getcompiled(f, [int])
 
216
        assert fn(65) == 65
 
217
        assert fn(-12) == -42
 
218
        assert fn(sys.maxint) == -42
 
219
 
 
220
    def test_list_indexerror(self):
 
221
        def f(i):
 
222
            lst = [123, 456]
 
223
            try:
 
224
                lst[i] = 789
 
225
            except IndexError:
 
226
                return 42
 
227
            return lst[0]
 
228
        fn = self.getcompiled(f, [int])
 
229
        assert fn(1) == 123
 
230
        assert fn(2) == 42
 
231
        assert fn(-2) == 789
 
232
        assert fn(-3) == 42
 
233
 
 
234
    def test_long_long(self):
 
235
        def f(i):
 
236
            return 4*i
 
237
        fn = self.getcompiled(f, [r_ulonglong], view=False)
 
238
        assert fn(sys.maxint) == 4*sys.maxint
 
239
 
 
240
        def g(i):
 
241
            return 4*i
 
242
        gn = self.getcompiled(g, [r_longlong], view=False)
 
243
        assert gn(sys.maxint) == 4*sys.maxint
 
244
 
 
245
    def test_specializing_int_functions(self):
 
246
        def f(i):
 
247
            return i + 1
 
248
        f._annspecialcase_ = "specialize:argtype(0)"
 
249
        def g(n):
 
250
            if n > 0:
 
251
                return f(r_longlong(0))
 
252
            else:
 
253
                return f(0)
 
254
 
 
255
        fn = self.getcompiled(g, [int])
 
256
        assert g(0) == 1
 
257
        assert g(1) == 1
 
258
 
 
259
    def test_downcast_int(self):
 
260
        def f(i):
 
261
            return int(i)
 
262
        fn = self.getcompiled(f, [r_longlong])
 
263
        assert fn(0) == 0
 
264
 
 
265
    def test_function_ptr(self):
 
266
        def f1():
 
267
            return 1
 
268
        def f2():
 
269
            return 2
 
270
        def g(i):
 
271
            if i:
 
272
                f = f1
 
273
            else:
 
274
                f = f2
 
275
            return f()
 
276
        fn = self.getcompiled(g, [int])
 
277
        assert fn(0) == 2
 
278
        assert fn(1) == 1
 
279
 
 
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
 
283
        def wrapper():
 
284
            lst = snippet.call_five()
 
285
            return len(lst), lst[0]
 
286
        call_five = self.getcompiled(wrapper)
 
287
        result = call_five()
 
288
        assert result == (1, 5)
 
289
 
 
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')]
 
293
            del l[:1]
 
294
            bound = len(l)-1
 
295
            if bound >= 0:
 
296
                del l[bound:]
 
297
            del l[2:4]
 
298
            #l[:1] = [3]
 
299
            #bound = len(l)-1
 
300
            #assert bound >= 0
 
301
            #l[bound:] = [9]    no setting slice into lists for now
 
302
            #l[2:4] = [8,11]
 
303
            l[0], l[-1], l[2], l[3] =3, 9, 8, 11
 
304
 
 
305
            list_3_c = l[:2]
 
306
            list_9 = l[5:]
 
307
            list_11_h = l[3:5]
 
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)
 
313
        result = fn()
 
314
        assert result == (6, 3, ord('c'), 8, 11, ord('h'), 9,
 
315
                          2, 3, ord('c'),
 
316
                          1, 9,
 
317
                          2, 11, ord('h'))
 
318
 
 
319
    def test_is(self):
 
320
        def testfn():
 
321
            l1 = []
 
322
            return l1 is l1
 
323
        fn = self.getcompiled(testfn)
 
324
        result = fn()
 
325
        assert result is True
 
326
        def testfn():
 
327
            l1 = []
 
328
            return l1 is None
 
329
        fn = self.getcompiled(testfn)
 
330
        result = fn()
 
331
        assert result is False
 
332
 
 
333
    def test_str_compare(self):
 
334
        def testfn(i, j):
 
335
            s1 = ['one', 'two']
 
336
            s2 = ['one', 'two', 'o', 'on', 'twos', 'foobar']
 
337
            return s1[i] == s2[j]
 
338
        fn = self.getcompiled(testfn, [int, int])
 
339
        for i in range(2):
 
340
            for j in range(6):
 
341
                res = fn(i, j)
 
342
                assert res is testfn(i, j)
 
343
 
 
344
        def testfn(i, j):
 
345
            s1 = ['one', 'two']
 
346
            s2 = ['one', 'two', 'o', 'on', 'twos', 'foobar']
 
347
            return s1[i] != s2[j]
 
348
        fn = self.getcompiled(testfn, [int, int])
 
349
        for i in range(2):
 
350
            for j in range(6):
 
351
                res = fn(i, j)
 
352
                assert res is testfn(i, j)
 
353
                
 
354
        def testfn(i, j):
 
355
            s1 = ['one', 'two']
 
356
            s2 = ['one', 'two', 'o', 'on', 'twos', 'foobar']
 
357
            return s1[i] < s2[j]
 
358
        fn = self.getcompiled(testfn, [int, int])
 
359
        for i in range(2):
 
360
            for j in range(6):
 
361
                res = fn(i, j)
 
362
                assert res is testfn(i, j)
 
363
                
 
364
        def testfn(i, j):
 
365
            s1 = ['one', 'two']
 
366
            s2 = ['one', 'two', 'o', 'on', 'twos', 'foobar']
 
367
            return s1[i] <= s2[j]
 
368
        fn = self.getcompiled(testfn, [int, int])
 
369
        for i in range(2):
 
370
            for j in range(6):
 
371
                res = fn(i, j)
 
372
                assert res is testfn(i, j)
 
373
                
 
374
        def testfn(i, j):
 
375
            s1 = ['one', 'two']
 
376
            s2 = ['one', 'two', 'o', 'on', 'twos', 'foobar']
 
377
            return s1[i] > s2[j]
 
378
        fn = self.getcompiled(testfn, [int, int])
 
379
        for i in range(2):
 
380
            for j in range(6):
 
381
                res = fn(i, j)
 
382
                assert res is testfn(i, j)
 
383
                
 
384
        def testfn(i, j):
 
385
            s1 = ['one', 'two']
 
386
            s2 = ['one', 'two', 'o', 'on', 'twos', 'foobar']
 
387
            return s1[i] >= s2[j]
 
388
        fn = self.getcompiled(testfn, [int, int])
 
389
        for i in range(2):
 
390
            for j in range(6):
 
391
                res = fn(i, j)
 
392
                assert res is testfn(i, j)
 
393
                
 
394
    def test_str_methods(self):
 
395
        def testfn(i, j):
 
396
            s1 = ['one', 'two']
 
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])
 
400
        for i in range(2):
 
401
            for j in range(9):
 
402
                res = fn(i, j)
 
403
                assert res is testfn(i, j)
 
404
        def testfn(i, j):
 
405
            s1 = ['one', 'two']
 
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])
 
409
        for i in range(2):
 
410
            for j in range(9):
 
411
                res = fn(i, j)
 
412
                assert res is testfn(i, j)
 
413
 
 
414
    def test_str_join(self):
 
415
        def testfn(i, j):
 
416
            s1 = [ '', ',', ' and ']
 
417
            s2 = [ [], ['foo'], ['bar', 'baz', 'bazz']]
 
418
            return s1[i].join(s2[j])
 
419
        fn = self.getcompiled(testfn, [int, int])
 
420
        for i in range(3):
 
421
            for j in range(3):
 
422
                res = fn(i, j)
 
423
                assert res == fn(i, j)
 
424
    
 
425
    def test_unichr_eq(self):
 
426
        l = list(u'Hello world')
 
427
        def f(i, j):
 
428
            return l[i] == l[j]
 
429
        fn = self.getcompiled(f, [int, int])
 
430
        for i in range(len(l)):
 
431
            for j in range(len(l)):
 
432
                res = fn(i,j)
 
433
                assert res == f(i,j) 
 
434
    
 
435
    def test_unichr_ne(self):
 
436
        l = list(u'Hello world')
 
437
        def f(i, j):
 
438
            return l[i] != l[j]
 
439
        fn = self.getcompiled(f, [int, int])
 
440
        for i in range(len(l)):
 
441
            for j in range(len(l)):
 
442
                res = fn(i,j)
 
443
                assert res == f(i,j)
 
444
 
 
445
    def test_unichr_ord(self):
 
446
        l = list(u'Hello world')
 
447
        def f(i):
 
448
            return ord(l[i])
 
449
        fn = self.getcompiled(f, [int])
 
450
        for i in range(len(l)):
 
451
            res = fn(i)
 
452
            assert res == f(i)
 
453
 
 
454
    def test_unichr_unichr(self):
 
455
        l = list(u'Hello world')
 
456
        def f(i, j):
 
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]))
 
463
 
 
464
    def test_int_overflow(self):
 
465
        fn = self.getcompiled(snippet.add_func, [int])
 
466
        raises(OverflowError, fn, sys.maxint)
 
467
 
 
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)
 
472
 
 
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)
 
478
        n = sys.maxint / 4
 
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)
 
482
 
 
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)
 
487
 
 
488
    def test_int_rshift_val(self):
 
489
        fn = self.getcompiled(snippet.rshift_func, [int])
 
490
        raises(ValueError, fn, -1)
 
491
 
 
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)
 
496
 
 
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)
 
503
 
 
504
    # floats 
 
505
    def test_float_operations(self):
 
506
        import math
 
507
        def func(x, y):
 
508
            z = x + y / 2.1 * x
 
509
            z = math.fmod(z, 60.0)
 
510
            z = pow(z, 2)
 
511
            z = -z
 
512
            return int(z)
 
513
 
 
514
        fn = self.getcompiled(func, [float, float])
 
515
        assert fn(5.0, 6.0) == func(5.0, 6.0)
 
516
 
 
517
    def test_rpbc_bound_method_static_call(self):
 
518
        class R:
 
519
            def meth(self):
 
520
                return 0
 
521
        r = R()
 
522
        m = r.meth
 
523
        def fn():
 
524
            return m()
 
525
        res = self.getcompiled(fn)()
 
526
        assert res == 0
 
527
 
 
528
    def test_constant_return_disagreement(self):
 
529
        class R:
 
530
            def meth(self):
 
531
                return 0
 
532
        r = R()
 
533
        def fn():
 
534
            return r.meth()
 
535
        res = self.getcompiled(fn)()
 
536
        assert res == 0
 
537
 
 
538
 
 
539
    def test_stringformatting(self):
 
540
        def fn(i):
 
541
            return "you said %d, you did"%i
 
542
        f = self.getcompiled(fn, [int])
 
543
        assert f(1) == fn(1)
 
544
 
 
545
    def test_int2str(self):
 
546
        def fn(i):
 
547
            return str(i)
 
548
        f = self.getcompiled(fn, [int])
 
549
        assert f(1) == fn(1)
 
550
 
 
551
    def test_float2str(self):
 
552
        def fn(i):
 
553
            return str(i)
 
554
        f = self.getcompiled(fn, [float])
 
555
        res = f(1.0)
 
556
        assert type(res) is str and float(res) == 1.0
 
557
        
 
558
    def test_uint_arith(self):
 
559
        def fn(i):
 
560
            try:
 
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):
 
566
            i = r_uint(value)
 
567
            assert f(i) == fn(i)
 
568
 
 
569
    def test_ord_returns_a_positive(self):
 
570
        def fn(i):
 
571
            return ord(chr(i))
 
572
        f = self.getcompiled(fn, [int])
 
573
        assert f(255) == 255
 
574
 
 
575
    def test_hash_preservation(self):
 
576
        class C:
 
577
            pass
 
578
        class D(C):
 
579
            pass
 
580
        c = C()
 
581
        d = D()
 
582
        def fn():
 
583
            d2 = D()
 
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)
 
587
        
 
588
        f = self.getcompiled(fn)
 
589
 
 
590
        res = f()
 
591
 
 
592
        assert res[0] == True
 
593
        assert res[1] == intmask(hash(c)+hash(d))
 
594
 
 
595
    def test_list_basic_ops(self):
 
596
        def list_basic_ops(i, j):
 
597
            l = [1,2,3]
 
598
            l.insert(0, 42)
 
599
            del l[1]
 
600
            l.append(i)
 
601
            listlen = len(l)
 
602
            l.extend(l) 
 
603
            del l[listlen:]
 
604
            l += [5,6] 
 
605
            l[1] = i
 
606
            return l[j]
 
607
        f = self.getcompiled(list_basic_ops, [int, int])
 
608
        for i in range(6): 
 
609
            for j in range(6): 
 
610
                assert f(i,j) == list_basic_ops(i,j)
 
611
 
 
612
    def test_range2list(self):
 
613
        def fn():
 
614
            r = range(10, 37, 4)
 
615
            r.reverse()
 
616
            return r[0]
 
617
        f = self.getcompiled(fn)
 
618
        assert f() == fn()
 
619
 
 
620
    def test_range_idx(self):
 
621
        def fn(idx):
 
622
            r = range(10, 37, 4)
 
623
            try:
 
624
                return r[idx]
 
625
            except: raise
 
626
        f = self.getcompiled(fn, [int])
 
627
        assert f(0) == fn(0)
 
628
        assert f(-1) == fn(-1)
 
629
        raises(IndexError, f, 42)
 
630
 
 
631
    def test_range_step(self):
 
632
        def fn(step):
 
633
            r = range(10, 37, step)
 
634
            # we always raise on step = 0
 
635
            return r[-2]
 
636
        f = self.getcompiled(fn, [int])
 
637
        assert f(1) == fn(1)
 
638
        assert f(3) == fn(3)
 
639
        raises(ValueError, f, 0)
 
640
 
 
641
    def test_range_iter(self):
 
642
        def fn(start, stop, step):
 
643
            res = 0
 
644
            if step == 0:
 
645
                if stop >= start:
 
646
                    r = range(start, stop, 1)
 
647
                else:
 
648
                    r = range(start, stop, -1)
 
649
            else:
 
650
                r = range(start, stop, step)
 
651
            for i in r:
 
652
                res = res * 51 + i
 
653
            return res
 
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))
 
657
 
 
658
    def test_recursion_detection(self):
 
659
        def f(n, accum):
 
660
            if n == 0:
 
661
                return accum
 
662
            else:
 
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)
 
669
 
 
670
    def test_list_len_is_true(self):
 
671
 
 
672
        class X(object):
 
673
            pass
 
674
        class A(object):
 
675
            def __init__(self):
 
676
                self.l = []
 
677
 
 
678
            def append_to_list(self, e):
 
679
                self.l.append(e)
 
680
 
 
681
            def check_list_is_true(self):
 
682
                did_loop = 0
 
683
                while self.l:
 
684
                    did_loop = 1
 
685
                    if len(self.l):
 
686
                        break
 
687
                return did_loop
 
688
            
 
689
        a1 = A()
 
690
        def f():
 
691
            for ii in range(1):
 
692
                a1.append_to_list(X())
 
693
            return a1.check_list_is_true()
 
694
        fn = self.getcompiled(f)
 
695
        assert fn() == 1
 
696
 
 
697
    def test_infinite_recursion(self):
 
698
        def f(x):
 
699
            if x:
 
700
                return f(x)
 
701
            return 1
 
702
        def g(x):
 
703
            try:
 
704
                f(x)
 
705
            except RuntimeError:
 
706
                return 42
 
707
            return 1
 
708
        fn = self.getcompiled(g, [int])
 
709
        assert fn(0) == 1
 
710
        assert fn(1) == 42
 
711
 
 
712
    def test_r_dict_exceptions(self):
 
713
        from pypy.rlib.objectmodel import r_dict
 
714
        
 
715
        def raising_hash(obj):
 
716
            if obj.startswith("bla"):
 
717
                raise TypeError
 
718
            return 1
 
719
        def eq(obj1, obj2):
 
720
            return obj1 is obj2
 
721
        def f():
 
722
            d1 = r_dict(eq, raising_hash)
 
723
            d1['xxx'] = 1
 
724
            try:
 
725
                x = d1["blabla"]
 
726
            except Exception:
 
727
                return 42
 
728
            return x
 
729
        fn = self.getcompiled(f)
 
730
        res = fn()
 
731
        assert res == 42
 
732
 
 
733
        def f():
 
734
            d1 = r_dict(eq, raising_hash)
 
735
            d1['xxx'] = 1
 
736
            try:
 
737
                x = d1["blabla"]
 
738
            except TypeError:
 
739
                return 42
 
740
            return x
 
741
        fn = self.getcompiled(f)
 
742
        res = fn()
 
743
        assert res == 42
 
744
 
 
745
        def f():
 
746
            d1 = r_dict(eq, raising_hash)
 
747
            d1['xxx'] = 1
 
748
            try:
 
749
                d1["blabla"] = 2
 
750
            except TypeError:
 
751
                return 42
 
752
            return 0
 
753
        fn = self.getcompiled(f)
 
754
        res = fn()
 
755
        assert res == 42
 
756
 
 
757
    def test_float(self):
 
758
        ex = ['', '    ', '0', '1', '-1.5', '1.5E2', '2.5e-1', ' 0 ', '?']
 
759
        def f(i):
 
760
            s = ex[i]
 
761
            try:
 
762
                return float(s)
 
763
            except ValueError:
 
764
                return -999.0
 
765
        
 
766
        fn = self.getcompiled(f, [int])
 
767
 
 
768
        for i in range(len(ex)):
 
769
            expected = f(i)
 
770
            res = fn(i)
 
771
            assert res == expected
 
772