Cython has moved to github.
cython-devel
view tests/run/inplace.pyx @ 1622:4f0327bdebc9
test for temp allocation bug in call
| author | Robert Bradshaw <robertwb@math.washington.edu> |
|---|---|
| date | Sat Jan 17 01:25:34 2009 -0800 (3 years ago) |
| parents | 9adedfac5b0b |
| children | 82d312a9f1fc |
line source
1 __doc__ = u"""
2 >>> str(f(5, 7))
3 '29509034655744'
5 >>> g(13, 4)
6 32
8 >>> h(56, 7)
9 105.0
11 >>> arrays()
12 19
14 >>> attributes()
15 26 26 26
17 >>> smoketest()
18 10
20 >>> test_side_effects()
21 side effect 1
22 c side effect 2
23 side effect 3
24 c side effect 4
25 ([0, 11, 102, 3, 4], [0, 1, 2, 13, 104])
26 """
28 def f(a,b):
29 a += b
30 a *= b
31 a **= b
32 return a
34 def g(int a, int b):
35 a -= b
36 a /= b
37 a <<= b
38 return a
40 def h(double a, double b):
41 a /= b
42 a += b
43 a *= b
44 return a
46 cimport stdlib
48 def arrays():
49 cdef char* buf = <char*>stdlib.malloc(10)
50 cdef int i = 2
51 cdef object j = 2
52 buf[2] = 0
53 buf[i] += 2
54 buf[2] *= 10
55 buf[j] -= 1
56 print buf[2]
57 stdlib.free(buf)
59 cdef class A:
60 cdef attr
61 cdef int attr2
62 cdef char* buf
63 def __init__(self):
64 self.attr = 3
65 self.attr2 = 3
67 class B:
68 attr = 3
70 def attributes():
71 cdef A a = A()
72 b = B()
73 a.attr += 10
74 a.attr *= 2
75 a.attr2 += 10
76 a.attr2 *= 2
77 b.attr += 10
78 b.attr *= 2
79 print a.attr, a.attr2, b.attr
81 def get_2(): return 2
82 cdef int identity(int value): return value
84 def smoketest():
85 cdef char* buf = <char*>stdlib.malloc(10)
86 cdef A a = A()
87 a.buf = buf
88 a.buf[identity(1)] = 0
89 (a.buf + identity(4) - <int>(2*get_2() - 1))[get_2() - 2*identity(1)] += 10
90 print a.buf[1]
91 stdlib.free(buf)
94 def side_effect(x):
95 print u"side effect", x
96 return x
98 cdef int c_side_effect(int x):
99 print u"c side effect", x
100 return x
102 def test_side_effects():
103 a = list(range(5))
104 a[side_effect(1)] += 10
105 a[c_side_effect(2)] += 100
106 cdef int i
107 cdef int b[5]
108 for i from 0 <= i < 5:
109 b[i] = i
110 b[side_effect(3)] += 10
111 b[c_side_effect(4)] += 100
112 return a, [b[i] for i from 0 <= i < 5]
