Cython has moved to github.
cython-devel
view tests/run/arrayassign.pyx @ 1334:41d2107a2074
compile time/runtime checks for array slice assignments
| author | Stefan Behnel <scoder@users.berlios.de> |
|---|---|
| date | Fri Nov 14 19:20:43 2008 +0100 (3 years ago) |
| parents | cf41fa30ad5f |
| children | 82d312a9f1fc |
line source
1 __doc__ = u"""
2 >>> test_literal_list_slice_all()
3 (1, 2, 3, 4, 5)
4 >>> test_literal_list_slice_start()
5 (1, 2, 3, 4, 5)
6 >>> test_literal_list_slice_end()
7 (1, 2, 3, 4, 5)
8 >>> test_literal_list_slice_start_end()
9 (1, 2, 3, 4, 5)
11 >>> test_literal_list_slice_start_param(4)
12 (1, 2, 3, 4, 5)
13 >>> test_literal_list_slice_start_param(3)
14 Traceback (most recent call last):
15 ValueError: Assignment to slice of wrong length, expected 5, got 6
16 >>> test_literal_list_slice_start_param(5)
17 Traceback (most recent call last):
18 ValueError: Assignment to slice of wrong length, expected 5, got 4
20 >>> test_literal_list_slice_end_param(5)
21 (1, 2, 3, 4, 5)
22 >>> test_literal_list_slice_end_param(4)
23 Traceback (most recent call last):
24 ValueError: Assignment to slice of wrong length, expected 5, got 4
25 >>> test_literal_list_slice_end_param(6)
26 Traceback (most recent call last):
27 ValueError: Assignment to slice of wrong length, expected 5, got 6
29 >>> test_literal_list_slice_start_end_param(2,7)
30 (1, 2, 3, 4, 5)
31 >>> test_literal_list_slice_start_end_param(3,7)
32 Traceback (most recent call last):
33 ValueError: Assignment to slice of wrong length, expected 5, got 4
34 >>> test_literal_list_slice_start_end_param(1,7)
35 Traceback (most recent call last):
36 ValueError: Assignment to slice of wrong length, expected 5, got 6
37 >>> test_literal_list_slice_start_end_param(2,6)
38 Traceback (most recent call last):
39 ValueError: Assignment to slice of wrong length, expected 5, got 4
40 >>> test_literal_list_slice_start_end_param(2,8)
41 Traceback (most recent call last):
42 ValueError: Assignment to slice of wrong length, expected 5, got 6
43 >>> test_literal_list_slice_start_end_param(3,6)
44 Traceback (most recent call last):
45 ValueError: Assignment to slice of wrong length, expected 5, got 3
46 >>> test_literal_list_slice_start_end_param(1,8)
47 Traceback (most recent call last):
48 ValueError: Assignment to slice of wrong length, expected 5, got 7
50 >>> test_ptr_literal_list_slice_all()
51 (1, 2, 3, 4, 5)
52 >>> test_ptr_literal_list_slice_start()
53 (1, 2, 3, 4, 5)
54 >>> test_ptr_literal_list_slice_end()
55 (1, 2, 3, 4, 5)
56 """
58 # this doesn't work - it would reassign the array address!
59 #
60 #def test_literal_list():
61 # cdef int a[5]
62 # a = [1,2,3,4,5]
63 # return (a[0], a[1], a[2], a[3], a[4])
65 def test_literal_list_slice_all():
66 cdef int a[5] # = [5,4,3,2,1]
67 a[:] = [1,2,3,4,5]
68 return (a[0], a[1], a[2], a[3], a[4])
70 def test_literal_list_slice_start():
71 cdef int a[7] # = [7,6,5,4,3,2,1]
72 a[2:] = [1,2,3,4,5]
73 return (a[2], a[3], a[4], a[5], a[6])
75 def test_literal_list_slice_end():
76 cdef int a[7] # = [7,6,5,4,3,2,1]
77 a[:5] = [1,2,3,4,5]
78 return (a[0], a[1], a[2], a[3], a[4])
80 def test_literal_list_slice_start_end():
81 cdef int a[9] # = [9,8,7,6,5,4,3,2,1]
82 a[2:7] = [1,2,3,4,5]
83 return (a[2], a[3], a[4], a[5], a[6])
85 def test_literal_list_slice_start_param(s):
86 cdef int a[9] # = [9,8,7,6,5,4,3,2,1]
87 a[s:] = [1,2,3,4,5]
88 return (a[4], a[5], a[6], a[7], a[8])
89 # return a[s:]
91 def test_literal_list_slice_end_param(e):
92 cdef int a[9] # = [9,8,7,6,5,4,3,2,1]
93 a[:e] = [1,2,3,4,5]
94 return (a[0], a[1], a[2], a[3], a[4])
95 # return a[:e]
97 def test_literal_list_slice_start_end_param(s,e):
98 cdef int a[9] # = [9,8,7,6,5,4,3,2,1]
99 a[s:e] = [1,2,3,4,5]
100 return (a[2], a[3], a[4], a[5], a[6])
101 # return a[s:e]
103 def test_ptr_literal_list_slice_all():
104 cdef int *a = [6,5,4,3,2]
105 a[:] = [1,2,3,4,5]
106 return (a[0], a[1], a[2], a[3], a[4])
108 def test_ptr_literal_list_slice_start():
109 cdef int *a = [6,5,4,3,2,1]
110 a[1:] = [1,2,3,4,5]
111 return (a[1], a[2], a[3], a[4], a[5])
113 def test_ptr_literal_list_slice_end():
114 cdef int *a = [6,5,4,3,2,1]
115 a[:5] = [1,2,3,4,5]
116 return (a[0], a[1], a[2], a[3], a[4])
118 # tuples aren't supported (yet)
119 #
120 #def test_literal_tuple():
121 # cdef int a[5]
122 # a = (1,2,3,4,5)
123 # return (a[0], a[1], a[2], a[3], a[4])
125 # this would be nice to have:
126 #
127 #def test_list(list l):
128 # cdef int a[5]
129 # a[:] = l
130 # return (a[0], a[1], a[2], a[3], a[4])
